001: /*
002: ** $Id: SchemaView.java,v 1.17 2000/10/26 08:34:16 mrw Exp $
003: **
004: ** Frame to view schema details. Contains a table of schema contents
005: ** (tables etc) and a tab set to view columns, primary keys or foreign
006: ** keys.
007: **
008: ** Mike Wilson, September 2000, mrw@whisperingwind.co.uk
009: **
010: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
011: **
012: ** This program is free software; you can redistribute it and/or modify
013: ** it under the terms of the GNU General Public License as published by
014: ** the Free Software Foundation; either version 2 of the License, or
015: ** (at your option) any later version.
016: **
017: ** This program is distributed in the hope that it will be useful,
018: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
019: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: ** GNU General Public License for more details.
021: **
022: ** You should have received a copy of the GNU Library General
023: ** Public License along with this library; if not, write to the
024: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
025: ** Boston, MA 02111-1307 USA.
026: */
027:
028: package uk.co.whisperingwind.vienna;
029:
030: import java.awt.BorderLayout;
031: import java.awt.Container;
032: import java.awt.Cursor;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.WindowAdapter;
036: import java.awt.event.WindowEvent;
037: import java.sql.Connection;
038: import java.sql.DatabaseMetaData;
039: import java.sql.SQLException;
040: import java.util.TreeMap;
041: import javax.swing.DefaultListSelectionModel;
042: import javax.swing.event.ChangeEvent;
043: import javax.swing.event.ChangeListener;
044: import javax.swing.event.ListSelectionEvent;
045: import javax.swing.JMenu;
046: import javax.swing.JMenuBar;
047: import javax.swing.JRootPane;
048: import javax.swing.JScrollPane;
049: import javax.swing.JSplitPane;
050: import javax.swing.JTabbedPane;
051: import javax.swing.JTable;
052: import javax.swing.JToolBar;
053: import javax.swing.table.TableModel;
054: import uk.co.whisperingwind.framework.ActionFactory;
055: import uk.co.whisperingwind.framework.ExceptionDialog;
056: import uk.co.whisperingwind.framework.FrameView;
057: import uk.co.whisperingwind.framework.ModelEvent;
058: import uk.co.whisperingwind.framework.StatusBar;
059:
060: class SchemaView extends FrameView implements ActionListener,
061: ChangeListener {
062: private TreeMap viewMap = new TreeMap();
063: private TableTable tableTable = null;
064: private JTable columnTable = null;
065: private JTable primaryTable = null;
066: private JTable foreignTable = null;
067: private JTabbedPane tabbedPane = null;
068: private StatusBar statusBar = null;
069: private Cursor originalCursor = null;
070:
071: private ActionFactory.DefaultAction newAction = null;
072: private ActionFactory.DefaultAction closeAction = null;
073: private ActionFactory.DefaultAction tableAction = null;
074: private ActionFactory.DefaultAction viewAction = null;
075: private ActionFactory.DefaultAction synonymAction = null;
076: private ActionFactory.DefaultAction sequenceAction = null;
077:
078: public SchemaView(Connection connection, TableModel tableModel,
079: TableModel columnModel, TableModel primaryModel,
080: TableModel foreignModel, String schemaName) {
081: try {
082: createActions();
083: createWindow(connection.getMetaData(), tableModel,
084: columnModel, primaryModel, foreignModel, content
085: .getRootPane(), content.getContentPane());
086:
087: content.setTitle("Schema " + schemaName);
088: setStatus("Loading schema");
089: content.pack();
090: content.setVisible(true);
091: content.addWindowListener(new DeadlyWindowListener());
092: } catch (SQLException ex) {
093: new ExceptionDialog(ex);
094: }
095: }
096:
097: public int getSelectedTab() {
098: return tabbedPane.getSelectedIndex();
099: }
100:
101: public void setStatus(String status) {
102: statusBar.setText(status);
103: }
104:
105: public String getSelectedName() {
106: String selected = null;
107: int i = tableTable.getSelectedRow();
108:
109: if (i >= 0 && i < tableTable.getRowCount())
110: selected = (String) tableTable.getModel().getValueAt(i, 0);
111:
112: return selected;
113: }
114:
115: public String getSelectedType() {
116: String selected = null;
117: int i = tableTable.getSelectedRow();
118:
119: if (i >= 0 && i < tableTable.getRowCount())
120: selected = (String) tableTable.getModel().getValueAt(i, 1);
121:
122: return selected;
123: }
124:
125: /*
126: ** Clear all selections in the tables table.
127: */
128:
129: public void clearTableSelection() {
130: tableTable.clearSelection();
131: }
132:
133: /*
134: ** Clear all selections in the column, primary key and foreign key
135: ** tables.
136: */
137:
138: public void clearColumnSelection() {
139: columnTable.clearSelection();
140: primaryTable.clearSelection();
141: foreignTable.clearSelection();
142: }
143:
144: /**
145: ** Change the model used for the Table table.
146: */
147:
148: public void setTableModel(TableModel tableModel) {
149: tableTable.setModel(tableModel);
150: }
151:
152: /**
153: ** Change the model used for the Column table.
154: */
155:
156: public void setColumnModel(TableModel columnModel) {
157: columnTable.setModel(columnModel);
158: }
159:
160: /**
161: ** Change the model used for the Primary key table.
162: */
163:
164: public void setPrimaryModel(TableModel primaryModel) {
165: primaryTable.setModel(primaryModel);
166: }
167:
168: /**
169: ** Change the model used for the Foreign key table.
170: */
171:
172: public void setForeignModel(TableModel foreignModel) {
173: foreignTable.setModel(foreignModel);
174: }
175:
176: public void busyCursor(boolean busy) {
177: if (busy) {
178: if (originalCursor == null) {
179: originalCursor = content.getCursor();
180: content.setCursor(new Cursor(Cursor.WAIT_CURSOR));
181: }
182: } else {
183: content.setCursor(originalCursor);
184: originalCursor = null;
185: }
186: }
187:
188: private void createActions() {
189: SchemaActionFactory factory = new SchemaActionFactory(
190: "/uk/co/whisperingwind/images");
191:
192: newAction = factory.createAction("new");
193: closeAction = factory.createAction("close");
194: sequenceAction = factory.createAction("sequence");
195: synonymAction = factory.createAction("synonym");
196: tableAction = factory.createAction("table");
197: viewAction = factory.createAction("view");
198:
199: newAction.addActionListener(actionListener);
200: closeAction.addActionListener(actionListener);
201: sequenceAction.addActionListener(actionListener);
202: synonymAction.addActionListener(actionListener);
203: tableAction.addActionListener(actionListener);
204: viewAction.addActionListener(actionListener);
205: }
206:
207: /*
208: ** Create the visible components of the Schema View.
209: */
210:
211: private void createWindow(DatabaseMetaData metaData,
212: TableModel tableModel, TableModel columnModel,
213: TableModel primaryModel, TableModel foreignModel,
214: JRootPane root, Container container) throws SQLException {
215: container.setLayout(new BorderLayout());
216:
217: createMenus(metaData, root, container);
218: createToolbar(metaData, root, container);
219:
220: JSplitPane splitter = new JSplitPane();
221: container.add(splitter, BorderLayout.CENTER);
222:
223: tableTable = new TableTable(tableModel);
224: JScrollPane tableScroller = new JScrollPane(tableTable);
225:
226: columnTable = new JTable(columnModel);
227: JScrollPane columnScroller = new JScrollPane(columnTable);
228:
229: tabbedPane = new JTabbedPane();
230: tabbedPane.add("Columns", columnScroller);
231: tabbedPane.addChangeListener(this );
232:
233: /*
234: ** JTable for primary keys.
235: */
236:
237: primaryTable = new JTable(primaryModel);
238: JScrollPane primaryScroller = new JScrollPane(primaryTable);
239: tabbedPane.add("Primary", primaryScroller);
240:
241: /*
242: ** JTable for foreign keys.
243: */
244:
245: foreignTable = new JTable(foreignModel);
246: JScrollPane foreignScroller = new JScrollPane(foreignTable);
247: tabbedPane.add("Foreign", foreignScroller);
248:
249: splitter.add(tableScroller, JSplitPane.LEFT);
250: splitter.add(tabbedPane, JSplitPane.RIGHT);
251:
252: statusBar = new StatusBar();
253: container.add(statusBar, BorderLayout.SOUTH);
254: }
255:
256: private void createMenus(DatabaseMetaData metaData, JRootPane root,
257: Container container) throws SQLException {
258: JMenuBar menuBar = new JMenuBar();
259: root.setJMenuBar(menuBar);
260:
261: JMenu menuFile = new JMenu("File");
262:
263: newAction.menuItemFactory(menuFile);
264: closeAction.menuItemFactory(menuFile);
265:
266: JMenu menuView = new JMenu("View");
267: sequenceAction.menuItemFactory(menuView);
268: synonymAction.menuItemFactory(menuView);
269: tableAction.menuItemFactory(menuView);
270: viewAction.menuItemFactory(menuView);
271:
272: menuBar.add(menuFile);
273: menuBar.add(menuView);
274: }
275:
276: private void createToolbar(DatabaseMetaData metaData,
277: JRootPane root, Container container) {
278: JToolBar toolBar = new JToolBar();
279: container.add(toolBar, BorderLayout.NORTH);
280:
281: newAction.toolButtonFactory(toolBar);
282: }
283:
284: public boolean getShowTables() {
285: return tableAction.getState();
286: }
287:
288: public boolean getShowViews() {
289: return viewAction.getState();
290: }
291:
292: public boolean getShowSynonyms() {
293: return synonymAction.getState();
294: }
295:
296: public boolean getShowSequences() {
297: return sequenceAction.getState();
298: }
299:
300: public int getSelectedTable() {
301: return tableTable.getSelectedRow();
302: }
303:
304: public void actionPerformed(ActionEvent event) {
305: }
306:
307: /*
308: ** ChangeListener callback.
309: */
310:
311: public void stateChanged(ChangeEvent event) {
312: TableModel model = tableTable.getModel();
313: int row = tableTable.getSelectedRow();
314:
315: if (row >= 0)
316: fireEvent("table", model.getValueAt(row, 0));
317: }
318:
319: protected void modelEvent(ModelEvent event) {
320: }
321:
322: protected void cleanUp() {
323: }
324:
325: /*
326: ** JTable for the list of tables. Fires a ViewEvent when the table
327: ** selection changes.
328: */
329:
330: private class TableTable extends JTable {
331: public TableTable(TableModel model) {
332: super (model);
333:
334: DefaultListSelectionModel singleSelectionModel = new DefaultListSelectionModel();
335: singleSelectionModel
336: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
337: setSelectionModel(singleSelectionModel);
338: }
339:
340: /*
341: ** Let anyone who's interesed know that the table selection
342: ** has changed.
343: */
344:
345: public void valueChanged(ListSelectionEvent event) {
346: super .valueChanged(event);
347: TableModel model = getModel();
348:
349: if (SchemaView.this != null) {
350: if (model != null) {
351: int row = getSelectedRow();
352:
353: if (row >= 0 && row < model.getRowCount())
354: fireEvent("table", model.getValueAt(row, 0));
355: else
356: fireEvent("table", null);
357: }
358: }
359: }
360: }
361:
362: /*
363: ** SchemaView has one instance of this. It listens out for window
364: ** closing events and notifies the controller.
365: */
366:
367: private class DeadlyWindowListener extends WindowAdapter {
368: public void windowClosing(WindowEvent evt) {
369: fireEvent("close");
370: }
371: }
372: }
|