001: /*
002: * TableConstraintsPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui.table;
023:
024: import java.awt.BorderLayout;
025: import java.awt.event.FocusListener;
026: import java.awt.event.KeyAdapter;
027: import java.awt.event.KeyEvent;
028:
029: import java.util.Vector;
030:
031: import javax.swing.DefaultCellEditor;
032: import javax.swing.JPanel;
033: import javax.swing.JScrollPane;
034: import javax.swing.JTable;
035: import javax.swing.table.AbstractTableModel;
036: import javax.swing.table.TableCellEditor;
037: import javax.swing.table.TableColumn;
038: import javax.swing.table.TableColumnModel;
039: import org.executequery.gui.DefaultTable;
040:
041: import org.executequery.gui.browser.ColumnConstraint;
042: import org.underworldlabs.swing.table.ComboBoxCellEditor;
043: import org.underworldlabs.swing.table.StringCellEditor;
044:
045: import org.executequery.gui.browser.ColumnData;
046:
047: /* ----------------------------------------------------------
048: * CVS NOTE: Changes to the CVS repository prior to the
049: * release of version 3.0.0beta1 has meant a
050: * resetting of CVS revision numbers.
051: * ----------------------------------------------------------
052: */
053:
054: /**
055: *
056: * @author Takis Diakoumis
057: * @version $Revision: 1.5 $
058: * @date $Date: 2006/07/15 13:14:12 $
059: */
060: public abstract class TableConstraintsPanel extends JPanel implements
061: CreateTableSQLSyntax {
062:
063: /** The table containing the constraint data */
064: protected JTable table;
065:
066: /** The table's model */
067: protected ColumnConstraintModel model;
068:
069: /** The constraint name cell editor */
070: protected StringCellEditor conNameEditor;
071:
072: /** The string cell editor */
073: protected DefaultCellEditor strEditor;
074:
075: /** The keys combo box cell editor */
076: protected ComboBoxCellEditor keysCombo;
077:
078: public TableConstraintsPanel() {
079: super (new BorderLayout());
080:
081: try {
082: jbInit();
083: } catch (Exception e) {
084: e.printStackTrace();
085: }
086:
087: }
088:
089: private void jbInit() throws Exception {
090: table = new DefaultTable();
091:
092: conNameEditor = new StringCellEditor();
093:
094: // create the key listener to notify changes
095: KeyAdapter colKeyListener = new KeyAdapter() {
096: public void keyReleased(KeyEvent e) {
097: columnValuesChanged(table.getEditingColumn(), table
098: .getEditingRow(), conNameEditor.getValue());
099: }
100: };
101:
102: conNameEditor.addKeyListener(colKeyListener);
103:
104: add(new JScrollPane(table), BorderLayout.CENTER);
105:
106: if (getMode() == CREATE_TABLE_MODE) {
107: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
108: }
109:
110: table.setColumnSelectionAllowed(false);
111: table.setRowSelectionAllowed(false);
112: table.getTableHeader().setReorderingAllowed(false);
113: table.setRowHeight(20);
114:
115: keysCombo = new ComboBoxCellEditor(
116: CreateTableSQLSyntax.KEY_NAMES);
117:
118: strEditor = new DefaultCellEditor(conNameEditor) {
119: public Object getCellEditorValue() {
120: return conNameEditor.getValue();
121: }
122: };
123: }
124:
125: public abstract ColumnData[] getTableColumnData();
126:
127: public abstract void updateCellEditor(int col, int row, String value);
128:
129: public abstract void columnValuesChanged();
130:
131: public abstract void columnValuesChanged(int col, int row,
132: String value);
133:
134: public abstract int getMode();
135:
136: public Vector getKeys() {
137: return model.getKeys();
138: }
139:
140: public ColumnConstraint[] getColumnConstraintArray() {
141: Vector keys = model.getKeys();
142: int v_size = keys.size();
143:
144: ColumnConstraint[] cca = new ColumnConstraint[v_size];
145: for (int i = 0; i < v_size; i++) {
146: cca[i] = new ColumnConstraint();
147: cca[i].setValues((ColumnConstraint) keys.elementAt(i));
148: }
149:
150: return cca;
151: }
152:
153: public void fireEditingStopped() {
154: table.editingStopped(null);
155: if (table.isEditing()) {
156: table.removeEditor();
157: }
158: }
159:
160: public void setData(Vector keys, boolean fillCombos) {
161:
162: boolean keysEmpty = keys.isEmpty();
163:
164: if (table.isEditing()) {
165: table.removeEditor();
166: }
167:
168: // add an empty constraint for input if vector empty
169: if (keysEmpty) {
170: keys
171: .add(new ColumnConstraint(
172: getMode() == EDIT_TABLE_MODE));
173: }
174:
175: if (model == null) {
176: model = new ColumnConstraintModel(keys);
177: setModel(model);
178: setColumnProperties();
179: } else {
180: model.setNewData(keys);
181: setModel(model);
182: }
183:
184: if (keysEmpty || fillCombos) {
185: try {
186: table.getColumnModel().getColumn(2).setCellEditor(
187: keysCombo);
188: table.getColumnModel().getColumn(3).setCellEditor(
189: new ComboBoxCellEditor(getTableColumnData()));
190: } catch (ArrayIndexOutOfBoundsException e) { // TODO: what is this - test
191: e.printStackTrace();
192: }
193: }
194:
195: model.fireTableDataChanged();
196: }
197:
198: public int getSelectedRow() {
199: return table.getSelectedRow();
200: }
201:
202: public void insertRowAfter() {
203: model.insertRowAfter(getMode() == EDIT_TABLE_MODE);
204: }
205:
206: public void deleteSelectedRow() {
207: int row = table.getSelectedRow();
208: if (row == -1) {
209: return;
210: }
211:
212: table.editingStopped(null);
213: if (table.isEditing()) {
214: table.removeEditor();
215: }
216:
217: model.deleteRow(row);
218: model.fireTableRowsDeleted(row, row);
219:
220: if (model.getKeys().size() == 0) {
221: model.insertRowAfter(getMode() == EDIT_TABLE_MODE);
222: table.setEditingRow(0);
223: } else {
224: table.setEditingRow(row);
225: }
226:
227: table.setEditingColumn(1);
228: columnValuesChanged();
229: }
230:
231: public void setCellEditor(int col, TableCellEditor editor) {
232: table.getColumnModel().getColumn(col).setCellEditor(editor);
233: }
234:
235: /**
236: * Sets some default column property values on the table
237: * display such as renderers, editors and column widths.
238: */
239: protected void setColumnProperties() {
240: TableColumnModel tcm = table.getColumnModel();
241: tcm.getColumn(0).setPreferredWidth(25);
242: //tcm.getColumn(0).setMinWidth(25);
243: tcm.getColumn(0).setMaxWidth(25);
244: tcm.getColumn(1).setPreferredWidth(125);
245: tcm.getColumn(2).setPreferredWidth(75);
246: tcm.getColumn(3).setPreferredWidth(110);
247: tcm.getColumn(4).setPreferredWidth(120);
248: tcm.getColumn(5).setPreferredWidth(120);
249: tcm.getColumn(6).setPreferredWidth(120);
250:
251: tcm.getColumn(0).setCellRenderer(new ConstraintCellRenderer());
252: tcm.getColumn(1).setCellEditor(strEditor);
253: tcm.getColumn(2).setCellEditor(keysCombo);
254: }
255:
256: public boolean tableHasFocus(JTable _table) {
257: return table == _table;
258: }
259:
260: private void setTableProperty(int col, int width,
261: DefaultCellEditor editor) {
262:
263: TableColumn column = table.getColumnModel().getColumn(col);
264: if (editor != null) {
265: column.setCellEditor(editor);
266: }
267: }
268:
269: /** <p>Adds the specified focus listener to the table.
270: *
271: * @param the listener to add to the table
272: */
273: public void addTableFocusListener(FocusListener listener) {
274: table.addFocusListener(listener);
275: }
276:
277: /** <p>Sets the specified table model to the table.
278: *
279: * @param the table model
280: */
281: public void setModel(AbstractTableModel model) {
282: table.setModel(model);
283: }
284:
285: public ColumnConstraint getConstraintAt(int row) {
286: return model.getConstraintAt(row);
287: }
288:
289: class ColumnConstraintModel extends AbstractTableModel {
290:
291: private String[] header = { "", "Name", "Type", "Table Column",
292: "Reference Schema", "Reference Table",
293: "Reference Column" };
294:
295: private Vector keys;
296:
297: public ColumnConstraintModel(Vector v) {
298: keys = v;
299: }
300:
301: public void setNewData(Vector v) {
302: keys = v;
303: }
304:
305: public int getColumnCount() {
306: return header.length;
307: }
308:
309: public int getRowCount() {
310: return keys.size();
311: }
312:
313: /**
314: * Inserts a constraint to the end of this model.
315: *
316: * @param isNew - whether the constraint will be marked as new
317: */
318: public void insertRowAfter(boolean isNew) {
319: keys.add(new ColumnConstraint(isNew));
320: int newIndex = keys.size() - 1; // end of vector
321: fireTableRowsInserted(newIndex, newIndex);
322: }
323:
324: public Object getValueAt(int row, int col) {
325: ColumnConstraint cc = (ColumnConstraint) keys
326: .elementAt(row);
327:
328: // check the column type
329: boolean canHaveReference = (cc.getType() == ColumnConstraint.FOREIGN_KEY);
330:
331: switch (col) {
332: case 0:
333: return cc;
334: case 1:
335: return cc.getName();
336: case 2:
337: return cc.getTypeName();
338: case 3:
339: return cc.getColumn();
340: case 4:
341: if (!canHaveReference) {
342: return null;
343: }
344: return cc.getRefSchema();
345: case 5:
346: if (!canHaveReference) {
347: return null;
348: }
349: return cc.getRefTable();
350: case 6:
351: if (!canHaveReference) {
352: return null;
353: }
354: return cc.getRefColumn();
355: default:
356: return null;
357: }
358: }
359:
360: public void setValueAt(Object value, int row, int col) {
361:
362: if (row < 0 || row > (keys.size() - 1)) {
363: return;
364: }
365:
366: ColumnConstraint cc = (ColumnConstraint) keys
367: .elementAt(row);
368:
369: switch (col) {
370: case 0:
371: return;
372: case 1:
373: cc.setName((String) value);
374: columnValuesChanged(col, row, cc.getName());
375: break;
376: case 2:
377: String colType = (String) value;
378: if (colType == cc.PRIMARY) {
379: cc.setType(cc.PRIMARY_KEY);
380: } else if (colType == cc.FOREIGN) {
381: cc.setType(cc.FOREIGN_KEY);
382: } else if (colType == cc.UNIQUE) {
383: cc.setType(cc.UNIQUE_KEY);
384: }
385:
386: if (colType != null) {
387: updateCellEditor(col, row, colType);
388: columnValuesChanged(col, row, null);
389: }
390:
391: cc.setColumn(cc.EMPTY);
392: cc.setRefSchema(cc.EMPTY);
393: cc.setRefTable(cc.EMPTY);
394: cc.setRefColumn(cc.EMPTY);
395: break;
396: case 3:
397: cc.setColumn(value.toString());
398: columnValuesChanged(col, row, null);
399: break;
400: case 4:
401: String schema = (String) value;
402: cc.setRefSchema(schema);
403: cc.setRefTable(cc.EMPTY);
404: cc.setRefColumn(cc.EMPTY);
405:
406: if (schema != null) {
407: updateCellEditor(col, row, schema);
408: columnValuesChanged(col, row, null);
409: }
410:
411: break;
412: case 5:
413: String tbl = (String) value;
414: cc.setRefColumn(cc.EMPTY);
415: cc.setRefTable(tbl);
416: if (tbl != null) {
417: updateCellEditor(col, row, tbl);
418: columnValuesChanged(col, row, null);
419: }
420:
421: break;
422: case 6:
423: cc.setRefColumn((String) value);
424: columnValuesChanged(col, row, null);
425:
426: break;
427: }
428:
429: fireTableRowsUpdated(row, row);
430: }
431:
432: public ColumnConstraint getConstraintAt(int row) {
433: return (ColumnConstraint) keys.elementAt(row);
434: }
435:
436: public boolean isCellEditable(int row, int col) {
437: ColumnConstraint cc = (ColumnConstraint) keys
438: .elementAt(row);
439:
440: // check if its a new table create
441: if (getMode() == CREATE_TABLE_MODE) {
442: switch (col) {
443: case 0:
444: return false;
445: case 1:
446: case 2:
447: case 3:
448: return true;
449: case 4:
450: case 5:
451: case 6:
452: if ((cc.getType() == cc.UNIQUE_KEY || cc.getType() == cc.PRIMARY_KEY)) {
453: return false;
454: }
455: return true;
456: }
457: } else {
458: if (col == 1) {
459: return true;
460: }
461:
462: if (cc.isNewConstraint()) {
463:
464: if (col > 3
465: && (cc.getType() == cc.UNIQUE_KEY || cc
466: .getType() == cc.PRIMARY_KEY)) {
467: return false;
468: } else {
469: return true;
470: }
471:
472: }
473:
474: }
475: return false;
476: /*
477: if (cc.isNewConstraint()) {
478:
479: if ((cc.getType() == cc.UNIQUE_KEY ||
480: cc.getType() == cc.PRIMARY_KEY) && col > 3) {
481: return false;
482: }
483: else {
484: return true;
485: }
486:
487: }
488: else if (col == 1) {
489: return true;
490: }
491: else {
492: return false;
493: }
494: */
495: }
496:
497: public void deleteRow(int row) {
498: keys.remove(row);
499: }
500:
501: public String getColumnName(int col) {
502: return header[col];
503: }
504:
505: public void deleteConstraint(String refColumn) {
506: int v_size = keys.size();
507: if (v_size == 0) {
508: insertRowAfter(getMode() == EDIT_TABLE_MODE);
509: return;
510: }
511:
512: for (int i = 0; i < v_size; i++) {
513: ColumnConstraint cc = (ColumnConstraint) keys
514: .elementAt(i);
515:
516: if (cc.getColumn() != null
517: && cc.getColumn().equalsIgnoreCase(refColumn)) {
518: deleteRow(i);
519: fireTableRowsDeleted(i, i);
520: break;
521: }
522:
523: }
524:
525: }
526:
527: public void deleteConstraint(int index) {
528: deleteRow(index);
529: fireTableRowsDeleted(index, index);
530: }
531:
532: public Vector getKeys() {
533: return keys;
534: }
535:
536: } // class ColumnConstraintModel
537:
538: }
|