001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui.components;
029
030import java.awt.Color;
031import java.awt.Font;
032import java.awt.GridBagConstraints;
033import java.awt.GridBagLayout;
034import java.awt.event.MouseEvent;
035
036import javax.swing.Box;
037import javax.swing.ImageIcon;
038import javax.swing.JLabel;
039import javax.swing.JPanel;
040import javax.swing.text.JTextComponent;
041
042import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
043import org.opends.guitools.controlpanel.util.Utilities;
044import org.forgerock.i18n.LocalizableMessage;
045
046/**
047 * A panel containing a label an a help icon.  A customized tool tip is used,
048 * the tool tip is also displayed when the user clicks on the help icon.
049 * The main difference with {@code LabelWithHelpIcon} is that this uses
050 * a {@code JEditorPane} as label.
051 *
052 */
053public class SelectableLabelWithHelpIcon extends JPanel
054{
055  private static final long serialVersionUID = 4502977901098910797L;
056  /**
057   * The label with the text.
058   */
059  private JTextComponent label = Utilities.makeHtmlPane("",
060      ColorAndFontConstants.defaultFont);
061  /**
062   * The label with the icon.
063   */
064  private JLabel iconLabel = new JLabel(icon);
065  private static final ImageIcon icon =
066    Utilities.createImageIcon("org/opends/quicksetup/images/help_small.gif");
067
068  /**
069   * The left inset of the help icon.
070   */
071  private final int INSET_WITH_ICON= 3;
072
073  /**
074   * The constructor of this panel.
075   * @param text the text of the panel.
076   * @param tooltipIcon the tool tip of the help icon.
077   */
078  public SelectableLabelWithHelpIcon(LocalizableMessage text, LocalizableMessage tooltipIcon)
079  {
080    super(new GridBagLayout());
081    setOpaque(false);
082    label.setText(Utilities.applyFont(text.toString(),
083        label.getFont()));
084    if (tooltipIcon != null)
085    {
086      iconLabel.setToolTipText(tooltipIcon.toString());
087    }
088    GridBagConstraints gbc = new GridBagConstraints();
089    gbc.gridx = 0;
090    gbc.gridy = 0;
091    gbc.gridwidth = 1;
092    add(label, gbc);
093    gbc.gridx ++;
094    gbc.insets.left = INSET_WITH_ICON;
095    add(iconLabel, gbc);
096    gbc.insets.left = 0;
097    gbc.weightx = 1.0;
098    gbc.fill = GridBagConstraints.HORIZONTAL;
099    gbc.gridx ++;
100    add(Box.createHorizontalGlue(), gbc);
101
102    Utilities.addClickTooltipListener(iconLabel);
103  }
104
105  /**
106   * Sets the text on the label.  The text is assumed to be in HTML format
107   * but the font will be imposed by the font specified using {@link #setFont}.
108   * @param text the text to be displayed.
109   */
110  public void setText(String text)
111  {
112    label.setText(Utilities.applyFont(text, label.getFont()));
113  }
114
115  /**
116   * Returns the text displayed on the panel.
117   * @return the text displayed on the panel.
118   */
119  public String getText()
120  {
121    return label.getText();
122  }
123
124  /**
125   * Sets the font to be used in this panel.
126   * @param font the font.
127   */
128  public void setFont(Font font)
129  {
130    // This is called by the constructor of JPanel.
131    if (label != null)
132    {
133      label.setFont(font);
134    }
135  }
136
137  /**
138   * Sets the tool tip to be used in the help icon.
139   * @param tooltip the tool tip text.
140   */
141  public void setHelpTooltip(String tooltip)
142  {
143    iconLabel.setToolTipText(tooltip);
144  }
145
146  /**
147   * Returns the tool tip to be used in the help icon.
148   * @return the tool tip to be used in the help icon.
149   */
150  public String getHelpTooltip()
151  {
152    return iconLabel.getToolTipText();
153  }
154
155  /**
156   * Sets whether the help icon is visible or not.
157   * @param visible whether the help icon is visible or not.
158   */
159  public void setHelpIconVisible(boolean visible)
160  {
161    if (visible)
162    {
163      if (iconLabel.getIcon() != icon)
164      {
165        iconLabel.setIcon(icon);
166      }
167    }
168    else if (iconLabel.getIcon() != null)
169    {
170      iconLabel.setIcon(null);
171    }
172  }
173
174  /**
175   * Sets the foreground color for the text in this panel.
176   * @param color the foreground color for the text in this panel.
177   */
178  public void setForeground(Color color)
179  {
180    super.setForeground(color);
181    if (label != null)
182    {
183      // This is called in the constructor of the object.
184      label.setForeground(color);
185    }
186  }
187
188  /** {@inheritDoc} */
189  public String getToolTipText(MouseEvent ev)
190  {
191    int x = ev.getPoint().x;
192    boolean display = x > label.getPreferredSize().width - 10;
193
194    if (display)
195    {
196      return getHelpTooltip();
197    }
198    else
199    {
200      return null;
201    }
202  }
203}