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