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 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017 018package org.opends.guitools.controlpanel.ui.components; 019 020import static org.opends.messages.AdminToolMessages.*; 021 022import java.awt.GridBagConstraints; 023import java.awt.GridBagLayout; 024import java.awt.event.ActionListener; 025import java.awt.event.KeyEvent; 026import java.util.Set; 027import java.util.TreeSet; 028 029import javax.swing.ImageIcon; 030import javax.swing.JLabel; 031import javax.swing.JPanel; 032import javax.swing.KeyStroke; 033 034import org.opends.guitools.controlpanel.browser.IconPool; 035import org.opends.guitools.controlpanel.datamodel.ObjectClassValue; 036import org.opends.guitools.controlpanel.ui.ColorAndFontConstants; 037import org.opends.guitools.controlpanel.util.Utilities; 038import org.forgerock.i18n.LocalizableMessage; 039import org.forgerock.i18n.LocalizableMessageBuilder; 040 041/** 042 * A simple panel used in the LDAP entry viewers to display the object class 043 * values of an entry. It displays the structural and auxiliary object classes. 044 * It does not allow to edit directly the object class value. It is used for 045 * instance in the entry editors (simplified and table views). 046 * 047 */ 048public class ObjectClassCellPanel extends JPanel 049{ 050 private static final long serialVersionUID = -2362754512894888888L; 051 private JLabel label; 052 private CellEditorButton editButton; 053 private ObjectClassValue value; 054 private JLabel lockLabel = Utilities.createDefaultLabel(); 055 056 private ImageIcon lockIcon = 057 Utilities.createImageIcon(IconPool.IMAGE_PATH+"/field-locked.png"); 058 059 /** 060 * Default constructor. 061 * 062 */ 063 public ObjectClassCellPanel() 064 { 065 super(new GridBagLayout()); 066 setOpaque(false); 067 GridBagConstraints gbc = new GridBagConstraints(); 068 gbc.fill = GridBagConstraints.HORIZONTAL; 069 gbc.gridx = 0; 070 gbc.gridy = 0; 071 label = Utilities.createDefaultLabel( 072 INFO_CTRL_PANEL_NO_VALUE_SPECIFIED.get()); 073 gbc.weightx = 1.0; 074 add(label, gbc); 075 gbc.gridx ++; 076 editButton = new CellEditorButton(INFO_CTRL_PANEL_EDIT_BUTTON_LABEL.get()); 077 editButton.setForeground(ColorAndFontConstants.buttonForeground); 078 editButton.setOpaque(false); 079 gbc.insets.left = 5; 080 gbc.anchor = GridBagConstraints.NORTH; 081 gbc.weightx = 0.0; 082 add(editButton, gbc); 083 gbc.weightx = 0.0; 084 gbc.insets.left = 5; 085 gbc.gridx ++; 086 add(lockLabel, gbc); 087 lockLabel.setVisible(false); 088 } 089 090 /** 091 * Sets the object class value to be displayed in this panel. 092 * @param value the object class value to be displayed in this panel. 093 */ 094 public void setValue(ObjectClassValue value) 095 { 096 label.setText(getMessage(value).toString()); 097 this.value = value; 098 } 099 100 /** 101 * Returns the object class value displayed in this panel. 102 * @return the object class value displayed in this panel. 103 */ 104 public ObjectClassValue getValue() 105 { 106 return value; 107 } 108 109 /** 110 * Updates the visibility of the lock icon. 111 * @param visible whether the lock icon is visible or not. 112 */ 113 public void setLockIconVisible(boolean visible) 114 { 115 if (visible) 116 { 117 lockLabel.setIcon(lockIcon); 118 lockLabel.setVisible(true); 119 } 120 else 121 { 122 lockLabel.setIcon(null); 123 lockLabel.setVisible(false); 124 } 125 } 126 127 /** 128 * Explicitly request the focus for the edit button of this panel. 129 * 130 */ 131 public void requestFocusForButton() 132 { 133 editButton.requestFocusInWindow(); 134 } 135 136 137 /** 138 * Adds an action listener to this panel. The action listener will be 139 * invoked when the user clicks on the 'Edit' button. 140 * @param listener the action listener. 141 */ 142 public void addEditActionListener(ActionListener listener) 143 { 144 editButton.addActionListener(listener); 145 } 146 147 /** 148 * Removes an action listener previously added with the method 149 * addEditActionListener. 150 * @param listener the action listener. 151 */ 152 public void removeEditActionListener(ActionListener listener) 153 { 154 editButton.removeActionListener(listener); 155 } 156 157 /** 158 * Updates the visibility of the edit button. 159 * @param visible whether the edit button must be visible or not. 160 */ 161 public void setEditButtonVisible(boolean visible) 162 { 163 editButton.setVisible(visible); 164 } 165 166 /** {@inheritDoc} */ 167 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 168 int condition, boolean pressed) 169 { 170 return editButton.processKeyBinding(ks, e, condition, pressed); 171 } 172 173 /** 174 * Returns the message describing the provided object class value. 175 * @param value the object class value. 176 * @return the message describing the provided object class value. 177 */ 178 public LocalizableMessage getMessage(ObjectClassValue value) 179 { 180 LocalizableMessageBuilder sb = new LocalizableMessageBuilder(); 181 if (value != null) 182 { 183 Set<String> aux = new TreeSet<>(value.getAuxiliary()); 184 aux.remove("top"); 185 if (value.getStructural() != null) 186 { 187 sb.append(value.getStructural()); 188 } 189 if (!aux.isEmpty()) 190 { 191 if (sb.length() > 0) 192 { 193 sb.append("<br>"); 194 } 195 sb.append(INFO_CTRL_PANEL_OBJECTCLASS_CELL_PANEL_AUXILIARY.get( 196 Utilities.getStringFromCollection(aux, ", "))); 197 } 198 } 199 if (sb.length() > 0) 200 { 201 return LocalizableMessage.raw("<html>"+Utilities.applyFont(sb.toString(), 202 ColorAndFontConstants.defaultFont)); 203 } 204 else 205 { 206 return INFO_CTRL_PANEL_NO_VALUE_SPECIFIED.get(); 207 } 208 } 209}