Source Code Cross Referenced for WebStart.java in  » Development » jga-Generic-Algorithms » net » sf » jga » swing » spreadsheet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jga Generic Algorithms » net.sf.jga.swing.spreadsheet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &copy; 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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.