001: /*
002: ** $Id: MainView.java,v 1.14 2000/11/01 08:43:37 mrw Exp $
003: **
004: ** The top-level ViennaSQL view. Contains a menubar, toolbar and tabset for
005: ** a load of queries.
006: **
007: ** Mike Wilson, July 2000, mrw@whisperingwind.co.uk
008: **
009: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
010: **
011: ** This program is free software; you can redistribute it and/or modify
012: ** it under the terms of the GNU General Public License as published by
013: ** the Free Software Foundation; either version 2 of the License, or
014: ** (at your option) any later version.
015: **
016: ** This program is distributed in the hope that it will be useful,
017: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
018: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: ** GNU General Public License for more details.
020: **
021: ** You should have received a copy of the GNU Library General
022: ** Public License along with this library; if not, write to the
023: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
024: ** Boston, MA 02111-1307 USA.
025: */
026:
027: package uk.co.whisperingwind.vienna;
028:
029: import java.awt.BorderLayout;
030: import java.awt.Component;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.InputEvent;
034: import java.awt.event.ItemEvent;
035: import java.awt.event.ItemListener;
036: import java.awt.event.KeyEvent;
037: import java.awt.event.KeyListener;
038: import java.awt.event.WindowAdapter;
039: import java.awt.event.WindowEvent;
040: import javax.swing.event.ChangeEvent;
041: import javax.swing.event.ChangeListener;
042: import javax.swing.JButton;
043: import javax.swing.JMenu;
044: import javax.swing.JMenuBar;
045: import javax.swing.JPanel;
046: import javax.swing.JTabbedPane;
047: import javax.swing.JToolBar;
048: import uk.co.whisperingwind.framework.ActionFactory;
049: import uk.co.whisperingwind.framework.FrameView;
050: import uk.co.whisperingwind.framework.ModelEvent;
051:
052: class MainView extends FrameView implements ActionListener,
053: ChangeListener {
054: private JTabbedPane queryTabs = null;
055: private ConnectionList connectionList = null;
056: private ConnectionListModel connectionListModel = null;
057:
058: private ActionFactory.DefaultAction newAction = null;
059: private ActionFactory.DefaultAction openAction = null;
060: private ActionFactory.DefaultAction saveAction = null;
061: private ActionFactory.DefaultAction saveAsAction = null;
062: private ActionFactory.DefaultAction saveAllAction = null;
063: private ActionFactory.DefaultAction closeAction = null;
064: private ActionFactory.DefaultAction closeAllAction = null;
065: private ActionFactory.DefaultAction configAction = null;
066: private ActionFactory.DefaultAction quitAction = null;
067: private ActionFactory.DefaultAction cutAction = null;
068: private ActionFactory.DefaultAction copyAction = null;
069: private ActionFactory.DefaultAction pasteAction = null;
070: private ActionFactory.DefaultAction undoAction = null;
071: private ActionFactory.DefaultAction redoAction = null;
072: private ActionFactory.DefaultAction clearAction = null;
073: private ActionFactory.DefaultAction executeAction = null;
074: private ActionFactory.DefaultAction cancelAction = null;
075: private ActionFactory.DefaultAction commitAction = null;
076: private ActionFactory.DefaultAction rollbackAction = null;
077: private ActionFactory.DefaultAction schemaAction = null;
078: private ActionFactory.DefaultAction exportAction = null;
079: private ActionFactory.DefaultAction aboutAction = null;
080:
081: public MainView(ConfigModel configModel) {
082: super ();
083: setFileTitle("");
084: createActions();
085: createToolbar(configModel);
086: createMenu();
087: createComponents();
088:
089: content.addKeyListener(new KeyPressListener());
090: }
091:
092: public void pack() {
093: content.pack();
094: content.setVisible(true);
095: }
096:
097: /*
098: ** Set the main frame title with the app name and a file path.
099: */
100:
101: public void setFileTitle(String fileName) {
102: if (fileName.length() > 0)
103: content.setTitle("ViennaSQL - " + fileName);
104: else
105: content.setTitle("ViennaSQL");
106: }
107:
108: /*
109: ** Add a new tab to the tabset.
110: */
111:
112: public void addTab(String title, JPanel content) {
113: queryTabs.add(title, content);
114: queryTabs.setSelectedComponent(content);
115: }
116:
117: /*
118: ** Returns the number of open queries.
119: */
120:
121: public int getQueryCount() {
122: return queryTabs.getTabCount();
123: }
124:
125: /*
126: ** Select the query tab at queryIdx.
127: */
128:
129: public void selectQuery(int queryIdx) {
130: if (queryIdx >= 0 && queryIdx < getQueryCount())
131: queryTabs.setSelectedIndex(queryIdx);
132: }
133:
134: /*
135: ** Remove the selected tab.
136: */
137:
138: public Component removeSelected() {
139: Component removed = getSelected();
140: queryTabs.remove(removed);
141:
142: return removed;
143: }
144:
145: private void createActions() {
146: MainActionFactory factory = new MainActionFactory(
147: "/uk/co/whisperingwind/images");
148:
149: newAction = factory.createAction("new");
150: openAction = factory.createAction("open");
151: saveAction = factory.createAction("save");
152: saveAsAction = factory.createAction("saveAs");
153: saveAllAction = factory.createAction("saveAll");
154: closeAction = factory.createAction("close");
155: closeAllAction = factory.createAction("closeAll");
156: configAction = factory.createAction("config");
157: quitAction = factory.createAction("quit");
158:
159: cutAction = factory.createAction("cut");
160: copyAction = factory.createAction("copy");
161: pasteAction = factory.createAction("paste");
162: clearAction = factory.createAction("clear");
163: undoAction = factory.createAction("undo");
164: redoAction = factory.createAction("redo");
165:
166: executeAction = factory.createAction("execute");
167: cancelAction = factory.createAction("cancel");
168: commitAction = factory.createAction("commit");
169: rollbackAction = factory.createAction("rollback");
170: schemaAction = factory.createAction("schema");
171: exportAction = factory.createAction("export");
172: aboutAction = factory.createAction("about");
173:
174: newAction.addActionListener(actionListener);
175: openAction.addActionListener(actionListener);
176: saveAction.addActionListener(actionListener);
177: saveAsAction.addActionListener(actionListener);
178: saveAllAction.addActionListener(actionListener);
179: closeAction.addActionListener(actionListener);
180: closeAllAction.addActionListener(actionListener);
181: configAction.addActionListener(actionListener);
182: quitAction.addActionListener(actionListener);
183:
184: cutAction.addActionListener(actionListener);
185: copyAction.addActionListener(actionListener);
186: pasteAction.addActionListener(actionListener);
187: clearAction.addActionListener(actionListener);
188: undoAction.addActionListener(actionListener);
189: redoAction.addActionListener(actionListener);
190:
191: executeAction.addActionListener(actionListener);
192: cancelAction.addActionListener(actionListener);
193: commitAction.addActionListener(actionListener);
194: rollbackAction.addActionListener(actionListener);
195: schemaAction.addActionListener(actionListener);
196: exportAction.addActionListener(actionListener);
197: aboutAction.addActionListener(actionListener);
198: }
199:
200: /*
201: ** Create my toolbar.
202: */
203:
204: private void createToolbar(ConfigModel configModel) {
205: JToolBar toolBar = new JToolBar();
206: JButton toolButton;
207:
208: newAction.toolButtonFactory(toolBar);
209: openAction.toolButtonFactory(toolBar);
210: saveAction.toolButtonFactory(toolBar);
211: saveAllAction.toolButtonFactory(toolBar);
212:
213: toolBar.addSeparator();
214: executeAction.toolButtonFactory(toolBar);
215: cancelAction.toolButtonFactory(toolBar);
216: toolBar.addSeparator();
217: commitAction.toolButtonFactory(toolBar);
218: rollbackAction.toolButtonFactory(toolBar);
219:
220: toolBar.addSeparator();
221: connectionListModel = configModel.getConnectionListModel();
222: connectionList = new ConnectionList(connectionListModel
223: .getComboBoxModel());
224: connectionList.addActionListener(this );
225: connectionList.setToolTipText("Select connection");
226:
227: toolBar.add(connectionList);
228: content.getContentPane().add(toolBar, BorderLayout.NORTH);
229: }
230:
231: /*
232: ** Create my menu bar.
233: */
234:
235: private void createMenu() {
236: JMenuBar menuBar = new JMenuBar();
237: JMenu menuFile = new JMenu("File");
238: JMenu menuEdit = new JMenu("Edit");
239: JMenu menuQuery = new JMenu("Query");
240: JMenu menuHelp = new JMenu("Help");
241:
242: ItemListener actionListener = new ItemListener() {
243: public void itemStateChanged(ItemEvent event) {
244: if (event.getStateChange() == ItemEvent.SELECTED) {
245: fireEvent("popup");
246: }
247: }
248: };
249:
250: menuEdit.addItemListener(actionListener);
251:
252: newAction.menuItemFactory(menuFile);
253: openAction.menuItemFactory(menuFile);
254: saveAction.menuItemFactory(menuFile);
255: saveAsAction.menuItemFactory(menuFile);
256: saveAllAction.menuItemFactory(menuFile);
257: closeAction.menuItemFactory(menuFile);
258: closeAllAction.menuItemFactory(menuFile);
259: configAction.menuItemFactory(menuFile);
260:
261: menuFile.addSeparator();
262:
263: quitAction.menuItemFactory(menuFile);
264:
265: undoAction.menuItemFactory(menuEdit);
266: redoAction.menuItemFactory(menuEdit);
267: cutAction.menuItemFactory(menuEdit);
268: copyAction.menuItemFactory(menuEdit);
269: pasteAction.menuItemFactory(menuEdit);
270: clearAction.menuItemFactory(menuEdit);
271:
272: executeAction.menuItemFactory(menuQuery);
273: commitAction.menuItemFactory(menuQuery);
274: rollbackAction.menuItemFactory(menuQuery);
275: cancelAction.menuItemFactory(menuQuery);
276: schemaAction.menuItemFactory(menuQuery);
277: exportAction.menuItemFactory(menuQuery);
278:
279: aboutAction.menuItemFactory(menuHelp);
280:
281: menuBar.add(menuFile);
282: menuBar.add(menuEdit);
283: menuBar.add(menuQuery);
284: menuBar.add(menuHelp);
285: //menuBar.setHelpMenu (menuHelp);
286:
287: content.setJMenuBar(menuBar);
288: }
289:
290: /*
291: ** Create the main part of the view.
292: */
293:
294: private void createComponents() {
295: queryTabs = new JTabbedPane();
296: queryTabs.addChangeListener(this );
297: content.addWindowListener(new DeadlyWindowListener());
298: content.getContentPane().add(queryTabs, BorderLayout.CENTER);
299: }
300:
301: /*
302: ** Returns the selected component -- the one on top of the tabset
303: ** stack.
304: */
305:
306: public Component getSelected() {
307: return queryTabs.getSelectedComponent();
308: }
309:
310: /*
311: ** Return the title of the selcted tab.
312: */
313:
314: public String getSelectedTitle() {
315: return queryTabs.getTitleAt(queryTabs.getSelectedIndex());
316: }
317:
318: /*
319: ** Change the title of the selected tab.
320: */
321:
322: public void setSelectedTitle(String title) {
323: int i = queryTabs.getSelectedIndex();
324: queryTabs.setTitleAt(i, title);
325: }
326:
327: /*
328: ** Enable or disable some buttons and menu items depending on
329: ** whether the is a connection, open file, executing query etc.
330: */
331:
332: public void enableGUI(boolean connected, boolean haveFile,
333: boolean executing, boolean updated, boolean haveSelection,
334: boolean haveClip, boolean canExport, boolean canUndo,
335: boolean canRedo) {
336: saveAction.setEnabled(haveFile);
337: saveAllAction.setEnabled(haveFile);
338: saveAsAction.setEnabled(haveFile);
339:
340: executeAction.setEnabled(connected && haveFile && !executing);
341: cancelAction.setEnabled(executing);
342: commitAction.setEnabled(updated);
343: rollbackAction.setEnabled(updated);
344: schemaAction.setEnabled(connected);
345:
346: cutAction.setEnabled(haveSelection);
347: copyAction.setEnabled(haveSelection);
348: pasteAction.setEnabled(haveClip);
349: clearAction.setEnabled(haveSelection);
350:
351: exportAction.setEnabled(canExport);
352: undoAction.setEnabled(canUndo);
353: redoAction.setEnabled(canRedo);
354:
355: connectionList.setEnabled(!updated);
356: }
357:
358: /*
359: ** ActionListener callback. Something happened in a button, menu
360: ** item etc.
361: */
362:
363: public void actionPerformed(ActionEvent event) {
364: if (event.getSource() == connectionList) {
365: if (event.getActionCommand().equals("comboBoxChanged")) {
366: String selected = (String) connectionList
367: .getSelectedItem();
368:
369: if (selected != null)
370: fireEvent("connect", selected);
371: }
372: }
373: }
374:
375: public String getSelectedConnection() {
376: return (String) connectionList.getSelectedItem();
377: }
378:
379: /*
380: ** Set the combo box selection to the given item. Returns true if
381: ** the item can be selected.
382: */
383:
384: public boolean setSelectedConnection(String connection) {
385: boolean selected = true;
386:
387: if (connection == null) {
388: connectionList.setSelectedIndex(0);
389: } else {
390: if (connectionListModel.contains(connection)) {
391: connectionList.setSelectedItem(connection);
392: } else {
393: connectionList.setSelectedIndex(0);
394: selected = false;
395: }
396: }
397:
398: return selected;
399: }
400:
401: /*
402: ** ChangeListener callback.
403: */
404:
405: public void stateChanged(ChangeEvent event) {
406: fireEvent("changeTab");
407: }
408:
409: protected void modelEvent(ModelEvent event) {
410: }
411:
412: protected void cleanUp() {
413: }
414:
415: public void setStatus(String status) {
416: }
417:
418: /*
419: ** MainView has one instance of this. It listens out for window
420: ** closing events and notifies the controller.
421: */
422:
423: private class DeadlyWindowListener extends WindowAdapter {
424: public void windowClosing(WindowEvent evt) {
425: fireEvent("quit");
426: }
427: }
428:
429: class KeyPressListener implements KeyListener {
430: public void keyPressed(KeyEvent event) {
431: if ((event.getModifiers() & InputEvent.CTRL_MASK) != 0) {
432: int keyCode = event.getKeyCode();
433:
434: if (keyCode >= KeyEvent.VK_0
435: && keyCode <= KeyEvent.VK_9)
436: selectQuery(keyCode - 0x30 - 1);
437: }
438: }
439:
440: public void keyReleased(KeyEvent event) {
441: }
442:
443: public void keyTyped(KeyEvent event) {
444: }
445: }
446: }
|