| 1 31 32 package org.antlr.xjlib.appkit.document; 33 34 import org.antlr.xjlib.appkit.app.XJApplication; 35 import org.antlr.xjlib.appkit.frame.XJWindow; 36 import org.antlr.xjlib.appkit.utils.XJAlert; 37 import org.antlr.xjlib.appkit.utils.XJFileChooser; 38 import org.antlr.xjlib.appkit.utils.XJLocalizable; 39 import org.antlr.xjlib.foundation.XJObject; 40 import org.antlr.xjlib.foundation.XJUtils; 41 42 import java.awt.*; 43 import java.io.*; 44 import java.util.List ; 45 46 public class XJDocument extends XJObject { 47 48 protected XJData documentData = null; 49 protected XJWindow documentWindow = null; 50 protected String documentTitle = XJLocalizable.getXJString("DocUntitled"); 51 protected String documentPath = null; 52 53 protected List documentFileExts = null; 54 protected String documentFileExtDescription = null; 55 56 protected boolean dirty = false; 57 protected boolean firstDocument = false; 58 protected boolean writing = false; 59 60 protected Component javaContainer = null; 61 62 protected static int absoluteCounter = 0; 63 64 public XJDocument() { 65 XJApplication.shared().addDocument(this); 66 this.firstDocument = absoluteCounter == 0; 67 absoluteCounter++; 68 } 69 70 72 public boolean isFirstDocument() { 73 return firstDocument; 74 } 75 76 public void setDirty(boolean dirty) { 77 this.dirty = dirty; 78 } 79 80 public boolean isDirty() { 81 return dirty; 82 } 83 84 public void changeDone() { 85 if(dirty) 86 return; 87 88 dirty = true; 89 if(documentWindow != null) 90 documentWindow.setDirty(); 91 } 92 93 public void changeReset() { 94 dirty = false; 95 if(documentWindow != null) 96 documentWindow.resetDirty(); 97 } 98 99 public void setTitle(String title) { 100 documentTitle = title; 101 if(documentWindow != null) 102 documentWindow.setTitle(documentTitle); 103 } 104 105 public void setWindow(XJWindow window) { 106 documentWindow = window; 107 if(documentWindow != null) { 108 documentWindow.setDocument(this); 109 documentWindow.setTitle(documentTitle); 110 } 111 } 112 113 public XJWindow getWindow() { 114 return documentWindow; 115 } 116 117 public void setJavaContainer(Component container) { 118 this.javaContainer = container; 119 } 120 121 public Component getJavaContainer() { 122 if(javaContainer == null) 123 return getWindow() == null ? null : getWindow().getJavaContainer(); 124 else 125 return javaContainer; 126 } 127 128 public void showWindow() { 129 if(documentWindow != null) 130 documentWindow.show(); 131 } 132 133 public void setDocumentData(XJData data) { 134 this.documentData = data; 135 if(documentData != null) { 136 documentData.addObserver(this); 137 } 138 } 139 140 public XJData getDocumentData() { 141 return documentData; 142 } 143 144 public String getDocumentPath() { 145 return documentPath; 146 } 147 148 public String getDocumentName() { 149 return XJUtils.getLastPathComponent(documentPath); 150 } 151 152 public void setDocumentFileType(List ext, String description) { 153 documentFileExts = ext; 154 documentFileExtDescription = description; 155 } 156 157 public void observeValueForKey(Object sender, String key, Object object) { 158 if(!writing) 159 changeDone(); 160 } 161 162 165 166 protected long lastModifiedOnDisk = 0; 167 168 public long getDateOfModificationOnDisk() { 169 if(getDocumentPath() == null) 170 return 0; 171 172 File f = null; 173 try { 174 f = new File(getDocumentPath()); 175 } catch(Exception e) { 176 } 178 179 if(f == null) 180 return 0; 181 else 182 return f.lastModified(); 183 } 184 185 public void synchronizeLastModifiedDate() { 186 lastModifiedOnDisk = getDateOfModificationOnDisk(); 187 } 188 189 public boolean isModifiedOnDisk() { 190 return lastModifiedOnDisk != getDateOfModificationOnDisk(); 191 } 192 193 196 197 private boolean performLoad_() { 198 if(!dirty) 199 return XJApplication.YES; 200 201 202 int r = XJAlert.displayAlertYESNOCANCEL(getJavaContainer(), 203 XJLocalizable.getXJString("DocLoad"), 204 XJLocalizable.getStringFormat("DocSaveChanges", 205 documentTitle)); 206 switch(r) { 207 case XJAlert.YES: 208 return performSave(false); 209 210 case XJAlert.NO: 211 return XJApplication.YES; 212 213 case XJAlert.CANCEL: 214 return XJApplication.NO; 215 } 216 217 return XJApplication.YES; 218 } 219 220 public boolean performLoad(String file) { 221 documentPath = file; 222 try { 223 readDocument(documentPath); 224 } catch(Exception e) { 225 e.printStackTrace(); 226 XJAlert.display(getJavaContainer(), XJLocalizable.getXJString("DocError"), XJLocalizable.getXJString("DocLoadError")+" "+e.toString()); 227 return XJApplication.NO; 228 } 229 230 setTitle(documentPath); 231 changeReset(); 232 233 return XJApplication.YES; 234 } 235 236 public boolean performLoad() { 237 if(!performLoad_()) 238 return XJApplication.NO; 239 240 if(!XJFileChooser.shared().displayOpenDialog(getJavaContainer(), documentFileExts, documentFileExtDescription, false)) 241 return XJApplication.NO; 242 243 String path = XJFileChooser.shared().getSelectedFilePath(); 244 XJDocument document = XJApplication.shared().getDocumentForPath(path); 245 if(document != null && document != this) { 246 XJAlert.display(getJavaContainer(), XJLocalizable.getXJString("DocError"), XJLocalizable.getXJString("DocLoadExists")); 247 return XJApplication.NO; 248 } else { 249 XJApplication.shared().addRecentFile(path); 250 return performLoad(XJFileChooser.shared().getSelectedFilePath()); 251 } 252 } 253 254 public boolean reload() { 255 try { 256 readDocument(documentPath); 257 } catch(Exception e) { 258 XJAlert.display(getJavaContainer(), XJLocalizable.getXJString("DocError"), XJLocalizable.getXJString("DocLoadError")+" "+e.toString()); 259 return XJApplication.NO; 260 } 261 return XJApplication.YES; 262 } 263 264 public boolean performAutoSave() { 265 return !(getDocumentPath() != null && isDirty()) || performSave(false); 266 } 267 268 public boolean performSave(boolean saveAs) { 269 if(documentPath == null || saveAs) { 270 if(!XJFileChooser.shared().displaySaveDialog(getJavaContainer(), documentFileExts, documentFileExtDescription, true)) 271 return XJApplication.NO; 272 273 documentPath = XJFileChooser.shared().getSelectedFilePath(); 274 XJApplication.shared().addRecentFile(documentPath); 275 } 276 277 try { 278 writeDocument(documentPath); 279 } catch(Exception e) { 280 e.printStackTrace(); 281 XJAlert.display(getJavaContainer(), XJLocalizable.getXJString("DocError"), XJLocalizable.getXJString("DocSaveError")+" "+e.toString()); 282 return XJApplication.NO; 283 } 284 285 setTitle(documentPath); 286 changeReset(); 287 return XJApplication.YES; 288 } 289 290 protected boolean performClose_() { 291 if(!dirty || !XJApplication.shared().supportsPersistence()) 292 return XJApplication.YES; 293 294 if(documentWindow != null) 295 documentWindow.bringToFront(); 296 297 int r = XJAlert.displayAlertYESNOCANCEL(getJavaContainer(), XJLocalizable.getXJString("DocCloseTitle"), XJLocalizable.getStringFormat("DocCloseMessage", documentTitle)); 298 switch(r) { 299 case XJAlert.YES: 300 return performSave(false); 301 302 case XJAlert.NO: 303 return XJApplication.YES; 304 305 case XJAlert.CANCEL: 306 return XJApplication.NO; 307 } 308 309 return XJApplication.YES; 310 } 311 312 public boolean performClose(boolean force) { 313 boolean r = force?XJApplication.YES:performClose_(); 314 if(r) { 315 XJApplication.shared().addRecentFile(getDocumentPath()); 316 XJApplication.shared().removeDocument(this); 317 if(documentWindow != null) 318 documentWindow.close(); 319 } 320 return r; 321 } 322 323 public boolean performClose() { 324 return performClose(false); 325 } 326 327 private void beginWrite() { 328 writing = true; 329 } 330 331 private void endWrite() { 332 writing = false; 333 } 334 335 private void writeDocument(String file) throws IOException { 336 beginWrite(); 337 try { 338 documentWillWriteData(); 339 documentData.setFile(file); 340 switch(documentData.dataType()) { 341 case XJData.DATA_INPUTSTREAM: { 342 OutputStream os = new FileOutputStream(file); 343 documentData.writeData(os); 344 os.close(); 345 break; 346 } 347 348 case XJData.DATA_OBJECTINPUTSTREAM: { 349 OutputStream os = new FileOutputStream(file); 350 documentData.writeData(new ObjectOutputStream(os)); 351 os.close(); 352 break; 353 } 354 355 case XJData.DATA_PLAINTEXT: 356 documentData.writeData(); 357 break; 358 359 case XJData.DATA_XML: 360 documentData.writeData(); 361 break; 362 } 363 synchronizeLastModifiedDate(); 364 } finally { 365 endWrite(); 366 } 367 } 368 369 private void readDocument(String file) throws IOException, ClassNotFoundException { 370 documentWillReadData(); 371 documentData.setFile(file); 372 switch(documentData.dataType()) { 373 case XJData.DATA_INPUTSTREAM: { 374 InputStream is = new FileInputStream(file); 375 documentData.readData(is); 376 is.close(); 377 break; 378 } 379 380 case XJData.DATA_OBJECTINPUTSTREAM: { 381 InputStream is = new FileInputStream(file); 382 documentData.readData(new ObjectInputStream(is)); 383 is.close(); 384 break; 385 } 386 387 case XJData.DATA_PLAINTEXT: 388 documentData.readData(); 389 break; 390 391 case XJData.DATA_XML: 392 documentData.readData(); 393 break; 394 } 395 documentDidReadData(); 396 synchronizeLastModifiedDate(); 397 } 398 399 401 public void documentWillWriteData() { 402 403 } 404 405 public void documentWillReadData() { 406 407 } 408 409 public void documentDidReadData() { 410 411 } 412 413 } 414 | Popular Tags |