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 javax.swing.ButtonGroup;
021: import javax.swing.DefaultListModel;
022: import javax.swing.JDialog;
023: import javax.swing.JOptionPane;
024: import javax.swing.ListSelectionModel;
025:
026: import net.sourceforge.squirrel_sql.client.session.ISession;
027: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
028: import net.sourceforge.squirrel_sql.fw.util.ICommand;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
032: import net.sourceforge.squirrel_sql.plugins.mysql.util.DBUtils;
033:
034: /*
035: * CopyTableCommand.java
036: *
037: * Created on June 18, 2003, 5:37 PM
038: *
039: * @author Arun Kapilan.P
040: */
041: public class CopyTableCommand implements ICommand {
042: private static final StringManager s_stringMgr = StringManagerFactory
043: .getStringManager(CopyTableCommand.class);
044:
045: private javax.swing.JCheckBox chAllFields;
046: private javax.swing.JButton buttonOk;
047: private javax.swing.JButton buttonCancel;
048: private javax.swing.JLabel lbCopyToNewTable;
049: private javax.swing.JList listFields;
050: private javax.swing.JRadioButton rdStructure;
051: private javax.swing.JRadioButton rdStructureData;
052: private javax.swing.JTextField tfTableName;
053: private javax.swing.JDialog jd;
054: private DBUtils dbUtils;
055: private String[] colNames;
056: //private JCheckBox[] chFields;
057: private ITableInfo oldTableName;
058: private String newTableName;
059: private String SQLCommandRoot = "";
060: private String SQLCommand = "";
061: private String SQLQuery = "";
062: private boolean isStructure = true;
063: private DefaultListModel listModel;
064: // private Object[] fields;
065: private boolean isAllFields = true;
066:
067: /** Logger for this class. */
068: // private final static ILogger s_log =
069: // LoggerController.createLogger(CopyTableCommand.class);
070: /** Current session. */
071: private ISession _session;
072:
073: /** Current plugin. */
074: private final MysqlPlugin _plugin;
075:
076: /**
077: * Ctor specifying the current session.
078: */
079: public CopyTableCommand(ISession session, MysqlPlugin plugin) {
080: super ();
081: _session = session;
082: _plugin = plugin;
083: }
084:
085: public void execute() {
086: initComponents();
087: }
088:
089: private void initComponents() {
090: lbCopyToNewTable = new javax.swing.JLabel();
091: tfTableName = new javax.swing.JTextField();
092: rdStructure = new javax.swing.JRadioButton();
093: rdStructureData = new javax.swing.JRadioButton();
094: listFields = new javax.swing.JList(new DefaultListModel());
095: chAllFields = new javax.swing.JCheckBox();
096: dbUtils = new DBUtils(_session, _plugin);
097: colNames = dbUtils.getColumnNames();
098: oldTableName = dbUtils.getTableInfo();
099: //chFields = new JCheckBox[colNames.length];
100: listFields
101: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
102: listModel = (DefaultListModel) listFields.getModel();
103: for (int i = 0; i < colNames.length; i++) {
104: listModel.addElement(colNames[i]);
105: }
106: buttonOk = new javax.swing.JButton();
107: buttonCancel = new javax.swing.JButton();
108: jd = new JDialog(_session.getApplication().getMainFrame(),
109: // i18n[mysql.copyTable=Copy Table...]
110: s_stringMgr.getString("mysql.copyTable"));
111: jd.getContentPane().setLayout(null);
112:
113: jd.addWindowListener(new java.awt.event.WindowAdapter() {
114: public void windowClosing(java.awt.event.WindowEvent evt) {
115: closeDialog(evt);
116: }
117: });
118:
119: lbCopyToNewTable.setFont(new java.awt.Font("Dialog", 0, 12));
120: // i18n[mysql.copyToNewTable=Copy to new Table:]
121: lbCopyToNewTable.setText(s_stringMgr
122: .getString("mysql.copyToNewTable"));
123: jd.getContentPane().add(lbCopyToNewTable);
124: lbCopyToNewTable.setBounds(20, 20, 110, 16);
125:
126: jd.getContentPane().add(tfTableName);
127: tfTableName.setBounds(20, 40, 350, 20);
128:
129: rdStructure.setFont(new java.awt.Font("Dialog", 0, 12));
130: // i18n[mysql.structure=Structure]
131: rdStructure.setText(s_stringMgr.getString("mysql.structure"));
132: rdStructure
133: .addActionListener(new java.awt.event.ActionListener() {
134: public void actionPerformed(
135: java.awt.event.ActionEvent evt) {
136: rdStructureActionPerformed(evt);
137: }
138: });
139: jd.getContentPane().add(rdStructure);
140: rdStructure.setBounds(220, 90, 74, 24);
141: rdStructure.setSelected(true);
142:
143: rdStructureData.setFont(new java.awt.Font("Dialog", 0, 12));
144: // i18n[mysql.structureAndData=Structure and Data]
145: rdStructureData.setText(s_stringMgr
146: .getString("mysql.structureAndData"));
147: rdStructureData
148: .addActionListener(new java.awt.event.ActionListener() {
149: public void actionPerformed(
150: java.awt.event.ActionEvent evt) {
151: rdStructureDataActionPerformed(evt);
152: }
153: });
154: jd.getContentPane().add(rdStructureData);
155: rdStructureData.setBounds(220, 130, 130, 24);
156:
157: ButtonGroup group = new ButtonGroup();
158: group.add(rdStructure);
159: group.add(rdStructureData);
160:
161: jd.getContentPane().add(listFields);
162: listFields.setBounds(30, 90, 130, 170);
163: listFields
164: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
165: public void valueChanged(
166: javax.swing.event.ListSelectionEvent evt) {
167: listFieldsValueChanged(evt);
168: }
169: });
170: listFields.setEnabled(false);
171:
172: buttonOk.setFont(new java.awt.Font("Dialog", 0, 12));
173: // i18n[mysql.copyOk=Ok]
174: buttonOk.setText(s_stringMgr.getString("mysql.copyOk"));
175: buttonOk.addActionListener(new java.awt.event.ActionListener() {
176: public void actionPerformed(java.awt.event.ActionEvent evt) {
177: buttonOkActionPerformed(evt);
178: }
179: });
180:
181: jd.getContentPane().add(buttonOk);
182: buttonOk.setBounds(190, 230, 70, 26);
183:
184: buttonCancel.setFont(new java.awt.Font("Dialog", 0, 12));
185: // i18n[mysql.copyCancel=Cancel]
186: buttonCancel.setText(s_stringMgr.getString("mysql.copyCancel"));
187: buttonCancel
188: .addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(
190: java.awt.event.ActionEvent evt) {
191: buttonCancelActionPerformed(evt);
192: }
193: });
194:
195: jd.getContentPane().add(buttonCancel);
196: buttonCancel.setBounds(280, 230, 73, 26);
197: chAllFields.setFont(new java.awt.Font("Dialog", 0, 12));
198: // i18n[mysql.withAllFields=With all Fields]
199: chAllFields.setText(s_stringMgr
200: .getString("mysql.withAllFields"));
201: chAllFields.setSelected(true);
202: chAllFields
203: .addActionListener(new java.awt.event.ActionListener() {
204: public void actionPerformed(
205: java.awt.event.ActionEvent evt) {
206: chAllFieldsActionPerformed(evt);
207: }
208: });
209:
210: jd.getContentPane().add(chAllFields);
211: chAllFields.setBounds(20, 60, 110, 24);
212:
213: jd.pack();
214: jd.setSize(400, 300);
215: jd.setLocation(100, 100);
216: jd.setVisible(true);
217: }
218:
219: private void listFieldsValueChanged(@SuppressWarnings("unused")
220: javax.swing.event.ListSelectionEvent evt) {
221:
222: }
223:
224: //Set the list disabled if allfields of table are to be copied
225: private void chAllFieldsActionPerformed(@SuppressWarnings("unused")
226: java.awt.event.ActionEvent evt) {
227: if (chAllFields.isSelected()) {
228: listFields.setEnabled(false);
229: isAllFields = true;
230: } else {
231: listFields.setEnabled(true);
232: isAllFields = false;
233: }
234: }
235:
236: //Set the boolean value when the user selects b/w structure and data
237: private void rdStructureDataActionPerformed(
238: @SuppressWarnings("unused")
239: java.awt.event.ActionEvent evt) {
240: if (rdStructureData.isSelected())
241: isStructure = false;
242: else
243: isStructure = true;
244: }
245:
246: private void rdStructureActionPerformed(@SuppressWarnings("unused")
247: java.awt.event.ActionEvent evt) {
248: if (rdStructure.isSelected())
249: isStructure = true;
250: else
251: isStructure = false;
252: }
253:
254: private void buttonCancelActionPerformed(
255: @SuppressWarnings("unused")
256: java.awt.event.ActionEvent evt) {
257: jd.setVisible(false);
258: jd.dispose();
259: }
260:
261: private void buttonOkActionPerformed(@SuppressWarnings("unused")
262: java.awt.event.ActionEvent evt) {
263: newTableName = tfTableName.getText();
264: String selectedFields = "";
265: String fields = "";
266: Object[] obj = listFields.getSelectedValues();
267: for (int i = 0; i < obj.length; i++) {
268: selectedFields += obj[i];
269: if (i < obj.length - 1)
270: selectedFields += ", ";
271: }
272: if (isAllFields)
273: fields = "*";
274: else
275: fields = selectedFields;
276:
277: if (isStructure)
278: SQLCommandRoot += "SELECT " + fields + " FROM "
279: + oldTableName + " WHERE 1=0 ;";
280: else
281: SQLCommandRoot += "SELECT " + fields + " FROM "
282: + oldTableName + " ;";
283:
284: SQLQuery = getQuery() + SQLCommandRoot;
285: dbUtils.execute(SQLQuery);
286: _session.getSessionInternalFrame().getObjectTreeAPI()
287: .refreshTree();
288: jd.setVisible(false);
289: jd.dispose();
290: JOptionPane.showMessageDialog(null, "Table " + newTableName
291: + " created");
292:
293: }
294:
295: public String getQuery() {
296: String primaryKeyData = "";
297: //DefaultListModel listModel = (DefaultListModel) listFields.getModel();
298: SQLCommand = "CREATE TABLE " + newTableName + " ( ";
299: primaryKeyData = dbUtils.getPrimaryKeyColumn();
300: if (primaryKeyData.length() > 0)
301: SQLCommand += "PRIMARY KEY ( " + primaryKeyData + " )";
302: SQLCommand += " ) ";
303: return (SQLCommand);
304: }
305:
306: /** Closes the dialog */
307: private void closeDialog(@SuppressWarnings("unused")
308: java.awt.event.WindowEvent evt) {
309: jd.setVisible(false);
310: jd.dispose();
311: }
312:
313: }
|