KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > document > XJDocument


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

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 JavaDoc;
45
46 public class XJDocument extends XJObject {
47
48     protected XJData documentData = null;
49     protected XJWindow documentWindow = null;
50     protected String JavaDoc documentTitle = XJLocalizable.getXJString("DocUntitled");
51     protected String JavaDoc documentPath = null;
52
53     protected List JavaDoc documentFileExts = null;
54     protected String JavaDoc 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     // *** Public methods
71

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 JavaDoc 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 JavaDoc getDocumentPath() {
145         return documentPath;
146     }
147
148     public String JavaDoc getDocumentName() {
149         return XJUtils.getLastPathComponent(documentPath);
150     }
151
152     public void setDocumentFileType(List JavaDoc ext, String JavaDoc description) {
153         documentFileExts = ext;
154         documentFileExtDescription = description;
155     }
156
157     public void observeValueForKey(Object JavaDoc sender, String JavaDoc key, Object JavaDoc object) {
158         if(!writing)
159             changeDone();
160     }
161
162     /** Automatically handle external modification detection
163      *
164      */

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 JavaDoc e) {
176             // ignore excepton
177
}
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     /** General methods
194      *
195      */

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 JavaDoc file) {
221         documentPath = file;
222         try {
223             readDocument(documentPath);
224         } catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc file) throws IOException, ClassNotFoundException JavaDoc {
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     // Subclasses only
400

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