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 1997-2007 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.sql.framework.ui.view.graph;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: import javax.swing.table.AbstractTableModel;
049:
050: import org.netbeans.modules.sql.framework.model.DBColumn;
051: import org.netbeans.modules.sql.framework.common.utils.NativeColumnOrderComparator;
052: import org.netbeans.modules.sql.framework.model.DBTable;
053: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
054:
055: /**
056: * This class provide a model for JMetaTableModel
057: *
058: * @author radval
059: * @version $Revision$
060: */
061: public class MetaTableModel extends AbstractTableModel {
062:
063: /**
064: * Description of the column names This should come from some config file
065: */
066: private ArrayList disableRowsList = new ArrayList();
067:
068: private String[] columnNames = { "Column Name" };
069:
070: private ArrayList columns;
071:
072: //private DBTable table;
073:
074: private ArrayList cdws;
075:
076: /**
077: * Creates a new instance of MetaTableModel
078: */
079: public MetaTableModel() {
080: }
081:
082: /**
083: * Creates a new instance of MetaTableModel, associating the given DBTable object with
084: * it.
085: *
086: * @param table DBTable object to associate with this instance
087: */
088: public MetaTableModel(DBTable table, int tType) {
089: List columnList = table.getColumnList();
090: Collections.sort(columnList, NativeColumnOrderComparator
091: .getInstance());
092: columns = new ArrayList();
093:
094: Iterator it = columnList.iterator();
095: int i = 0;
096: cdws = new ArrayList();
097: while (it.hasNext()) {
098: SQLDBColumn columnData = (SQLDBColumn) it.next();
099: if (columnData.isVisible()) {
100: columns.add(columnData);
101: ColumnDataWrapper cdw = new ColumnDataWrapper(
102: (DBColumn) columns.get(i));
103: cdw.setFilter(cdw.hasFilter);
104: cdws.add(cdw);
105: i++;
106: }
107: }
108: }
109:
110: /**
111: * Gets the columnClass attribute of the ColumnTableModel object
112: *
113: * @param columnIndex Description of the Parameter
114: * @return The columnClass value
115: */
116: public Class getColumnClass(int columnIndex) {
117: Class klass = String.class;
118: if (columnIndex == 3) {
119: klass = Boolean.class;
120: }
121: return klass;
122: }
123:
124: /**
125: * Gets the columnCount attribute of the ColumnTableModel object
126: *
127: * @return The columnCount value
128: */
129: public int getColumnCount() {
130: return columnNames.length;
131: }
132:
133: /**
134: * Gets the columnName attribute of the ColumnTableModel object
135: *
136: * @param columnIndex Description of the Parameter
137: * @return The columnName value
138: */
139: public String getColumnName(int columnIndex) {
140: String ret = null;
141:
142: if (columnIndex >= 0 && columnIndex < columnNames.length) {
143: ret = columnNames[columnIndex];
144: }
145:
146: return ret;
147: }
148:
149: /**
150: * get the SQLColumnMetadata for a table row
151: *
152: * @param idx index of column whose metadata will be retrieved
153: * @return SQLColumnMetadata instance associated with column
154: */
155: public DBColumn getSQLColumn(int idx) {
156: return (DBColumn) columns.get(idx);
157: }
158:
159: /**
160: * get the SQLColumnMetadata for a table row
161: *
162: * @param idx index of column whose metadata will be retrieved
163: * @return SQLColumnMetadata instance associated with column
164: */
165: public DBColumn getColumn(int idx) {
166: return (DBColumn) columns.get(idx);
167: }
168:
169: public void addColumn(DBColumn column) {
170: columns.add(column);
171: Collections.sort(columns, NativeColumnOrderComparator
172: .getInstance());
173:
174: int pos = columns.indexOf(column);
175: if (pos != -1) {
176: this .fireTableRowsInserted(pos, pos);
177: }
178: }
179:
180: public boolean containsColumn(DBColumn column) {
181: return this .columns.contains(column);
182: }
183:
184: public void removeColumn(DBColumn column) {
185: int row = -1;
186: for (int i = 0; i < columns.size(); i++) {
187: if (column.equals(columns.get(i))) {
188: row = i;
189: break;
190: }
191: }
192: //FIXME here order of deletion first notifys listener
193: //then actually deletes the column, This is to make sure
194: //if deleted column is linked to somewhere then that link is deleted
195: //This is a ok fix and should find a better way to handle it
196: if (row != -1) {
197: this .fireTableRowsDeleted(row, row);
198: columns.remove(row);
199: }
200: }
201:
202: public void updateColumn(DBColumn column) {
203: int row = -1;
204: for (int i = 0; i < columns.size(); i++) {
205: if (column.equals(columns.get(i))) {
206: row = i;
207: break;
208: }
209: }
210:
211: if (row != -1) {
212: this .fireTableRowsUpdated(row, row);
213: }
214: }
215:
216: public void updateColumn(String newColName, SQLDBColumn newCol) {
217: int row = -1;
218: for (int i = 0; i < columns.size(); i++) {
219: SQLDBColumn oldCol = (SQLDBColumn) columns.get(i);
220: if (oldCol.getName().equals((newColName))) {
221: row = i;
222: break;
223: }
224: }
225:
226: if (row != -1) {
227: columns.set(row, newCol);
228: this .fireTableRowsUpdated(row, row);
229: }
230: }
231:
232: /**
233: * Gets the rowCount attribute of the ColumnTableModel object
234: *
235: * @return The rowCount value
236: */
237: public int getRowCount() {
238: return (columns == null) ? 0 : columns.size();
239: }
240:
241: /**
242: * Gets the valueAt attribute of the ColumnTableModel object
243: *
244: * @param rowIndex Description of the Parameter
245: * @param columnIndex Description of the Parameter
246: * @return The valueAt value
247: */
248: public Object getValueAt(int rowIndex, int columnIndex) {
249: DBColumn column = (DBColumn) columns.get(rowIndex);
250: switch (columnIndex) {
251: case 0:
252: return column;
253: }
254:
255: return String.valueOf(columnIndex + "?");
256:
257: }
258:
259: /**
260: * Gets the cellEditable attribute of the ColumnTableModel object
261: *
262: * @param rowIndex Description of the Parameter
263: * @param columnIndex Description of the Parameter
264: * @return The cellEditable value
265: */
266: public boolean isCellEditable(int rowIndex, int columnIndex) {
267: /*
268: * if ((!disableRowsList.contains(new Integer(rowIndex))) && columnIndex == 3) {
269: * return true; }
270: */
271: return false;
272: }
273:
274: /**
275: * Sets the valueAt attribute of the ColumnTableModel object
276: *
277: * @param aValue The new valueAt value
278: * @param rowIndex The new valueAt value
279: * @param columnIndex The new valueAt value
280: */
281: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
282: /*
283: * if (columnIndex == 3) { /index of include column ColumnDescriptor cd =
284: * (ColumnDescriptor) rows.get(rowIndex); this.fireTableDataChanged(); }
285: */
286: }
287:
288: /**
289: * Description of the Method
290: *
291: * @param rowIndex Description of the Parameter
292: */
293: public void delistDisableRow(int rowIndex) {
294: if (disableRowsList.contains(new Integer(rowIndex))) {
295: disableRowsList.remove(new Integer(rowIndex));
296: }
297: }
298:
299: /**
300: * Description of the Method
301: *
302: * @param rowIndex Description of the Parameter
303: */
304: public void disableRows(int rowIndex) {
305: disableRowsList.add(new Integer(rowIndex));
306: }
307:
308: /**
309: * Description of the Method
310: */
311: public void resetDisabledRowsList() {
312: disableRowsList.clear();
313: }
314:
315: /**
316: * Sets the filter for the column
317: *
318: * @param name - column name
319: * @param filter - boolean flag
320: */
321: public void setFilter(String name, boolean filter) {
322: int rowCount = this .getRowCount();
323: for (int i = 0; i < rowCount; i++) {
324: DBColumn rowValue = (DBColumn) getValueAt(i, 0);
325: if (rowValue != null
326: && (rowValue.getName().trim()).equals(name.trim())) {
327: ((ColumnDataWrapper) cdws.get(i)).setFilter(filter);
328: }
329: }
330: }
331:
332: /**
333: * Resets the filter flags in all columns, each time we click ok.
334: */
335: public void resetFilters() {
336: int rowCount = 0;
337: if (cdws != null) {
338: rowCount = cdws.size();
339: }
340:
341: for (int i = 0; i < rowCount; i++) {
342: ((ColumnDataWrapper) cdws.get(i)).setFilter(false);
343: }
344: }
345:
346: /**
347: * Sets validation flag for the column name passed.
348: *
349: * @param name
350: * @param filter
351: */
352: public void setValidationFlag(String name, boolean flag) {
353: int rowCount = this .getRowCount();
354: for (int i = 0; i < rowCount; i++) {
355: DBColumn rowValue = (DBColumn) getValueAt(i, 0);
356: if (rowValue != null
357: && (rowValue.getName().trim()).equals(name.trim())) {
358: ((ColumnDataWrapper) cdws.get(i))
359: .setValidationPresent(flag);
360: }
361: }
362: }
363:
364: /**
365: * Resets the validation flag.
366: */
367: public void resetValidationFlag() {
368: int rowCount = 0;
369: if (cdws != null) {
370: rowCount = cdws.size();
371: }
372:
373: for (int i = 0; i < rowCount; i++) {
374: ((ColumnDataWrapper) cdws.get(i))
375: .setValidationPresent(false);
376: }
377: }
378:
379: /**
380: * Tells whether the filter has been applied to the particular column.
381: *
382: * @param row - row index
383: * @return bool
384: */
385: public boolean isFiltered(int row) {
386: if ((cdws != null) && (cdws.size() > row)) {
387: ColumnDataWrapper colMetaData = ((ColumnDataWrapper) cdws
388: .get(row));
389: return colMetaData.hasFilter();
390: } else {
391: return false;
392: }
393:
394: }
395:
396: /**
397: * Whether perticular column is part of Validation condition.
398: *
399: * @param row - row index
400: * @return bool
401: */
402: public boolean isValidationPresent(int row) {
403: if ((cdws != null) && (cdws.size() > row)) {
404: ColumnDataWrapper colMetaData = ((ColumnDataWrapper) cdws
405: .get(row));
406: return colMetaData.isValidationPresent();
407: } else {
408: return false;
409: }
410: }
411:
412: class ColumnDataWrapper {
413: private DBColumn colData;
414: private boolean hasFilter = false;
415: private boolean validationPresent = false;
416:
417: /**
418: * Constructor
419: *
420: * @param colData - DBColumn to be wrapped.
421: */
422: public ColumnDataWrapper(DBColumn colData) {
423: this .colData = colData;
424: }
425:
426: /**
427: * Gets name of wrapped column.
428: *
429: * @return column name
430: */
431: public String getColumnName() {
432: return colData.getName();
433: }
434:
435: /**
436: * Gets String representation of SQL type of wrapped column.
437: *
438: * @return SQL type string
439: */
440: public String getTypeName() {
441: return colData.getJdbcTypeString();
442: }
443:
444: /**
445: * Gets precision of wrapped column.
446: *
447: * @return precision
448: */
449: public int getPrecision() {
450: return colData.getPrecision();
451: }
452:
453: /**
454: * Gets scale of wrapped column.
455: *
456: * @return scale
457: */
458: public int getScale() {
459: return colData.getScale();
460: }
461:
462: /**
463: * Gets JDBC SQL type.
464: *
465: * @return int value representing JDBC SQL type
466: * @see java.sql.Types
467: */
468: public int getJdbcSQLType() {
469: return colData.getJdbcType();
470: }
471:
472: /**
473: * Sets whether this column has a filter
474: *
475: * @param filter true if filtered, false otherwise
476: */
477: public void setFilter(boolean filter) {
478: hasFilter = filter;
479: }
480:
481: /**
482: * Indicates whether the column has filter.
483: *
484: * @return true if filtered, false otherwise
485: */
486: public boolean hasFilter() {
487: return hasFilter;
488: }
489:
490: /**
491: * @return Returns the validationPresent.
492: */
493: public boolean isValidationPresent() {
494: return validationPresent;
495: }
496:
497: /**
498: * @param validationPresent The validationPresent to set.
499: */
500: public void setValidationPresent(boolean validationPresent) {
501: this.validationPresent = validationPresent;
502: }
503: }
504: }
|