001: /*
002: * TableSelectorPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.tools;
013:
014: import java.awt.Color;
015: import java.awt.EventQueue;
016: import java.awt.event.ActionListener;
017: import java.awt.event.ItemEvent;
018: import java.awt.event.ItemListener;
019: import java.beans.PropertyChangeEvent;
020: import java.beans.PropertyChangeListener;
021: import java.util.Collections;
022: import java.util.List;
023:
024: import javax.swing.JPanel;
025: import workbench.db.TableIdentifier;
026: import workbench.db.TableNameComparator;
027: import workbench.db.WbConnection;
028: import workbench.gui.WbSwingUtilities;
029: import workbench.gui.components.FlatButton;
030: import workbench.log.LogMgr;
031: import workbench.resource.ResourceMgr;
032: import workbench.util.StringUtil;
033:
034: /**
035: * A Panel to select a table.
036: *
037: * A list of available schemas and tables is displayed using two dropdowns.
038: * If the selected table has changed, a PropertyChangeListener
039: * is notified.
040: *
041: * @author support@sql-workbench.net
042: */
043: public class TableSelectorPanel extends JPanel implements ItemListener,
044: ActionListener {
045: private WbConnection dbConnection;
046: private String currentSchema;
047: protected TableIdentifier currentTable;
048: private PropertyChangeListener client;
049: private String clientPropName;
050: private boolean allowNewTable = false;
051: private TableIdentifier newTableId = new TableIdentifier();
052:
053: public TableSelectorPanel() {
054: initComponents();
055: this .schemaLabel.setText(ResourceMgr.getString("LblSchema"));
056: this .tableLabel.setText(ResourceMgr.getString("LblTable"));
057: this .tableSelector.setMaximumRowCount(15);
058: this .editNewTableNameButton.setVisible(false);
059: }
060:
061: public void reset() {
062: this .schemaSelector.removeItemListener(this );
063: this .tableSelector.removeItemListener(this );
064: this .schemaSelector.removeAllItems();
065: this .tableSelector.removeAllItems();
066: }
067:
068: public void resetNewTableItem() {
069: if (this .newTableId != null) {
070: this .newTableId.setNewTable(true);
071: this .newTableId.setTable(null);
072: this .newTableId.setSchema(null);
073: WbSwingUtilities.repaintNow(this );
074: }
075: }
076:
077: public void allowNewTable(boolean flag) {
078: this .allowNewTable = flag;
079: this .editNewTableNameButton.removeActionListener(this );
080: int count = this .tableSelector.getItemCount();
081: if (count > 0) {
082: this .tableSelector.removeItemListener(this );
083: if (this .allowNewTable) {
084: int newTableIndex = -1;
085:
086: for (int i = 0; i < count; i++) {
087: Object item = this .tableSelector.getItemAt(i);
088: if (item instanceof TableIdentifier) {
089: newTableIndex = i;
090: break;
091: }
092: }
093: if (newTableIndex == -1) {
094: this .tableSelector.addItem(this .newTableId);
095: }
096: } else {
097: this .tableSelector.removeItem(this .newTableId);
098: }
099: this .tableSelector.addItemListener(this );
100: }
101: this .editNewTableNameButton.setVisible(flag);
102: if (flag) {
103: this .editNewTableNameButton.addActionListener(this );
104: }
105: }
106:
107: public void removeChangeListener() {
108: this .client = null;
109: this .clientPropName = null;
110: }
111:
112: public void setChangeListener(PropertyChangeListener l,
113: String propName) {
114: this .client = l;
115: this .clientPropName = propName;
116: }
117:
118: public void setConnection(WbConnection conn) {
119: this .dbConnection = conn;
120: this .refreshButton.setEnabled(this .dbConnection != null);
121: if (this .dbConnection != null) {
122: this .retrieveSchemas();
123: } else {
124: this .reset();
125: }
126: }
127:
128: /**
129: * Sets the name of the JComboBox component
130: * that contains the table list.
131: * Needed for automated GUI testing.
132: * @param name
133: */
134: public void setTableDropDownName(String name) {
135: this .tableSelector.setName(name);
136: }
137:
138: public void retrieveSchemas() {
139: try {
140: WbSwingUtilities.showWaitCursor(this );
141: StringBuilder s = new StringBuilder(this .dbConnection
142: .getMetadata().getSchemaTerm().toLowerCase());
143: s.setCharAt(0, Character.toUpperCase(s.charAt(0)));
144: this .schemaLabel.setText(s.toString());
145:
146: this .schemaSelector.removeItemListener(this );
147: this .tableSelector.removeItemListener(this );
148:
149: this .schemaSelector.removeAllItems();
150: this .tableSelector.removeAllItems();
151:
152: this .schemaSelector.addItem("*");
153: this .currentSchema = null;
154:
155: List schemas = this .dbConnection.getMetadata().getSchemas();
156: String current = this .dbConnection.getMetadata()
157: .getCurrentSchema();
158:
159: for (int i = 0; i < schemas.size(); i++) {
160: String schema = (String) schemas.get(i);
161: if (StringUtil.isEmptyString(schema))
162: continue;
163: this .schemaSelector.addItem(schema);
164: if (current != null && schema.equalsIgnoreCase(current))
165: this .currentSchema = schema;
166: }
167: if (this .currentSchema != null) {
168: schemaSelector.setSelectedItem(this .currentSchema);
169: } else {
170: this .schemaSelector.setSelectedIndex(0);
171: }
172: this .retrieveTables();
173: this .schemaSelector.addItemListener(this );
174: } catch (Exception e) {
175: LogMgr.logError("TableSelectorPanel.retrieveSchemas()",
176: "Could not retrieve schema list", e);
177: } finally {
178: WbSwingUtilities.showDefaultCursor(this );
179: }
180: }
181:
182: public void setEnabled(boolean enable) {
183: super .setEnabled(enable);
184: this .schemaSelector.setEnabled(enable);
185: this .tableSelector.setEnabled(enable);
186:
187: if (enable) {
188: this .tableLabel.setForeground(Color.BLACK);
189: this .schemaLabel.setForeground(Color.BLACK);
190: } else {
191: this .tableLabel.setForeground(Color.DARK_GRAY);
192: this .schemaLabel.setForeground(Color.DARK_GRAY);
193: }
194: if (!enable) {
195: this .tableSelector.setSelectedItem(null);
196: this .schemaSelector.setSelectedItem(null);
197: }
198: this .repaint();
199: }
200:
201: public void retrieveTables() {
202: try {
203: this .tableSelector.removeItemListener(this );
204: List<TableIdentifier> tables = this .dbConnection
205: .getMetadata().getSelectableObjectsList(
206: this .currentSchema);
207: Collections.sort(tables, new TableNameComparator());
208: this .tableSelector.removeAllItems();
209: if (this .allowNewTable) {
210: this .tableSelector.addItem(this .newTableId);
211: }
212: int count = tables.size();
213: for (TableIdentifier table : tables) {
214: table.setShowTablenameOnly(true);
215: this .tableSelector.addItem(table);
216: }
217: this .editNewTableNameButton.setEnabled(false);
218: tableSelector.setSelectedItem(null);
219: TableIdentifier old = this .currentTable;
220: this .currentTable = null;
221: this .firePropertyChange(old, null);
222: } catch (Exception e) {
223: LogMgr.logError("TableSelectorPanel.retrieveTables()",
224: "Could not retrieve table list", e);
225: } finally {
226: this .tableSelector.addItemListener(this );
227: }
228: }
229:
230: public TableIdentifier getSelectedTable() {
231: if (!this .isEnabled())
232: return null;
233: Object selected = this .tableSelector.getSelectedItem();
234: if (selected == null)
235: return null;
236:
237: return (TableIdentifier) selected;
238: }
239:
240: public void findAndSelectTable(String aTable) {
241: if (aTable == null)
242: return;
243:
244: int count = this .tableSelector.getItemCount();
245: for (int i = 0; i < count; i++) {
246: String table = null;
247: Object item = this .tableSelector.getItemAt(i);
248: if (item instanceof TableIdentifier) {
249: table = ((TableIdentifier) item).getTableName();
250: }
251: if (table == null)
252: continue;
253:
254: if (aTable.equalsIgnoreCase(table)) {
255: this .tableSelector.setSelectedIndex(i);
256: break;
257: }
258: }
259: }
260:
261: public void itemStateChanged(ItemEvent e) {
262: if (e.getStateChange() != ItemEvent.SELECTED)
263: return;
264:
265: if (e.getSource() == this .schemaSelector) {
266: this .currentSchema = (String) this .schemaSelector
267: .getSelectedItem();
268: if (this .currentSchema != null) {
269: this .retrieveTables();
270: }
271: this .newTableId.setSchema(this .currentSchema);
272: } else if (e.getSource() == this .tableSelector) {
273: final TableIdentifier old = this .currentTable;
274: this .currentTable = this .getSelectedTable();
275: if (this .currentTable != null) {
276: this .editNewTableNameButton.setEnabled(currentTable
277: .isNewTable());
278: } else {
279: this .editNewTableNameButton.setEnabled(false);
280: }
281: EventQueue.invokeLater(new Runnable() {
282: public void run() {
283: firePropertyChange(old, currentTable);
284: }
285: });
286: }
287: }
288:
289: private void firePropertyChange(TableIdentifier oldTable,
290: TableIdentifier newTable) {
291: if (this .client == null)
292: return;
293: if (oldTable == null && newTable == null)
294: return;
295: if (oldTable != null && newTable != null
296: && oldTable.equals(newTable))
297: return;
298:
299: PropertyChangeEvent evt = new PropertyChangeEvent(this ,
300: this .clientPropName, oldTable, newTable);
301: this .client.propertyChange(evt);
302: }
303:
304: public void actionPerformed(java.awt.event.ActionEvent e) {
305: if (e.getSource() == this .editNewTableNameButton) {
306: Object item = this .tableSelector.getSelectedItem();
307: if (item instanceof TableIdentifier) {
308: TableIdentifier id = (TableIdentifier) item;
309: String name = id.getTableName();
310: name = WbSwingUtilities.getUserInput(this , ResourceMgr
311: .getString("TxtEnterNewTableName"), name);
312: if (name != null) {
313: id.setTable(name);
314: this .tableSelector.repaint();
315: }
316: }
317: }
318: }
319:
320: /** This method is called from within the constructor to
321: * initialize the form.
322: * WARNING: Do NOT modify this code. The content of this method is
323: * always regenerated by the Form Editor.
324: */
325: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
326: private void initComponents() {
327: java.awt.GridBagConstraints gridBagConstraints;
328:
329: schemaSelector = new javax.swing.JComboBox();
330: tableSelector = new javax.swing.JComboBox();
331: schemaLabel = new javax.swing.JLabel();
332: tableLabel = new javax.swing.JLabel();
333: editNewTableNameButton = new FlatButton();
334: refreshButton = new FlatButton();
335:
336: setLayout(new java.awt.GridBagLayout());
337:
338: setMinimumSize(new java.awt.Dimension(68, 65));
339: setPreferredSize(new java.awt.Dimension(68, 65));
340: schemaSelector.setMaximumRowCount(0);
341: schemaSelector.setAlignmentX(0.0F);
342: schemaSelector.setAlignmentY(0.0F);
343: schemaSelector.setMaximumSize(new java.awt.Dimension(200, 25));
344: gridBagConstraints = new java.awt.GridBagConstraints();
345: gridBagConstraints.gridx = 0;
346: gridBagConstraints.gridy = 1;
347: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
348: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
349: gridBagConstraints.weightx = 0.3;
350: add(schemaSelector, gridBagConstraints);
351:
352: tableSelector.setMaximumRowCount(0);
353: tableSelector.setAlignmentX(0.0F);
354: tableSelector.setAlignmentY(0.0F);
355: tableSelector.setMaximumSize(new java.awt.Dimension(80, 25));
356: gridBagConstraints = new java.awt.GridBagConstraints();
357: gridBagConstraints.gridx = 1;
358: gridBagConstraints.gridy = 1;
359: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
360: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
361: gridBagConstraints.weightx = 0.7;
362: add(tableSelector, gridBagConstraints);
363:
364: schemaLabel.setText("jLabel1");
365: schemaLabel
366: .setVerticalAlignment(javax.swing.SwingConstants.TOP);
367: schemaLabel.setMaximumSize(new java.awt.Dimension(32768, 21));
368: schemaLabel.setMinimumSize(new java.awt.Dimension(34, 21));
369: schemaLabel.setPreferredSize(new java.awt.Dimension(34, 21));
370: gridBagConstraints = new java.awt.GridBagConstraints();
371: gridBagConstraints.gridx = 0;
372: gridBagConstraints.gridy = 0;
373: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
374: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
375: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
376: add(schemaLabel, gridBagConstraints);
377:
378: tableLabel.setText("jLabel1");
379: tableLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
380: tableLabel.setMaximumSize(new java.awt.Dimension(32768, 21));
381: tableLabel.setMinimumSize(new java.awt.Dimension(34, 21));
382: tableLabel.setPreferredSize(new java.awt.Dimension(34, 21));
383: gridBagConstraints = new java.awt.GridBagConstraints();
384: gridBagConstraints.gridx = 1;
385: gridBagConstraints.gridy = 0;
386: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
387: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
388: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
389: add(tableLabel, gridBagConstraints);
390:
391: editNewTableNameButton.setIcon(ResourceMgr.getImage("Rename"));
392: editNewTableNameButton.setToolTipText(ResourceMgr
393: .getString("LblEditNewTableName"));
394: editNewTableNameButton.setEnabled(false);
395: editNewTableNameButton.setMaximumSize(new java.awt.Dimension(
396: 22, 22));
397: editNewTableNameButton.setMinimumSize(new java.awt.Dimension(
398: 22, 22));
399: editNewTableNameButton.setPreferredSize(new java.awt.Dimension(
400: 22, 22));
401: gridBagConstraints = new java.awt.GridBagConstraints();
402: gridBagConstraints.gridx = 3;
403: gridBagConstraints.gridy = 1;
404: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
405: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
406: add(editNewTableNameButton, gridBagConstraints);
407:
408: refreshButton.setIcon(ResourceMgr.getImage("Refresh"));
409: refreshButton.setToolTipText("");
410: refreshButton.setEnabled(false);
411: refreshButton.setMaximumSize(new java.awt.Dimension(22, 22));
412: refreshButton.setMinimumSize(new java.awt.Dimension(22, 22));
413: refreshButton.setPreferredSize(new java.awt.Dimension(22, 22));
414: refreshButton
415: .addActionListener(new java.awt.event.ActionListener() {
416: public void actionPerformed(
417: java.awt.event.ActionEvent evt) {
418: refreshButtonActionPerformed(evt);
419: }
420: });
421:
422: gridBagConstraints = new java.awt.GridBagConstraints();
423: gridBagConstraints.gridx = 2;
424: gridBagConstraints.gridy = 1;
425: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
426: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
427: add(refreshButton, gridBagConstraints);
428:
429: }// </editor-fold>//GEN-END:initComponents
430:
431: private void refreshButtonActionPerformed(
432: java.awt.event.ActionEvent evt)//GEN-FIRST:event_refreshButtonActionPerformed
433: {//GEN-HEADEREND:event_refreshButtonActionPerformed
434: retrieveSchemas();
435: }//GEN-LAST:event_refreshButtonActionPerformed
436:
437: // Variables declaration - do not modify//GEN-BEGIN:variables
438: private javax.swing.JButton editNewTableNameButton;
439: private javax.swing.JButton refreshButton;
440: private javax.swing.JLabel schemaLabel;
441: private javax.swing.JComboBox schemaSelector;
442: private javax.swing.JLabel tableLabel;
443: private javax.swing.JComboBox tableSelector;
444: // End of variables declaration//GEN-END:variables
445:
446: }
|