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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.awt.Component;
033import java.awt.GridBagConstraints;
034import java.awt.event.KeyAdapter;
035import java.awt.event.KeyEvent;
036import java.awt.event.MouseAdapter;
037import java.awt.event.MouseEvent;
038import java.util.Comparator;
039import java.util.TreeSet;
040
041import javax.swing.DefaultListModel;
042import javax.swing.JLabel;
043import javax.swing.JList;
044
045import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
046import org.opends.guitools.controlpanel.ui.components.TitlePanel;
047import org.opends.guitools.controlpanel.util.LowerCaseComparator;
048import org.opends.guitools.controlpanel.util.Utilities;
049import org.forgerock.i18n.LocalizableMessage;
050import org.forgerock.opendj.ldap.schema.Syntax;
051import org.opends.server.types.AttributeType;
052import org.opends.server.types.Schema;
053
054/**
055 * Panel containing information about an attribute syntax.
056 *
057 */
058public class AttributeSyntaxPanel extends SchemaElementPanel
059{
060  private static final long serialVersionUID = -2426247742251904863L;
061  private TitlePanel titlePanel = new TitlePanel(LocalizableMessage.EMPTY,
062      LocalizableMessage.EMPTY);
063  private JLabel name = Utilities.createDefaultLabel();
064  private JLabel oid = Utilities.createDefaultLabel();
065  private JLabel description = Utilities.createDefaultLabel();
066  private JList usedByAttributes = new JList(new DefaultListModel());
067
068  /**
069   * Default constructor.
070   *
071   */
072  public AttributeSyntaxPanel()
073  {
074    super();
075    createLayout();
076  }
077
078  /** {@inheritDoc} */
079  public LocalizableMessage getTitle()
080  {
081    return INFO_CTRL_PANEL_ATTRIBUTE_SYNTAX_TITLE.get();
082  }
083
084  /** {@inheritDoc} */
085  public Component getPreferredFocusComponent()
086  {
087    return usedByAttributes;
088  }
089
090  /** {@inheritDoc} */
091  public void configurationChanged(ConfigurationChangeEvent ev)
092  {
093  }
094
095  /** {@inheritDoc} */
096  public void okClicked()
097  {
098  }
099
100  /**
101   * Creates the layout of the panel (but the contents are not populated here).
102   */
103  private void createLayout()
104  {
105    GridBagConstraints gbc = new GridBagConstraints();
106    titlePanel.setTitle(INFO_CTRL_PANEL_ATTRIBUTE_SYNTAX_DETAILS.get());
107    gbc.fill = GridBagConstraints.NONE;
108    gbc.anchor = GridBagConstraints.WEST;
109    gbc.gridwidth = 2;
110    gbc.gridy = 0;
111    gbc.insets.top = 5;
112    gbc.insets.bottom = 7;
113    add(titlePanel, gbc);
114
115    gbc.insets.bottom = 0;
116    gbc.insets.top = 8;
117
118    LocalizableMessage[] labels = {INFO_CTRL_PANEL_ATTRIBUTE_SYNTAX_NAME.get(),
119        INFO_CTRL_PANEL_ATTRIBUTE_SYNTAX_OID.get(),
120        INFO_CTRL_PANEL_ATTRIBUTE_SYNTAX_DESCRIPTION.get()};
121    JLabel[] values = {name, oid, description};
122    gbc.gridy ++;
123    gbc.gridwidth = 1;
124    gbc.anchor = GridBagConstraints.WEST;
125    for (int i=0; i < labels.length; i++)
126    {
127      gbc.insets.left = 0;
128      gbc.gridx = 0;
129      JLabel l = Utilities.createPrimaryLabel(labels[i]);
130      add(l, gbc);
131      gbc.insets.left = 10;
132      gbc.gridx = 1;
133      add(values[i], gbc);
134      gbc.gridy ++;
135    }
136
137    usedByAttributes.setVisibleRowCount(15);
138    gbc.anchor = GridBagConstraints.NORTHWEST;
139    gbc.insets.left = 0;
140    gbc.gridx = 0;
141    JLabel l = Utilities.createPrimaryLabel(
142        INFO_CTRL_PANEL_USED_BY_ATTRIBUTES.get());
143    gbc.weightx = 0.0;
144    gbc.fill = GridBagConstraints.HORIZONTAL;
145    add(l, gbc);
146    gbc.insets.left = 10;
147    gbc.gridx = 1;
148    gbc.weighty = 1.0;
149    gbc.weightx = 1.0;
150    gbc.fill = GridBagConstraints.BOTH;
151    gbc.insets.top = 10;
152    add(Utilities.createScrollPane(usedByAttributes), gbc);
153
154    MouseAdapter clickListener = new MouseAdapter()
155    {
156      public void mouseClicked(MouseEvent ev)
157      {
158        if (ev.getClickCount() == 1)
159        {
160          usedBySelected();
161        }
162      }
163    };
164    usedByAttributes.addMouseListener(clickListener);
165
166    KeyAdapter keyListener = new KeyAdapter()
167    {
168      /** {@inheritDoc} */
169      public void keyTyped(KeyEvent ev)
170      {
171        if (ev.getKeyChar() == KeyEvent.VK_SPACE ||
172            ev.getKeyChar() == KeyEvent.VK_ENTER)
173        {
174          usedBySelected();
175        }
176      }
177    };
178    usedByAttributes.addKeyListener(keyListener);
179
180    setBorder(PANEL_BORDER);
181  }
182
183  /**
184   * Updates the contents of the panel.
185   * @param syntax the attribute syntax that the panel must display.
186   * @param schema the schema.
187   */
188  public void update(Syntax syntax, Schema schema)
189  {
190    String n = syntax.getName();
191    if (n == null)
192    {
193      n = NOT_APPLICABLE.toString();
194    }
195    titlePanel.setDetails(LocalizableMessage.raw(n));
196    name.setText(n);
197    oid.setText(syntax.getOID());
198
199    n = syntax.getDescription();
200    if (n == null)
201    {
202      n = NOT_APPLICABLE.toString();
203    }
204    description.setText(n);
205
206    Comparator<String> lowerCaseComparator = new LowerCaseComparator();
207    TreeSet<String> attributes = new TreeSet<>(lowerCaseComparator);
208    for (AttributeType attr : schema.getAttributeTypes().values())
209    {
210      if (syntax == attr.getSyntax())
211      {
212        attributes.add(attr.getNameOrOID());
213      }
214    }
215    DefaultListModel model = (DefaultListModel)usedByAttributes.getModel();
216    model.clear();
217    for (String attr : attributes)
218    {
219      model.addElement(attr);
220    }
221  }
222
223  private void usedBySelected()
224  {
225    String o = (String)usedByAttributes.getSelectedValue();
226    if (o != null)
227    {
228      Schema schema = getInfo().getServerDescriptor().getSchema();
229      if (schema != null)
230      {
231        AttributeType attr = schema.getAttributeType(o.toLowerCase());
232        if (attr != null)
233        {
234          notifySchemaSelectionListeners(attr);
235        }
236      }
237    }
238  }
239}