KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > 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.java.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.openide.filesystems.FileObject;
36 import org.openide.loaders.DataObject;
37
38 /**
39  *
40  * @author Jan Lahoda
41  */

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