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