001: package net.sourceforge.squirrel_sql.plugins.mysql.action;
002:
003: /*
004: * Copyright (C) 2003 Arun Kapilan.P
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library 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 GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.sql.SQLException;
021:
022: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
023: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
024: import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
025: import net.sourceforge.squirrel_sql.fw.util.BaseException;
026: import net.sourceforge.squirrel_sql.fw.util.ICommand; //import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
027: //import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
028:
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030:
031: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
032: import net.sourceforge.squirrel_sql.plugins.mysql.gui.AlterTableDialog;
033:
034: /**
035: * AlterTableCommand.java
036: *
037: * Created on June 10, 2003, 3:08 PM
038: *
039: * @author Arun Kapilan.P
040: */
041: public class AlterTableCommand implements ICommand {
042: // private javax.swing.JComboBox cbFieldType;
043: // private javax.swing.JCheckBox chAutoIncrement;
044: // private javax.swing.JCheckBox chBinary;
045: // private javax.swing.JCheckBox chNotNull;
046: // private javax.swing.JCheckBox chUnsigned;
047: // private javax.swing.JLabel lbAttributes;
048: // private javax.swing.JLabel lbDefault;
049: // private javax.swing.JLabel lbFieldLength;
050: // private javax.swing.JLabel lbFieldName;
051: // private javax.swing.JLabel lbFieldType;
052: // private javax.swing.JLabel lbFields;
053: // private javax.swing.JTextField tfFieldDefault;
054: // private javax.swing.JTextField tfFieldLength;
055: // private javax.swing.JComboBox cbFieldName;
056: // private javax.swing.JButton buttonUpdate;
057: // private javax.swing.JButton buttonCancel;
058: // private JDialog _dlog;
059: // private FieldDetails fd;
060: // private int selectedIndex;
061: // private DBUtils dbUtils;
062: // protected String SQLCommandRoot = "ALTER TABLE ";
063: // protected String SQLCommand = "";
064: // End of variables declaration
065:
066: // /** Logger for this class. */
067: // private final static ILogger s_log =
068: // LoggerController.createLogger(AlterTableCommand.class);
069:
070: /** Current session. */
071: private ISession _session;
072:
073: /** Current plugin. */
074: private final MysqlPlugin _plugin;
075:
076: /** Points to the table we want to modify. */
077: private final ITableInfo _ti;
078:
079: /**
080: * Ctor.
081: *
082: * @param session Current session.
083: * @param plugin This plugin.
084: * @param ti Points to table to be modified.
085: *
086: * @throws IllegalArgumentException
087: * Thrown if <TT>null</TT> <TT>ISession</T>>,
088: * <TT>MysqlPlugin</TT> or <<TT>ITableInfo</TT> passed.
089: */
090: public AlterTableCommand(ISession session, MysqlPlugin plugin,
091: ITableInfo ti) {
092: super ();
093: if (session == null) {
094: throw new IllegalArgumentException("ISession == null");
095: }
096: if (plugin == null) {
097: throw new IllegalArgumentException("MysqlPlugin == null");
098: }
099: if (ti == null) {
100: throw new IllegalArgumentException("ITableInfo == null");
101: }
102:
103: _session = session;
104: _plugin = plugin;
105: _ti = ti;
106: // initComponents();
107: }
108:
109: public void execute() throws BaseException {
110: try {
111: AlterTableDialog dlog = new AlterTableDialog(_session,
112: _plugin, _ti);
113: dlog.pack();
114: GUIUtils.centerWithinParent(dlog);
115: dlog.setVisible(true);
116: } catch (SQLException ex) {
117: throw new WrappedSQLException(ex);
118: }
119: }
120:
121: // private void initComponents()
122: // {
123: // lbFields = new javax.swing.JLabel();
124: // lbFieldName = new javax.swing.JLabel();
125: // lbFieldLength = new javax.swing.JLabel();
126: // lbFieldType = new javax.swing.JLabel();
127: // lbDefault = new javax.swing.JLabel();
128: // lbAttributes = new javax.swing.JLabel();
129: // chBinary = new javax.swing.JCheckBox();
130: // chNotNull = new javax.swing.JCheckBox();
131: // chUnsigned = new javax.swing.JCheckBox();
132: // chAutoIncrement = new javax.swing.JCheckBox();
133: // cbFieldName = new javax.swing.JComboBox();
134: // cbFieldType = new javax.swing.JComboBox();
135: // tfFieldLength = new javax.swing.JTextField();
136: // tfFieldDefault = new javax.swing.JTextField();
137: // buttonUpdate = new javax.swing.JButton();
138: // buttonCancel = new javax.swing.JButton();
139: // dbUtils = new DBUtils(_session, _plugin);
140: // SQLCommandRoot += dbUtils.getTableInfo() + " CHANGE ";
141: // _dlog = new JDialog(_session.getApplication().getMainFrame(),
142: // "Alter Table...");
143: // _dlog.getContentPane().setLayout(null);
144: //
145: // _dlog.addWindowListener(new java.awt.event.WindowAdapter()
146: // {
147: // public void windowClosing(java.awt.event.WindowEvent evt)
148: // {
149: // closeDialog(evt);
150: // }
151: // });
152: //
153: // lbFields.setText("Fields");
154: // lbFields.setBorder(
155: // new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
156: // _dlog.getContentPane().add(lbFields);
157: // lbFields.setBounds(20, 20, 40, 18);
158: //
159: // lbFieldName.setFont(new java.awt.Font("Dialog", 0, 12));
160: // lbFieldName.setText("Name:");
161: // _dlog.getContentPane().add(lbFieldName);
162: // lbFieldName.setBounds(20, 60, 40, 16);
163: //
164: // lbFieldLength.setFont(new java.awt.Font("Dialog", 0, 12));
165: // lbFieldLength.setText("Length/Set:");
166: // _dlog.getContentPane().add(lbFieldLength);
167: // lbFieldLength.setBounds(20, 120, 62, 16);
168: //
169: // lbFieldType.setFont(new java.awt.Font("Dialog", 0, 12));
170: // lbFieldType.setText("Type:");
171: // _dlog.getContentPane().add(lbFieldType);
172: // lbFieldType.setBounds(20, 90, 29, 16);
173: //
174: // lbDefault.setFont(new java.awt.Font("Dialog", 0, 12));
175: // lbDefault.setText("Default:");
176: // _dlog.getContentPane().add(lbDefault);
177: // lbDefault.setBounds(20, 150, 50, 16);
178: //
179: // lbAttributes.setText("Attributes");
180: // lbAttributes.setBorder(
181: // new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
182: // _dlog.getContentPane().add(lbAttributes);
183: // lbAttributes.setBounds(20, 190, 60, 18);
184: //
185: // chBinary.setFont(new java.awt.Font("Dialog", 0, 12));
186: // chBinary.setText("Binary");
187: // _dlog.getContentPane().add(chBinary);
188: // chBinary.setBounds(20, 230, 70, 24);
189: //
190: // chNotNull.setFont(new java.awt.Font("Dialog", 0, 12));
191: // chNotNull.setText("Not Null");
192: // _dlog.getContentPane().add(chNotNull);
193: // chNotNull.setBounds(130, 230, 70, 24);
194: //
195: // chUnsigned.setFont(new java.awt.Font("Dialog", 0, 12));
196: // chUnsigned.setText("Unsigned");
197: // _dlog.getContentPane().add(chUnsigned);
198: // chUnsigned.setBounds(20, 270, 79, 24);
199: //
200: // chAutoIncrement.setFont(new java.awt.Font("Dialog", 0, 12));
201: // chAutoIncrement.setText("AutoIncrement");
202: // _dlog.getContentPane().add(chAutoIncrement);
203: // chAutoIncrement.setBounds(130, 270, 110, 24);
204: //
205: // cbFieldName.setEditable(false);
206: // cbFieldName.addActionListener(new java.awt.event.ActionListener()
207: // {
208: // public void actionPerformed(java.awt.event.ActionEvent evt)
209: // {
210: // cbFieldNameActionPerformed(evt);
211: // }
212: // });
213: //
214: // _dlog.getContentPane().add(cbFieldName);
215: // cbFieldName.setBounds(110, 60, 120, 20);
216: //
217: // cbFieldType.setFont(new java.awt.Font("Dialog", 0, 12));
218: // cbFieldType.addActionListener(new java.awt.event.ActionListener()
219: // {
220: // public void actionPerformed(java.awt.event.ActionEvent evt)
221: // {
222: // cbFieldTypeActionPerformed(evt);
223: // }
224: // });
225: // cbFieldType.setModel(
226: // new javax.swing.DefaultComboBoxModel(
227: // new String[] {
228: // "TINYINT",
229: // "SMALLINT",
230: // "MEDIUMINT",
231: // "INT",
232: // "BIGINT",
233: // "FLOAT",
234: // "DOUBLE",
235: // "DECIMAL",
236: // "DATE",
237: // "DATETIME",
238: // "TIMESTAMP",
239: // "TIME",
240: // "YEAR",
241: // "CHAR",
242: // "VARCHAR",
243: // "TINYBLOB",
244: // "TINYTEXT",
245: // "TEXT",
246: // "BLOB",
247: // "MEDIUMBLOB",
248: // "MEDIUMTEXT",
249: // "LONGBLOB",
250: // "LONGTEXT",
251: // "ENUM",
252: // "SET" }));
253: // cbFieldType.addActionListener(new java.awt.event.ActionListener()
254: // {
255: // public void actionPerformed(java.awt.event.ActionEvent evt)
256: // {
257: // cbFieldTypeActionPerformed(evt);
258: // }
259: // });
260: //
261: // _dlog.getContentPane().add(cbFieldType);
262: // cbFieldType.setBounds(110, 90, 120, 20);
263: //
264: // _dlog.getContentPane().add(tfFieldLength);
265: // tfFieldLength.setBounds(110, 120, 120, 20);
266: //
267: // _dlog.getContentPane().add(tfFieldDefault);
268: // tfFieldDefault.setBounds(110, 150, 120, 20);
269: //
270: // buttonUpdate.setFont(new java.awt.Font("Dialog", 0, 12));
271: // buttonUpdate.setText("Update..");
272: // buttonUpdate.addActionListener(new java.awt.event.ActionListener()
273: // {
274: // public void actionPerformed(java.awt.event.ActionEvent evt)
275: // {
276: // buttonUpdateActionPerformed(evt);
277: // }
278: // });
279: //
280: // _dlog.getContentPane().add(buttonUpdate);
281: // buttonUpdate.setBounds(130, 310, 81, 26);
282: //
283: // buttonCancel.setFont(new java.awt.Font("Dialog", 0, 12));
284: // buttonCancel.setText("Cancel");
285: // buttonCancel.addActionListener(new java.awt.event.ActionListener()
286: // {
287: // public void actionPerformed(java.awt.event.ActionEvent evt)
288: // {
289: // buttonCancelActionPerformed(evt);
290: // }
291: // });
292: //
293: // _dlog.getContentPane().add(buttonCancel);
294: // buttonCancel.setBounds(220, 310, 81, 26);
295: //
296: // _dlog.pack();
297: // }
298:
299: //Action performed for the cancel button
300: // private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt)
301: // {
302: // _dlog.setVisible(false);
303: // _dlog.dispose();
304: // }
305:
306: //Action performed for the Update button
307: // private void buttonUpdateActionPerformed(java.awt.event.ActionEvent evt)
308: // {
309: // String query = getQuery();
310: // dbUtils.execute(query);
311: // _dlog.setVisible(false);
312: // _dlog.dispose();
313: // JOptionPane.showMessageDialog(null, "Field updated");
314: // }
315:
316: //Action performed for the combo box
317: // private void cbFieldNameActionPerformed(java.awt.event.ActionEvent evt)
318: // {
319: //
320: // selectedIndex = cbFieldName.getSelectedIndex();
321: // DefaultComboBoxModel comboModel =
322: // (DefaultComboBoxModel) cbFieldName.getModel();
323: // FieldDetails fd = (FieldDetails) comboModel.getElementAt(selectedIndex);
324: //
325: // cbFieldName.setSelectedItem(fd.getFieldName());
326: // cbFieldType.setSelectedItem(fd.getFieldType());
327: // tfFieldLength.setText(fd.getFieldLength());
328: // tfFieldDefault.setText(fd.getDefault());
329: // chAutoIncrement.setSelected(fd.IsAutoIncrement());
330: // chNotNull.setSelected(fd.IsNotNull());
331: //
332: // }
333:
334: //Action performed on selection of field type
335: // private void cbFieldTypeActionPerformed(java.awt.event.ActionEvent evt)
336: // {
337: // if (cbFieldType.getSelectedItem().equals("VARCHAR"))
338: // {
339: // chBinary.setEnabled(true);
340: // chUnsigned.setEnabled(false);
341: // }
342: // else
343: // {
344: // chUnsigned.setEnabled(false);
345: // }
346: // if (cbFieldType.getSelectedItem().equals("INT"))
347: // {
348: // chBinary.setEnabled(false);
349: // chUnsigned.setEnabled(true);
350: // }
351: // }
352:
353: /** Closes the dialog */
354: // private void closeDialog(java.awt.event.WindowEvent evt)
355: // {
356: // _dlog.setVisible(false);
357: // _dlog.dispose();
358: // }
359: //Populate the dialog with the field properties when its loaded
360: // public void execute() throws BaseException
361: // {
362: // JDialog jd =
363: // new JDialog(
364: // _session.getApplication().getMainFrame(),
365: // "Properties",
366: // true);
367: // final SQLConnection conn = _session.getSQLConnection();
368: // ResultSetMetaData rsmd;
369: // int colCount = 0;
370: //
371: // try
372: // {
373: // SQLDatabaseMetaData dmd = conn.getSQLMetaData();
374: // IObjectTreeAPI treeAPI = _session.getObjectTreeAPI(_plugin);
375: // IDatabaseObjectInfo[] dbInfo = treeAPI.getSelectedDatabaseObjects();
376: // ITableInfo tableInfo = null;
377: //
378: // // can't work with multiple selected objects
379: // if (dbInfo.length != 1)
380: // {
381: // return;
382: // }
383: //
384: // // get the table info for the selected table
385: // if (dbInfo[0] instanceof ITableInfo)
386: // {
387: // tableInfo = (ITableInfo) dbInfo[0];
388: // }
389: //
390: // final ResultSet rs = dmd.getColumns(tableInfo);
391: // rsmd = rs.getMetaData();
392: // colCount = rsmd.getColumnCount();
393: // DefaultComboBoxModel comboModel =
394: // (DefaultComboBoxModel) cbFieldName.getModel();
395: //
396: // //Store the column info in a object (Field details) and add to the combo
397: // while (rs.next())
398: // {
399: // fd = new FieldDetails();
400: // fd.setFieldName(rs.getString(4));
401: // fd.setFieldType(rs.getString(6).toUpperCase());
402: // fd.setFieldLength(rs.getString(7));
403: // fd.setDefault(rs.getString(13));
404: // if ("auto_increment".equals(rs.getString(12)))
405: // fd.setAutoIncrement(true);
406: // else
407: // fd.setAutoIncrement(false);
408: // if ("0".equals(rs.getString(11)))
409: // fd.setNotNull(true);
410: // else
411: // fd.setNotNull(false);
412: //
413: // comboModel.addElement(fd);
414: //
415: // }
416: // }
417: // catch (SQLException ex)
418: // {
419: // _session.getMessageHandler().showErrorMessage(ex);
420: // }
421: //
422: // JButton buttonUpdate = new JButton("Update");
423: // buttonUpdate.addActionListener(new java.awt.event.ActionListener()
424: // {
425: // public void actionPerformed(java.awt.event.ActionEvent evt)
426: // {
427: // buttonUpdateActionPerformed(evt);
428: // }
429: // });
430: // _dlog.setLocation(100, 100);
431: // _dlog.setSize(325, 400);
432: // _dlog.setVisible(true);
433: //
434: // }
435: //Get the query to alter the table
436: // public String getQuery()
437: // {
438: // DefaultComboBoxModel comboModel =
439: // (DefaultComboBoxModel) cbFieldName.getModel();
440: // FieldDetails fd = (FieldDetails) comboModel.getElementAt(selectedIndex);
441: // SQLCommand += fd.getFieldName();
442: // SQLCommand += " ";
443: // SQLCommand += cbFieldName.getSelectedItem();
444: // SQLCommand += " ";
445: // SQLCommand += cbFieldType.getSelectedItem();
446: // SQLCommand += "(" + tfFieldLength.getText() + ")";
447: // if (chAutoIncrement.isSelected())
448: // SQLCommand += " AUTO_INCREMENT ";
449: // if (chNotNull.isSelected())
450: // SQLCommand += " NOT NULL ";
451: // if (chUnsigned.isSelected())
452: // SQLCommand += " UNSIGNED ";
453: // SQLCommand = SQLCommandRoot + SQLCommand;
454: // return (SQLCommand);
455: // }
456: }
|