001: /*
002: * NewTableConstraintsPanel.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.util.Vector;
025:
026: import org.executequery.gui.browser.*;
027: import org.executequery.gui.table.TableConstraintFunction;
028: import org.underworldlabs.swing.table.ComboBoxCellEditor;
029:
030: /* ----------------------------------------------------------
031: * CVS NOTE: Changes to the CVS repository prior to the
032: * release of version 3.0.0beta1 has meant a
033: * resetting of CVS revision numbers.
034: * ----------------------------------------------------------
035: */
036:
037: /**
038: *
039: * @author Takis Diakoumis
040: * @version $Revision: 1.4 $
041: * @date $Date: 2006/05/14 06:56:52 $
042: */
043: public class NewTableConstraintsPanel extends TableConstraintsPanel
044: implements CreateTableSQLSyntax {
045:
046: /** The table creator object - parent to this */
047: private TableConstraintFunction creator;
048:
049: /** The buffer for the current line */
050: private StringBuffer line;
051:
052: /** The buffer off all SQL generated */
053: private StringBuffer sqlBuffer;
054:
055: public NewTableConstraintsPanel(TableConstraintFunction creator) {
056: super ();
057: this .creator = creator;
058: line = new StringBuffer(50);
059: sqlBuffer = new StringBuffer(100);
060: }
061:
062: public ColumnData[] getTableColumnData() {
063: return creator.getTableColumnData();
064: }
065:
066: public int getMode() {
067: return CREATE_TABLE_MODE;
068: }
069:
070: public void updateCellEditor(int col, int row, String value) {
071: ColumnConstraint cc = getConstraintAt(row);
072:
073: switch (col) {
074:
075: case 0:
076: case 1:
077: return;
078:
079: case 2:
080: setCellEditor(3, new ComboBoxCellEditor(creator
081: .getTableColumnDataVector()));
082:
083: if (cc.getType() != -1
084: && cc.getTypeName() == ColumnConstraint.FOREIGN) {
085: Vector schemas = creator.getHostedSchemasVector();
086: setCellEditor(4, new ComboBoxCellEditor(schemas));
087:
088: if (schemas == null || schemas.size() == 0)
089: setCellEditor(5, new ComboBoxCellEditor(creator
090: .getSchemaTables(value)));
091:
092: }
093: break;
094:
095: case 3:
096: break;
097:
098: case 4:
099: setCellEditor(5, new ComboBoxCellEditor(creator
100: .getSchemaTables(value)));
101: break;
102:
103: case 5:
104: String schema = cc.getRefSchema();
105: if (schema == null || schema.length() == 0) {
106: schema = "";
107: }
108:
109: try {
110: setCellEditor(6, new ComboBoxCellEditor(creator
111: .getColumnNamesVector(value, schema)));
112: } catch (NullPointerException nullExc) {
113: }
114: break;
115:
116: }
117:
118: }
119:
120: public void columnValuesChanged(int col, int row, String value) {
121: Vector v = getKeys();
122: String name = null;
123: boolean hasName = false;
124:
125: int v_size = v.size();
126: sqlBuffer.setLength(0);
127:
128: for (int i = 0; i < v_size; i++) {
129: ColumnConstraint cc = (ColumnConstraint) v.elementAt(i);
130:
131: if (i == row && value != null && value.length() != 0) {
132: name = value;
133: hasName = true;
134: }
135:
136: else if (cc.getName() != cc.EMPTY) {
137: name = cc.getName();
138: hasName = true;
139: }
140:
141: else {
142: hasName = false;
143: }
144:
145: if (hasName) {
146:
147: sqlBuffer.append(COMMA).append(NEW_LINE_2).append(
148: CONSTRAINT);
149: sqlBuffer.append(name).append(SPACE);
150:
151: if (cc.getType() != -1) {
152:
153: if (cc.getType() == cc.UNIQUE_KEY) {
154: sqlBuffer.append(cc.UNIQUE).append(B_OPEN);
155: sqlBuffer.append(cc.getColumn())
156: .append(B_CLOSE);
157: }
158:
159: else {
160: sqlBuffer.append(cc.getTypeName()).append(KEY)
161: .append(B_OPEN);
162: sqlBuffer.append(cc.getColumn());
163: sqlBuffer.append(B_CLOSE);
164:
165: if (cc.getType() == cc.FOREIGN_KEY) {
166: sqlBuffer.append(INDENT).append(REFERENCES);
167:
168: if (cc.hasSchema())
169: sqlBuffer.append(cc.getRefSchema())
170: .append(DOT);
171:
172: sqlBuffer.append(cc.getRefTable()).append(
173: B_OPEN).append(cc.getRefColumn())
174: .append(B_CLOSE);
175: }
176:
177: }
178:
179: }
180:
181: }
182:
183: }
184: creator.setSQLText(sqlBuffer.toString(),
185: TableModifier.CONSTRAINT_VALUES);
186: }
187:
188: public void resetSQLText() {
189: columnValuesChanged(0, 0, null);
190: }
191:
192: public String getSQLText() {
193: return sqlBuffer.toString();
194: }
195:
196: public void columnValuesChanged() {
197: columnValuesChanged(-1, -1, null);
198: }
199:
200: } // class
|