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: import java.sql.Statement;
022:
023: import javax.swing.DefaultListModel;
024: import javax.swing.JDialog;
025: import javax.swing.JOptionPane;
026: import javax.swing.JScrollPane;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
030: import net.sourceforge.squirrel_sql.fw.util.ICommand;
031: import net.sourceforge.squirrel_sql.fw.util.StringManager;
032: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
033: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
034: import net.sourceforge.squirrel_sql.plugins.mysql.util.FieldDetails;
035:
036: /**
037: * CreateTableCommand.java
038: *
039: * Created on June 15, 2003, 2:44 PM
040: *
041: * @author Arun Kapilan.P
042: */
043: public class CreateTableCommand extends JDialog implements ICommand {
044: private static final long serialVersionUID = 1L;
045:
046: private static final StringManager s_stringMgr = StringManagerFactory
047: .getStringManager(CreateTableCommand.class);
048:
049: private javax.swing.JButton btAdd;
050: private javax.swing.JButton btRemove;
051: private javax.swing.JButton btCancel;
052: private javax.swing.JButton btCreateTable;
053: private javax.swing.JComboBox cbFieldType;
054: private javax.swing.JComboBox cbTableType;
055: private javax.swing.JCheckBox chAuto;
056: private javax.swing.JCheckBox chBinary;
057: private javax.swing.JCheckBox chIndex;
058: private javax.swing.JCheckBox chNotNull;
059: private javax.swing.JCheckBox chPrimary;
060: private javax.swing.JCheckBox chUnique;
061: private javax.swing.JCheckBox chUnsigned;
062: private javax.swing.JCheckBox chZeroFill;
063: private javax.swing.JList listFields;
064: private javax.swing.JLabel lbDefault;
065: private javax.swing.JLabel lbFieldLength;
066: private javax.swing.JLabel lbFieldProp;
067: private javax.swing.JLabel lbFieldType;
068: private javax.swing.JLabel lbFields;
069: private javax.swing.JLabel lbTableName;
070: private javax.swing.JLabel lbTableType;
071: private javax.swing.JTextField tfDefault;
072: private javax.swing.JTextField tfFieldLength;
073: private javax.swing.JTextField tfFieldName;
074: private javax.swing.JTextField tfTableName;
075: private javax.swing.JDialog jd;
076: // private DBUtils dbUtils;
077: protected String SQLCommandRoot = "CREATE TABLE ";
078: protected String SQLCommand = "";
079:
080: /** Logger for this class. */
081: // private final static ILogger s_log =
082: // LoggerController.createLogger(CreateMysqlTableScriptCommand.class);
083: /** Current session. */
084: private ISession _session;
085:
086: /** Current plugin. */
087: @SuppressWarnings("unused")
088: private final MysqlPlugin _plugin;
089:
090: public CreateTableCommand(ISession session, MysqlPlugin plugin) {
091: super ();
092: _session = session;
093: _plugin = plugin;
094: }
095:
096: public void execute() {
097: initComponents();
098: }
099:
100: /** This method is called from within the constructor to
101: * initialize the form.
102: * WARNING: Do NOT modify this code. The content of this method is
103: * always regenerated by the Form Editor.
104: */
105: private void initComponents() {
106: lbTableName = new javax.swing.JLabel();
107: tfTableName = new javax.swing.JTextField();
108: lbTableType = new javax.swing.JLabel();
109: cbTableType = new javax.swing.JComboBox();
110: lbFields = new javax.swing.JLabel();
111: tfFieldName = new javax.swing.JTextField();
112: btAdd = new javax.swing.JButton();
113: lbFieldProp = new javax.swing.JLabel();
114: lbFieldType = new javax.swing.JLabel();
115: cbFieldType = new javax.swing.JComboBox();
116: lbFieldLength = new javax.swing.JLabel();
117: tfFieldLength = new javax.swing.JTextField();
118: lbDefault = new javax.swing.JLabel();
119: tfDefault = new javax.swing.JTextField();
120: chPrimary = new javax.swing.JCheckBox();
121: chIndex = new javax.swing.JCheckBox();
122: chUnique = new javax.swing.JCheckBox();
123: chBinary = new javax.swing.JCheckBox();
124: chNotNull = new javax.swing.JCheckBox();
125: chUnsigned = new javax.swing.JCheckBox();
126: chAuto = new javax.swing.JCheckBox();
127: chZeroFill = new javax.swing.JCheckBox();
128: listFields = new javax.swing.JList(new DefaultListModel());
129: btCreateTable = new javax.swing.JButton();
130: btRemove = new javax.swing.JButton();
131: btCancel = new javax.swing.JButton();
132: jd = new JDialog(_session.getApplication().getMainFrame(),
133: // i18n[mysql.createTableComm=Create Table...]
134: s_stringMgr.getString("mysql.createTableComm"));
135: jd.getContentPane().setLayout(null);
136:
137: addWindowListener(new java.awt.event.WindowAdapter() {
138: public void windowClosing(java.awt.event.WindowEvent evt) {
139: closeDialog(evt);
140: }
141: });
142:
143: // i18n[mysql.tableNamelbl=Table Name:]
144: lbTableName
145: .setText(s_stringMgr.getString("mysql.tableNamelbl"));
146: jd.getContentPane().add(lbTableName);
147: lbTableName.setBounds(20, 30, 70, 16);
148:
149: tfTableName.setText("TableName");
150: jd.getContentPane().add(tfTableName);
151: tfTableName.setBounds(120, 30, 100, 20);
152:
153: lbTableType.setFont(new java.awt.Font("Dialog", 0, 12));
154: lbTableType.setText("Table Type:");
155: jd.getContentPane().add(lbTableType);
156: lbTableType.setBounds(20, 60, 70, 16);
157:
158: cbTableType.setFont(new java.awt.Font("Dialog", 0, 12));
159: cbTableType.setModel(new javax.swing.DefaultComboBoxModel(
160: new String[] { "<Automatic>", "ISAM", "MyISAM",
161: "MERGE", "InnoDb", "HEAP", "BDB" }));
162: jd.getContentPane().add(cbTableType);
163: cbTableType.setBounds(120, 60, 100, 20);
164:
165: lbFields.setText("Fields:");
166: lbFields.setBorder(new javax.swing.border.EmptyBorder(
167: new java.awt.Insets(1, 1, 1, 1)));
168: jd.getContentPane().add(lbFields);
169: lbFields.setBounds(10, 120, 40, 18);
170:
171: tfFieldName.setText("FieldName");
172: tfFieldName.addFocusListener(new java.awt.event.FocusAdapter() {
173: public void focusGained(java.awt.event.FocusEvent evt) {
174: tfFieldNameFocusGained(evt);
175: }
176: });
177: jd.getContentPane().add(tfFieldName);
178: tfFieldName.setBounds(10, 150, 110, 20);
179:
180: btAdd.setFont(new java.awt.Font("Dialog", 0, 12));
181: // i18n[mysql.createAdd=Add]
182: btAdd.setText(s_stringMgr.getString("mysql.createAdd"));
183: btAdd.addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(java.awt.event.ActionEvent evt) {
185: btAddActionPerformed(evt);
186: }
187: });
188:
189: jd.getContentPane().add(btAdd);
190: btAdd.setBounds(130, 150, 80, 26);
191:
192: // i18n[mysql.fieldProps=Field Properties:]
193: lbFieldProp.setText(s_stringMgr.getString("mysql.fieldProps"));
194: lbFieldProp.setBorder(new javax.swing.border.EmptyBorder(
195: new java.awt.Insets(1, 1, 1, 1)));
196: jd.getContentPane().add(lbFieldProp);
197: lbFieldProp.setBounds(220, 150, 100, 20);
198:
199: lbFieldType.setFont(new java.awt.Font("Dialog", 0, 12));
200: lbFieldType.setText("Type");
201: jd.getContentPane().add(lbFieldType);
202: lbFieldType.setBounds(220, 190, 41, 16);
203:
204: cbFieldType.setFont(new java.awt.Font("Dialog", 0, 12));
205: cbFieldType.setModel(new javax.swing.DefaultComboBoxModel(
206: new String[] { "TINYINT", "SMALLINT", "MEDIUMINT",
207: "INT", "BIGINT", "FLOAT", "DOUBLE", "DECIMAL",
208: "DATE", "DATETIME", "TIMESTAMP", "TIME",
209: "YEAR", "CHAR", "VARCHAR", "TINYBLOB",
210: "TINYTEXT", "TEXT", "BLOB", "MEDIUMBLOB",
211: "MEDIUMTEXT", "LONGBLOB", "LONGTEXT", "ENUM",
212: "SET" }));
213: cbFieldType
214: .addActionListener(new java.awt.event.ActionListener() {
215: public void actionPerformed(
216: java.awt.event.ActionEvent evt) {
217: cbFieldTypeActionPerformed(evt);
218: }
219: });
220: jd.getContentPane().add(cbFieldType);
221: cbFieldType.setBounds(300, 190, 110, 20);
222:
223: lbFieldLength.setFont(new java.awt.Font("Dialog", 0, 12));
224: // i18n[mysql.lengtSet=Length/Set]
225: lbFieldLength.setText(s_stringMgr.getString("mysql.lengtSet"));
226: jd.getContentPane().add(lbFieldLength);
227: lbFieldLength.setBounds(220, 220, 70, 16);
228:
229: jd.getContentPane().add(tfFieldLength);
230: tfFieldLength.setBounds(300, 220, 110, 20);
231:
232: lbDefault.setFont(new java.awt.Font("Dialog", 0, 12));
233: // i18n[mysql.defaultValue=Default Value]
234: lbDefault.setText(s_stringMgr.getString("mysql.defaultValue"));
235: jd.getContentPane().add(lbDefault);
236: lbDefault.setBounds(220, 250, 80, 16);
237:
238: jd.getContentPane().add(tfDefault);
239: tfDefault.setBounds(300, 250, 110, 20);
240:
241: chPrimary.setFont(new java.awt.Font("Dialog", 0, 12));
242: chPrimary.setText("Primary");
243: chPrimary.addItemListener(new java.awt.event.ItemListener() {
244: public void itemStateChanged(java.awt.event.ItemEvent evt) {
245: chPrimaryItemStateChanged(evt);
246: }
247: });
248: jd.getContentPane().add(chPrimary);
249: chPrimary.setBounds(220, 290, 67, 20);
250:
251: chIndex.setFont(new java.awt.Font("Dialog", 0, 12));
252: chIndex.setText("Index");
253: jd.getContentPane().add(chIndex);
254: chIndex.setBounds(290, 290, 54, 20);
255:
256: chUnique.setFont(new java.awt.Font("Dialog", 0, 12));
257: chUnique.setText("Unique");
258: jd.getContentPane().add(chUnique);
259: chUnique.setBounds(350, 290, 65, 20);
260:
261: chBinary.setFont(new java.awt.Font("Dialog", 0, 12));
262: chBinary.setText("Binary");
263: jd.getContentPane().add(chBinary);
264: chBinary.setBounds(220, 320, 70, 20);
265:
266: chNotNull.setFont(new java.awt.Font("Dialog", 0, 12));
267: chNotNull.setText("Not Null");
268: jd.getContentPane().add(chNotNull);
269: chNotNull.setBounds(290, 320, 70, 20);
270:
271: chUnsigned.setFont(new java.awt.Font("Dialog", 0, 12));
272: chUnsigned.setText("Unsigned");
273: jd.getContentPane().add(chUnsigned);
274: chUnsigned.setBounds(360, 320, 80, 20);
275:
276: chAuto.setFont(new java.awt.Font("Dialog", 0, 12));
277: chAuto.setText("Auto Increment");
278: jd.getContentPane().add(chAuto);
279: chAuto.setBounds(220, 350, 110, 20);
280:
281: chZeroFill.setFont(new java.awt.Font("Dialog", 0, 12));
282: chZeroFill.setText("Zero Fill");
283: jd.getContentPane().add(chZeroFill);
284: chZeroFill.setBounds(330, 350, 69, 20);
285:
286: listFields
287: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
288: public void valueChanged(
289: javax.swing.event.ListSelectionEvent evt) {
290: listFieldsValueChanged(evt);
291: }
292: });
293: JScrollPane scrollPane = new JScrollPane(listFields);
294: jd.getContentPane().add(scrollPane);
295: scrollPane.setBounds(10, 190, 110, 150);
296:
297: btCreateTable.setFont(new java.awt.Font("Dialog", 0, 12));
298: // i18n[mysql.createTableBtn=Create table]
299: btCreateTable.setText(s_stringMgr
300: .getString("mysql.createTableBtn"));
301: btCreateTable
302: .addActionListener(new java.awt.event.ActionListener() {
303: public void actionPerformed(
304: java.awt.event.ActionEvent evt) {
305: btCreateTableActionPerformed(evt);
306: }
307: });
308: jd.getContentPane().add(btCreateTable);
309:
310: btCreateTable.setBounds(221, 380, 110, 26);
311: btRemove.setFont(new java.awt.Font("Dialog", 0, 12));
312: // i18n[mysql.btRemove=Remove]
313: btRemove.setText(s_stringMgr.getString("mysql.btRemove"));
314: btRemove.addActionListener(new java.awt.event.ActionListener() {
315: public void actionPerformed(java.awt.event.ActionEvent evt) {
316: btRemoveActionPerformed(evt);
317: }
318: });
319: jd.getContentPane().add(btRemove);
320: btRemove.setBounds(130, 190, 80, 26);
321:
322: btCancel.setFont(new java.awt.Font("Dialog", 0, 12));
323: // i18n[mysql.btCancel=Cancel]
324: btCancel.setText(s_stringMgr.getString("mysql.btCancel"));
325: btCancel.addActionListener(new java.awt.event.ActionListener() {
326: public void actionPerformed(java.awt.event.ActionEvent evt) {
327: btCancelActionPerformed(evt);
328: }
329: });
330:
331: jd.getContentPane().add(btCancel);
332: btCancel.setBounds(340, 380, 73, 26);
333:
334: jd.pack();
335: jd.setSize(450, 450);
336: jd.setLocation(100, 100);
337: jd.setVisible(true);
338:
339: }
340:
341: //Set the attributes for the fields in the class FieldDetails
342: private void btAddActionPerformed(@SuppressWarnings("unused")
343: java.awt.event.ActionEvent evt) {
344: FieldDetails fd = new FieldDetails();
345:
346: fd.setFieldName(tfFieldName.getText());
347: fd.setFieldLength(tfFieldLength.getText());
348: fd.setFieldType((String) cbFieldType.getSelectedItem());
349: fd.setDefault(tfDefault.getText());
350: fd.setPrimary(chPrimary.isSelected());
351: fd.setUnique(chUnique.isSelected());
352: fd.setIndex(chIndex.isSelected());
353: fd.setBinary(chBinary.isSelected());
354: fd.setNotNull(chNotNull.isSelected());
355: fd.setUnisigned(chUnsigned.isSelected());
356: fd.setAutoIncrement(chAuto.isSelected());
357: fd.setZeroFill(chZeroFill.isSelected());
358: DefaultListModel listModel = (DefaultListModel) listFields
359: .getModel();
360: listModel.addElement(fd);
361:
362: }
363:
364: //Display the field attributes when selected in the list
365: private void listFieldsValueChanged(@SuppressWarnings("unused")
366: javax.swing.event.ListSelectionEvent evt) {
367:
368: int index = listFields.getSelectedIndex();
369: DefaultListModel listModel = (DefaultListModel) listFields
370: .getModel();
371: FieldDetails fd = (FieldDetails) listModel.elementAt(index);
372:
373: tfFieldName.setText(fd.getFieldName());
374: cbFieldType.setSelectedItem(fd.getFieldType());
375: tfFieldLength.setText(fd.getFieldLength());
376: tfDefault.setText(fd.getDefault());
377: chPrimary.setSelected(fd.IsPrimary());
378: chUnique.setSelected(fd.IsUnique());
379: chIndex.setSelected(fd.IsIndex());
380: chBinary.setSelected(fd.IsBinary());
381: chNotNull.setSelected(fd.IsNotNull());
382: chUnsigned.setSelected(fd.IsUnisigned());
383: chAuto.setSelected(fd.IsAutoIncrement());
384: chZeroFill.setSelected(fd.IsZeroFill());
385:
386: }
387:
388: private void btCreateTableActionPerformed(
389: @SuppressWarnings("unused")
390: java.awt.event.ActionEvent evt) {
391: ISQLConnection con = _session.getSQLConnection();
392: String query = getQuery();
393: try {
394: Statement stmt = con.createStatement();
395: stmt.execute(query);
396: _session.getSessionInternalFrame().getObjectTreeAPI()
397: .refreshTree();
398: jd.setVisible(false);
399: jd.dispose();
400: JOptionPane.showMessageDialog(null,
401: // i18n[mysql.msgTableCreated=Table {0} created]
402: s_stringMgr.getString("mysql.msgTableCreated",
403: tfTableName.getText()));
404: } catch (SQLException ex) {
405: _session.showErrorMessage(ex);
406:
407: }
408: }
409:
410: private void chPrimaryItemStateChanged(@SuppressWarnings("unused")
411: java.awt.event.ItemEvent evt) {
412: if (chPrimary.isSelected())
413: chNotNull.setSelected(true);
414:
415: }
416:
417: private void cbFieldTypeActionPerformed(@SuppressWarnings("unused")
418: java.awt.event.ActionEvent evt) {
419: tfFieldLength.setText("5");
420:
421: if (cbFieldType.getSelectedItem().equals("VARCHAR")) {
422: chBinary.setEnabled(true);
423: chUnsigned.setEnabled(false);
424: chZeroFill.setEnabled(false);
425: } else {
426: chUnsigned.setEnabled(false);
427: chZeroFill.setEnabled(false);
428: }
429: if (cbFieldType.getSelectedItem().equals("INT")) {
430: chBinary.setEnabled(false);
431: chUnsigned.setEnabled(true);
432: chZeroFill.setEnabled(true);
433: }
434: }
435:
436: //Reset the checkbox selected index false when fieldname gains focus
437: private void tfFieldNameFocusGained(@SuppressWarnings("unused")
438: java.awt.event.FocusEvent evt) {
439: cbFieldType.setSelectedIndex(0);
440: chAuto.setSelected(false);
441: chBinary.setSelected(false);
442: chIndex.setSelected(false);
443: chNotNull.setSelected(false);
444: chPrimary.setSelected(false);
445: chUnique.setSelected(false);
446: chUnsigned.setSelected(false);
447: chZeroFill.setSelected(false);
448: }
449:
450: /** Closes the dialog */
451: private void closeDialog(@SuppressWarnings("unused")
452: java.awt.event.WindowEvent evt) {
453: setVisible(false);
454: dispose();
455: }
456:
457: //Remove the Fields added to the list
458: private void btRemoveActionPerformed(@SuppressWarnings("unused")
459: java.awt.event.ActionEvent evt) {
460: int index = listFields.getSelectedIndex();
461: DefaultListModel listModel = (DefaultListModel) listFields
462: .getModel();
463: listModel.remove(index);
464: listFields.invalidate();
465: }
466:
467: private void btCancelActionPerformed(@SuppressWarnings("unused")
468: java.awt.event.ActionEvent evt) {
469: jd.setVisible(false);
470: jd.dispose();
471: jd.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
472: }
473:
474: public String getQuery() {
475:
476: DefaultListModel listModel = (DefaultListModel) listFields
477: .getModel();
478: SQLCommand = "(";
479: String[] rowData = new String[listModel.getSize()];
480: for (int i = 0; i < rowData.length; i++)
481: rowData[i] = "";
482: for (int i = 0; i < rowData.length; i++) {
483: FieldDetails fd = (FieldDetails) listModel.elementAt(i);
484: rowData[i] += fd.getFieldName();
485: rowData[i] += " ";
486: rowData[i] += fd.getFieldType();
487: rowData[i] += "(";
488: rowData[i] += fd.getFieldLength();
489: rowData[i] += ")";
490: if (fd.IsUnisigned())
491: rowData[i] += " UNSIGNED ";
492: if (fd.IsBinary())
493: rowData[i] += " BINARY ";
494: if (fd.IsZeroFill())
495: rowData[i] += " ZEROFILL ";
496: if (fd.getDefault().length() > 0)
497: rowData[i] += " DEFAULT '" + fd.getDefault() + "'";
498: if (fd.IsNotNull())
499: rowData[i] += " NOT NULL ";
500: if (fd.IsAutoIncrement())
501: rowData[i] += "AUTO_INCREMENT ";
502: if (fd.IsPrimary())
503: rowData[i] += ", PRIMARY KEY(" + fd.getFieldName()
504: + ")";
505: if (fd.IsUnique())
506: rowData[i] += ", UNIQUE(" + fd.getFieldName() + ")";
507: if (fd.IsIndex())
508: rowData[i] += ", INDEX(" + fd.getFieldName() + ")";
509:
510: }
511: for (int i = 0; i < rowData.length; i++) {
512: SQLCommand += rowData[i];
513: if (i < (rowData.length - 1))
514: SQLCommand += ", ";
515:
516: }
517: SQLCommand += ")";
518: if (cbTableType.getSelectedIndex() > 0)
519: SQLCommand += " TYPE = " + cbTableType.getSelectedItem()
520: + " ;";
521: SQLCommandRoot += tfTableName.getText();
522: SQLCommand = SQLCommandRoot + SQLCommand;
523: return (SQLCommand);
524: }
525:
526: }
|