1 33 package net.sf.jga.swing.spreadsheet; 34 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.io.PipedInputStream ; 39 import java.io.PipedOutputStream ; 40 import java.net.URL ; 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 ; 47 import javax.swing.UIManager ; 48 49 57 58 public class WebStart extends Application { 59 60 private FileContents _fc; 61 62 66 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 _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 ("file:///"+_fc.getName())); 85 controller.setSheetDirty(false); 86 return Controller.YES_OPTION; 87 } 88 catch (Exception x) { 89 displayError(x); 90 return Controller.CANCEL_OPTION; 91 } 92 93 } 94 95 96 99 public int saveFile(Spreadsheet sheet, boolean promptForName) { 100 PipedInputStream pipeIn = null; 101 PipedOutputStream pipeOut = null; 102 try { 103 FileSaveService fss = (FileSaveService) 105 ServiceManager.lookup("javax.jnlp.FileSaveService"); 106 if (fss == null) { 107 return Controller.CANCEL_OPTION; 108 } 109 110 pipeIn = new PipedInputStream (); 113 pipeOut = new PipedOutputStream (pipeIn); 114 115 Thread writeThread = new Thread (new Writer(sheet, pipeOut)); 117 writeThread.setDaemon(true); 118 writeThread.start(); 119 120 Controller controller = getController(); 122 String hint = getClue(_fc, controller.getSheetSource()); 123 124 if (promptForName && _fc != null) { 128 _fc = fss.saveAsFileDialog(hint, new String []{ "hwks", }, _fc); 129 } 130 else { 131 _fc = fss.saveFileDialog(hint, new String []{ "hwks", }, pipeIn, hint); 132 } 133 134 if (_fc == null) { 135 return Controller.CANCEL_OPTION; 136 } 137 138 controller.setSheetSource(new URL ("file:///"+_fc.getName())); 140 controller.setSheetDirty(false); 141 pipeIn.close(); 142 return Controller.YES_OPTION; 143 } 144 catch (Exception x) { 145 displayError(x); 146 return Controller.CANCEL_OPTION; 147 } 148 } 149 150 152 153 private String getClue(FileContents fc, URL source) { 154 if (fc != null) 155 try { return fc.getName(); } 156 catch (IOException x) {} 158 if (source != null) 159 return source.getPath(); 160 161 return "worksheet.hwks"; 162 } 163 164 165 168 private class Writer implements Runnable { 169 private PipedOutputStream _os; 170 private Spreadsheet _sheet; 171 public Writer(Spreadsheet sheet, PipedOutputStream os) { 172 _os = os; _sheet = sheet; 173 } 174 public void run() { 175 try { 176 _sheet.writeSpreadsheet(_os); 177 } 178 catch (Exception x) { 179 displayError(x); 180 } 181 182 try { 183 _os.close(); 184 } 185 catch (IOException x) { 186 displayError(x); 187 } 188 } 189 } 190 191 192 196 private synchronized void displayError(final Exception x) { 197 if (!SwingUtilities.isEventDispatchThread()) { 199 SwingUtilities.invokeLater(new Runnable () { 200 public void run () { 201 displayError(x); 202 } 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 220 static public void main(String [] args) { 221 printStartupHeader(); 222 223 try { 224 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 225 } 226 catch (Exception x) { 227 System.err.println("Error loading L&F:" +x); 229 } 230 231 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 |