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