1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.swing.Action ; 29 import javax.swing.SwingUtilities ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import javax.swing.text.Document ; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.ui.OpenProjects; 35 import org.netbeans.editor.AnnotationType; 36 import org.netbeans.editor.AnnotationTypes; 37 import org.netbeans.editor.BaseKit; 38 import org.netbeans.editor.Registry; 39 import org.netbeans.editor.Settings; 40 import org.netbeans.editor.SettingsNames; 41 import org.netbeans.editor.SettingsUtil; 42 import org.netbeans.lib.editor.bookmarks.actions.BookmarksKitInstallAction; 43 import org.netbeans.lib.editor.bookmarks.api.BookmarkList; 44 import org.openide.modules.ModuleInstall; 45 46 51 public class EditorBookmarksModule extends ModuleInstall { 52 53 private PropertyChangeListener openProjectsListener; 54 private static List listeners = new ArrayList (); private PropertyChangeListener annotationTypesListener; 56 private List lastOpenProjects; 57 58 public void restored () { 59 synchronized (Settings.class) { 60 SettingsUtil.updateListSetting(BaseKit.class, 61 SettingsNames.CUSTOM_ACTION_LIST, 62 new Object [] { BookmarksKitInstallAction.INSTANCE } 63 ); 64 SettingsUtil.updateListSetting(BaseKit.class, 65 SettingsNames.KIT_INSTALL_ACTION_NAME_LIST, 66 new Object [] { BookmarksKitInstallAction.INSTANCE.getValue(Action.NAME) } 67 ); 68 Settings.addInitializer(new BookmarksSettingsInitializer()); 69 } 70 71 openProjectsListener = new PropertyChangeListener () { 73 public void propertyChange(PropertyChangeEvent evt) { 74 List openProjects = Arrays.asList(OpenProjects.getDefault().getOpenProjects()); 75 lastOpenProjects.removeAll(openProjects); 77 for (Iterator it = lastOpenProjects.iterator(); it.hasNext();) { 78 Project prj = (Project)it.next(); 79 PersistentBookmarks.saveProjectBookmarks(prj); 80 } 81 lastOpenProjects = new ArrayList (openProjects); 82 } 83 }; 84 OpenProjects op = OpenProjects.getDefault(); 85 lastOpenProjects = new ArrayList (Arrays.asList(op.getOpenProjects())); 86 op.addPropertyChangeListener(openProjectsListener); 87 88 SwingUtilities.invokeLater(new Runnable (){ 89 public void run(){ 90 final Iterator it = Registry.getDocumentIterator(); 91 if (!it.hasNext()){ 92 return ; 93 } 94 95 AnnotationType type = AnnotationTypes.getTypes().getType(NbBookmarkImplementation.BOOKMARK_ANNOTATION_TYPE); 96 if (type == null){ 97 AnnotationTypes.getTypes().addPropertyChangeListener(annotationTypesListener = new PropertyChangeListener (){ 99 public void propertyChange(PropertyChangeEvent evt) { 100 AnnotationType type = AnnotationTypes.getTypes().getType(NbBookmarkImplementation.BOOKMARK_ANNOTATION_TYPE); 101 if (type != null){ 102 AnnotationTypes.getTypes().removePropertyChangeListener(annotationTypesListener); 103 while(it.hasNext()){ 104 Document doc = (Document )it.next(); 105 BookmarkList.get(doc); } 107 } 108 } 109 }); 110 } else { 111 while(it.hasNext()){ 112 Document doc = (Document )it.next(); 113 BookmarkList.get(doc); } 115 } 116 } 117 }); 118 } 119 120 123 public void close() { 124 finish(); 125 } 126 127 130 public void uninstalled() { 131 finish(); 132 } 133 134 private void finish() { 135 OpenProjects.getDefault().removePropertyChangeListener(openProjectsListener); 137 138 synchronized (Settings.class) { 139 List l = SettingsUtil.getClonedList(BaseKit.class, 140 SettingsNames.CUSTOM_ACTION_LIST); 141 } 142 143 Settings.removeInitializer(BookmarksSettingsInitializer.NAME); 144 Settings.reset(); 145 146 PersistentBookmarks.saveAllProjectBookmarks(); 148 149 fireChange(); 152 } 153 154 private static final class BookmarksSettingsInitializer extends Settings.AbstractInitializer { 155 156 static final String NAME = "bookmarks-settings-initializer"; 158 BookmarksSettingsInitializer() { 159 super(NAME); 160 } 161 162 public void updateSettingsMap(Class kitClass, java.util.Map settingsMap) { 163 if (kitClass == BaseKit.class) { 164 SettingsUtil.updateListSetting(settingsMap, 165 SettingsNames.CUSTOM_ACTION_LIST, 166 new Object [] { BookmarksKitInstallAction.INSTANCE } 167 ); 168 SettingsUtil.updateListSetting(settingsMap, 169 SettingsNames.KIT_INSTALL_ACTION_NAME_LIST, 170 new Object [] { BookmarksKitInstallAction.INSTANCE.getValue(Action.NAME) } 171 ); 172 } 173 } 174 175 } 176 177 public static synchronized void addChangeListener(ChangeListener l) { 178 listeners.add(l); 179 } 180 181 public static synchronized void removeChangeListener(ChangeListener l) { 182 listeners.remove(l); 183 } 184 185 private static void fireChange() { 186 ChangeEvent ev = new ChangeEvent (EditorBookmarksModule.class); 187 ChangeListener [] ls; 188 synchronized (EditorBookmarksModule.class) { 189 ls = (ChangeListener []) listeners.toArray(new ChangeListener [listeners.size()]); 190 } 191 for (int i = 0; i < ls.length; i++) { 192 ls[i].stateChanged(ev); 193 } 194 } 195 } 196 | Popular Tags |