KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > InnerTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.multiview;
21
22 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer;
23
24 import javax.swing.table.AbstractTableModel JavaDoc;
25 import javax.swing.table.TableCellEditor JavaDoc;
26 import javax.swing.*;
27
28 /**
29  * @author pfiala
30  */

31 public abstract class InnerTableModel extends AbstractTableModel JavaDoc {
32
33     private XmlMultiViewDataSynchronizer synchronizer;
34     protected final String JavaDoc[] columnNames;
35     private int[] columnWidths;
36     private int rowCount = -1;
37
38     public InnerTableModel(XmlMultiViewDataSynchronizer synchronizer, String JavaDoc[] columnNames, int[] columnWidths) {
39         this.synchronizer = synchronizer;
40         this.columnNames = columnNames;
41         this.columnWidths = columnWidths;
42     }
43
44     public boolean isCellEditable(int rowIndex, int columnIndex) {
45         return true;
46     }
47
48     public String JavaDoc getColumnName(int column) {
49         return columnNames[column];
50     }
51
52     public TableCellEditor JavaDoc getCellEditor(int columnIndex) {
53         return null;
54     }
55
56     public int getColumnCount() {
57         return columnNames.length;
58     }
59
60     public int[] getColumnWidths() {
61         return columnWidths;
62     }
63
64     public abstract int addRow();
65
66     public abstract void removeRow(int selectedRow);
67
68     public int getDefaultColumnWidth(int i) {
69         return columnWidths[i];
70     }
71
72     public void refreshView() {
73         fireTableDataChanged();
74     }
75
76     protected void tableChanged() {
77         if (!checkRowCount()) {
78             fireTableDataChanged();
79         }
80     }
81
82     private boolean checkRowCount() {
83         int n = getRowCount();
84         if (rowCount == -1) {
85             rowCount = n;
86         }
87         if (n != rowCount) {
88             while (rowCount < n) {
89                 rowCount++;
90                 fireTableRowsInserted(0, 0);
91             }
92             while (rowCount > n) {
93                 rowCount--;
94                 fireTableRowsDeleted(0, 0);
95             }
96             return true;
97         } else {
98             return false;
99         }
100     }
101
102     protected void modelUpdatedFromUI() {
103         if (synchronizer != null) {
104             synchronizer.requestUpdateData();
105         }
106     }
107
108     public TableCellEditor JavaDoc getTableCellEditor(int column) {
109         return null;
110     }
111
112     protected TableCellEditor JavaDoc createComboBoxCellEditor(Object JavaDoc[] items) {
113         return createComboBoxCellEditor(items, false);
114     }
115
116     private static TableCellEditor JavaDoc createComboBoxCellEditor(Object JavaDoc[] items, final boolean editable) {
117         final JComboBox comboBox = new JComboBox(items);
118         comboBox.setEditable(editable);
119         return new DefaultCellEditor(comboBox);
120     }
121 }
122
Popular Tags