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