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-2016 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.forgerock.i18n.LocalizableMessage; 035import org.forgerock.i18n.LocalizableMessageBuilder; 036import org.opends.guitools.controlpanel.browser.IconPool; 037import org.opends.guitools.controlpanel.datamodel.ObjectClassValue; 038import org.opends.guitools.controlpanel.ui.ColorAndFontConstants; 039import org.opends.guitools.controlpanel.util.Utilities; 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 */ 047public class ObjectClassCellPanel extends JPanel 048{ 049 private static final long serialVersionUID = -2362754512894888888L; 050 private final JLabel label; 051 private final CellEditorButton editButton; 052 private ObjectClassValue value; 053 private final JLabel lockLabel = Utilities.createDefaultLabel(); 054 055 private final ImageIcon lockIcon = 056 Utilities.createImageIcon(IconPool.IMAGE_PATH+"/field-locked.png"); 057 058 /** Default constructor. */ 059 public ObjectClassCellPanel() 060 { 061 super(new GridBagLayout()); 062 setOpaque(false); 063 GridBagConstraints gbc = new GridBagConstraints(); 064 gbc.fill = GridBagConstraints.HORIZONTAL; 065 gbc.gridx = 0; 066 gbc.gridy = 0; 067 label = Utilities.createDefaultLabel( 068 INFO_CTRL_PANEL_NO_VALUE_SPECIFIED.get()); 069 gbc.weightx = 1.0; 070 add(label, gbc); 071 gbc.gridx ++; 072 editButton = new CellEditorButton(INFO_CTRL_PANEL_EDIT_BUTTON_LABEL.get()); 073 editButton.setForeground(ColorAndFontConstants.buttonForeground); 074 editButton.setOpaque(false); 075 gbc.insets.left = 5; 076 gbc.anchor = GridBagConstraints.NORTH; 077 gbc.weightx = 0.0; 078 add(editButton, gbc); 079 gbc.weightx = 0.0; 080 gbc.insets.left = 5; 081 gbc.gridx ++; 082 add(lockLabel, gbc); 083 lockLabel.setVisible(false); 084 } 085 086 /** 087 * Sets the object class value to be displayed in this panel. 088 * @param value the object class value to be displayed in this panel. 089 */ 090 public void setValue(ObjectClassValue value) 091 { 092 label.setText(getMessage(value).toString()); 093 this.value = value; 094 } 095 096 /** 097 * Returns the object class value displayed in this panel. 098 * @return the object class value displayed in this panel. 099 */ 100 public ObjectClassValue getValue() 101 { 102 return value; 103 } 104 105 /** 106 * Updates the visibility of the lock icon. 107 * @param visible whether the lock icon is visible or not. 108 */ 109 public void setLockIconVisible(boolean visible) 110 { 111 if (visible) 112 { 113 lockLabel.setIcon(lockIcon); 114 lockLabel.setVisible(true); 115 } 116 else 117 { 118 lockLabel.setIcon(null); 119 lockLabel.setVisible(false); 120 } 121 } 122 123 /** 124 * Adds an action listener to this panel. The action listener will be 125 * invoked when the user clicks on the 'Edit' button. 126 * @param listener the action listener. 127 */ 128 public void addEditActionListener(ActionListener listener) 129 { 130 editButton.addActionListener(listener); 131 } 132 133 /** 134 * Removes an action listener previously added with the method 135 * addEditActionListener. 136 * @param listener the action listener. 137 */ 138 public void removeEditActionListener(ActionListener listener) 139 { 140 editButton.removeActionListener(listener); 141 } 142 143 /** 144 * Updates the visibility of the edit button. 145 * @param visible whether the edit button must be visible or not. 146 */ 147 public void setEditButtonVisible(boolean visible) 148 { 149 editButton.setVisible(visible); 150 } 151 152 @Override 153 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 154 int condition, boolean pressed) 155 { 156 return editButton.processKeyBinding(ks, e, condition, pressed); 157 } 158 159 /** 160 * Returns the message describing the provided object class value. 161 * @param value the object class value. 162 * @return the message describing the provided object class value. 163 */ 164 public LocalizableMessage getMessage(ObjectClassValue value) 165 { 166 LocalizableMessageBuilder sb = new LocalizableMessageBuilder(); 167 if (value != null) 168 { 169 Set<String> aux = new TreeSet<>(value.getAuxiliary()); 170 aux.remove("top"); 171 if (value.getStructural() != null) 172 { 173 sb.append(value.getStructural()); 174 } 175 if (!aux.isEmpty()) 176 { 177 if (sb.length() > 0) 178 { 179 sb.append("<br>"); 180 } 181 sb.append(INFO_CTRL_PANEL_OBJECTCLASS_CELL_PANEL_AUXILIARY.get( 182 Utilities.getStringFromCollection(aux, ", "))); 183 } 184 } 185 if (sb.length() > 0) 186 { 187 return LocalizableMessage.raw("<html>"+Utilities.applyFont(sb.toString(), 188 ColorAndFontConstants.defaultFont)); 189 } 190 else 191 { 192 return INFO_CTRL_PANEL_NO_VALUE_SPECIFIED.get(); 193 } 194 } 195}