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