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 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.awt.CardLayout;
033import java.awt.Component;
034import java.awt.GridBagConstraints;
035
036import javax.swing.JPanel;
037
038import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
039import org.opends.guitools.controlpanel.datamodel.IndexDescriptor;
040import org.opends.guitools.controlpanel.datamodel.VLVIndexDescriptor;
041import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
042import org.opends.guitools.controlpanel.event.IndexSelectionListener;
043import org.forgerock.i18n.LocalizableMessage;
044
045/**
046 * The panel on the right of the 'Manage Indexes' panel.
047 *
048 */
049public class IndexBrowserRightPanel extends StatusGenericPanel
050{
051  private static final long serialVersionUID = -6904674789074101772L;
052  private JPanel mainPanel;
053  private IndexPanel standardIndexPanel = new IndexPanel();
054  private VLVIndexPanel vlvIndexPanel = new VLVIndexPanel();
055  private BackendIndexesPanel backendIndexesPanel = new BackendIndexesPanel();
056  private BackendVLVIndexesPanel backendVLVIndexesPanel =
057    new BackendVLVIndexesPanel();
058
059  private static final String NOTHING_SELECTED = "Nothing Selected";
060  private static final String MULTIPLE_SELECTED = "Multiple Selected";
061
062  /**
063   * Default constructor.
064   *
065   */
066  public IndexBrowserRightPanel()
067  {
068    super();
069    createLayout();
070  }
071
072  /**
073   * Displays a panel informing that no item is selected.
074   *
075   */
076  public void displayVoid()
077  {
078    ((CardLayout)mainPanel.getLayout()).show(mainPanel, NOTHING_SELECTED);
079  }
080
081  /**
082   * Displays a panel informing that multiple items are selected.
083   *
084   */
085  public void displayMultiple()
086  {
087    ((CardLayout)mainPanel.getLayout()).show(mainPanel, MULTIPLE_SELECTED);
088  }
089
090  /**
091   * Adds an index selection listener.
092   * @param listener the index selection listener.
093   */
094  public void addIndexSelectionListener(IndexSelectionListener listener)
095  {
096    backendIndexesPanel.addIndexSelectionListener(listener);
097    backendVLVIndexesPanel.addIndexSelectionListener(listener);
098  }
099
100  /**
101   * Removes an index selection listener.
102   * @param listener the index selection listener.
103   */
104  public void removeIndexSelectionListener(IndexSelectionListener listener)
105  {
106    backendIndexesPanel.removeIndexSelectionListener(listener);
107    backendVLVIndexesPanel.removeIndexSelectionListener(listener);
108  }
109
110  /** {@inheritDoc} */
111  public void setInfo(ControlPanelInfo info)
112  {
113    super.setInfo(info);
114    standardIndexPanel.setInfo(info);
115    vlvIndexPanel.setInfo(info);
116    backendIndexesPanel.setInfo(info);
117    backendVLVIndexesPanel.setInfo(info);
118  }
119
120  /**
121   * Updates the contents of the panel with an standard index.
122   * @param index the index to be used to update the contents of the panel.
123   */
124  public void updateStandardIndex(IndexDescriptor index)
125  {
126    standardIndexPanel.update(index);
127    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
128        standardIndexPanel.getTitle().toString());
129  }
130
131  /**
132   * Updates the contents of the panel with a VLV index.
133   * @param index the index to be used to update the contents of the panel.
134   */
135  public void updateVLVIndex(VLVIndexDescriptor index)
136  {
137    vlvIndexPanel.update(index);
138    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
139        vlvIndexPanel.getTitle().toString());
140  }
141
142  /**
143   * Updates the contents of the panel with the indexes on the provided backend.
144   * A table with all the indexes of the backend will be displayed.
145   * @param backendName the name of the backend.
146   */
147  public void updateBackendIndexes(String backendName)
148  {
149    backendIndexesPanel.update(backendName);
150    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
151        backendIndexesPanel.getTitle().toString());
152  }
153
154  /**
155   * Updates the contents of the panel with the VLV indexes on the provided
156   * backend.
157   * A table with all the VLV indexes of the backend will be displayed.
158   * @param backendName the name of the backend.
159   */
160  public void updateBackendVLVIndexes(String backendName)
161  {
162    backendVLVIndexesPanel.update(backendName);
163    ((CardLayout)mainPanel.getLayout()).show(mainPanel,
164        backendVLVIndexesPanel.getTitle().toString());
165  }
166
167  /**
168   * Creates the layout of the panel (but the contents are not populated here).
169   */
170  private void createLayout()
171  {
172    GridBagConstraints gbc = new GridBagConstraints();
173    CardLayout cardLayout = new CardLayout();
174    mainPanel = new JPanel(cardLayout);
175    mainPanel.setOpaque(false);
176    NoItemSelectedPanel noEntryPanel = new NoItemSelectedPanel();
177    mainPanel.add(noEntryPanel, NOTHING_SELECTED);
178    NoItemSelectedPanel multipleEntryPanel = new NoItemSelectedPanel();
179    multipleEntryPanel.setMessage(
180        INFO_CTRL_PANEL_MULTIPLE_ITEMS_SELECTED_LABEL.get());
181    mainPanel.add(multipleEntryPanel, MULTIPLE_SELECTED);
182    StatusGenericPanel[] panels =
183    {
184        standardIndexPanel,
185        backendIndexesPanel,
186        backendVLVIndexesPanel,
187        vlvIndexPanel
188    };
189    for (StatusGenericPanel panel : panels)
190    {
191      mainPanel.add(panel, panel.getTitle().toString());
192    }
193    cardLayout.show(mainPanel, NOTHING_SELECTED);
194    gbc.gridx = 0;
195    gbc.gridy = 0;
196    gbc.weightx = 1.0;
197    gbc.weighty = 1.0;
198    gbc.fill = GridBagConstraints.BOTH;
199    add(mainPanel, gbc);
200  }
201
202  /** {@inheritDoc} */
203  public void okClicked()
204  {
205    // No ok button
206  }
207
208  /** {@inheritDoc} */
209  public GenericDialog.ButtonType getButtonType()
210  {
211    return GenericDialog.ButtonType.NO_BUTTON;
212  }
213
214  /** {@inheritDoc} */
215  public LocalizableMessage getTitle()
216  {
217    return INFO_CTRL_PANEL_INDEX_BROWSER_RIGHT_PANEL_TITLE.get();
218  }
219
220  /** {@inheritDoc} */
221  public Component getPreferredFocusComponent()
222  {
223    // TODO
224    return null;
225  }
226
227  /** {@inheritDoc} */
228  public void configurationChanged(ConfigurationChangeEvent ev)
229  {
230  }
231
232  /**
233   * Method used to know if there are unsaved changes or not.  It is used by
234   * the index selection listener when the user changes the selection.
235   * @return <CODE>true</CODE> if there are unsaved changes (and so the
236   * selection of the index should be canceled) and <CODE>false</CODE>
237   * otherwise.
238   */
239  public boolean mustCheckUnsavedChanges()
240  {
241    boolean mustCheckUnsavedChanges;
242    if (vlvIndexPanel.isVisible())
243    {
244      mustCheckUnsavedChanges = vlvIndexPanel.mustCheckUnsavedChanges();
245    }
246    else if (standardIndexPanel.isVisible())
247    {
248      mustCheckUnsavedChanges = standardIndexPanel.mustCheckUnsavedChanges();
249    }
250    else
251    {
252      mustCheckUnsavedChanges = false;
253    }
254    return mustCheckUnsavedChanges;
255  }
256
257  /**
258   * Tells whether the user chose to save the changes in the panel, to not save
259   * them or simply cancelled the selection in the tree.
260   * @return the value telling whether the user chose to save the changes in the
261   * panel, to not save them or simply cancelled the selection in the tree.
262   */
263  public UnsavedChangesDialog.Result checkUnsavedChanges()
264  {
265    UnsavedChangesDialog.Result result;
266    if (vlvIndexPanel.isVisible())
267    {
268      result = vlvIndexPanel.checkUnsavedChanges();
269    }
270    else if (standardIndexPanel.isVisible())
271    {
272      result = standardIndexPanel.checkUnsavedChanges();
273    }
274    else
275    {
276      result = UnsavedChangesDialog.Result.DO_NOT_SAVE;
277    }
278    return result;
279  }
280}