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