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