001: /*
002: * NewTablePanel.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 org.executequery.gui.browser.ColumnData;
025: import org.executequery.util.Log;
026: import org.underworldlabs.util.MiscUtils;
027:
028: /* ----------------------------------------------------------
029: * CVS NOTE: Changes to the CVS repository prior to the
030: * release of version 3.0.0beta1 has meant a
031: * resetting of CVS revision numbers.
032: * ----------------------------------------------------------
033: */
034:
035: /**
036: *
037: * @author Takis Diakoumis
038: * @version $Revision: 1.7 $
039: * @date $Date: 2006/07/16 15:46:54 $
040: */
041: public class NewTablePanel extends TableDefinitionPanel implements
042: CreateTableSQLSyntax {
043:
044: /** The table creator object - parent to this */
045: private TableModifier creator;
046:
047: /** The buffer for the current line */
048: private StringBuffer line;
049:
050: /** The buffer off all SQL generated */
051: private StringBuffer sqlText;
052:
053: public NewTablePanel(TableModifier creator) {
054: super ();
055: this .creator = creator;
056:
057: line = new StringBuffer(50);
058: sqlText = new StringBuffer(100);
059: }
060:
061: /**
062: * Returns the SQL scriptlet text.
063: *
064: * @return the SQL text
065: */
066: public String getSQLText() {
067: return sqlText.toString();
068: }
069:
070: /**
071: * Resets the SQL text.
072: */
073: public void resetSQLText() {
074: addColumnLines(-1);
075: }
076:
077: /**
078: * Indicates that the table value for the specified row and
079: * column has changed to the value specified.
080: *
081: * @param col - the last updated col
082: * @param row - the last updated row
083: * @param value - the new value
084: */
085: public void tableChanged(int col, int row, String value) {
086:
087: //Log.debug("tableChanged [row: "+row+" col: "+col+" value: "+value + "]");
088:
089: if (value == null) {
090: updateScript(row, col);
091: return;
092: }
093:
094: //if (row == -1 || (col == 1 && value == null)) {
095: if (row == -1) {// || (col == 1 && value == null)) {
096: return;
097: }
098:
099: ColumnData cd = (ColumnData) tableVector.get(row);
100: switch (col) {
101: case 1:
102: cd.setColumnName(value);
103: break;
104: case 2:
105: cd.setColumnType(value);
106: break;
107: case 3:
108: if (!MiscUtils.isNull(value)) {
109: int _value = Integer.parseInt(value);
110: cd.setColumnSize(_value);
111: }
112: break;
113: case 4:
114: if (!MiscUtils.isNull(value)) {
115: int _value = Integer.parseInt(value);
116: cd.setColumnScale(_value);
117: }
118: break;
119: }
120: updateScript(row, col);
121: }
122:
123: /**
124: * Updates the generated scriptlet using the specified
125: * row and col as the last upfdaed/modified value.
126: *
127: * @param row - the last updated row
128: * @param col - the last updated col
129: */
130: private void updateScript(int row, int col) {
131: line.setLength(0);
132: ColumnData cd = (ColumnData) tableVector.get(row);
133: line.setLength(0);
134: line.append(NEW_LINE_2)
135: .append(
136: cd.getColumnName() == null ? EMPTY : cd
137: .getColumnName()).append(SPACE);
138:
139: if (cd.getColumnType() != null) {
140: line.append(cd.getFormattedDataType());
141: }
142:
143: line.append(cd.isRequired() ? NOT_NULL : EMPTY);
144:
145: if (row < tableVector.size() - 1) {
146: line.append(COMMA);
147: }
148:
149: if (cd.isNewColumn()) {
150: cd.setNewColumn(false);
151: }
152:
153: addColumnLines(row);
154: }
155:
156: /**
157: * Adds all the column definition lines to
158: * the SQL text buffer for display.
159: *
160: * @param the current row being edited
161: */
162: public void addColumnLines(int row) {
163:
164: sqlText.setLength(0);
165:
166: for (int i = 0, k = tableVector.size(); i < k; i++) {
167: ColumnData cd = (ColumnData) tableVector.elementAt(i);
168:
169: if (i == row) {
170: sqlText.append(line);
171: }
172:
173: else if (!cd.isNewColumn()) {
174:
175: sqlText.append(NEW_LINE_2).append(
176: cd.getColumnName() == null ? EMPTY : cd
177: .getColumnName()).append(SPACE);
178:
179: if (cd.getColumnType() != null) {
180: sqlText.append(cd.getColumnType().toUpperCase());
181:
182: if (!cd.getColumnType().equalsIgnoreCase(DATE)) {
183: sqlText.append(B_OPEN).append(
184: cd.getColumnSize());
185:
186: if (cd.getColumnScale() != 0) {
187: sqlText.append(COMMA).append(
188: cd.getColumnScale());
189: }
190:
191: sqlText.append(B_CLOSE);
192: }
193:
194: }
195: sqlText.append(cd.isRequired() ? NOT_NULL : EMPTY);
196:
197: if (i != k - 1) {
198: sqlText.append(COMMA);
199: }
200:
201: }
202:
203: }
204:
205: creator.setSQLText(sqlText.toString(),
206: TableModifier.COLUMN_VALUES);
207:
208: }
209:
210: }
|