KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > image > ImageOpenSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.image;
22
23
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyVetoException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32
33 import org.openide.cookies.CloseCookie;
34 import org.openide.cookies.OpenCookie;
35 import org.openide.filesystems.FileChangeListener;
36 import org.openide.filesystems.FileChangeAdapter;
37 import org.openide.filesystems.FileEvent;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.MultiDataObject;
40 import org.openide.loaders.OpenSupport;
41 import org.openide.NotifyDescriptor;
42 import org.openide.DialogDisplayer;
43 import org.openide.windows.CloneableOpenSupport;
44 import org.openide.windows.CloneableTopComponent;
45 import org.openide.util.NbBundle;
46 import org.openide.util.RequestProcessor;
47 import org.openide.util.RequestProcessor.Task;
48
49
50 /**
51  * OpenSupport flavored with some <code>CloneableEditorSupport</code> features like
52  * listening on changes of image file and renames on dataobject,
53  * so it can work appropriate in Editor window.
54  *
55  * @author Peter Zavadsky
56  * @author Marian Petras
57  */

58
59 public class ImageOpenSupport extends OpenSupport implements OpenCookie, CloseCookie {
60
61     /** Saves last modified time. */
62     private long lastSaveTime;
63
64     /** Listens for changes on file. */
65     private FileChangeListener fileChangeL;
66
67     /** Reloading task. */
68     private Task reloadTask;
69     
70
71     /** Constructs ImageOpenSupportObject on given MultiDataObject.Entry. */
72     public ImageOpenSupport(MultiDataObject.Entry entry) {
73         super(entry, new Environment(entry.getDataObject())); // TEMP
74
}
75
76     
77     /** Creates the CloenableTOPComponent viewer of image. */
78     public CloneableTopComponent createCloneableTopComponent () {
79         prepareViewer();
80         return new ImageViewer((ImageDataObject)entry.getDataObject());
81     }
82
83     /** Set listener for changes on image file. */
84     void prepareViewer() {
85         // listen for changes on the image file
86
if(fileChangeL == null) {
87             fileChangeL = new FileChangeAdapter() {
88                 public void fileChanged(final FileEvent evt) {
89                     if(allEditors.isEmpty()) {
90                         return;
91                     }
92                     
93                     if(evt.getFile().isVirtual()) {
94                         entry.getFile().removeFileChangeListener(this);
95                         // File doesn't exist on disk - simulate env
96
// invalidation.
97
((Environment)ImageOpenSupport.this.env).fileRemoved();
98                         entry.getFile().addFileChangeListener(this);
99                         return;
100                     }
101                     
102                     if (evt.getTime() > lastSaveTime) {
103                         lastSaveTime = System.currentTimeMillis();
104                         
105                         // Post in new task.
106
if(reloadTask == null || reloadTask.isFinished()) {
107                         
108                             reloadTask = RequestProcessor.postRequest(
109                                 new Runnable JavaDoc() {
110                                     public void run() {
111                                         reload(evt);
112                                     }
113                                 }
114                             );
115                         }
116                     }
117                 }
118             };
119         }
120         entry.getFile().addFileChangeListener(fileChangeL);
121         lastSaveTime = System.currentTimeMillis();
122     }
123
124     /** Ask and reload/close image views. */
125     private void reload(FileEvent evt) {
126         // ask if reload?
127
// XXX the following is a resource path in NB 3.x and a URL after build system
128
// merge; better to produce something nicer (e.g. FileUtil.toFile):
129
String JavaDoc msg = NbBundle.getMessage(ImageOpenSupport.class, "MSG_ExternalChange", entry.getFile() );
130         NotifyDescriptor nd = new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.YES_NO_OPTION);
131         Object JavaDoc ret = DialogDisplayer.getDefault().notify(nd);
132
133         if (NotifyDescriptor.YES_OPTION.equals(ret)) {
134             // due to compiler 1.2 bug only
135
final ImageDataObject imageObj = (ImageDataObject)entry.getDataObject();
136             final CloneableTopComponent.Ref editors = allEditors;
137
138             Enumeration JavaDoc e = editors.getComponents();
139             while(e.hasMoreElements()) {
140                 final Object JavaDoc pane = e.nextElement();
141                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
142                     public void run() {
143                         ((ImageViewer)pane).updateView(imageObj);
144                     }
145                 });
146             }
147         }
148     }
149     
150     /** Environment for image open support. */
151     private static class Environment extends OpenSupport.Env {
152         /** generated Serialized Version UID */
153         static final long serialVersionUID = -1934890789745432254L;
154
155         /** Constructor. */
156         public Environment(DataObject dataObject) {
157             super(dataObject);
158         }
159         
160         
161         /** Overrides superclass method. Gets from OpenCookie. */
162         public CloneableOpenSupport findCloneableOpenSupport() {
163             return (CloneableOpenSupport)getDataObject().getCookie(OpenCookie.class);
164         }
165         
166         /** Called from enclosing support.
167          * The components are going to be closed anyway and in case of
168          * modified document its asked before if to save the change. */

169         private void fileRemoved() {
170             try {
171                 fireVetoableChange(PROP_VALID, Boolean.TRUE, Boolean.FALSE);
172             } catch(PropertyVetoException JavaDoc pve) {
173                 // Ignore.
174
}
175             
176             firePropertyChange(PROP_VALID, Boolean.TRUE, Boolean.FALSE);
177         }
178     } // End of nested Environment class.
179
}
180
181
Popular Tags