KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > dlg > DataModel


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.db.explorer.dlg;
21
22
23 import java.util.Vector JavaDoc;
24
25 import javax.swing.event.TableModelEvent JavaDoc;
26 import javax.swing.table.AbstractTableModel JavaDoc;
27
28 import org.openide.util.NbBundle;
29
30 /**
31 * Table data model
32 * Uses a vector of objects to store the data
33 */

34 public class DataModel extends AbstractTableModel JavaDoc
35 {
36     /** Column data */
37     private Vector JavaDoc data;
38
39     transient private Vector JavaDoc primaryKeys = new Vector JavaDoc();
40     transient private Vector JavaDoc uniqueKeys = new Vector JavaDoc();
41
42     static final long serialVersionUID =4162743695966976536L;
43     
44     public DataModel()
45     {
46         super();
47         data = new Vector JavaDoc(1);
48     }
49
50     public Vector JavaDoc getData()
51     {
52         return data;
53     }
54
55     public int getColumnCount()
56     {
57         return ColumnItem.getProperties().size();
58     }
59
60     public int getRowCount()
61     {
62         return data.size();
63     }
64
65     public Object JavaDoc getValue(String JavaDoc pname, int row)
66     {
67         ColumnItem xcol = (ColumnItem)data.elementAt(row);
68         return xcol.getProperty(pname);
69     }
70
71     public Object JavaDoc getValueAt(int row, int col)
72     {
73         return getValue((String JavaDoc)ColumnItem.getColumnNames().elementAt(col), row);
74     }
75
76     public void setValue(Object JavaDoc val, String JavaDoc pname, int row)
77     {
78         if( row < getRowCount() ) {
79             int srow = row, erow = row;
80             ColumnItem xcol = (ColumnItem)data.elementAt(row);
81             xcol.setProperty(pname, val);
82             if (pname.equals(ColumnItem.PRIMARY_KEY) && val.equals(Boolean.TRUE)) {
83
84                 if (xcol.allowsNull()) xcol.setProperty(ColumnItem.NULLABLE, Boolean.FALSE);
85                 if (!xcol.isIndexed()) xcol.setProperty(ColumnItem.INDEX, Boolean.TRUE);
86                 if (!xcol.isUnique()) xcol.setProperty(ColumnItem.UNIQUE, Boolean.TRUE);
87                 /*for (int i=0; i<data.size();i++) {
88                     ColumnItem eitem = (ColumnItem)data.elementAt(i);
89                     if (i!=row && eitem.isPrimaryKey()) {
90                         eitem.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
91                         if (i<row) srow = i; else erow = i;
92                     }
93                 }*/

94                 primaryKeys.add(xcol);
95             }
96             
97             if (pname.equals(ColumnItem.PRIMARY_KEY) && val.equals(Boolean.FALSE)) {
98                 primaryKeys.remove((ColumnItem)data.elementAt(row));
99             }
100
101             if (pname.equals(ColumnItem.NULLABLE)) {
102                 if (val.equals(Boolean.TRUE)) {
103                     //xcol.setProperty(ColumnItem.UNIQUE, Boolean.FALSE);
104
//xcol.setProperty(ColumnItem.INDEX, Boolean.FALSE);
105
xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
106                     primaryKeys.remove((ColumnItem)data.elementAt(row));
107                 }
108             }
109
110             if (pname.equals(ColumnItem.INDEX)) {
111                 if (val.equals(Boolean.FALSE)) {
112                     if (xcol.isUnique()) xcol.setProperty(ColumnItem.UNIQUE, Boolean.FALSE);
113                     if (xcol.isPrimaryKey()) {
114                         xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
115                         primaryKeys.remove((ColumnItem)data.elementAt(row));
116                     }
117                     //xcol.setProperty(ColumnItem.UNIQUE, Boolean.FALSE);
118
//xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
119
} //else xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
120
}
121
122             if (pname.equals(ColumnItem.UNIQUE)) {
123                 if (val.equals(Boolean.TRUE)) {
124                     if (!xcol.isIndexed()) xcol.setProperty(ColumnItem.INDEX, Boolean.TRUE);
125                 } else {
126                     xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE);
127                     primaryKeys.remove((ColumnItem)data.elementAt(row));
128                     xcol.setProperty(ColumnItem.INDEX, Boolean.FALSE);
129                 }
130             }
131
132             fireTableRowsUpdated(srow, erow);
133         }
134     }
135
136     public void setValueAt(Object JavaDoc val, int row, int col) {
137         //fixed bug 23788 (http://db.netbeans.org/issues/show_bug.cgi?id=23788)
138
if (row == -1 || col == -1)
139             return;
140         
141         if (row < getRowCount() && col < getColumnCount()) {
142             String JavaDoc pname = (String JavaDoc) ColumnItem.getColumnNames().elementAt(col);
143             setValue(val, pname, row);
144         }
145     }
146
147     public String JavaDoc getColumnName(int col) {
148         return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("CreateTable_" + col); //NOI18N
149
}
150
151     public Class JavaDoc getColumnClass(int c)
152     {
153         return getValueAt(0,c).getClass();
154     }
155
156     public boolean isCellEditable(int row, int col)
157     {
158         return true;
159     }
160
161     public boolean isTablePrimaryKey()
162     {
163         return primaryKeys.size()>1;
164     }
165     
166     public Vector JavaDoc getTablePrimaryKeys()
167     {
168         return primaryKeys;
169     }
170
171     public Vector JavaDoc getTableUniqueKeys()
172     {
173         return uniqueKeys;
174     }
175
176     public boolean isTableUniqueKey()
177     {
178         return uniqueKeys.size()>1;
179     }
180
181     /**
182     * Add a row to the end of the model.
183     * Notification of the row being added will be generated.
184     * @param object Object to add
185     */

186     public void addRow(Object JavaDoc object)
187     {
188         data.addElement(object);
189         fireTableChanged(new TableModelEvent JavaDoc(this, getRowCount()-1, getRowCount()-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
190     }
191
192     /**
193     * Insert a row at <i>row</i> in the model.
194     * Notification of the row being added will be generated.
195     * @param row The row index of the row to be inserted
196     * @param object Object to add
197     * @exception ArrayIndexOutOfBoundsException If the row was invalid.
198     */

199     public void insertRow(int row, Object JavaDoc object)
200     {
201         data.insertElementAt(object, row);
202         fireTableRowsInserted(row, row);
203     }
204
205     /**
206     * Remove the row at <i>row</i> from the model. Notification
207     * of the row being removed will be sent to all the listeners.
208     * @param row The row index of the row to be removed
209     * @exception ArrayIndexOutOfBoundsException If the row was invalid.
210     */

211     public void removeRow(int row)
212     {
213         if (row < data.size()) {
214             data.removeElementAt(row);
215             fireTableRowsDeleted(row, row);
216         }
217     }
218 }
219
Popular Tags