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