1 19 package org.netbeans.api.java.source.support; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import javax.swing.JEditorPane ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.JTextComponent ; 34 import org.netbeans.editor.Registry; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataObject; 37 38 42 class OpenedEditors implements ChangeListener , PropertyChangeListener { 43 44 private List <JTextComponent > visibleEditors = new ArrayList <JTextComponent >(); 45 private Map <JTextComponent , FileObject> visibleEditors2Files = new HashMap <JTextComponent , FileObject>(); 46 private List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 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 l) { 63 listeners.add(l); 64 } 65 66 public synchronized void removeChangeListener(ChangeListener l) { 67 listeners.remove(l); 68 } 69 70 private void fireChangeEvent() { 71 ChangeEvent e = new ChangeEvent (this); 72 List <ChangeListener > listenersCopy = null; 73 74 synchronized (this) { 75 listenersCopy = new ArrayList (listeners); 76 } 77 78 for (ChangeListener l : listenersCopy) { 79 l.stateChanged(e); 80 } 81 } 82 83 public synchronized List <JTextComponent > getVisibleEditors() { 84 return Collections.unmodifiableList(visibleEditors); 85 } 86 87 public synchronized Collection <FileObject> getVisibleEditorsFiles() { 88 return Collections.unmodifiableCollection(visibleEditors2Files.values()); 89 } 90 91 public synchronized void stateChanged(ChangeEvent e) { 92 for (JTextComponent c : visibleEditors) { 93 c.removePropertyChangeListener(this); 94 visibleEditors2Files.remove(c); 95 } 96 97 visibleEditors.clear(); 98 99 JTextComponent editor = Registry.getMostActiveComponent(); 100 101 if (editor instanceof JEditorPane && "text/x-java".equals(((JEditorPane ) editor).getContentType())) { 102 visibleEditors.add(editor); 103 } 104 105 for (JTextComponent c : visibleEditors) { 106 c.addPropertyChangeListener(this); 107 visibleEditors2Files.put(c, getFileObject(c)); 108 } 109 110 fireChangeEvent(); 111 } 112 113 public synchronized void propertyChange(PropertyChangeEvent evt) { 114 JTextComponent c = (JTextComponent ) 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 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 |