001: package org.jsqltool.gui.tableplugins;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import javax.swing.event.*;
006: import javax.swing.table.*;
007: import java.awt.event.*;
008: import org.jsqltool.conn.DbConnectionUtil;
009: import org.jsqltool.model.CustomTableModel;
010: import java.sql.*;
011: import java.util.*;
012: import org.jsqltool.gui.*;
013: import org.jsqltool.*;
014: import org.jsqltool.gui.tablepanel.*;
015: import org.jsqltool.gui.panel.*;
016: import org.jsqltool.utils.Options;
017:
018: /**
019: * <p>Title: JSqlTool Project</p>
020: * <p>Description: Dialog used to add a column to a table.
021: * </p>
022: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
023: *
024: * <p> This file is part of JSqlTool project.
025: * This library is free software; you can redistribute it and/or
026: * modify it under the terms of the (LGPL) Lesser General Public
027: * License as published by the Free Software Foundation;
028: *
029: * GNU LESSER GENERAL PUBLIC LICENSE
030: * Version 2.1, February 1999
031: *
032: * This library is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
035: * Library General Public License for more details.
036: *
037: * You should have received a copy of the GNU Library General Public
038: * License along with this library; if not, write to the Free
039: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
040: *
041: * The author may be contacted at:
042: * maurocarniel@tin.it</p>
043: *
044: * @author Mauro Carniel
045: * @version 1.0
046: */
047: public class AddColumnDialog extends JDialog {
048:
049: JPanel mainPnel = new JPanel();
050:
051: /** current table name */
052: private String tableName = null;
053:
054: /** database connection util */
055: private DbConnectionUtil dbConnUtil = null;
056:
057: GridBagLayout gridBagLayout1 = new GridBagLayout();
058: JLabel colNameLabel = new JLabel();
059: JTextField colNameTF = new JTextField();
060: JLabel colTypeLabel = new JLabel();
061: JComboBox colTypeComboBox = new JComboBox();
062: JLabel sizeLabel = new JLabel();
063: JTextField sizeTF = new JTextField();
064: JLabel decLabel = new JLabel();
065: JTextField decTF = new JTextField();
066: JLabel defLabel = new JLabel();
067: JTextField defTF = new JTextField();
068: JPanel buttonsPanel = new JPanel();
069: JButton execButton = new JButton();
070: JButton cancelButton = new JButton();
071: private JFrame parent = null;
072: JCheckBox notNullCheckBox = new JCheckBox();
073:
074: public AddColumnDialog(JFrame parent, String tableName,
075: DbConnectionUtil dbConnUtil) {
076: super (parent, Options.getInstance().getResource("add column"),
077: true);
078: this .parent = parent;
079: this .tableName = tableName;
080: this .dbConnUtil = dbConnUtil;
081: try {
082: jbInit();
083: init();
084: setSize(400, 200);
085: Dimension screenSize = Toolkit.getDefaultToolkit()
086: .getScreenSize();
087: setLocation((int) screenSize.getWidth() / 2 - 170,
088: (int) screenSize.getHeight() / 2 - 30);
089: setVisible(true);
090: } catch (Exception ex) {
091: ex.printStackTrace();
092: }
093: }
094:
095: /**
096: * Initialize column type combo box.
097: */
098: private void init() {
099: DefaultComboBoxModel model = new DefaultComboBoxModel();
100: model.addElement("CHAR");
101: model.addElement("VARCHAR");
102: model.addElement("VARCHAR2");
103: model.addElement("LONG");
104: model.addElement("LONGRAW");
105: model.addElement("LONGVARCHAR");
106: model.addElement("NUMBER");
107: model.addElement("NUMERIC");
108: model.addElement("INTEGER");
109: model.addElement("INT");
110: model.addElement("DECIMAL");
111: model.addElement("BIGINT");
112: model.addElement("REAL");
113: model.addElement("DOUBLE");
114: model.addElement("FLOAT");
115: model.addElement("SMALLINT");
116: model.addElement("BOOLEAN");
117: model.addElement("DATE");
118: model.addElement("DATETIME");
119: model.addElement("TIMESTAMP");
120: model.addElement("LONGVARBINARY");
121: model.addElement("BLOB");
122: model.addElement("CLOB");
123:
124: colTypeComboBox.setModel(model);
125: }
126:
127: private void jbInit() throws Exception {
128: mainPnel.setLayout(gridBagLayout1);
129: colNameLabel.setText(Options.getInstance().getResource(
130: "column name"));
131: colTypeLabel.setToolTipText("");
132: colTypeLabel.setText(Options.getInstance().getResource(
133: "column type"));
134: sizeLabel.setToolTipText("");
135: sizeLabel.setText(Options.getInstance().getResource("size"));
136: sizeTF.setColumns(5);
137: decLabel.setText(",");
138: decTF.setColumns(5);
139: defLabel.setText(Options.getInstance().getResource("default"));
140: mainPnel.setBorder(BorderFactory.createEtchedBorder());
141: execButton
142: .setText(Options.getInstance().getResource("execute"));
143: execButton
144: .addActionListener(new AddColumnDialog_execButton_actionAdapter(
145: this ));
146: cancelButton.setText(Options.getInstance().getResource(
147: "cancelbutton.text"));
148: cancelButton
149: .addActionListener(new AddColumnDialog_cancelButton_actionAdapter(
150: this ));
151: notNullCheckBox.setText(Options.getInstance().getResource(
152: "not null"));
153: getContentPane().add(mainPnel);
154: mainPnel.add(colNameLabel, new GridBagConstraints(0, 0, 1, 1,
155: 0.0, 0.0, GridBagConstraints.NORTHWEST,
156: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
157: mainPnel.add(colNameTF, new GridBagConstraints(1, 0, 4, 1, 1.0,
158: 0.0, GridBagConstraints.NORTHWEST,
159: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
160: 0, 0));
161: mainPnel.add(colTypeLabel, new GridBagConstraints(0, 1, 1, 1,
162: 0.0, 0.0, GridBagConstraints.NORTHWEST,
163: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
164: mainPnel.add(colTypeComboBox,
165: new GridBagConstraints(1, 1, 4, 1, 0.0, 0.0,
166: GridBagConstraints.NORTHWEST,
167: GridBagConstraints.NONE,
168: new Insets(5, 5, 5, 5), 150, 0));
169: mainPnel.add(sizeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0,
170: 0.0, GridBagConstraints.NORTHWEST,
171: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
172: mainPnel.add(sizeTF, new GridBagConstraints(1, 2, 1, 1, 0.0,
173: 0.0, GridBagConstraints.NORTHWEST,
174: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
175: mainPnel.add(decLabel, new GridBagConstraints(2, 2, 1, 1, 0.0,
176: 0.0, GridBagConstraints.NORTHWEST,
177: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
178: mainPnel.add(decTF, new GridBagConstraints(3, 2, 1, 1, 0.0,
179: 0.0, GridBagConstraints.NORTHWEST,
180: GridBagConstraints.NONE, new Insets(5, 5, 5, 0), 0, 0));
181: mainPnel.add(defLabel, new GridBagConstraints(0, 3, 1, 1, 0.0,
182: 0.0, GridBagConstraints.NORTHWEST,
183: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
184: mainPnel.add(defTF, new GridBagConstraints(1, 3, 4, 1, 1.0,
185: 1.0, GridBagConstraints.NORTHWEST,
186: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
187: 0, 0));
188: mainPnel.add(notNullCheckBox, new GridBagConstraints(4, 2, 1,
189: 1, 0.0, 0.0, GridBagConstraints.CENTER,
190: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
191: this .getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
192: buttonsPanel.add(execButton, null);
193: buttonsPanel.add(cancelButton, null);
194: }
195:
196: void execButton_actionPerformed(ActionEvent e) {
197: if (colNameTF.getText().trim().length() == 0) {
198: JOptionPane.showMessageDialog(parent, Options.getInstance()
199: .getResource("please specify the column name"),
200: Options.getInstance().getResource("attention"),
201: JOptionPane.WARNING_MESSAGE);
202: return;
203: }
204: if (colTypeComboBox.getSelectedIndex() == -1) {
205: JOptionPane.showMessageDialog(parent, Options.getInstance()
206: .getResource("please specify the column type"),
207: Options.getInstance().getResource("attention"),
208: JOptionPane.WARNING_MESSAGE);
209: return;
210: }
211: if (sizeTF.getText().trim().length() == 0
212: && !(colTypeComboBox.getSelectedItem().equals("DATE")
213: || colTypeComboBox.getSelectedItem().equals(
214: "DATETIME")
215: || colTypeComboBox.getSelectedItem().equals(
216: "TIMESTAMP")
217: || colTypeComboBox.getSelectedItem().equals(
218: "BLOB")
219: || colTypeComboBox.getSelectedItem().equals(
220: "CLOB") || colTypeComboBox
221: .getSelectedItem().equals("NUMBER"))) {
222: JOptionPane.showMessageDialog(parent, Options.getInstance()
223: .getResource("please specify the column size"),
224: Options.getInstance().getResource("attention"),
225: JOptionPane.WARNING_MESSAGE);
226: return;
227: }
228:
229: try {
230: String sql = "ALTER TABLE " + tableName + " ADD "
231: + colNameTF.getText() + " "
232: + colTypeComboBox.getSelectedItem();
233: if (!(colTypeComboBox.getSelectedItem().equals("DATE")
234: || colTypeComboBox.getSelectedItem().equals(
235: "DATETIME")
236: || colTypeComboBox.getSelectedItem().equals(
237: "TIMESTAMP")
238: || colTypeComboBox.getSelectedItem().equals("BLOB") || colTypeComboBox
239: .getSelectedItem().equals("CLOB"))
240: && sizeTF.getText().trim().length() > 0) {
241: sql += "(" + sizeTF.getText().trim();
242: if (decTF.getText().trim().length() > 0)
243: sql += "," + decTF.getText().trim();
244: sql += ") ";
245: if (defTF.getText().length() > 0)
246: sql += " DEFAULT " + defTF.getText();
247: if (notNullCheckBox.isSelected())
248: sql += " NOT NULL";
249: }
250:
251: PreparedStatement pstmt = dbConnUtil.getConn()
252: .prepareStatement(sql);
253: pstmt.execute();
254: pstmt.close();
255: setVisible(false);
256: dispose();
257: } catch (Exception ex) {
258: JOptionPane.showMessageDialog(parent, Options.getInstance()
259: .getResource("error while adding column:")
260: + "\n" + ex.getMessage(), Options.getInstance()
261: .getResource("error"), JOptionPane.ERROR_MESSAGE);
262: }
263:
264: }
265:
266: void cancelButton_actionPerformed(ActionEvent e) {
267: setVisible(false);
268: dispose();
269: }
270:
271: }
272:
273: class AddColumnDialog_execButton_actionAdapter implements
274: java.awt.event.ActionListener {
275: AddColumnDialog adaptee;
276:
277: AddColumnDialog_execButton_actionAdapter(AddColumnDialog adaptee) {
278: this .adaptee = adaptee;
279: }
280:
281: public void actionPerformed(ActionEvent e) {
282: adaptee.execButton_actionPerformed(e);
283: }
284: }
285:
286: class AddColumnDialog_cancelButton_actionAdapter implements
287: java.awt.event.ActionListener {
288: AddColumnDialog adaptee;
289:
290: AddColumnDialog_cancelButton_actionAdapter(AddColumnDialog adaptee) {
291: this .adaptee = adaptee;
292: }
293:
294: public void actionPerformed(ActionEvent e) {
295: adaptee.cancelButton_actionPerformed(e);
296: }
297: }
|