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-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024import java.awt.event.MouseAdapter;
025import java.awt.event.MouseEvent;
026import java.awt.event.MouseMotionAdapter;
027import java.util.HashSet;
028import java.util.Set;
029
030import javax.swing.JScrollPane;
031import javax.swing.JTable;
032import javax.swing.ListSelectionModel;
033import javax.swing.SwingUtilities;
034
035import org.opends.guitools.controlpanel.datamodel.AbstractIndexDescriptor;
036import org.opends.guitools.controlpanel.datamodel.AbstractIndexTableModel;
037import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
038import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
039import org.opends.guitools.controlpanel.event.IndexSelectionEvent;
040import org.opends.guitools.controlpanel.event.IndexSelectionListener;
041import org.opends.guitools.controlpanel.ui.renderer.SelectableTableCellRenderer;
042import org.opends.guitools.controlpanel.util.Utilities;
043
044/**
045 * The abstract class used to refactor some code.  The classes that extend this
046 * class are the two panels that appear on the right side of the
047 * 'Manage Indexes...' dialog when the user clicks on 'Indexes' or
048 * 'VLV Indexes'.
049 *
050 */
051public abstract class AbstractBackendIndexesPanel extends StatusGenericPanel
052{
053  private static final long serialVersionUID = 2702054131388877743L;
054  private String backendName;
055  /**
056   * The table model.
057   */
058  protected AbstractIndexTableModel tableModel;
059  /**
060   * The table contained by this panel.
061   */
062  protected JTable table;
063  /**
064   * The scroll pane that contains the table.
065   */
066  protected JScrollPane tableScroll;
067  private Set<IndexSelectionListener> indexListeners = new HashSet<>();
068  private int lastRowMouseOver = -1;
069
070  /** Default constructor. */
071  protected AbstractBackendIndexesPanel()
072  {
073    createLayout();
074  }
075
076  /** {@inheritDoc} */
077  public Component getPreferredFocusComponent()
078  {
079    return table;
080  }
081
082  /** {@inheritDoc} */
083  public void configurationChanged(ConfigurationChangeEvent ev)
084  {
085    update(backendName);
086  }
087
088  /**
089   * The contents of the panel are updated with the indexes of the provided
090   * backend.
091   * @param backendName the backend name.
092   */
093  public void update(String backendName)
094  {
095    this.backendName = backendName;
096
097    BackendDescriptor backend = getBackend(backendName);
098    if (backend != null)
099    {
100      final BackendDescriptor b = backend;
101      SwingUtilities.invokeLater(new Runnable()
102      {
103        public void run()
104        {
105          updateTableModel(b);
106          Utilities.updateTableSizes(table);
107          Utilities.updateScrollMode(tableScroll, table);
108        }
109      });
110    }
111    else
112    {
113      updateErrorPane(errorPane,
114          ERR_CTRL_PANEL_BACKEND_NOT_FOUND_SUMMARY.get(),
115          ColorAndFontConstants.errorTitleFont,
116          ERR_CTRL_PANEL_BACKEND_NOT_FOUND_DETAILS.get(backendName),
117          ColorAndFontConstants.defaultFont);
118    }
119  }
120
121  private BackendDescriptor getBackend(String backendID)
122  {
123    for (BackendDescriptor b : getInfo().getServerDescriptor().getBackends())
124    {
125      if (b.getBackendID().equals(backendID))
126      {
127        return b;
128      }
129    }
130    return null;
131  }
132
133  /**
134   * The method that is called to update the table model with the contents of
135   * the specified backend.
136   * @param backend the backend.
137   */
138  protected abstract void updateTableModel(BackendDescriptor backend);
139
140  /** {@inheritDoc} */
141  public void okClicked()
142  {
143    // No OK button
144  }
145
146  /** {@inheritDoc} */
147  public GenericDialog.ButtonType getButtonType()
148  {
149    return GenericDialog.ButtonType.NO_BUTTON;
150  }
151
152  /**
153   * Adds an index selection listener.
154   * @param listener the index selection listener.
155   */
156  public void addIndexSelectionListener(IndexSelectionListener listener)
157  {
158    indexListeners.add(listener);
159  }
160
161  /**
162   * Removes an index selection listener.
163   * @param listener the index selection listener.
164   */
165  public void removeIndexSelectionListener(IndexSelectionListener listener)
166  {
167    indexListeners.remove(listener);
168  }
169
170  /**
171   * Returns the index table model used by this panel.
172   * @return the index table model used by this panel.
173   */
174  protected abstract AbstractIndexTableModel getIndexTableModel();
175
176  /**
177   * Creates the layout of the panel (but the contents are not populated here).
178   *
179   */
180  private void createLayout()
181  {
182    GridBagConstraints gbc = new GridBagConstraints();
183    gbc.gridx = 0;
184    gbc.gridy = 0;
185    gbc.gridwidth = 1;
186    addErrorPane(gbc);
187    gbc.gridy ++;
188    tableModel = getIndexTableModel();
189    SelectableTableCellRenderer renderer = new SelectableTableCellRenderer();
190    table = Utilities.createSortableTable(tableModel, renderer);
191    renderer.setTable(table);
192    table.getSelectionModel().setSelectionMode(
193        ListSelectionModel.SINGLE_SELECTION);
194    table.setDragEnabled(false);
195    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
196    table.addMouseListener(new MouseAdapter()
197    {
198      public void mouseReleased(MouseEvent ev)
199      {
200        int selectedRow = table.getSelectedRow();
201        if (selectedRow != -1 && lastRowMouseOver == selectedRow)
202        {
203          AbstractIndexDescriptor index = tableModel.getIndexAt(selectedRow);
204          final IndexSelectionEvent ise = new IndexSelectionEvent(table, index);
205          SwingUtilities.invokeLater(new Runnable()
206          {
207            /** Call it this way to let the painting events happen. */
208            public void run()
209            {
210              for (IndexSelectionListener listener : indexListeners)
211              {
212                listener.indexSelected(ise);
213              }
214            }
215          });
216        }
217      }
218    });
219    table.addMouseMotionListener(new MouseMotionAdapter()
220    {
221      public void mouseMoved(MouseEvent ev)
222      {
223        lastRowMouseOver = table.rowAtPoint(ev.getPoint());
224
225      }
226
227      public void mouseDragged(MouseEvent ev)
228      {
229        lastRowMouseOver = -1;
230      }
231    });
232
233    tableScroll = Utilities.createBorderLessScrollBar(table);
234    tableScroll.getViewport().setOpaque(false);
235    tableScroll.setOpaque(false);
236    tableScroll.getViewport().setBackground(ColorAndFontConstants.background);
237    tableScroll.setBackground(ColorAndFontConstants.background);
238    gbc.weightx = 1.0;
239    gbc.weighty = 1.0;
240    gbc.fill = GridBagConstraints.BOTH;
241    add(tableScroll, gbc);
242  }
243}