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 2008-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.Component;
032import java.awt.Font;
033import java.awt.GridBagConstraints;
034import java.awt.GridBagLayout;
035import java.awt.event.MouseEvent;
036
037import javax.swing.Box;
038import javax.swing.ImageIcon;
039import javax.swing.JLabel;
040import javax.swing.JPanel;
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 *
050 */
051public class LabelWithHelpIcon extends JPanel
052{
053  private static final long serialVersionUID = 4502977901538910797L;
054  /**
055   * The label with the text.
056   */
057  protected JLabel label = Utilities.createDefaultLabel();
058  /**
059   * The label with the icon.
060   */
061  protected JLabel iconLabel = new JLabel(icon);
062  private static final ImageIcon icon =
063    Utilities.createImageIcon("org/opends/quicksetup/images/help_small.gif");
064
065
066  /**
067   * The left inset of the help icon.
068   */
069  protected final int INSET_WITH_ICON= 3;
070
071  /**
072   * The constructor of this panel.
073   * @param text the text of the panel.
074   * @param tooltipIcon the tool tip of the help icon.
075   */
076  public LabelWithHelpIcon(LocalizableMessage text, LocalizableMessage tooltipIcon)
077  {
078    super(new GridBagLayout());
079    setOpaque(false);
080    label.setText(text.toString());
081    label.setForeground(ColorAndFontConstants.foreground);
082    if (tooltipIcon != null)
083    {
084      iconLabel.setToolTipText(tooltipIcon.toString());
085    }
086    GridBagConstraints gbc = new GridBagConstraints();
087    gbc.gridx = 0;
088    gbc.gridy = 0;
089    gbc.gridwidth = 1;
090    gbc.anchor = GridBagConstraints.WEST;
091    add(label, gbc);
092    gbc.gridx ++;
093    gbc.insets.left = INSET_WITH_ICON;
094    add(iconLabel, gbc);
095    gbc.insets.left = 0;
096    gbc.weightx = 1.0;
097    gbc.fill = GridBagConstraints.HORIZONTAL;
098    add(Box.createHorizontalGlue(), gbc);
099
100    Utilities.addClickTooltipListener(iconLabel);
101
102    updateAccessibleContext();
103  }
104
105  /**
106   * Set the component this is labeling. Can be {@code null} if this does not
107   * label a {@code Component}.
108   * @param comp the {@code Component} to be labeled.
109   */
110  public void setLabelFor(Component comp)
111  {
112    label.setLabelFor(comp);
113  }
114
115  /**
116   * Sets the text on the label.
117   * @param text the text to be displayed.
118   */
119  public void setText(String text)
120  {
121    label.setText(text);
122    updateAccessibleContext();
123  }
124
125  /**
126   * Returns the text displayed on the panel.
127   * @return the text displayed on the panel.
128   */
129  public String getText()
130  {
131    return label.getText();
132  }
133
134  /**
135   * Sets the font to be used in this panel.
136   * @param font the font.
137   */
138  public void setFont(Font font)
139  {
140    // This is call by the constructor of JPanel.
141    if (label != null)
142    {
143      label.setFont(font);
144    }
145  }
146
147  /**
148   * Sets the tool tip to be used in the help icon.
149   * @param tooltip the tool tip text.
150   */
151  public void setHelpTooltip(String tooltip)
152  {
153    iconLabel.setToolTipText(tooltip);
154    updateAccessibleContext();
155  }
156
157  /**
158   * Returns the tool tip to be used in the help icon.
159   * @return the tool tip to be used in the help icon.
160   */
161  public String getHelpTooltip()
162  {
163    return iconLabel.getToolTipText();
164  }
165
166  /**
167   * Sets whether the help icon is visible or not.
168   * @param visible whether the help icon is visible or not.
169   */
170  public void setHelpIconVisible(boolean visible)
171  {
172    if (visible)
173    {
174      if (iconLabel.getIcon() != icon)
175      {
176        iconLabel.setIcon(icon);
177      }
178    }
179    else if (iconLabel.getIcon() != null)
180    {
181      iconLabel.setIcon(null);
182    }
183  }
184
185  /**
186   * Sets the foreground color for the text in this panel.
187   * @param color the foreground color for the text in this panel.
188   */
189  public void setForeground(Color color)
190  {
191    super.setForeground(color);
192    if (label != null)
193    {
194      // This is called in the constructor of the object.
195      label.setForeground(color);
196    }
197  }
198
199  /** {@inheritDoc} */
200  public String getToolTipText(MouseEvent ev)
201  {
202    int x = ev.getPoint().x;
203    boolean display = x > label.getPreferredSize().width - 10;
204
205    if (display)
206    {
207      return getHelpTooltip();
208    }
209    else
210    {
211      return null;
212    }
213  }
214
215  private void updateAccessibleContext()
216  {
217    StringBuilder sb = new StringBuilder();
218    String s = label.getText();
219    if (s != null)
220    {
221      sb.append(s);
222    }
223    if (iconLabel.getIcon() != null)
224    {
225      String toolTip = iconLabel.getToolTipText();
226      toolTip = Utilities.stripHtmlToSingleLine(toolTip);
227      if (toolTip != null)
228      {
229        sb.append(" - ").append(toolTip);
230      }
231    }
232    getAccessibleContext().setAccessibleName(sb.toString());
233  }
234}