001: /* ====================================================================
002: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: */
033: package org.glasser.qform;
034:
035: import javax.swing.*;
036: import java.util.*;
037: import javax.swing.border.*;
038: import java.awt.*;
039: import javax.swing.event.*;
040: import java.awt.event.*;
041:
042: import org.glasser.swing.*;
043: import java.sql.*;
044: import javax.sql.*;
045: import org.glasser.sql.*;
046:
047: public class TableSelector extends JDialog implements ActionListener {
048:
049: private static boolean debug = false;
050:
051: private class DataSourceListItem implements Comparable {
052:
053: private String displayName = null;
054:
055: private Integer sourceId = null;
056:
057: public DataSourceListItem(Integer sourceId, String displayName) {
058: this .sourceId = sourceId;
059: this .displayName = "(" + sourceId + ") " + displayName;
060: }
061:
062: public String toString() {
063: return displayName;
064: }
065:
066: public String getDisplayName() {
067: return displayName;
068: }
069:
070: public Integer getSourceId() {
071: return sourceId;
072: }
073:
074: public boolean equals(Object other) {
075: try {
076: return ((DataSourceListItem) other).getSourceId()
077: .equals(sourceId);
078: } catch (Exception ex) {
079: return this == other;
080: }
081: }
082:
083: public int hashCode() {
084: return sourceId.intValue();
085: }
086:
087: public int compareTo(Object other) {
088: try {
089: return sourceId.compareTo(((DataSourceListItem) other)
090: .getSourceId());
091: } catch (Exception ex) {
092: return 0;
093: }
094: }
095: }
096:
097: protected JComboBox sourceList = new JComboBox();
098:
099: protected Vector sourceVector = new Vector();
100:
101: protected DefaultComboBoxModel sourceModel = new DefaultComboBoxModel(
102: sourceVector);
103:
104: JComboBox schemaList = new JComboBox();
105:
106: private ComboBoxModel emptySchemaListModel = schemaList.getModel();
107:
108: private Vector emptyVector = new Vector();
109:
110: JList tableList = new KeySelectableJList();
111:
112: JButton btnOK = new JButton("OK");
113:
114: JButton btnCancel = new JButton("Cancel");
115:
116: // HashMap tableMap = new HashMap();
117: //
118: // HashMap schemaListMap = new HashMap();
119:
120: HashMap schemaModelMap = new HashMap();
121:
122: HashMap tableListMap = new HashMap();
123:
124: private Object[] selections = null;
125:
126: private TableInfo selectedTableInfo = null;
127:
128: private Integer selectedSourceId = null;
129:
130: public void addDataSource(Integer sourceId, String sourceName,
131: HashMap tables) {
132:
133: DataSourceListItem item = new DataSourceListItem(sourceId,
134: sourceName);
135: sourceVector.add(item);
136: Collections.sort(sourceVector);
137: sourceList.setModel(sourceModel);
138:
139: Vector schemas = new Vector();
140: for (Iterator i = tables.keySet().iterator(); i.hasNext();) {
141: schemas.add(i.next());
142: }
143:
144: Collections.sort(schemas);
145:
146: schemaModelMap.put(sourceId, new DefaultComboBoxModel(schemas));
147:
148: tableListMap.put(sourceId, tables);
149: sourceList.setSelectedItem(item);
150:
151: }
152:
153: public void removeDataSource(Integer sourceId) {
154:
155: for (int j = sourceVector.size() - 1; j >= 0; j--) {
156: DataSourceListItem li = (DataSourceListItem) sourceVector
157: .get(j);
158: if (sourceId.equals(li.getSourceId())) {
159: if (debug)
160: System.out.println("GOT IT.");
161: sourceVector.remove(j);
162: }
163: }
164:
165: if (debug)
166: System.out.println("new sourceModel size is "
167: + sourceModel.getSize());
168:
169: Object o = schemaModelMap.remove(sourceId);
170: if (debug)
171: System.out.println("schemaModel removed: " + o);
172: o = tableListMap.remove(sourceId);
173: if (debug)
174: System.out.println("tableList removed : " + (o != null));
175:
176: sourceList.setModel(sourceModel);
177: try {
178: sourceList.setSelectedIndex(-1);
179: } catch (Exception ex) {
180: ex.printStackTrace();
181: }
182: schemaList.setModel(this .emptySchemaListModel);
183: tableList.setListData(emptyVector);
184: if (sourceList.getSelectedIndex() < 0
185: && sourceModel.getSize() > 0) {
186: sourceList.setSelectedIndex(0);
187: }
188: }
189:
190: public TableSelector() {
191: this (null);
192: }
193:
194: public TableSelector(Frame parent) {
195:
196: super (parent);
197:
198: setTitle("Select a table");
199:
200: sourceList.setModel(sourceModel);
201:
202: JPanel p1 = new JPanel();
203: p1.setLayout(new BorderLayout());
204: p1.add(new JLabel("Data Source"), BorderLayout.NORTH);
205: p1.add(sourceList, BorderLayout.CENTER);
206:
207: JPanel panel = new JPanel();
208: JPanel topPanel = new JPanel();
209: JPanel centerPanel = new JPanel();
210: topPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
211: centerPanel.setBorder(new EmptyBorder(0, 10, 10, 10));
212: panel.setLayout(new BorderLayout());
213: topPanel.setLayout(new BorderLayout());
214: centerPanel.setLayout(new BorderLayout());
215:
216: JPanel p2 = new JPanel();
217: p2.setLayout(new BorderLayout());
218: p2.add(new JLabel("Table Owner"), BorderLayout.NORTH);
219: p2.add(schemaList, BorderLayout.CENTER);
220: p2.setBorder(new EmptyBorder(10, 0, 0, 0));
221:
222: topPanel.add(p1, BorderLayout.NORTH);
223: topPanel.add(p2, BorderLayout.CENTER);
224:
225: panel.add(topPanel, BorderLayout.NORTH);
226: centerPanel
227: .add(new JScrollPane(tableList), BorderLayout.CENTER);
228: centerPanel.add(new JLabel("Tables/Views (Views in ITALICS)"),
229: BorderLayout.NORTH);
230: panel.add(centerPanel, BorderLayout.CENTER);
231:
232: JPanel buttonPanel = new JPanel();
233:
234: GUIHelper.enterPressesWhenFocused(btnOK);
235: GUIHelper.enterPressesWhenFocused(btnCancel);
236:
237: buttonPanel.add(btnOK);
238: buttonPanel.add(btnCancel);
239: btnOK.setPreferredSize(btnCancel.getPreferredSize());
240: buttonPanel.setBorder(new EmptyBorder(0, 10, 10, 10));
241: panel.add(buttonPanel, BorderLayout.SOUTH);
242:
243: btnOK.addActionListener(this );
244: btnCancel.addActionListener(this );
245: this
246: .addKeyListener(new EnterEscapeKeyHandler(btnOK,
247: btnCancel));
248:
249: sourceList.addActionListener(this );
250: schemaList.addActionListener(this );
251: tableList.addMouseListener(new MouseAdapter() {
252: public void mouseClicked(MouseEvent e) {
253: if (e.getClickCount() == 2) {
254: setSelections();
255: if (selections != null) {
256: setVisible(false);
257: }
258: }
259: }
260: });
261: tableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
262:
263: tableList.setCellRenderer(new TableInfoListCellRenderer());
264:
265: setModal(true);
266: this .addComponentListener(new ComponentAdapter() {
267: public void componentShown(ComponentEvent e) {
268: tableList.requestFocus();
269: if (tableList.getSelectedIndex() < 0
270: && tableList.getModel().getSize() > 0) {
271: try {
272: tableList.setSelectedIndex(0);
273: } catch (Exception ex) {
274: }
275: }
276: }
277:
278: public void componentResized(ComponentEvent e) {
279: int index = tableList.getSelectedIndex();
280: if (index > -1) {
281: tableList.ensureIndexIsVisible(index);
282: }
283: }
284: });
285:
286: // make the OK button close the default so ENTER invokes it
287: this .getRootPane().setDefaultButton(this .btnOK);
288:
289: // make the Escape key click the cancel button.
290: KeyStroke esc = KeyStroke.getKeyStroke("ESCAPE");
291: Action closer = new ButtonClicker(btnCancel);
292: panel.getInputMap(panel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
293: .put(esc, "_ESCAPE_");
294: panel.getInputMap(panel.WHEN_IN_FOCUSED_WINDOW).put(esc,
295: "_ESCAPE_");
296: panel.getActionMap().put("_ESCAPE_", closer);
297:
298: setContentPane(panel);
299: pack();
300:
301: }
302:
303: // protected void handleSchemaChange
304:
305: public void actionPerformed(ActionEvent event) {
306: Object source = event.getSource();
307:
308: if (source == sourceList) {
309:
310: DataSourceListItem sourceItem = (DataSourceListItem) sourceList
311: .getSelectedItem();
312: if (sourceItem == null)
313: return;
314: Integer sourceId = sourceItem.getSourceId();
315: DefaultComboBoxModel schemaModel = (DefaultComboBoxModel) schemaModelMap
316: .get(sourceId);
317: schemaList.setModel(schemaModel);
318: try {
319: schemaList.setSelectedIndex(0);
320: } catch (Exception ex) {
321: ex.printStackTrace();
322: }
323: return;
324: }
325:
326: if (source == schemaList) {
327: DataSourceListItem sourceItem = (DataSourceListItem) sourceList
328: .getSelectedItem();
329: if (sourceItem == null)
330: return; // all lists are empty.
331: Integer sourceId = sourceItem.getSourceId();
332: String owner = (String) schemaList.getSelectedItem();
333: HashMap schemaTables = (HashMap) tableListMap.get(sourceId);
334:
335: if (owner != null) {
336: Vector v = (Vector) schemaTables.get(owner);
337: tableList.setListData(v);
338: tableList.requestFocus();
339: try {
340: if (v.size() > 0) {
341: tableList.setSelectedIndex(0);
342: }
343: } catch (Exception ex) {
344: ex.printStackTrace();
345: }
346: }
347: } else if (source == btnCancel) {
348: selections = null;
349: setVisible(false);
350: } else if (source == btnOK) {
351: setSelections();
352: setVisible(false);
353: }
354:
355: }
356:
357: /**
358: * If a selection was made, returns an Object[3] array. The first element
359: * is an Integer representing the DataSource id, the second element is
360: * a String representing the schema, or table owner, and the third
361: * element is the name of the table.
362: */
363: protected void setSelections() {
364:
365: selections = null;
366: DataSourceListItem source = (DataSourceListItem) sourceList
367: .getSelectedItem();
368: if (source == null)
369: return;
370:
371: selectedTableInfo = (TableInfo) tableList.getSelectedValue();
372: selectedSourceId = (Integer) source.getSourceId();
373: if (selectedTableInfo == null) {
374: return;
375: }
376:
377: selections = new Object[3];
378: selections[0] = source.getSourceId();
379: selections[1] = schemaList.getSelectedItem();
380: selections[2] = selectedTableInfo;
381:
382: }
383:
384: public void setVisible(boolean b) {
385: if (b) {
386: selections = null;
387: selectedTableInfo = null;
388: selectedSourceId = null;
389: }
390: super .setVisible(b);
391: }
392:
393: public Object[] getSelection() {
394: return selections;
395: }
396:
397: public TableInfo getSelectedTableInfo() {
398: return selectedTableInfo;
399: }
400:
401: public Integer getSelectedSourceId() {
402: return selectedSourceId;
403: }
404:
405: public static void main(String[] args) throws Exception {
406: TableSelector ts = new TableSelector();
407: // ts.setDefaultCloseOperation(ts.EXIT_ON_CLOSE);
408: Config config = new Config(new java.io.File("C:/0/qform.xml"));
409: LocalDataSourceConfig[] lds = config
410: .getLocalDataSourceConfigs();
411: for (int j = 0; j < lds.length; j++) {
412: try {
413: DataSource ds = DataSourceManager.getLocalDataSource(
414: lds[j], null, null);
415: Connection c = ds.getConnection();
416: DatabaseMetaData dbmd = c.getMetaData();
417: ResultSet rs = dbmd.getTables(null, null, "%",
418: new String[] { "VIEW", "TABLE" });
419: TableInfo[] tis = DBUtil.getTableInfos(rs);
420: rs.close();
421: c.close();
422: HashMap map = DBUtil.getTableInfoLists(tis,
423: "<DEFAULT SCHEMA>");
424: ts.addDataSource(new Integer(j + 1), lds[j]
425: .getDisplayName(), map);
426: } catch (Exception ex) {
427: ex.printStackTrace();
428: }
429: }
430:
431: ts.setModal(true);
432: ts.setVisible(true);
433:
434: Object[] sels = ts.getSelection();
435: if (debug) {
436: for (int j = 0; sels != null && j < sels.length; j++) {
437: System.out.println("---" + sels[j]);
438: }
439: }
440:
441: ts.removeDataSource(new Integer(1));
442: ts.setVisible(true);
443:
444: sels = ts.getSelection();
445: if (debug) {
446: for (int j = 0; sels != null && j < sels.length; j++) {
447: System.out.println("---" + sels[j]);
448: }
449: }
450:
451: ts.removeDataSource(new Integer(2));
452: ts.setVisible(true);
453:
454: sels = ts.getSelection();
455: if (debug) {
456: for (int j = 0; sels != null && j < sels.length; j++) {
457: System.out.println("---" + sels[j]);
458: }
459: }
460:
461: }
462:
463: }
|