001: /*
002: ** $Id: ConfigController.java,v 1.12 2000/10/26 08:34:15 mrw Exp $
003: **
004: ** Controller for the configuration view.
005: **
006: ** Mike Wilson, July 2000, mrw@whisperingwind.co.uk
007: **
008: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
009: **
010: ** This program is free software; you can redistribute it and/or modify
011: ** it under the terms of the GNU General Public License as published by
012: ** the Free Software Foundation; either version 2 of the License, or
013: ** (at your option) any later version.
014: **
015: ** This program is distributed in the hope that it will be useful,
016: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
017: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: ** GNU General Public License for more details.
019: **
020: ** You should have received a copy of the GNU Library General
021: ** Public License along with this library; if not, write to the
022: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: ** Boston, MA 02111-1307 USA.
024: */
025:
026: package uk.co.whisperingwind.vienna;
027:
028: import java.awt.Font;
029: import java.io.FileNotFoundException;
030: import java.io.IOException;
031: import java.util.Enumeration;
032: import java.util.Vector;
033: import javax.swing.JFrame;
034: import uk.co.whisperingwind.framework.Controller;
035: import uk.co.whisperingwind.framework.Dialogs;
036: import uk.co.whisperingwind.framework.ExceptionDialog;
037: import uk.co.whisperingwind.framework.FontChooser;
038: import uk.co.whisperingwind.framework.ModelEvent;
039: import uk.co.whisperingwind.framework.ViewEvent;
040:
041: class ConfigController extends Controller {
042: private static ConfigController instance = null;
043: private ConfigModel configModel = null;
044: private ConfigView configView = null;
045: private ConfigConnectionView connectionView = null;
046: private ConfigModel originalConfigModel = null;
047: private FontChooser fontChooser = null;
048: private Vector testViewList = new Vector();
049:
050: public static ConfigController singleton(JFrame parent,
051: ConfigModel theConfigModel) {
052: ConfigController result = null;
053:
054: if (instance == null) {
055: instance = new ConfigController(parent, theConfigModel);
056: result = instance;
057: }
058:
059: return result;
060: }
061:
062: public ConfigController(JFrame parent, ConfigModel theConfigModel) {
063: originalConfigModel = theConfigModel;
064: configModel = new ConfigModel(theConfigModel);
065:
066: configView = new ConfigView(parent, configModel, false);
067: configView.addObserver(this );
068:
069: connectionView = new ConfigConnectionView(parent, false);
070: connectionView.addObserver(this );
071: }
072:
073: public void modelEvent(ModelEvent event) {
074: }
075:
076: /*
077: **
078: */
079:
080: public void viewEvent(ViewEvent event) {
081: if (event.getInitiator() == configView)
082: handleViewEvent(event);
083: else if (event.getInitiator() == connectionView)
084: handleConnectionEvent(event);
085: else if (event.getInitiator() instanceof TestView)
086: handleTestEvent(event);
087: }
088:
089: private void handleViewEvent(ViewEvent event) {
090: String action = event.getArg1();
091:
092: if (action.equals("ok")) {
093: configModel.setSavePasswords(configView.getSavePasswords());
094:
095: try {
096: String maxRows = configView.getMaxRows();
097: configModel.setMaxRows(Integer.decode(maxRows)
098: .intValue());
099:
100: configView.closeDialog();
101: configView.deleteObserver(this );
102: configView = null;
103:
104: connectionView.closeDialog();
105: instance = null;
106:
107: originalConfigModel.assign(configModel);
108: originalConfigModel.save();
109: } catch (NumberFormatException ex) {
110: Dialogs.showError("Invalid configuration",
111: "Maximum rows must be an integer");
112: } catch (FileNotFoundException ex) {
113: Dialogs.showError(
114: "Unexpected 'File not found' problem", ex
115: .getMessage());
116: new ExceptionDialog(ex);
117: } catch (IOException ex) {
118: Dialogs.showError("Unexpected I/O problem", ex
119: .getMessage());
120: new ExceptionDialog(ex);
121: }
122: } else if (action.equals("cancel")) {
123: configView.closeDialog();
124: connectionView.closeDialog();
125: configView.deleteObserver(this );
126: configView = null;
127: instance = null;
128:
129: Enumeration i = testViewList.elements();
130:
131: while (i.hasMoreElements()) {
132: ((TestView) i.nextElement()).closeDialog();
133: }
134: } else if (action.equals("textfont")) {
135: Font font = FontChooser.chooseFont(null, configModel
136: .getTextFont());
137:
138: if (font != null) {
139: configModel.setTextFont(font);
140: configView.setTextFont(font);
141: }
142: } else if (action.equals("tablefont")) {
143: Font font = FontChooser.chooseFont(null, configModel
144: .getTableFont());
145:
146: if (font != null)
147: configModel.setTableFont(font);
148: } else if (action.equals("new")) {
149: newConnection();
150: } else if (action.equals("edit")) {
151: editConnection(configView.getSelectedConnection());
152: } else if (action.equals("delete")) {
153: deleteConnection(configView.getSelectedConnection());
154: }
155: }
156:
157: private void handleConnectionEvent(ViewEvent event) {
158: String action = event.getArg1();
159:
160: if (action.equals("ok")) {
161: String originalName = connectionView.getOriginalName();
162: String newName = connectionView.getName();
163: String url = connectionView.getUrl();
164: String driverClass = connectionView.getDriverClass();
165: String userName = connectionView.getUserName();
166: String password = connectionView.getPassword();
167:
168: try {
169: if (originalName == null) {
170: configModel.addConnection(newName, url,
171: driverClass, userName, password);
172: } else {
173: configModel.amendConnection(originalName, newName,
174: url, driverClass, userName, password);
175: }
176:
177: connectionView.setVisible(false);
178: } catch (ConfigException ex) {
179: Dialogs.showError("Invalid configuration", ex
180: .getMessage());
181: }
182: } else if (action.equals("cancel")) {
183: connectionView.setVisible(false);
184: } else if (action.equals("test")) {
185: String url = connectionView.getUrl();
186: String driverClass = connectionView.getDriverClass();
187: String userName = connectionView.getUserName();
188: String password = connectionView.getPassword();
189: TestView testView = new TestView(url, driverClass,
190: userName, password);
191: testView.addObserver(this );
192: testView.startTest();
193: testViewList.add(testView);
194: }
195: }
196:
197: private void handleTestEvent(ViewEvent event) {
198: String action = event.getArg1();
199:
200: if (action.equals("dismiss")) {
201: TestView view = (TestView) event.getInitiator();
202: view.closeDialog();
203: }
204: }
205:
206: private void editConnection(String name) {
207: if (!connectionView.isVisible()) {
208: connectionView.setOriginalName(name);
209: connectionView.setName(name);
210: connectionView.setDriverClass(configModel
211: .getConnectionDriverClass(name));
212: connectionView.setUrl(configModel.getConnectionUrl(name));
213: connectionView.setUserName(configModel
214: .getConnectionUserName(name));
215: connectionView.setPassword(configModel
216: .getConnectionPassword(name));
217: }
218:
219: connectionView.setVisible(true);
220: }
221:
222: private void newConnection() {
223: if (!connectionView.isVisible()) {
224: connectionView.setOriginalName(null);
225: }
226:
227: connectionView.setVisible(true);
228: }
229:
230: private void deleteConnection(String name) {
231: configModel.removeConnection(name);
232: }
233: }
|