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