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-2015 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.opends.guitools.controlpanel.ui.ColorAndFontConstants; 033import org.opends.guitools.controlpanel.util.Utilities; 034import org.forgerock.i18n.LocalizableMessage; 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 * 042 */ 043public class SelectableLabelWithHelpIcon extends JPanel 044{ 045 private static final long serialVersionUID = 4502977901098910797L; 046 /** 047 * The label with the text. 048 */ 049 private JTextComponent label = Utilities.makeHtmlPane("", 050 ColorAndFontConstants.defaultFont); 051 /** 052 * The label with the icon. 053 */ 054 private JLabel iconLabel = new JLabel(icon); 055 private static final ImageIcon icon = 056 Utilities.createImageIcon("org/opends/quicksetup/images/help_small.gif"); 057 058 /** 059 * The left inset of the help icon. 060 */ 061 private final int INSET_WITH_ICON= 3; 062 063 /** 064 * The constructor of this panel. 065 * @param text the text of the panel. 066 * @param tooltipIcon the tool tip of the help icon. 067 */ 068 public SelectableLabelWithHelpIcon(LocalizableMessage text, LocalizableMessage tooltipIcon) 069 { 070 super(new GridBagLayout()); 071 setOpaque(false); 072 label.setText(Utilities.applyFont(text.toString(), 073 label.getFont())); 074 if (tooltipIcon != null) 075 { 076 iconLabel.setToolTipText(tooltipIcon.toString()); 077 } 078 GridBagConstraints gbc = new GridBagConstraints(); 079 gbc.gridx = 0; 080 gbc.gridy = 0; 081 gbc.gridwidth = 1; 082 add(label, gbc); 083 gbc.gridx ++; 084 gbc.insets.left = INSET_WITH_ICON; 085 add(iconLabel, gbc); 086 gbc.insets.left = 0; 087 gbc.weightx = 1.0; 088 gbc.fill = GridBagConstraints.HORIZONTAL; 089 gbc.gridx ++; 090 add(Box.createHorizontalGlue(), gbc); 091 092 Utilities.addClickTooltipListener(iconLabel); 093 } 094 095 /** 096 * Sets the text on the label. The text is assumed to be in HTML format 097 * but the font will be imposed by the font specified using {@link #setFont}. 098 * @param text the text to be displayed. 099 */ 100 public void setText(String text) 101 { 102 label.setText(Utilities.applyFont(text, label.getFont())); 103 } 104 105 /** 106 * Returns the text displayed on the panel. 107 * @return the text displayed on the panel. 108 */ 109 public String getText() 110 { 111 return label.getText(); 112 } 113 114 /** 115 * Sets the font to be used in this panel. 116 * @param font the font. 117 */ 118 public void setFont(Font font) 119 { 120 // This is called by the constructor of JPanel. 121 if (label != null) 122 { 123 label.setFont(font); 124 } 125 } 126 127 /** 128 * Sets the tool tip to be used in the help icon. 129 * @param tooltip the tool tip text. 130 */ 131 public void setHelpTooltip(String tooltip) 132 { 133 iconLabel.setToolTipText(tooltip); 134 } 135 136 /** 137 * Returns the tool tip to be used in the help icon. 138 * @return the tool tip to be used in the help icon. 139 */ 140 public String getHelpTooltip() 141 { 142 return iconLabel.getToolTipText(); 143 } 144 145 /** 146 * Sets whether the help icon is visible or not. 147 * @param visible whether the help icon is visible or not. 148 */ 149 public void setHelpIconVisible(boolean visible) 150 { 151 if (visible) 152 { 153 if (iconLabel.getIcon() != icon) 154 { 155 iconLabel.setIcon(icon); 156 } 157 } 158 else if (iconLabel.getIcon() != null) 159 { 160 iconLabel.setIcon(null); 161 } 162 } 163 164 /** 165 * Sets the foreground color for the text in this panel. 166 * @param color the foreground color for the text in this panel. 167 */ 168 public void setForeground(Color color) 169 { 170 super.setForeground(color); 171 if (label != null) 172 { 173 // This is called in the constructor of the object. 174 label.setForeground(color); 175 } 176 } 177 178 /** {@inheritDoc} */ 179 public String getToolTipText(MouseEvent ev) 180 { 181 int x = ev.getPoint().x; 182 boolean display = x > label.getPreferredSize().width - 10; 183 184 if (display) 185 { 186 return getHelpTooltip(); 187 } 188 else 189 { 190 return null; 191 } 192 } 193}