001: /*
002: * Copyright (C) 2006 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package net.sourceforge.squirrel_sql.client.gui.db;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.ComponentEvent;
031: import java.awt.event.ComponentListener;
032: import java.util.ArrayList;
033:
034: import javax.swing.JButton;
035: import javax.swing.JDialog;
036: import javax.swing.JFrame;
037: import javax.swing.JLabel;
038: import javax.swing.JList;
039: import javax.swing.JOptionPane;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.JTextField;
043: import javax.swing.ListSelectionModel;
044: import javax.swing.SwingConstants;
045: import javax.swing.border.Border;
046: import javax.swing.border.EmptyBorder;
047: import javax.swing.event.ListSelectionEvent;
048: import javax.swing.event.ListSelectionListener;
049:
050: import net.sourceforge.squirrel_sql.client.ApplicationArguments;
051: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
052: import net.sourceforge.squirrel_sql.fw.util.StringManager;
053: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
054:
055: /**
056: * A dialog that can be used to get column(s) selected by the user
057: */
058: public class ColumnListDialog extends JDialog {
059:
060: private static final long serialVersionUID = 1L;
061:
062: private JLabel tableNameLabel = null;
063: private JLabel primaryKeyNameLabel = null;
064: private JTextField tableNameTextField = null;
065: private JLabel columnListLabel = null;
066: private JList columnList = null;
067:
068: private JButton executeButton = null;
069: private JButton editSQLButton = null;
070: private JButton showSQLButton = null;
071: private JButton cancelButton = null;
072: private JTextField primaryKeyNameTF = null;
073:
074: /** Internationalized strings for this class. */
075: private static final StringManager s_stringMgr = StringManagerFactory
076: .getStringManager(ColumnListDialog.class);
077:
078: private interface i18n {
079: //i18n[ColumnListDialog.executeButtonLabel=Execute]
080: String EXECUTE_BUTTON_LABEL = s_stringMgr
081: .getString("ColumnListDialog.executeButtonLabel");
082: //i18n[ColumnListDialog.cancelButtonLabel=Cancel]
083: String CANCEL_BUTTON_LABEL = s_stringMgr
084: .getString("ColumnListDialog.cancelButtonLabel");
085: //i18n[ColumnListDialog.columnNameLabel=Column: ]
086: String COLUMN_NAME_LABEL = s_stringMgr
087: .getString("ColumnListDialog.columnNameLabel");
088: //i18n[ColumnListDialog.dropErrorMessage=Can't drop all columns - a
089: //table must have at least one column
090: String DROP_ERROR_MESSAGE = s_stringMgr
091: .getString("ColumnListDialog.dropErrorMessage");
092: //i18n[ColumnListDialog.dropErrorTitle=Too Many Columns Selected]
093: String DROP_ERROR_TITLE = s_stringMgr
094: .getString("ColumnListDialog.dropErrorTitle");
095: //i18n[ColumnListDialog.dropPrimaryKeyTitle=Drop Primary Key]
096: String DROP_PRIMARY_KEY_TITLE = s_stringMgr
097: .getString("ColumnListDialog.dropPrimaryKeyTitle");
098: //i18n[ColumnListDialog.dropTitle=Select Column(s) To Drop]
099: String DROP_TITLE = s_stringMgr
100: .getString("ColumnListDialog.dropTitle");
101: //i18n[ColumnDetailsDialog.modifyButtonLabel=Modify Column]
102: String MODIFY_BUTTON_LABEL = s_stringMgr
103: .getString("ColumnDetailsDialog.modifyButtonLabel");
104: //i18n[ColumnListDialog.modifyTitle=Select Column To Modify]
105: String MODIFY_TITLE = s_stringMgr
106: .getString("ColumnListDialog.modifyTitle");
107: //i18n[ColumnListDialog.primaryKeyTitle=Choose column(s) for primary key]
108: String PRIMARY_KEY_TITLE = s_stringMgr
109: .getString("ColumnListDialog.primaryKeyTitle");
110:
111: //i18n[ColumnListDialog.primaryKeyNameLabel=Primary Key Name: ]
112: String PRIMARY_KEY_NAME_LABEL = s_stringMgr
113: .getString("ColumnListDialog.primaryKeyNameLabel");
114:
115: //i18n[ColumnListDialog.showSQLButtonLabel=Show SQL]
116: String SHOWSQL_BUTTON_LABEL = s_stringMgr
117: .getString("ColumnListDialog.showSQLButtonLabel");
118: //i18n[ColumnListDialog.tableNameLabel=Table Name: ]
119: String TABLE_NAME_LABEL = s_stringMgr
120: .getString("ColumnListDialog.tableNameLabel");
121: //i18n[ColumnListDialog.editSQLButtonLabel=Edit SQL]
122: String EDIT_BUTTON_LABEL = s_stringMgr
123: .getString("ColumnListDialog.editSQLButtonLabel");
124: }
125:
126: public static final int DROP_COLUMN_MODE = 0;
127: public static final int MODIFY_COLUMN_MODE = 1;
128: public static final int ADD_PRIMARY_KEY_MODE = 2;
129: public static final int DROP_PRIMARY_KEY_MODE = 3;
130:
131: private int _mode = DROP_COLUMN_MODE;
132:
133: private TableColumnInfo[] colInfos = null;
134:
135: /**
136: * @param columnInfos the list of columns to display
137: * @param mode the mode in which the dialog is acting.
138: */
139: public ColumnListDialog(TableColumnInfo[] columnInfos, int mode) {
140: _mode = mode;
141: setColumnList(columnInfos);
142: }
143:
144: public void setColumnList(TableColumnInfo[] columnInfos) {
145: colInfos = columnInfos;
146: ArrayList<String> tmp = new ArrayList<String>();
147: for (int i = 0; i < colInfos.length; i++) {
148: TableColumnInfo info = colInfos[i];
149: tmp.add(info.getColumnName());
150: }
151: String[] cols = tmp.toArray(new String[tmp.size()]);
152: if (columnList != null) {
153: columnList.setListData(cols);
154: } else {
155: init(cols);
156: }
157: if (_mode == DROP_PRIMARY_KEY_MODE) {
158: columnList.setEnabled(false);
159: }
160: }
161:
162: public void setTableName(String tableName) {
163: tableNameTextField.setText(tableName);
164: }
165:
166: public String getTableName() {
167: return tableNameTextField.getText();
168: }
169:
170: public void setPrimaryKeyName(String primaryKeyName) {
171: primaryKeyNameTF.setText(primaryKeyName);
172: }
173:
174: public String getPrimaryKeyName() {
175: return primaryKeyNameTF.getText();
176: }
177:
178: public TableColumnInfo[] getSelectedColumnList() {
179: ArrayList<TableColumnInfo> result = new ArrayList<TableColumnInfo>();
180: Object[] selectedColNames = columnList.getSelectedValues();
181: for (int i = 0; i < selectedColNames.length; i++) {
182: String columnName = (String) selectedColNames[i];
183: result.add(getColInfoByName(columnName));
184: }
185: return result.toArray(new TableColumnInfo[result.size()]);
186: }
187:
188: private TableColumnInfo getColInfoByName(String columnName) {
189: for (int i = 0; i < colInfos.length; i++) {
190: TableColumnInfo colInfo = colInfos[i];
191: if (colInfo.getColumnName().equals(columnName)) {
192: return colInfo;
193: }
194: }
195: return null;
196: }
197:
198: public void addColumnSelectionListener(ActionListener listener) {
199: if (listener == null) {
200: throw new IllegalArgumentException(
201: "listener cannot be null");
202: }
203: executeButton.addActionListener(listener);
204: }
205:
206: public void addShowSQLListener(ActionListener listener) {
207: if (listener == null) {
208: throw new IllegalArgumentException(
209: "listener cannot be null");
210: }
211: showSQLButton.addActionListener(listener);
212: }
213:
214: public void addEditSQLListener(ActionListener listener) {
215: if (listener == null) {
216: throw new IllegalArgumentException(
217: "listener cannot be null");
218: }
219: editSQLButton.addActionListener(listener);
220: }
221:
222: public void setMultiSelection() {
223: columnList
224: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
225: }
226:
227: public void setSingleSelection() {
228: columnList
229: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
230: }
231:
232: private GridBagConstraints getLabelConstraints(GridBagConstraints c) {
233: c.gridx = 0;
234: c.gridy++;
235: c.anchor = GridBagConstraints.NORTHEAST;
236: c.fill = GridBagConstraints.NONE;
237: c.weightx = 0;
238: c.weighty = 0;
239: return c;
240: }
241:
242: private GridBagConstraints getFieldConstraints(GridBagConstraints c) {
243: c.gridx++;
244: c.anchor = GridBagConstraints.NORTHWEST;
245: c.weightx = 0;
246: c.weighty = 0;
247: c.fill = GridBagConstraints.HORIZONTAL;
248: return c;
249: }
250:
251: private JLabel getBorderedLabel(String text, Border border) {
252: JLabel result = new JLabel(text);
253: result.setBorder(border);
254: result.setPreferredSize(new Dimension(115, 20));
255: result.setHorizontalAlignment(SwingConstants.RIGHT);
256: return result;
257: }
258:
259: /**
260: * Creates the UI for this dialog.
261: */
262: private void init(String[] columnNames) {
263: super .setModal(true);
264: if (_mode == DROP_COLUMN_MODE) {
265: setTitle(i18n.DROP_TITLE);
266: }
267: if (_mode == MODIFY_COLUMN_MODE) {
268: setTitle(i18n.MODIFY_TITLE);
269: }
270: if (_mode == ADD_PRIMARY_KEY_MODE) {
271: setTitle(i18n.PRIMARY_KEY_TITLE);
272: }
273: if (_mode == DROP_PRIMARY_KEY_MODE) {
274: setTitle(i18n.DROP_PRIMARY_KEY_TITLE);
275: }
276: setSize(425, 250);
277: EmptyBorder border = new EmptyBorder(new Insets(5, 5, 5, 5));
278: Dimension mediumField = new Dimension(126, 20);
279:
280: JPanel pane = new JPanel();
281: pane.setLayout(new GridBagLayout());
282: pane.setBorder(new EmptyBorder(10, 0, 0, 30));
283:
284: GridBagConstraints c = new GridBagConstraints();
285: c.gridx = 0;
286: c.gridy = -1;
287:
288: // Table name
289: tableNameLabel = getBorderedLabel(i18n.TABLE_NAME_LABEL, border);
290: pane.add(tableNameLabel, getLabelConstraints(c));
291:
292: tableNameTextField = new JTextField();
293: tableNameTextField.setPreferredSize(mediumField);
294: tableNameTextField.setEditable(false);
295: pane.add(tableNameTextField, getFieldConstraints(c));
296:
297: // Primary Key name
298: if (_mode == ADD_PRIMARY_KEY_MODE
299: || _mode == DROP_PRIMARY_KEY_MODE) {
300: primaryKeyNameLabel = new JLabel(
301: i18n.PRIMARY_KEY_NAME_LABEL);
302: pane.add(primaryKeyNameLabel, getLabelConstraints(c));
303:
304: primaryKeyNameTF = new JTextField();
305: primaryKeyNameTF.setPreferredSize(mediumField);
306: if (_mode == ADD_PRIMARY_KEY_MODE) {
307: primaryKeyNameTF.setEditable(true);
308: } else {
309: primaryKeyNameTF.setEditable(false);
310: }
311: pane.add(primaryKeyNameTF, getFieldConstraints(c));
312: }
313:
314: // Column list
315: columnListLabel = getBorderedLabel(i18n.COLUMN_NAME_LABEL,
316: border);
317: columnListLabel.setVerticalAlignment(JLabel.NORTH);
318: pane.add(columnListLabel, getLabelConstraints(c));
319:
320: columnList = new JList(columnNames);
321: columnList
322: .addListSelectionListener(new ColumnListSelectionListener());
323:
324: JScrollPane sp = new JScrollPane(columnList);
325: c = getFieldConstraints(c);
326: c.weightx = 1;
327: c.weighty = 1;
328: c.fill = GridBagConstraints.BOTH;
329: pane.add(sp, c);
330:
331: Container contentPane = super .getContentPane();
332: contentPane.setLayout(new BorderLayout());
333: contentPane.add(pane, BorderLayout.CENTER);
334:
335: contentPane.add(getButtonPanel(), BorderLayout.SOUTH);
336: }
337:
338: private JPanel getButtonPanel() {
339: JPanel result = new JPanel();
340: if (_mode == MODIFY_COLUMN_MODE) {
341: executeButton = new JButton(i18n.MODIFY_BUTTON_LABEL);
342: } else {
343: executeButton = new JButton(i18n.EXECUTE_BUTTON_LABEL);
344: }
345: result.add(executeButton);
346:
347: if (_mode != MODIFY_COLUMN_MODE) {
348: editSQLButton = new JButton(i18n.EDIT_BUTTON_LABEL);
349: result.add(editSQLButton);
350: showSQLButton = new JButton(i18n.SHOWSQL_BUTTON_LABEL);
351: result.add(showSQLButton);
352: }
353: cancelButton = new JButton(i18n.CANCEL_BUTTON_LABEL);
354: cancelButton.addActionListener(new ActionListener() {
355: public void actionPerformed(ActionEvent e) {
356: setVisible(false);
357: }
358: });
359: result.add(cancelButton);
360: if (_mode != DROP_PRIMARY_KEY_MODE) {
361: executeButton.setEnabled(false);
362: if (_mode != MODIFY_COLUMN_MODE) {
363: editSQLButton.setEnabled(false);
364: showSQLButton.setEnabled(false);
365: }
366: }
367: return result;
368: }
369:
370: /**
371: * @param args
372: */
373: public static void main(String[] args) {
374: ApplicationArguments.initialize(new String[] {});
375: String[] data = new String[] {
376: "A_Really_Long_Nasty_Column_Called_ColumnA", "ColumnB",
377: "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG",
378: "ColumnH", "ColumnI", "ColumnJ", "ColumnK", "ColumnL",
379: "ColumnM", "ColumnN", "ColumnO", "ColumnP", "ColumnP",
380: "ColumnQ", "ColumnR", "ColumnS", "ColumnT" };
381:
382: TableColumnInfo[] infos = new TableColumnInfo[data.length];
383:
384: for (int i = 0; i < infos.length; i++) {
385: infos[i] = new TableColumnInfo("aCat", "aSchem", "aTab",
386: data[i], java.sql.Types.CHAR, "character", 10, 0,
387: 0, 0, "a comment", "defVal", 0, 0, "YES");
388: }
389:
390: final ColumnListDialog c = new ColumnListDialog(infos, 0);
391: c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
392: c.setTableName("FooTable");
393: c.addComponentListener(new ComponentListener() {
394: public void componentHidden(ComponentEvent e) {
395: }
396:
397: public void componentMoved(ComponentEvent e) {
398: }
399:
400: public void componentResized(ComponentEvent e) {
401: System.out.println("Current size = " + c.getSize());
402: }
403:
404: public void componentShown(ComponentEvent e) {
405: }
406: });
407: c.cancelButton.addActionListener(new ActionListener() {
408: public void actionPerformed(ActionEvent e) {
409: System.exit(1);
410: }
411: });
412: c.setVisible(true);
413:
414: }
415:
416: private void enable(JButton button) {
417: if (button != null) {
418: button.setEnabled(true);
419: }
420: }
421:
422: private void disable(JButton button) {
423: if (button != null) {
424: button.setEnabled(false);
425: }
426: }
427:
428: private class ColumnListSelectionListener implements
429: ListSelectionListener {
430:
431: /**
432: * Rules to handle enabling/disabling the buttons in this dialog. Handle
433: * all cases where buttons should be disable first; if every rule passes
434: * then enable.
435: */
436: public void valueChanged(ListSelectionEvent e) {
437: int[] selected = columnList.getSelectedIndices();
438:
439: // If no columns are selected, and we're not dropping the PK, then
440: // there is nothing to do.
441: if (_mode != DROP_PRIMARY_KEY_MODE) {
442: if (selected == null || selected.length == 0) {
443: disable(executeButton);
444: disable(editSQLButton);
445: disable(showSQLButton);
446: return;
447: }
448: }
449:
450: // User cannot be allowed to drop all columns from a table
451: if (_mode == DROP_COLUMN_MODE
452: && selected.length == columnList.getModel()
453: .getSize()) {
454: JOptionPane.showMessageDialog(ColumnListDialog.this ,
455: i18n.DROP_ERROR_MESSAGE, i18n.DROP_ERROR_TITLE,
456: JOptionPane.ERROR_MESSAGE);
457: columnList.clearSelection();
458: disable(executeButton);
459: disable(editSQLButton);
460: disable(showSQLButton);
461: return;
462: }
463:
464: // All rules passed, so enable the buttons.
465: enable(executeButton);
466: enable(editSQLButton);
467: enable(showSQLButton);
468: }
469:
470: }
471: }
|