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 2015 ForgeRock AS.
026 */
027package org.opends.guitools.controlpanel.ui;
028
029import java.util.HashSet;
030import java.util.LinkedHashSet;
031import java.util.Set;
032
033import javax.swing.JList;
034import javax.swing.border.Border;
035import javax.swing.border.EmptyBorder;
036
037import org.opends.guitools.controlpanel.event.SchemaElementSelectionEvent;
038import org.opends.guitools.controlpanel.event.SchemaElementSelectionListener;
039import org.opends.server.types.AttributeType;
040import org.opends.server.types.ObjectClass;
041import org.opends.server.types.Schema;
042
043/**
044 * Abstract class used to re-factor some code among the panels that display the
045 * contents of a schema element.
046 */
047public abstract class SchemaElementPanel extends StatusGenericPanel
048{
049  private static final long serialVersionUID = -8556383593966382604L;
050
051  private Set<SchemaElementSelectionListener> listeners = new HashSet<>();
052
053  /**
054   * The empty border shared by all the schema element panels.
055   */
056  protected Border PANEL_BORDER = new EmptyBorder(10, 10, 10, 10);
057
058  /**
059   * Adds a schema element selection listener.
060   * @param listener the listener.
061   */
062  public void addSchemaElementSelectionListener(
063      SchemaElementSelectionListener listener)
064  {
065    listeners.add(listener);
066  }
067
068  /**
069   * Removes a schema element selection listener.
070   * @param listener the listener.
071   */
072  public void removeSchemaElementSelectionListener(
073      SchemaElementSelectionListener listener)
074  {
075    listeners.add(listener);
076  }
077
078  /**
079   * Notifies to all the listeners that a new schema element was selected.
080   * @param schemaElement the new schema element that has been selected.
081   */
082  protected void notifySchemaSelectionListeners(Object schemaElement)
083  {
084    for (SchemaElementSelectionListener listener : listeners)
085    {
086      listener.schemaElementSelected(
087          new SchemaElementSelectionEvent(this, schemaElement));
088    }
089  }
090
091  /**
092   * Method used to know if there are unsaved changes or not.  It is used by
093   * the schema selection listener when the user changes the selection.
094   * @return <CODE>true</CODE> if there are unsaved changes (and so the
095   * selection of the schema should be canceled) and <CODE>false</CODE>
096   * otherwise.
097   */
098  public boolean mustCheckUnsavedChanges()
099  {
100    return false;
101  }
102
103  /**
104   * Tells whether the user chose to save the changes in the panel, to not save
105   * them or simply cancelled the selection in the tree.
106   * @return the value telling whether the user chose to save the changes in the
107   * panel, to not save them or simply cancelled the selection in the tree.
108   */
109  public UnsavedChangesDialog.Result checkUnsavedChanges()
110  {
111    return UnsavedChangesDialog.Result.DO_NOT_SAVE;
112  }
113
114  /**
115   * Method called when there is an object class selected in a list.
116   * @param list the list.
117   */
118  protected void objectClassSelected(JList list)
119  {
120    String o = (String)list.getSelectedValue();
121    if (o != null)
122    {
123      Schema schema = getInfo().getServerDescriptor().getSchema();
124      if (schema != null)
125      {
126        ObjectClass oc = schema.getObjectClass(o.toLowerCase());
127        if (oc != null)
128        {
129          notifySchemaSelectionListeners(oc);
130        }
131      }
132    }
133  }
134
135  /**
136   * Returns the list of aliases for the provided attribute.
137   * @param attr the attribute.
138   * @return the list of aliases for the provided attribute.
139   */
140  protected Set<String> getAliases(AttributeType attr)
141  {
142    return getAliases(attr.getNormalizedNames(), attr.getPrimaryName());
143  }
144
145  /**
146   * Returns the list of aliases for the provided object class.
147   * @param oc the object class.
148   * @return the list of aliases for the provided object class.
149   */
150  protected Set<String> getAliases(ObjectClass oc)
151  {
152    return getAliases(oc.getNormalizedNames(), oc.getPrimaryName());
153  }
154
155  private Set<String> getAliases(Set<String> names, String primaryName)
156  {
157    Set<String> aliases = new LinkedHashSet<>();
158    if (primaryName == null)
159    {
160      primaryName = "";
161    }
162    for (String name : names)
163    {
164      if (!name.equalsIgnoreCase(primaryName))
165      {
166        aliases.add(name);
167      }
168    }
169    return aliases;
170  }
171}