1 19 package org.netbeans.api.retouche.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.netbeans.modules.gsf.LanguageRegistry; 36 import org.openide.filesystems.FileObject; 37 import org.openide.loaders.DataObject; 38 39 48 class OpenedEditors implements ChangeListener , PropertyChangeListener { 49 50 private List <JTextComponent > visibleEditors = new ArrayList <JTextComponent >(); 51 private Map <JTextComponent , FileObject> visibleEditors2Files = new HashMap <JTextComponent , FileObject>(); 52 private List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 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 l) { 69 listeners.add(l); 70 } 71 72 public synchronized void removeChangeListener(ChangeListener l) { 73 listeners.remove(l); 74 } 75 76 private void fireChangeEvent() { 77 ChangeEvent e = new ChangeEvent (this); 78 List <ChangeListener > listenersCopy = null; 79 80 synchronized (this) { 81 listenersCopy = new ArrayList (listeners); 82 } 83 84 for (ChangeListener l : listenersCopy) { 85 l.stateChanged(e); 86 } 87 } 88 89 public synchronized List <JTextComponent > getVisibleEditors() { 90 return Collections.unmodifiableList(visibleEditors); 91 } 92 93 public synchronized Collection <FileObject> getVisibleEditorsFiles() { 94 return Collections.unmodifiableCollection(visibleEditors2Files.values()); 95 } 96 97 public synchronized void stateChanged(ChangeEvent e) { 98 for (JTextComponent c : visibleEditors) { 99 c.removePropertyChangeListener(this); 100 visibleEditors2Files.remove(c); 101 } 102 103 visibleEditors.clear(); 104 105 JTextComponent editor = Registry.getMostActiveComponent(); 106 107 if (editor instanceof JEditorPane && LanguageRegistry.getInstance().isSupported((((JEditorPane ) editor).getContentType()))) { 108 visibleEditors.add(editor); 109 } 110 111 for (JTextComponent c : visibleEditors) { 112 c.addPropertyChangeListener(this); 113 visibleEditors2Files.put(c, getFileObject(c)); 114 } 115 116 fireChangeEvent(); 117 } 118 119 public synchronized void propertyChange(PropertyChangeEvent evt) { 120 JTextComponent c = (JTextComponent ) 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 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 |