001: /*
002: ** $Id: QueryController.java,v 1.12 2000/11/01 08:43:37 mrw Exp $
003: **
004: ** Controller for the query view. The view contains two main
005: ** components: a JTable to view the query results and a JTextArea to
006: ** edit the SQL query.
007: **
008: ** Mike Wilson, July 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.Component;
031: import java.io.File;
032: import java.sql.Connection;
033: import javax.swing.JFileChooser;
034: import uk.co.whisperingwind.framework.Controller;
035: import uk.co.whisperingwind.framework.Dialogs;
036: import uk.co.whisperingwind.framework.ModelEvent;
037: import uk.co.whisperingwind.framework.ViewEvent;
038:
039: class QueryController extends Controller {
040: private QueryView queryView = null;
041: private QueryModel queryModel = null;
042: private ExportView exportView = null;
043: private JFileChooser fileChooser = null;
044:
045: public QueryController(ConfigModel configModel) {
046: queryModel = new QueryModel(configModel);
047: queryModel.addObserver(this );
048: queryView = new QueryView(configModel, queryModel);
049: queryView.addObserver(this );
050: }
051:
052: public Component getWidget() {
053: return queryView.getContent();
054: }
055:
056: public void openFile(String fileName) {
057: queryModel.openFile(fileName);
058: }
059:
060: public boolean saveFile() {
061: return queryModel.saveFile();
062: }
063:
064: public boolean saveFileAs(String fileName) {
065: return queryModel.saveFileAs(fileName);
066: }
067:
068: public void execute(Connection connection) {
069: queryModel.execute(connection);
070: }
071:
072: public void cancelExecute() {
073: queryModel.cancelExecute();
074: }
075:
076: public void editAction(String action) {
077: queryView.editAction(action);
078: }
079:
080: public void focusText() {
081: queryView.focusText();
082: }
083:
084: public boolean hasFileName() {
085: return getFileName().length() > 0;
086: }
087:
088: public String getFileName() {
089: return queryModel.getFileName();
090: }
091:
092: /*
093: ** Returns true if the query model needs saving.
094: */
095:
096: public boolean isDirty() {
097: return queryModel.isDirty();
098: }
099:
100: /*
101: ** Set the view's status bar text.
102: */
103:
104: public void setStatus(String status) {
105: queryView.setStatus(status);
106: }
107:
108: public boolean isExecuting() {
109: return queryModel.isExecuting();
110: }
111:
112: public boolean hasSelection() {
113: return queryView.hasSelection();
114: }
115:
116: public boolean canExport() {
117: return queryModel.canExport();
118: }
119:
120: public void exportResult() {
121: if (exportView == null) {
122: exportView = new ExportView(null, false);
123: exportView.addObserver(this );
124: }
125:
126: exportView.setVisible(true);
127: }
128:
129: public void viewEvent(ViewEvent event) {
130: if (event.getInitiator() instanceof QueryView)
131: fireEvent(event.getArg1(), (String) event.getArg2());
132: else if (event.getInitiator() instanceof ExportView)
133: handleExportEvent(event);
134: }
135:
136: public void undoEdit() {
137: queryView.undoEdit();
138: }
139:
140: public void redoEdit() {
141: queryView.redoEdit();
142: }
143:
144: public boolean canUndo() {
145: return queryView.canUndo();
146: }
147:
148: public boolean canRedo() {
149: return queryView.canRedo();
150: }
151:
152: private void handleExportEvent(ViewEvent event) {
153: String action = event.getArg1();
154:
155: if (action.equals("ok")) {
156: String fileName = exportView.getFileName();
157: String delimiter = exportView.getDelimiter();
158: String quoteString = exportView.getQuote();
159: boolean wantQuotes = exportView.getWantQuotes();
160: boolean wantTitles = exportView.getWantTitles();
161:
162: int rows = queryModel.exportResult(fileName, delimiter,
163: quoteString, wantQuotes, wantTitles);
164:
165: if (rows > 0) {
166: exportView.closeDialog();
167: exportView = null;
168: Dialogs.showInformation("Exported", "Exported " + rows
169: + " rows");
170: }
171:
172: } else if (action.equals("cancel")) {
173: exportView.closeDialog();
174: exportView = null;
175: } else if (action.equals("lookup")) {
176: if (fileChooser == null)
177: fileChooser = new JFileChooser();
178: else
179: fileChooser.rescanCurrentDirectory();
180:
181: fileChooser.setSelectedFile(new File(exportView
182: .getFileName()));
183: int result = fileChooser.showSaveDialog(exportView
184: .getContent());
185:
186: if (result == JFileChooser.APPROVE_OPTION) {
187: File theFile = fileChooser.getSelectedFile();
188:
189: if (theFile != null)
190: exportView.setFileName(theFile.getPath());
191: }
192: }
193: }
194:
195: public void modelEvent(ModelEvent event) {
196: String field = event.getField();
197:
198: if (field.equals("updated")) {
199: fireEvent("updated", "");
200: } else if (field.equals("execute")) {
201: String value = (String) event.getValue();
202:
203: if (value.equals("end"))
204: queryView.setTableModel(queryModel);
205: }
206: }
207:
208: public void cleanUp() {
209: queryView.cleanUp();
210: queryModel.deleteObserver(this);
211: queryView.deleteObserver(this);
212: queryView = null;
213: fileChooser = null;
214: }
215: }
|