001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2004-2005 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.jmx.mbeanwizard.tablemodel;
042:
043: import org.netbeans.modules.jmx.mbeanwizard.listener.TableRemoveListener;
044: import javax.swing.table.AbstractTableModel;
045: import java.util.ArrayList;
046: import java.util.HashSet;
047: import java.util.Iterator;
048:
049: import java.util.ResourceBundle;
050: import java.util.Set;
051: import javax.swing.JTable;
052: import javax.swing.SwingUtilities;
053: import javax.swing.event.TableModelEvent;
054:
055: /**
056: * Abstract class defining a generic table model
057: *
058: */
059: public abstract class AbstractJMXTableModel extends AbstractTableModel {
060:
061: protected static final int NBROW_MIN = 0;
062: protected ArrayList data;
063: protected transient ResourceBundle bundle;
064: protected String[] columnNames;
065:
066: /**
067: * Constructor
068: */
069: public AbstractJMXTableModel() {
070: }
071:
072: /**
073: * Adds a row to the current table model
074: */
075: public abstract void addRow();
076:
077: /**
078: * Method returning the size of the model i.e the number of lines
079: * @return int the number of lines
080: */
081: public int size() {
082:
083: return getRowCount();
084: }
085:
086: /**
087: * Method returning the number of rows
088: * @return int the number of rows
089: */
090: public int getRowCount() {
091: return data.size();
092: }
093:
094: /**
095: * Method returning the number of columns
096: * @return int the number of columns
097: */
098: public int getColumnCount() {
099: return columnNames.length;
100: }
101:
102: /**
103: * Method returning a value of the model
104: * @return Object the value contained in the model at (row,column)
105: * @param row the row of the value to get
106: * @param col the column of the value to get
107: */
108: public Object getValueAt(int row, int col) {
109: return ((ArrayList) data.get(row)).get(col);
110: }
111:
112: /**
113: * Sets a value in the model
114: * @param aValue the value to be set at (row,column)
115: * @param rowIndex the row
116: * @param columnIndex the column
117: */
118: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
119: if (rowIndex < this .size())
120: ((ArrayList) data.get(rowIndex)).set(columnIndex, aValue);
121: }
122:
123: /**
124: * Method returning the header of the column
125: * @param column the column number
126: * @return String the column header
127: */
128: public String getColumnName(int column) {
129: return columnNames[column];
130: }
131:
132: /**
133: * Method returning wheter the cell (r,c) is editable or not
134: * @param r the row of the cell
135: * @param c the column of the cell
136: * @return boolean true if the cell is editable
137: */
138: public boolean isCellEditable(int r, int c) {
139: return true;
140: }
141:
142: /**
143: * Removes a row in the model
144: * @param selectedRow the row number to be removed
145: * @param table the table to remove the row in
146: */
147: public void remRow(int selectedRow, JTable table) {
148: int nbRow = getRowCount();
149:
150: //if there is one or more elements in the model
151: //remove the selected row
152: if (nbRow > NBROW_MIN) {
153: data.remove(selectedRow);
154:
155: // fire an event of removal
156: fireTableRemoveEvent(selectedRow, table);
157:
158: if (table.isEditing())
159: table.getCellEditor().stopCellEditing();
160: }
161: this .fireTableDataChanged();
162: }
163:
164: /**
165: * Method selecting the next row after removal
166: * @param selectedRow the selected row
167: * @param table the given table
168: */
169: public void selectNextRow(int selectedRow, JTable table) {
170:
171: final JTable t = table;
172: final int sel = selectedRow;
173:
174: //first, empty the event queue before firethe event
175: //workaround swing event model
176: if (((AbstractJMXTableModel) t.getModel()).size() > 0) {
177: SwingUtilities.invokeLater(new Runnable() {
178: public void run() {
179: if (sel == 0) {
180: t.setRowSelectionInterval(0, 0);
181: } else {
182: if (sel == ((AbstractJMXTableModel) t
183: .getModel()).size()) {
184: t.setRowSelectionInterval(sel - 1, sel - 1);
185: } else {
186: t.setRowSelectionInterval(sel, sel);
187: }
188: }
189: }
190: });
191: }
192: }
193:
194: //=========================================
195: // Manual event gestion
196: //=========================================
197: private final Set listeners = new HashSet(1); // Set<ChangeListener>
198:
199: public final void addTableRemoveListener(TableRemoveListener l) {
200: synchronized (listeners) {
201: listeners.add(l);
202: }
203: }
204:
205: public final void removeTableRemoveListener(TableRemoveListener l) {
206: synchronized (listeners) {
207: listeners.remove(l);
208: }
209: }
210:
211: protected final void fireTableRemoveEvent(int selectedRow,
212: JTable table) {
213: Iterator it;
214: synchronized (listeners) {
215: it = new HashSet(listeners).iterator();
216: }
217:
218: TableModelEvent ev = new TableModelEvent(this , selectedRow,
219: selectedRow, table.getSelectedColumn(),
220: TableModelEvent.DELETE);
221: while (it.hasNext()) {
222: ((TableRemoveListener) it.next()).tableStateChanged(ev);
223: }
224: }
225: }
|