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 */
017package org.opends.guitools.controlpanel.ui;
018
019import java.awt.Component;
020import java.awt.GridBagConstraints;
021import java.awt.event.KeyAdapter;
022import java.awt.event.KeyEvent;
023import java.awt.event.MouseAdapter;
024import java.awt.event.MouseEvent;
025import java.util.TreeSet;
026
027import javax.swing.DefaultListModel;
028import javax.swing.JLabel;
029import javax.swing.JList;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.forgerock.opendj.ldap.schema.MatchingRule;
033import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
034import org.opends.guitools.controlpanel.ui.components.TitlePanel;
035import org.opends.guitools.controlpanel.util.LowerCaseComparator;
036import org.opends.guitools.controlpanel.util.Utilities;
037import org.forgerock.opendj.ldap.schema.Syntax;
038import org.forgerock.opendj.ldap.schema.AttributeType;
039import org.opends.server.types.Schema;
040
041import static org.opends.messages.AdminToolMessages.*;
042
043/** Class displaying the contents of a matching rule. */
044public class MatchingRulePanel extends SchemaElementPanel
045{
046  private static final long serialVersionUID = 2440493955626646008L;
047  private TitlePanel titlePanel = new TitlePanel(LocalizableMessage.EMPTY,
048      LocalizableMessage.EMPTY);
049  private JLabel name = Utilities.createDefaultLabel();
050  private JLabel oid = Utilities.createDefaultLabel();
051  private JLabel description = Utilities.createDefaultLabel();
052  private JLabel syntax = Utilities.createDefaultLabel();
053  private JList usedByAttributes = new JList(new DefaultListModel());
054
055  /** Default constructor. */
056  public MatchingRulePanel()
057  {
058    super();
059    createLayout();
060  }
061
062  @Override
063  public LocalizableMessage getTitle()
064  {
065    return INFO_CTRL_PANEL_MATCHING_RULE_PANEL_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    gbc.gridy ++;
089    titlePanel.setTitle(INFO_CTRL_PANEL_MATCHING_RULE_DETAILS.get());
090    gbc.fill = GridBagConstraints.NONE;
091    gbc.anchor = GridBagConstraints.WEST;
092    gbc.gridwidth = 2;
093    gbc.gridy = 0;
094    gbc.insets.top = 5;
095    gbc.insets.bottom = 7;
096    add(titlePanel, gbc);
097
098    gbc.insets.bottom = 0;
099    gbc.insets.top = 8;
100    LocalizableMessage[] labels = {
101        INFO_CTRL_PANEL_MATCHING_RULE_NAME.get(),
102        INFO_CTRL_PANEL_MATCHING_RULE_OID.get(),
103        INFO_CTRL_PANEL_MATCHING_RULE_DESCRIPTION.get(),
104        INFO_CTRL_PANEL_MATCHING_RULE_SYNTAX.get()
105        };
106    JLabel[] values = {name, oid, description, syntax};
107    gbc.gridy ++;
108    gbc.gridwidth = 1;
109    gbc.anchor = GridBagConstraints.WEST;
110    for (int i=0; i < labels.length; i++)
111    {
112      gbc.insets.left = 0;
113      gbc.gridx = 0;
114      JLabel l = Utilities.createPrimaryLabel(labels[i]);
115      add(l, gbc);
116      gbc.insets.left = 10;
117      gbc.gridx = 1;
118      add(values[i], gbc);
119      gbc.gridy ++;
120    }
121
122    usedByAttributes.setVisibleRowCount(15);
123    gbc.anchor = GridBagConstraints.NORTHWEST;
124    gbc.insets.left = 0;
125    gbc.gridx = 0;
126    JLabel l = Utilities.createPrimaryLabel(
127        INFO_CTRL_PANEL_MATCHING_RULE_USED_BY.get());
128    gbc.weightx = 0.0;
129    gbc.fill = GridBagConstraints.HORIZONTAL;
130    add(l, gbc);
131    gbc.insets.left = 10;
132    gbc.gridx = 1;
133    gbc.weighty = 1.0;
134    gbc.weightx = 1.0;
135    gbc.fill = GridBagConstraints.BOTH;
136    gbc.insets.top = 10;
137    add(Utilities.createScrollPane(usedByAttributes), gbc);
138
139    MouseAdapter clickListener = new MouseAdapter()
140    {
141      @Override
142      public void mouseClicked(MouseEvent ev)
143      {
144        if (ev.getClickCount() == 1)
145        {
146          usedBySelected();
147        }
148      }
149    };
150    usedByAttributes.addMouseListener(clickListener);
151
152    KeyAdapter keyListener = new KeyAdapter()
153    {
154      @Override
155      public void keyTyped(KeyEvent ev)
156      {
157        if (ev.getKeyChar() == KeyEvent.VK_SPACE ||
158            ev.getKeyChar() == KeyEvent.VK_ENTER)
159        {
160          usedBySelected();
161        }
162      }
163    };
164    usedByAttributes.addKeyListener(keyListener);
165    setBorder(PANEL_BORDER);
166  }
167
168  /**
169   * Updates the contents of the panel with the provided matching rule.
170   * @param matchingRule the matching rule.
171   * @param schema the schema.
172   */
173  public void update(MatchingRule matchingRule, Schema schema)
174  {
175    String n = matchingRule.getNameOrOID();
176    if (n == null)
177    {
178      n = NOT_APPLICABLE.toString();
179    }
180    titlePanel.setDetails(LocalizableMessage.raw(n));
181    name.setText(n);
182    oid.setText(matchingRule.getOID());
183    Syntax s = null;
184    String syntaxOID = matchingRule.getSyntax().getOID();
185    for (Syntax candidate : schema.getSyntaxes())
186    {
187      if (candidate.getOID().equals(syntaxOID))
188      {
189        s = candidate;
190        break;
191      }
192    }
193    if (s != null)
194    {
195      syntax.setText(Utilities.getSyntaxText(s));
196    }
197    else
198    {
199      syntax.setText(syntaxOID);
200    }
201
202    n = matchingRule.getDescription();
203    if (n == null)
204    {
205      n = NOT_APPLICABLE.toString();
206    }
207    description.setText(n);
208
209    TreeSet<String> attributes = new TreeSet<>(new LowerCaseComparator());
210    for (AttributeType attr : schema.getAttributeTypes())
211    {
212      if (matchingRule.equals(attr.getApproximateMatchingRule()) ||
213          matchingRule.equals(attr.getEqualityMatchingRule()) ||
214          matchingRule.equals(attr.getSubstringMatchingRule()) ||
215          matchingRule.equals(attr.getOrderingMatchingRule()))
216      {
217        attributes.add(attr.getNameOrOID());
218      }
219    }
220    DefaultListModel model = (DefaultListModel)usedByAttributes.getModel();
221    model.clear();
222    for (String attr : attributes)
223    {
224      model.addElement(attr);
225    }
226  }
227
228  private void usedBySelected()
229  {
230    String o = (String)usedByAttributes.getSelectedValue();
231    if (o != null)
232    {
233      Schema schema = getInfo().getServerDescriptor().getSchema();
234      if (schema != null)
235      {
236        AttributeType attr = schema.getAttributeType(o.toLowerCase());
237        if (attr != null)
238        {
239          notifySchemaSelectionListeners(attr);
240        }
241      }
242    }
243  }
244}