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