KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > support > OpenedEditors


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 package org.netbeans.api.retouche.source.support;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.JEditorPane JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
34 import org.netbeans.editor.Registry;
35 import org.netbeans.modules.gsf.LanguageRegistry;
36 import org.openide.filesystems.FileObject;
37 import org.openide.loaders.DataObject;
38
39 /**
40  * This file is originally from Retouche, the Java Support
41  * infrastructure in NetBeans. I have modified the file as little
42  * as possible to make merging Retouche fixes back as simple as
43  * possible.
44  *
45  *
46  * @author Jan Lahoda
47  */

48 class OpenedEditors implements ChangeListener JavaDoc, PropertyChangeListener JavaDoc {
49
50     private List JavaDoc<JTextComponent JavaDoc> visibleEditors = new ArrayList JavaDoc<JTextComponent JavaDoc>();
51     private Map JavaDoc<JTextComponent JavaDoc, FileObject> visibleEditors2Files = new HashMap JavaDoc<JTextComponent JavaDoc, FileObject>();
52     private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
53
54     private static OpenedEditors DEFAULT;
55
56     private OpenedEditors() {
57         Registry.addChangeListener(this);
58     }
59
60     public static synchronized OpenedEditors getDefault() {
61         if (DEFAULT == null) {
62             DEFAULT = new OpenedEditors();
63         }
64
65         return DEFAULT;
66     }
67
68     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
69         listeners.add(l);
70     }
71
72     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
73         listeners.remove(l);
74     }
75
76     private void fireChangeEvent() {
77         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
78         List JavaDoc<ChangeListener JavaDoc> listenersCopy = null;
79
80         synchronized (this) {
81             listenersCopy = new ArrayList JavaDoc(listeners);
82         }
83
84         for (ChangeListener JavaDoc l : listenersCopy) {
85             l.stateChanged(e);
86         }
87     }
88
89     public synchronized List JavaDoc<JTextComponent JavaDoc> getVisibleEditors() {
90         return Collections.unmodifiableList(visibleEditors);
91     }
92
93     public synchronized Collection JavaDoc<FileObject> getVisibleEditorsFiles() {
94         return Collections.unmodifiableCollection(visibleEditors2Files.values());
95     }
96
97     public synchronized void stateChanged(ChangeEvent JavaDoc e) {
98         for (JTextComponent JavaDoc c : visibleEditors) {
99             c.removePropertyChangeListener(this);
100             visibleEditors2Files.remove(c);
101         }
102
103         visibleEditors.clear();
104
105         JTextComponent JavaDoc editor = Registry.getMostActiveComponent();
106
107         if (editor instanceof JEditorPane JavaDoc && LanguageRegistry.getInstance().isSupported((((JEditorPane JavaDoc) editor).getContentType()))) {
108             visibleEditors.add(editor);
109         }
110
111         for (JTextComponent JavaDoc c : visibleEditors) {
112             c.addPropertyChangeListener(this);
113             visibleEditors2Files.put(c, getFileObject(c));
114         }
115
116         fireChangeEvent();
117     }
118
119     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
120         JTextComponent JavaDoc c = (JTextComponent JavaDoc) evt.getSource();
121         FileObject originalFile = visibleEditors2Files.get(c);
122         FileObject nueFile = getFileObject(c);
123
124         if (originalFile != nueFile) {
125             visibleEditors2Files.put(c, nueFile);
126             fireChangeEvent();
127         }
128     }
129
130     static FileObject getFileObject(JTextComponent JavaDoc pane) {
131         DataObject file = (DataObject) pane.getDocument().getProperty(Document.StreamDescriptionProperty);
132         
133         if (file != null) {
134             return file.getPrimaryFile();
135         }
136
137         return null;
138     }
139
140 }
141
Popular Tags