001: package org.jsqltool.gui;
002:
003: import javax.swing.*;
004: import javax.swing.table.*;
005: import javax.swing.event.*;
006: import java.awt.*;
007: import org.jsqltool.conn.DbConnectionUtil;
008: import java.awt.event.*;
009: import org.jsqltool.gui.panel.*;
010: import org.jsqltool.gui.tablepanel.*;
011: import org.jsqltool.utils.Options;
012: import org.jsqltool.utils.ImageLoader;
013:
014: /**
015: * <p>Title: JSqlTool Project</p>
016: * <p>Description: Window used to view the list of tables, views and synonyms.
017: * </p>
018: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
019: *
020: * <p> This file is part of JSqlTool project.
021: * This library is free software; you can redistribute it and/or
022: * modify it under the terms of the (LGPL) Lesser General Public
023: * License as published by the Free Software Foundation;
024: *
025: * GNU LESSER GENERAL PUBLIC LICENSE
026: * Version 2.1, February 1999
027: *
028: * This library is distributed in the hope that it will be useful,
029: * but WITHOUT ANY WARRANTY; without even the implied warranty of
030: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031: * Library General Public License for more details.
032: *
033: * You should have received a copy of the GNU Library General Public
034: * License along with this library; if not, write to the Free
035: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
036: *
037: * The author may be contacted at:
038: * maurocarniel@tin.it</p>
039: *
040: * @author Mauro Carniel
041: * @version 1.0
042: */
043: public class TableListFrame extends JInternalFrame implements
044: DbConnWindow {
045:
046: JPanel mainPanel = new JPanel();
047: JSplitPane splitPane = new JSplitPane();
048: TableDetailPanel detailPane = null;
049: BorderLayout borderLayout1 = new BorderLayout();
050: JTabbedPane tableTabbedPane = new JTabbedPane();
051: JScrollPane tableScrollPane = new JScrollPane();
052: JScrollPane viewScrollPane = new JScrollPane();
053: JScrollPane sinScrollPane = new JScrollPane();
054: JList tablesList = new JList();
055: JList viewList = new JList();
056: JList sinList = new JList();
057: private DbConnectionUtil dbConnUtil = null;
058: JPanel tablesPanel = new JPanel();
059: BorderLayout borderLayout2 = new BorderLayout();
060: JPanel catPanel = new JPanel();
061: JLabel catLabel = new JLabel();
062: JComboBox catComboBox = new JComboBox();
063: GridBagLayout gridBagLayout1 = new GridBagLayout();
064: JPanel tablePanel = new JPanel();
065: JPanel viewPanel = new JPanel();
066: JPanel synPanel = new JPanel();
067: BorderLayout borderLayout3 = new BorderLayout();
068: BorderLayout borderLayout4 = new BorderLayout();
069: BorderLayout borderLayout5 = new BorderLayout();
070:
071: private FilterListPanel tableFilterListPanel = new FilterListPanel(
072: tablesList, new TableFilterController());
073: private FilterListPanel viewFilterListPanel = new FilterListPanel(
074: viewList, new ViewFilterController());
075: private FilterListPanel synFilterListPanel = new FilterListPanel(
076: sinList, new SynFilterController());
077:
078: /** current selected schema name (if combo is empty, then schemaName is set to "" */
079: private String schemaName = "";
080:
081: public TableListFrame(MainFrame parent, DbConnectionUtil dbConnUtil) {
082: super (Options.getInstance().getResource("schema browser")
083: + " - " + dbConnUtil.getDbConnection().getName(), true,
084: true, true, true);
085: this .dbConnUtil = dbConnUtil;
086: this .detailPane = new TableDetailPanel(parent, dbConnUtil, this );
087: try {
088: jbInit();
089: new Thread() {
090: public void run() {
091: ProgressDialog.getInstance().startProgress();
092: try {
093: TableListFrame.this .init();
094: } catch (Throwable ex) {
095: } finally {
096: ProgressDialog.getInstance().stopProgress();
097: }
098: }
099: }.start();
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: }
104:
105: public TableListFrame() {
106: this (null, null);
107: }
108:
109: private void init() {
110: // catalogs list...
111: java.util.List cats = dbConnUtil.getSchemas();
112: for (int i = 0; i < cats.size(); i++)
113: if (cats.get(i) != null)
114: catComboBox.addItem(cats.get(i));
115: if (catComboBox.getItemCount() == 0)
116: catComboBox.addItem("");
117:
118: // listener to repaint lists on selecting a catalog...
119: catComboBox.setSelectedItem(dbConnUtil.getDbConnection()
120: .getUsername().toUpperCase());
121: updateLists();
122: catComboBox
123: .addItemListener(new TableListFrame_catComboBox_itemAdapter(
124: this ));
125:
126: tablesList
127: .addListSelectionListener(new ListSelectionListener() {
128: public void valueChanged(ListSelectionEvent e) {
129: if (!e.getValueIsAdjusting()
130: && tablesList.getSelectedIndex() != -1)
131: detailPane.updateContent(schemaName
132: + tablesList.getSelectedValue()
133: .toString());
134: // new Thread() {
135: // public void run() {
136: // ProgressDialog.getInstance().startProgress();
137: // try {
138: // detailPane.updateContent(schemaName+tablesList.getSelectedValue().toString());
139: // }
140: // catch (Throwable ex) {
141: // }
142: // finally {
143: // ProgressDialog.getInstance().stopProgress();
144: // }
145: // }
146: // }.start();
147:
148: }
149: });
150: sinList.addListSelectionListener(new ListSelectionListener() {
151: public void valueChanged(ListSelectionEvent e) {
152: if (!e.getValueIsAdjusting()
153: && sinList.getSelectedIndex() != -1)
154: detailPane.updateContent(schemaName
155: + sinList.getSelectedValue().toString());
156: // new Thread() {
157: // public void run() {
158: // ProgressDialog.getInstance().startProgress();
159: // try {
160: // detailPane.updateContent(schemaName+sinList.getSelectedValue().toString());
161: // }
162: // catch (Throwable ex) {
163: // }
164: // finally {
165: // ProgressDialog.getInstance().stopProgress();
166: // }
167: // }
168: // }.start();
169:
170: }
171: });
172: viewList.addListSelectionListener(new ListSelectionListener() {
173: public void valueChanged(ListSelectionEvent e) {
174: if (!e.getValueIsAdjusting()
175: && viewList.getSelectedIndex() != -1)
176:
177: detailPane.updateContent(schemaName
178: + viewList.getSelectedValue().toString());
179:
180: // new Thread() {
181: // public void run() {
182: // ProgressDialog.getInstance().startProgress();
183: // try {
184: // detailPane.updateContent(schemaName+viewList.getSelectedValue().toString());
185: // }
186: // catch (Throwable ex) {
187: // }
188: // finally {
189: // ProgressDialog.getInstance().stopProgress();
190: // }
191: // }
192: // }.start();
193:
194: }
195: });
196:
197: }
198:
199: public void updateLists() {
200: if (tableTabbedPane.getSelectedIndex() == 0) {
201: new Thread() {
202: public void run() {
203: loadTables();
204: loadViews();
205: loadSyns();
206: // if (tablesList.getModel().getSize()>0)
207: // tablesList.setSelectedIndex(0);
208: tablesList.requestFocus();
209: }
210: }.start();
211: } else if (tableTabbedPane.getSelectedIndex() == 1) {
212: new Thread() {
213: public void run() {
214: loadViews();
215: loadTables();
216: loadSyns();
217: // if (viewList.getModel().getSize()>0)
218: // viewList.setSelectedIndex(0);
219: viewList.requestFocus();
220: }
221: }.start();
222: } else if (tableTabbedPane.getSelectedIndex() == 2) {
223: new Thread() {
224: public void run() {
225: loadSyns();
226: loadTables();
227: loadViews();
228: // if (sinList.getModel().getSize()>0)
229: // sinList.setSelectedIndex(0);
230: sinList.requestFocus();
231: }
232: }.start();
233: }
234:
235: }
236:
237: /**
238: * Load tables.
239: */
240: private void loadTables() {
241: java.util.List tables = dbConnUtil.getTables(catComboBox
242: .getSelectedItem().toString(), "TABLE");
243: DefaultListModel model = new DefaultListModel();
244: String name = null;
245: boolean ok = true;
246: for (int i = 0; i < tables.size(); i++) {
247: name = tables.get(i).toString();
248: ok = true;
249: for (int j = 0; j < tableFilterListPanel.getFilterPattern()
250: .length(); j++)
251: if (name.indexOf(tableFilterListPanel
252: .getFilterPattern().charAt(j)) != -1) {
253: ok = false;
254: break;
255: }
256: if (ok)
257: model.addElement(name);
258: }
259: tablesList.setModel(model);
260: tablesList.revalidate();
261: // if (tablesList.getModel().getSize()>0)
262: // tablesList.setSelectedIndex(0);
263: tablesList.requestFocus();
264: }
265:
266: /**
267: * Load views.
268: */
269: private void loadViews() {
270: java.util.List views = dbConnUtil.getTables(catComboBox
271: .getSelectedItem().toString(), "VIEW");
272: DefaultListModel model = new DefaultListModel();
273: String name = null;
274: boolean ok = true;
275: for (int i = 0; i < views.size(); i++) {
276: name = views.get(i).toString();
277: ok = true;
278: for (int j = 0; j < viewFilterListPanel.getFilterPattern()
279: .length(); j++)
280: if (name.indexOf(viewFilterListPanel.getFilterPattern()
281: .charAt(j)) != -1) {
282: ok = false;
283: break;
284: }
285: if (ok)
286: model.addElement(name);
287: }
288: viewList.setModel(model);
289: viewList.revalidate();
290: // if (viewList.getModel().getSize()>0)
291: // viewList.setSelectedIndex(0);
292: viewList.requestFocus();
293: }
294:
295: /**
296: * Load synonyms.
297: */
298: private void loadSyns() {
299: java.util.List sin = dbConnUtil.getTables(catComboBox
300: .getSelectedItem().toString(), "SYNONYM");
301: DefaultListModel model = new DefaultListModel();
302: String name = null;
303: boolean ok = true;
304: for (int i = 0; i < sin.size(); i++) {
305: name = sin.get(i).toString();
306: ok = true;
307: for (int j = 0; j < synFilterListPanel.getFilterPattern()
308: .length(); j++)
309: if (name.indexOf(synFilterListPanel.getFilterPattern()
310: .charAt(j)) != -1) {
311: ok = false;
312: break;
313: }
314: if (ok)
315: model.addElement(name);
316: }
317: sinList.setModel(model);
318: sinList.revalidate();
319: // if (sinList.getModel().getSize()>0)
320: // sinList.setSelectedIndex(0);
321: sinList.requestFocus();
322: }
323:
324: private void jbInit() throws Exception {
325: mainPanel.setLayout(borderLayout1);
326: splitPane.setDebugGraphicsOptions(0);
327: tableScrollPane.setToolTipText(Options.getInstance()
328: .getResource("tables list"));
329: viewScrollPane.setRowHeader(null);
330: viewScrollPane.setToolTipText(Options.getInstance()
331: .getResource("views list"));
332: sinScrollPane.setToolTipText(Options.getInstance().getResource(
333: "synonyms list"));
334: tablesPanel.setLayout(borderLayout2);
335: catLabel.setText(Options.getInstance().getResource("catalog"));
336: catPanel.setLayout(gridBagLayout1);
337:
338: tablesList
339: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
340: viewList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
341: sinList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
342:
343: /*
344: tablesList.addKeyListener(new TableListFrame_tablesList_keyAdapter(this));
345: tablesList.addMouseListener(new TableListFrame_tablesList_mouseAdapter(this));
346: viewList.addMouseListener(new TableListFrame_viewList_mouseAdapter(this));
347: sinList.addMouseListener(new TableListFrame_sinList_mouseAdapter(this));
348: viewList.addKeyListener(new TableListFrame_viewList_keyAdapter(this));
349: sinList.addKeyListener(new TableListFrame_sinList_keyAdapter(this));
350: */
351: tableTabbedPane
352: .addChangeListener(new TableListFrame_tableTabbedPane_changeAdapter(
353: this ));
354: this .getContentPane().add(mainPanel, BorderLayout.CENTER);
355: mainPanel.add(splitPane, BorderLayout.CENTER);
356: splitPane.add(detailPane, JSplitPane.RIGHT);
357: splitPane.add(tablesPanel, JSplitPane.LEFT);
358: tablePanel.setLayout(borderLayout3);
359: viewPanel.setLayout(borderLayout4);
360: synPanel.setLayout(borderLayout5);
361: tablePanel.add(tableFilterListPanel, BorderLayout.NORTH);
362: tablePanel.add(tableScrollPane, BorderLayout.CENTER);
363: viewPanel.add(viewFilterListPanel, BorderLayout.NORTH);
364: viewPanel.add(viewScrollPane, BorderLayout.CENTER);
365: synPanel.add(synFilterListPanel, BorderLayout.NORTH);
366: synPanel.add(sinScrollPane, BorderLayout.CENTER);
367:
368: tableTabbedPane.add(tablePanel, Options.getInstance()
369: .getResource("tables"));
370: tableTabbedPane.add(viewPanel, Options.getInstance()
371: .getResource("views"));
372: tableTabbedPane.add(synPanel, Options.getInstance()
373: .getResource("synonyms"));
374: tablesPanel.add(catPanel, BorderLayout.NORTH);
375: tableScrollPane.getViewport().add(tablesList, null);
376: viewScrollPane.getViewport().add(viewList, null);
377: sinScrollPane.getViewport().add(sinList, null);
378: splitPane.setDividerLocation(200);
379: tablesPanel.add(tableTabbedPane, BorderLayout.CENTER);
380: catPanel.add(catLabel, new GridBagConstraints(0, 0, 1, 1, 0.0,
381: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
382: new Insets(5, 5, 5, 5), 0, 0));
383: catPanel.add(catComboBox, new GridBagConstraints(1, 0, 1, 1,
384: 1.0, 0.0, GridBagConstraints.CENTER,
385: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
386: 0, 0));
387: }
388:
389: public DbConnectionUtil getDbConnectionUtil() {
390: return dbConnUtil;
391: }
392:
393: void catComboBox_itemStateChanged(ItemEvent e) {
394: if (e.getStateChange() == e.SELECTED) {
395: if (catComboBox.getSelectedItem() != null
396: && !catComboBox.getSelectedItem().equals(""))
397: schemaName = catComboBox.getSelectedItem() + ".";
398: else
399: schemaName = "";
400:
401: new Thread() {
402: public void run() {
403: ProgressDialog.getInstance().startProgress();
404: try {
405: // update lists...
406: updateLists();
407: } catch (Throwable ex) {
408: } finally {
409: ProgressDialog.getInstance().stopProgress();
410: }
411: }
412: }.start();
413: }
414: }
415:
416: void tableTabbedPane_stateChanged(ChangeEvent e) {
417: if (tableTabbedPane.getSelectedIndex() == 0
418: && tablesList.getSelectedIndex() != -1)
419: detailPane.updateContent(schemaName
420: + tablesList.getSelectedValue().toString());
421: else if (tableTabbedPane.getSelectedIndex() == 1
422: && viewList.getSelectedIndex() != -1)
423: detailPane.updateContent(schemaName
424: + viewList.getSelectedValue().toString());
425: else if (tableTabbedPane.getSelectedIndex() == 2
426: && sinList.getSelectedIndex() != -1)
427: detailPane.updateContent(schemaName
428: + sinList.getSelectedValue().toString());
429: }
430:
431: /*
432: void tablesList_mouseClicked(MouseEvent e) {
433: if (tablesList.getSelectedIndex()!=-1)
434: detailPane.updateContent(tablesList.getSelectedValue().toString());
435: }
436:
437: void viewList_mouseClicked(MouseEvent e) {
438: if (viewList.getSelectedIndex()!=-1)
439: detailPane.updateContent(viewList.getSelectedValue().toString());
440: }
441:
442: void sinList_mouseClicked(MouseEvent e) {
443: if (sinList.getSelectedIndex()!=-1)
444: detailPane.updateContent(sinList.getSelectedValue().toString());
445: }
446:
447: void tablesList_keyTyped(KeyEvent e) {
448: if (e.getKeyChar()=='\n' && tablesList.getSelectedIndex()!=-1)
449: detailPane.updateContent(tablesList.getSelectedValue().toString());
450:
451: }
452:
453: void viewList_keyTyped(KeyEvent e) {
454: if (e.getKeyChar()=='\n' && viewList.getSelectedIndex()!=-1)
455: detailPane.updateContent(viewList.getSelectedValue().toString());
456:
457: }
458:
459: void sinList_keyTyped(KeyEvent e) {
460: if (e.getKeyChar()=='\n' && sinList.getSelectedIndex()!=-1)
461: detailPane.updateContent(sinList.getSelectedValue().toString());
462: }
463: */
464:
465: /**
466: * <p>Description: Inner class which manages filter events on tables.</p>
467: */
468: class TableFilterController implements FilterListController {
469:
470: /**
471: * Reload the list, which will be filtered by the specified pattern
472: */
473: public void reloadList() {
474: loadTables();
475: }
476:
477: }
478:
479: /**
480: * <p>Description: Inner class which manages filter events on views.</p>
481: */
482: class ViewFilterController implements FilterListController {
483:
484: /**
485: * Reload the list, which will be filtered by the specified pattern
486: */
487: public void reloadList() {
488: loadViews();
489: }
490:
491: }
492:
493: /**
494: * <p>Description: Inner class which manages filter events on synonyms.</p>
495: */
496: class SynFilterController implements FilterListController {
497:
498: /**
499: * Reload the list, which will be filtered by the specified pattern
500: */
501: public void reloadList() {
502: loadSyns();
503: }
504:
505: }
506:
507: }
508:
509: class TableListFrame_catComboBox_itemAdapter implements
510: java.awt.event.ItemListener {
511: TableListFrame adaptee;
512:
513: TableListFrame_catComboBox_itemAdapter(TableListFrame adaptee) {
514: this .adaptee = adaptee;
515: }
516:
517: public void itemStateChanged(ItemEvent e) {
518: adaptee.catComboBox_itemStateChanged(e);
519: }
520: }
521:
522: /*
523: class TableListFrame_tablesList_mouseAdapter extends java.awt.event.MouseAdapter {
524: TableListFrame adaptee;
525:
526: TableListFrame_tablesList_mouseAdapter(TableListFrame adaptee) {
527: this.adaptee = adaptee;
528: }
529: public void mouseClicked(MouseEvent e) {
530: adaptee.tablesList_mouseClicked(e);
531: }
532: }
533:
534: class TableListFrame_viewList_mouseAdapter extends java.awt.event.MouseAdapter {
535: TableListFrame adaptee;
536:
537: TableListFrame_viewList_mouseAdapter(TableListFrame adaptee) {
538: this.adaptee = adaptee;
539: }
540: public void mouseClicked(MouseEvent e) {
541: adaptee.viewList_mouseClicked(e);
542: }
543: }
544:
545: class TableListFrame_sinList_mouseAdapter extends java.awt.event.MouseAdapter {
546: TableListFrame adaptee;
547:
548: TableListFrame_sinList_mouseAdapter(TableListFrame adaptee) {
549: this.adaptee = adaptee;
550: }
551: public void mouseClicked(MouseEvent e) {
552: adaptee.sinList_mouseClicked(e);
553: }
554: }
555:
556: class TableListFrame_tablesList_keyAdapter extends java.awt.event.KeyAdapter {
557: TableListFrame adaptee;
558:
559: TableListFrame_tablesList_keyAdapter(TableListFrame adaptee) {
560: this.adaptee = adaptee;
561: }
562: public void keyTyped(KeyEvent e) {
563: adaptee.tablesList_keyTyped(e);
564: }
565: }
566:
567: class TableListFrame_viewList_keyAdapter extends java.awt.event.KeyAdapter {
568: TableListFrame adaptee;
569:
570: TableListFrame_viewList_keyAdapter(TableListFrame adaptee) {
571: this.adaptee = adaptee;
572: }
573: public void keyTyped(KeyEvent e) {
574: adaptee.viewList_keyTyped(e);
575: }
576: }
577:
578: class TableListFrame_sinList_keyAdapter extends java.awt.event.KeyAdapter {
579: TableListFrame adaptee;
580:
581: TableListFrame_sinList_keyAdapter(TableListFrame adaptee) {
582: this.adaptee = adaptee;
583: }
584: public void keyTyped(KeyEvent e) {
585: adaptee.sinList_keyTyped(e);
586: }
587: }
588:
589:
590: */
591:
592: class TableListFrame_tableTabbedPane_changeAdapter implements
593: javax.swing.event.ChangeListener {
594: TableListFrame adaptee;
595:
596: TableListFrame_tableTabbedPane_changeAdapter(TableListFrame adaptee) {
597: this .adaptee = adaptee;
598: }
599:
600: public void stateChanged(ChangeEvent e) {
601: adaptee.tableTabbedPane_stateChanged(e);
602: }
603: }
|