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