KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > swing > spreadsheet > WebStart


1 // ============================================================================
2
// $Id: WebStart.java,v 1.5 2005/08/02 23:45:21 davidahall Exp $
3
// Copyright (c) 2004-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.swing.spreadsheet;
34
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.PipedInputStream JavaDoc;
39 import java.io.PipedOutputStream JavaDoc;
40 import java.net.URL JavaDoc;
41 import javax.jnlp.FileContents;
42 import javax.jnlp.FileOpenService;
43 import javax.jnlp.FileSaveService;
44 import javax.jnlp.ServiceManager;
45 import javax.jnlp.UnavailableServiceException;
46 import javax.swing.SwingUtilities JavaDoc;
47 import javax.swing.UIManager JavaDoc;
48
49 /**
50  * An JNLP-based application wrapper for Spreadsheet, providing a main method to
51  * allow for standalone use. This version supplies input and output stream
52  * services based on the WebStart API.
53  * <p>
54  * Copyright &copy; 2004-2005 David A. Hall
55  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
56  */

57
58 public class WebStart extends Application {
59
60     private FileContents _fc;
61
62     // ----------------------
63
// Spreadsheet IO Methods
64
// ----------------------
65

66     /**
67      * Loads the file via the JNLP FileOpen service
68      */

69     public int loadFile(Spreadsheet sheet) {
70         final Controller controller = getController();
71         try {
72             FileOpenService fos = (FileOpenService)
73                 ServiceManager.lookup("javax.jnlp.FileOpenService");
74             if (fos == null) {
75                 return Controller.CANCEL_OPTION;
76             }
77          
78             // ask user to select a file through this service
79
_fc = fos.openFileDialog(null, null);
80             if (_fc == null)
81                 return Controller.CANCEL_OPTION;
82             
83             sheet.readSpreadsheet(_fc.getInputStream());
84             controller.setSheetSource(new URL JavaDoc("file:///"+_fc.getName()));
85             controller.setSheetDirty(false);
86             return Controller.YES_OPTION;
87         }
88         catch (Exception JavaDoc x) {
89             displayError(x);
90             return Controller.CANCEL_OPTION;
91         }
92
93     }
94
95
96     /**
97      * Writes the file via the JNLP FileSave service
98      */

99     public int saveFile(Spreadsheet sheet, boolean promptForName) {
100         PipedInputStream JavaDoc pipeIn = null;
101         PipedOutputStream JavaDoc pipeOut = null;
102         try {
103             // Get a handle to the necessary service.
104
FileSaveService fss = (FileSaveService)
105                 ServiceManager.lookup("javax.jnlp.FileSaveService");
106             if (fss == null) {
107                 return Controller.CANCEL_OPTION;
108             }
109
110             // use a PipeInput/PipeOutput to store the file. One or the other should
111
// be used on a separate thread.
112
pipeIn = new PipedInputStream JavaDoc();
113             pipeOut = new PipedOutputStream JavaDoc(pipeIn);
114
115             // Start up a background thread to write the spreadsheet to a pipe
116
Thread JavaDoc writeThread = new Thread JavaDoc(new Writer(sheet, pipeOut));
117             writeThread.setDaemon(true);
118             writeThread.start();
119
120             //See if we currently have any idea where the spreadsheet came from.
121
Controller controller = getController();
122             String JavaDoc hint = getClue(_fc, controller.getSheetSource());
123
124             // Prompt the user for the file to write to. By the time the dialog has come
125
// up, the write thread may already be completed (if the document is smaller
126
// than the pipe buffer).
127
if (promptForName && _fc != null) {
128                 _fc = fss.saveAsFileDialog(hint, new String JavaDoc[]{ "hwks", }, _fc);
129             }
130             else {
131                 _fc = fss.saveFileDialog(hint, new String JavaDoc[]{ "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 JavaDoc("file:///"+_fc.getName()));
140             controller.setSheetDirty(false);
141             pipeIn.close();
142             return Controller.YES_OPTION;
143         }
144         catch (Exception JavaDoc x) {
145             displayError(x);
146             return Controller.CANCEL_OPTION;
147         }
148     }
149
150     /**
151      */

152
153     private String JavaDoc getClue(FileContents fc, URL JavaDoc source) {
154         if (fc != null)
155             try { return fc.getName(); }
156             catch (IOException JavaDoc x) {} // just fall through: failure _is_ an option in this case
157

158         if (source != null)
159             return source.getPath();
160
161         return "worksheet.hwks";
162     }
163
164     
165     /**
166      * Writes the spreadsheet contents
167      */

168     private class Writer implements Runnable JavaDoc {
169         private PipedOutputStream JavaDoc _os;
170         private Spreadsheet _sheet;
171         public Writer(Spreadsheet sheet, PipedOutputStream JavaDoc os) {
172             _os = os; _sheet = sheet;
173         }
174         public void run() {
175             try {
176                 _sheet.writeSpreadsheet(_os);
177             }
178             catch (Exception JavaDoc x) {
179                 displayError(x);
180             }
181             
182             try {
183                 _os.close();
184             }
185             catch (IOException JavaDoc x) {
186                 displayError(x);
187             }
188         }
189     }
190     
191
192     /**
193      * Displays an error message to the user, via whatever method the Controller
194      * has been configured with.
195      */

196     private synchronized void displayError(final Exception JavaDoc x) {
197         // make sure that we're on the event thread.
198
if (!SwingUtilities.isEventDispatchThread()) {
199             SwingUtilities.invokeLater(new Runnable JavaDoc() {
200                     public void run () {
201                         displayError(x);
202                     }
203                 });
204         }
205         else {
206             Controller controller = getController();
207             String JavaDoc 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     // ------------------------
217
// Standalone entry point
218
// ------------------------
219

220     static public void main(String JavaDoc[] args) {
221         printStartupHeader();
222
223         try {
224             UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
225         }
226         catch (Exception JavaDoc x) {
227             // TODO: log this instead of simply dumping it
228
System.err.println("Error loading L&F:" +x);
229         }
230      
231         // TODO: do command line processing
232

233         new WebStart();
234     }
235     
236
237     static private void printStartupHeader() {
238         System.out.println("");
239         System.out.println("/**");
240         System.out.println(" * A Java Hacker's Worksheet: JNLP Edition");
241         System.out.println(" * Copyright (c) 2005 David A. Hall");
242         System.out.println(" */");
243         System.out.println("");
244     }
245 }
246
Popular Tags