00001
00056 package com.arcle.rmt.j2se.swing;
00057
00058
00059 import javax.swing.filechooser.FileFilter;
00060 import java.util.Collection;
00061 import java.util.Arrays;
00062 import java.io.File;
00063 import java.io.PrintWriter;
00064 import java.io.StringWriter;
00065
00066 import com.arcle.rmt.j2se.swing.ui.ExampleFileFilter;
00067 import com.arcle.rmt.j2se.swing.ui.MoreSwingUtilities;
00068 import com.arcle.rmt.j2se.swing.vc.AbstractDesktop;
00069 import com.arcle.rmt.j2se.swing.vc.Desktop;
00070 import com.arcle.rmt.j2se.model.RQMLDocument;
00071 import com.arcle.rmt.j2se.model.RQMLDocumentFacade;
00072 import com.arcle.rmt.j2se.model.RQMLDocumentFacadeException;
00073 import com.arcle.rmt.j2se.util.ExceptionWrapper;
00074 import com.arcle.rmt.j2se.framework.Document;
00075 import com.arcle.rmt.j2se.bridge.BridgeFacade;
00076
00081 public class Main extends AbstractMainWindow {
00082 public Main() {
00083 }
00084
00085
00086
00087
00088 protected void doNewDocument() {
00089 try {
00090 RQMLDocument impl = RQMLDocumentFacade.getInstance()
00091 .createRQMLDocument();
00092 setDocument(impl);
00093 setDocumentFile(null);
00094 setDirty(false);
00095 } catch(RQMLDocumentFacadeException e) {
00096 showException(e);
00097 }
00098 }
00099
00100
00101
00102
00103
00107 protected FileHandler getDefaultFileHandler() {
00108 if (_defaultHandler == null) {
00109 _defaultHandler = new RQMLFileHandler();
00110 }
00111 return _defaultHandler;
00112 }
00113
00114
00115
00116
00117
00118
00122 protected AbstractDesktop createDesktop() {
00123 AbstractDesktop d = new Desktop();
00124 return d;
00125 }
00126
00127 protected Collection createFileHandlers() {
00128 return Arrays.asList(new FileHandler[] {
00129 getDefaultFileHandler(),
00130 new SwFileHandler()
00131 });
00132 }
00133
00134
00135
00136
00137
00138
00142 private FileHandler _defaultHandler = null;
00143
00144
00145
00146
00150 protected class RQMLFileHandler extends FileHandler {
00151 public FileFilter getFileFilter() {
00152 if (_fileFilter == null) {
00153 _fileFilter = new ExampleFileFilter(new String[]
00154 {DEFAULT_EXTENSION, "xml"}, "RQML Documents");
00155 }
00156 return _fileFilter;
00157 }
00158
00159 public Document openFile(File f, StringBuffer log)
00160 throws ExceptionWrapper {
00161 RQMLDocument doc = RQMLDocumentFacade.getInstance()
00162 .openDocument(f);
00163 return doc;
00164 }
00165
00166
00167 public void saveFile(Document doc, File f, StringBuffer log)
00168 throws ExceptionWrapper {
00169 RQMLDocumentFacade.getInstance().saveDocument(
00170 (RQMLDocument)doc, f);
00171 }
00172
00173 public File filterFile(File inputFile) {
00174 return assignExtension(inputFile, DEFAULT_EXTENSION);
00175 }
00176
00180 private FileFilter _fileFilter;
00181
00182 private static final String DEFAULT_EXTENSION = "rqml";
00183 }
00184
00188 protected class SwFileHandler extends FileHandler {
00189 public FileFilter getFileFilter() {
00190 if (_fileFilter == null) {
00191 _fileFilter = new ExampleFileFilter(
00192 DEFAULT_EXTENSION,
00193 "Handheld Document");
00194 }
00195 return _fileFilter;
00196 }
00197
00198 public Document openFile(File f, StringBuffer log)
00199 throws ExceptionWrapper {
00200 StringWriter sw = new StringWriter();
00201 PrintWriter pw = new PrintWriter(sw);
00202
00203 Document doc = BridgeFacade.getInstance()
00204 .importSuperwabaDocument(f, pw);
00205
00206 if (sw.getBuffer().length() > 0) {
00207 log.append(sw.toString());
00208 }
00209 return doc;
00210 }
00211
00212
00213 public void saveFile(Document doc, File f, StringBuffer log)
00214 throws ExceptionWrapper {
00215 StringWriter sw = new StringWriter();
00216 PrintWriter pw = new PrintWriter(sw);
00217
00218 BridgeFacade.getInstance().exportSuperwabaDocument(
00219 (RQMLDocument) doc, f, pw);
00220
00221 if (sw.getBuffer().length() > 0) {
00222 log.append(sw.toString());
00223 }
00224 }
00225
00226 public File filterFile(File inputFile) {
00227 return assignExtension(inputFile, DEFAULT_EXTENSION);
00228 }
00229
00230
00234 private FileFilter _fileFilter;
00235
00236 private static final String DEFAULT_EXTENSION = "pdb";
00237 }
00238
00239
00240
00241
00242
00247 public static void main(String[] args) {
00248 try {
00249 final Main app = new Main();
00250 app.runAsApplication(args);
00251 } catch(Exception e) {
00252 MoreSwingUtilities.showException(null, e);
00253 }
00254 }
00255 }