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-2009 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.CardLayout;
023import java.awt.Component;
024import java.awt.GridBagConstraints;
025
026import javax.swing.JPanel;
027
028import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
029import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
030import org.opends.guitools.controlpanel.event.
031 ConfigurationElementCreatedListener;
032import org.opends.guitools.controlpanel.event.SchemaElementSelectionListener;
033import org.opends.guitools.controlpanel.util.Utilities;
034import org.forgerock.i18n.LocalizableMessage;
035import org.forgerock.opendj.ldap.schema.Syntax;
036import org.forgerock.opendj.ldap.schema.MatchingRule;
037import org.forgerock.opendj.ldap.schema.AttributeType;
038import org.opends.server.types.ObjectClass;
039import org.opends.server.types.Schema;
040
041/**
042 * The panel on the right of the 'Manage Schema' panel.
043 *
044 */
045public class SchemaBrowserRightPanel extends StatusGenericPanel
046{
047  private static final long serialVersionUID = 5294502011852239497L;
048  private JPanel mainPanel;
049  private StandardObjectClassPanel standardObjectClassPanel =
050    new StandardObjectClassPanel();
051  private ConfigurationObjectClassPanel configurationObjectClassPanel =
052    new ConfigurationObjectClassPanel();
053  private CustomObjectClassPanel customObjectClassPanel =
054    new CustomObjectClassPanel();
055  private StandardAttributePanel standardAttributePanel =
056    new StandardAttributePanel();
057  private ConfigurationAttributePanel configurationAttributePanel =
058    new ConfigurationAttributePanel();
059  private CustomAttributePanel customAttributePanel =
060    new CustomAttributePanel();
061  private MatchingRulePanel matchingRulePanel = new MatchingRulePanel();
062  private AttributeSyntaxPanel attributeSyntaxPanel =
063    new AttributeSyntaxPanel();
064
065  private NoItemSelectedPanel noEntryPanel = new NoItemSelectedPanel();
066
067  private final SchemaElementPanel[] panels =
068  {   standardObjectClassPanel, configurationObjectClassPanel,
069      customObjectClassPanel,
070      standardAttributePanel, configurationAttributePanel, customAttributePanel,
071      matchingRulePanel,
072      attributeSyntaxPanel
073  };
074
075  private final String NOTHING_SELECTED = "Nothing Selected";
076
077
078  private SchemaElementPanel schemaElementPanel;
079
080  /**
081   * Default constructor.
082   *
083   */
084  public SchemaBrowserRightPanel()
085  {
086    super();
087    createLayout();
088  }
089
090  /**
091   * Displays a panel containing a message.
092   * @param msg the message.
093   *
094   */
095  public void displayMessage(LocalizableMessage msg)
096  {
097    schemaElementPanel = null;
098    noEntryPanel.setMessage(msg);
099    ((CardLayout)mainPanel.getLayout()).show(mainPanel, NOTHING_SELECTED);
100  }
101
102  /** {@inheritDoc} */
103  public void setInfo(ControlPanelInfo info)
104  {
105    super.setInfo(info);
106    for (StatusGenericPanel panel : panels)
107    {
108      panel.setInfo(info);
109    }
110  }
111
112  /**
113   * Adds an schema element selection listener.
114   * @param listener the schema element selection listener.
115   */
116  public void addSchemaElementSelectionListener(
117      SchemaElementSelectionListener listener)
118  {
119    for (SchemaElementPanel panel : panels)
120    {
121      panel.addSchemaElementSelectionListener(listener);
122    }
123  }
124
125  /**
126   * Removes an schema element selection listener.
127   * @param listener the schema element selection listener.
128   */
129  public void removeSchemaElementSelectionListener(
130      SchemaElementSelectionListener listener)
131  {
132    for (SchemaElementPanel panel : panels)
133    {
134      panel.removeSchemaElementSelectionListener(listener);
135    }
136  }
137
138  /**
139   * Adds a configuration element created listener.
140   * @param listener the listener.
141   */
142  public void addConfigurationElementCreatedListener(
143      ConfigurationElementCreatedListener listener)
144  {
145    super.addConfigurationElementCreatedListener(listener);
146    for (SchemaElementPanel panel : panels)
147    {
148      panel.addConfigurationElementCreatedListener(listener);
149    }
150  }
151
152  /**
153   * Removes a configuration element created listener.
154   * @param listener the listener.
155   */
156  public void removeConfigurationElementCreatedListener(
157      ConfigurationElementCreatedListener listener)
158  {
159    super.removeConfigurationElementCreatedListener(listener);
160    for (SchemaElementPanel panel : panels)
161    {
162      panel.removeConfigurationElementCreatedListener(listener);
163    }
164  }
165
166  /**
167   * Updates the contents of the panel with the provided standard object class.
168   * @param oc the object class.
169   * @param schema the schema.
170   */
171  public void updateStandardObjectClass(ObjectClass oc, Schema schema)
172  {
173    standardObjectClassPanel.update(oc, schema);
174    schemaElementPanel = standardObjectClassPanel;
175    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
176        standardObjectClassPanel.getTitle().toString());
177  }
178
179  /**
180   * Updates the contents of the panel with the provided configuration object
181   * class.
182   * @param oc the object class.
183   * @param schema the schema.
184   */
185  public void updateConfigurationObjectClass(ObjectClass oc, Schema schema)
186  {
187    configurationObjectClassPanel.update(oc, schema);
188    schemaElementPanel = configurationObjectClassPanel;
189    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
190        configurationObjectClassPanel.getTitle().toString());
191  }
192
193  /**
194   * Updates the contents of the panel with the provided custom object class.
195   * @param oc the object class.
196   * @param schema the schema.
197   */
198  public void updateCustomObjectClass(ObjectClass oc, Schema schema)
199  {
200    customObjectClassPanel.update(oc, schema);
201    schemaElementPanel = customObjectClassPanel;
202    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
203        customObjectClassPanel.getTitle().toString());
204  }
205
206  /**
207   * Updates the contents of the panel with the provided standard attribute.
208   * @param attr the attribute.
209   * @param schema the schema.
210   */
211  public void updateStandardAttribute(AttributeType attr, Schema schema)
212  {
213    standardAttributePanel.update(attr, schema);
214    schemaElementPanel = standardAttributePanel;
215    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
216        standardAttributePanel.getTitle().toString());
217  }
218
219  /**
220   * Updates the contents of the panel with the provided configuration
221   * attribute.
222   * @param attr the attribute.
223   * @param schema the schema.
224   */
225  public void updateConfigurationAttribute(AttributeType attr, Schema schema)
226  {
227    configurationAttributePanel.update(attr, schema);
228    schemaElementPanel = configurationAttributePanel;
229    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
230        configurationAttributePanel.getTitle().toString());
231  }
232
233  /**
234   * Updates the contents of the panel with the provided custom attribute.
235   * @param attr the attribute.
236   * @param schema the schema.
237   */
238  public void updateCustomAttribute(AttributeType attr, Schema schema)
239  {
240    customAttributePanel.update(attr, schema);
241    schemaElementPanel = customAttributePanel;
242    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
243        customAttributePanel.getTitle().toString());
244  }
245
246  /**
247   * Updates the contents of the panel with the provided matching rule.
248   * @param matchingRule the matching rule.
249   * @param schema the schema.
250   */
251  public void updateMatchingRule(MatchingRule matchingRule, Schema schema)
252  {
253    matchingRulePanel.update(matchingRule, schema);
254    schemaElementPanel = matchingRulePanel;
255    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
256        matchingRulePanel.getTitle().toString());
257  }
258
259  /**
260   * Updates the contents of the panel with the provided attribute syntax.
261   * @param syntax the attribute syntax.
262   * @param schema the schema.
263   */
264  public void updateAttributeSyntax(Syntax syntax, Schema schema)
265  {
266    attributeSyntaxPanel.update(syntax, schema);
267    schemaElementPanel = attributeSyntaxPanel;
268    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
269        attributeSyntaxPanel.getTitle().toString());
270  }
271
272  /**
273   * Creates the layout of the panel (but the contents are not populated here).
274   */
275  private void createLayout()
276  {
277    GridBagConstraints gbc = new GridBagConstraints();
278    CardLayout cardLayout = new CardLayout();
279    mainPanel = new JPanel(cardLayout);
280    mainPanel.setOpaque(false);
281    noEntryPanel.setMessage(
282        INFO_CTRL_PANEL_NO_SCHEMA_ITEM_SELECTED_LABEL.get());
283    mainPanel.add(noEntryPanel, NOTHING_SELECTED);
284    StatusGenericPanel[] panelsWithScroll =
285    {
286        standardObjectClassPanel,
287        configurationObjectClassPanel,
288        standardAttributePanel,
289        configurationAttributePanel,
290        matchingRulePanel,
291        attributeSyntaxPanel
292    };
293    StatusGenericPanel[] panelsWithNoScroll =
294    {
295        customObjectClassPanel,
296        customAttributePanel
297    };
298    for (StatusGenericPanel panel : panelsWithScroll)
299    {
300      mainPanel.add(Utilities.createBorderLessScrollBar(panel),
301          panel.getTitle().toString());
302    }
303    for (StatusGenericPanel panel : panelsWithNoScroll)
304    {
305      mainPanel.add(panel, panel.getTitle().toString());
306    }
307    cardLayout.show(mainPanel, NOTHING_SELECTED);
308    gbc.gridx = 0;
309    gbc.gridy = 0;
310    gbc.weightx = 1.0;
311    gbc.weighty = 1.0;
312    gbc.fill = GridBagConstraints.BOTH;
313    add(mainPanel, gbc);
314  }
315
316  /** {@inheritDoc} */
317  public void okClicked()
318  {
319    // No ok button
320  }
321
322  /** {@inheritDoc} */
323  public GenericDialog.ButtonType getButtonType()
324  {
325    return GenericDialog.ButtonType.NO_BUTTON;
326  }
327
328  /** {@inheritDoc} */
329  public LocalizableMessage getTitle()
330  {
331    return INFO_CTRL_PANEL_SCHEMA_BROWSER_RIGHT_PANEL_TITLE.get();
332  }
333
334  /** {@inheritDoc} */
335  public Component getPreferredFocusComponent()
336  {
337    return null;
338  }
339
340  /** {@inheritDoc} */
341  public void configurationChanged(ConfigurationChangeEvent ev)
342  {
343  }
344
345  /**
346   * Method used to know if there are unsaved changes or not.  It is used by
347   * the schema selection listener when the user changes the selection.
348   * @return <CODE>true</CODE> if there are unsaved changes (and so the
349   * selection of the schema should be canceled) and <CODE>false</CODE>
350   * otherwise.
351   */
352  public boolean mustCheckUnsavedChanges()
353  {
354    return schemaElementPanel != null && schemaElementPanel.mustCheckUnsavedChanges();
355  }
356
357  /**
358   * Tells whether the user chose to save the changes in the panel, to not save
359   * them or simply canceled the selection in the tree.
360   * @return the value telling whether the user chose to save the changes in the
361   * panel, to not save them or simply canceled the selection in the tree.
362   */
363  public UnsavedChangesDialog.Result checkUnsavedChanges()
364  {
365    if (schemaElementPanel != null)
366    {
367      return schemaElementPanel.checkUnsavedChanges();
368    }
369    return UnsavedChangesDialog.Result.DO_NOT_SAVE;
370  }
371}