001: // ============================================================================
002: // $Id: WebStart.java,v 1.5 2005/08/02 23:45:21 davidahall Exp $
003: // Copyright (c) 2004-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
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.
031: // ============================================================================
032:
033: package net.sf.jga.swing.spreadsheet;
034:
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038: import java.io.PipedInputStream;
039: import java.io.PipedOutputStream;
040: import java.net.URL;
041: import javax.jnlp.FileContents;
042: import javax.jnlp.FileOpenService;
043: import javax.jnlp.FileSaveService;
044: import javax.jnlp.ServiceManager;
045: import javax.jnlp.UnavailableServiceException;
046: import javax.swing.SwingUtilities;
047: import javax.swing.UIManager;
048:
049: /**
050: * An JNLP-based application wrapper for Spreadsheet, providing a main method to
051: * allow for standalone use. This version supplies input and output stream
052: * services based on the WebStart API.
053: * <p>
054: * Copyright © 2004-2005 David A. Hall
055: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
056: */
057:
058: public class WebStart extends Application {
059:
060: private FileContents _fc;
061:
062: // ----------------------
063: // Spreadsheet IO Methods
064: // ----------------------
065:
066: /**
067: * Loads the file via the JNLP FileOpen service
068: */
069: public int loadFile(Spreadsheet sheet) {
070: final Controller controller = getController();
071: try {
072: FileOpenService fos = (FileOpenService) ServiceManager
073: .lookup("javax.jnlp.FileOpenService");
074: if (fos == null) {
075: return Controller.CANCEL_OPTION;
076: }
077:
078: // ask user to select a file through this service
079: _fc = fos.openFileDialog(null, null);
080: if (_fc == null)
081: return Controller.CANCEL_OPTION;
082:
083: sheet.readSpreadsheet(_fc.getInputStream());
084: controller.setSheetSource(new URL("file:///"
085: + _fc.getName()));
086: controller.setSheetDirty(false);
087: return Controller.YES_OPTION;
088: } catch (Exception x) {
089: displayError(x);
090: return Controller.CANCEL_OPTION;
091: }
092:
093: }
094:
095: /**
096: * Writes the file via the JNLP FileSave service
097: */
098: public int saveFile(Spreadsheet sheet, boolean promptForName) {
099: PipedInputStream pipeIn = null;
100: PipedOutputStream pipeOut = null;
101: try {
102: // Get a handle to the necessary service.
103: FileSaveService fss = (FileSaveService) ServiceManager
104: .lookup("javax.jnlp.FileSaveService");
105: if (fss == null) {
106: return Controller.CANCEL_OPTION;
107: }
108:
109: // use a PipeInput/PipeOutput to store the file. One or the other should
110: // be used on a separate thread.
111: pipeIn = new PipedInputStream();
112: pipeOut = new PipedOutputStream(pipeIn);
113:
114: // Start up a background thread to write the spreadsheet to a pipe
115: Thread writeThread = new Thread(new Writer(sheet, pipeOut));
116: writeThread.setDaemon(true);
117: writeThread.start();
118:
119: //See if we currently have any idea where the spreadsheet came from.
120: Controller controller = getController();
121: String hint = getClue(_fc, controller.getSheetSource());
122:
123: // Prompt the user for the file to write to. By the time the dialog has come
124: // up, the write thread may already be completed (if the document is smaller
125: // than the pipe buffer).
126: if (promptForName && _fc != null) {
127: _fc = fss.saveAsFileDialog(hint,
128: new String[] { "hwks", }, _fc);
129: } else {
130: _fc = fss.saveFileDialog(hint,
131: new String[] { "hwks", }, pipeIn, hint);
132: }
133:
134: if (_fc == null) {
135: return Controller.CANCEL_OPTION;
136: }
137:
138: // update the spreadsheet state associated with file i/o
139: controller.setSheetSource(new URL("file:///"
140: + _fc.getName()));
141: controller.setSheetDirty(false);
142: pipeIn.close();
143: return Controller.YES_OPTION;
144: } catch (Exception x) {
145: displayError(x);
146: return Controller.CANCEL_OPTION;
147: }
148: }
149:
150: /**
151: */
152:
153: private String getClue(FileContents fc, URL source) {
154: if (fc != null)
155: try {
156: return fc.getName();
157: } catch (IOException x) {
158: } // just fall through: failure _is_ an option in this case
159:
160: if (source != null)
161: return source.getPath();
162:
163: return "worksheet.hwks";
164: }
165:
166: /**
167: * Writes the spreadsheet contents
168: */
169: private class Writer implements Runnable {
170: private PipedOutputStream _os;
171: private Spreadsheet _sheet;
172:
173: public Writer(Spreadsheet sheet, PipedOutputStream os) {
174: _os = os;
175: _sheet = sheet;
176: }
177:
178: public void run() {
179: try {
180: _sheet.writeSpreadsheet(_os);
181: } catch (Exception x) {
182: displayError(x);
183: }
184:
185: try {
186: _os.close();
187: } catch (IOException x) {
188: displayError(x);
189: }
190: }
191: }
192:
193: /**
194: * Displays an error message to the user, via whatever method the Controller
195: * has been configured with.
196: */
197: private synchronized void displayError(final Exception x) {
198: // make sure that we're on the event thread.
199: if (!SwingUtilities.isEventDispatchThread()) {
200: SwingUtilities.invokeLater(new Runnable() {
201: public void run() {
202: displayError(x);
203: }
204: });
205: } else {
206: Controller controller = getController();
207: String msg = x.getMessage();
208: if (msg == null || msg.length() == 0)
209: msg = controller.getExceptionName(x);
210:
211: controller.notify(msg, controller.getExceptionName(x));
212: }
213: }
214:
215: // ------------------------
216: // Standalone entry point
217: // ------------------------
218:
219: static public void main(String[] args) {
220: printStartupHeader();
221:
222: try {
223: UIManager.setLookAndFeel(UIManager
224: .getCrossPlatformLookAndFeelClassName());
225: } catch (Exception x) {
226: // TODO: log this instead of simply dumping it
227: System.err.println("Error loading L&F:" + x);
228: }
229:
230: // TODO: do command line processing
231:
232: new WebStart();
233: }
234:
235: static private void printStartupHeader() {
236: System.out.println("");
237: System.out.println("/**");
238: System.out
239: .println(" * A Java Hacker's Worksheet: JNLP Edition");
240: System.out.println(" * Copyright (c) 2005 David A. Hall");
241: System.out.println(" */");
242: System.out.println("");
243: }
244: }
|