KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > bookmarks > EditorBookmarksModule


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
20 package org.netbeans.modules.editor.bookmarks;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.text.Document JavaDoc;
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 /**
47  * Module installation class for editor.
48  *
49  * @author Miloslav Metelka
50  */

51 public class EditorBookmarksModule extends ModuleInstall {
52
53     private PropertyChangeListener JavaDoc openProjectsListener;
54     private static List JavaDoc listeners = new ArrayList JavaDoc(); // List<ChangeListener>
55
private PropertyChangeListener JavaDoc annotationTypesListener;
56     private List JavaDoc lastOpenProjects;
57
58     public void restored () {
59         synchronized (Settings.class) {
60             SettingsUtil.updateListSetting(BaseKit.class,
61                     SettingsNames.CUSTOM_ACTION_LIST,
62                     new Object JavaDoc[] { BookmarksKitInstallAction.INSTANCE }
63             );
64             SettingsUtil.updateListSetting(BaseKit.class,
65                     SettingsNames.KIT_INSTALL_ACTION_NAME_LIST,
66                     new Object JavaDoc[] { BookmarksKitInstallAction.INSTANCE.getValue(Action.NAME) }
67             );
68             Settings.addInitializer(new BookmarksSettingsInitializer());
69         }
70         
71         // Start listening on project closing
72
openProjectsListener = new PropertyChangeListener JavaDoc() {
73             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
74                 List JavaDoc openProjects = Arrays.asList(OpenProjects.getDefault().getOpenProjects());
75                 // lastOpenProjects will contain the just closed projects
76
lastOpenProjects.removeAll(openProjects);
77                 for (Iterator JavaDoc it = lastOpenProjects.iterator(); it.hasNext();) {
78                     Project prj = (Project)it.next();
79                     PersistentBookmarks.saveProjectBookmarks(prj);
80                 }
81                 lastOpenProjects = new ArrayList JavaDoc(openProjects);
82             }
83         };
84         OpenProjects op = OpenProjects.getDefault();
85         lastOpenProjects = new ArrayList JavaDoc(Arrays.asList(op.getOpenProjects()));
86         op.addPropertyChangeListener(openProjectsListener);
87         
88         SwingUtilities.invokeLater(new Runnable JavaDoc(){
89             public void run(){
90                 final Iterator JavaDoc 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                     // bookmark type was not added into AnnotationTypes yet, wait for event
98
AnnotationTypes.getTypes().addPropertyChangeListener(annotationTypesListener = new PropertyChangeListener JavaDoc(){
99                         public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc doc = (Document JavaDoc)it.next();
105                                     BookmarkList.get(doc); // Initialize the bookmark list
106
}
107                             }
108                         }
109                     });
110                 } else {
111                     while(it.hasNext()){
112                         Document JavaDoc doc = (Document JavaDoc)it.next();
113                         BookmarkList.get(doc); // Initialize the bookmark list
114
}
115                 }
116             }
117         });
118     }
119     
120     /**
121      * Called when all modules agreed with closing and the IDE will be closed.
122      */

123     public void close() {
124         finish();
125     }
126     
127     /**
128      * Called when module is uninstalled.
129      */

130     public void uninstalled() {
131         finish();
132     }
133     
134     private void finish() {
135         // Stop listening on projects closing
136
OpenProjects.getDefault().removePropertyChangeListener(openProjectsListener);
137         
138         synchronized (Settings.class) {
139             List JavaDoc l = SettingsUtil.getClonedList(BaseKit.class,
140                     SettingsNames.CUSTOM_ACTION_LIST);
141         }
142         
143         Settings.removeInitializer(BookmarksSettingsInitializer.NAME);
144         Settings.reset();
145         
146         // Save bookmarks for all opened projects with touched bookmarks
147
PersistentBookmarks.saveAllProjectBookmarks();
148         
149         // notify NbBookmarkManager that the module is uninstalled and BookmarkList can be removed
150
// from doc.clientProperty
151
fireChange();
152     }
153
154     private static final class BookmarksSettingsInitializer extends Settings.AbstractInitializer {
155         
156         static final String JavaDoc NAME = "bookmarks-settings-initializer"; // NOI18N
157

158         BookmarksSettingsInitializer() {
159             super(NAME);
160         }
161
162         public void updateSettingsMap(Class JavaDoc kitClass, java.util.Map JavaDoc settingsMap) {
163             if (kitClass == BaseKit.class) {
164                 SettingsUtil.updateListSetting(settingsMap,
165                         SettingsNames.CUSTOM_ACTION_LIST,
166                         new Object JavaDoc[] { BookmarksKitInstallAction.INSTANCE }
167                 );
168                 SettingsUtil.updateListSetting(settingsMap,
169                         SettingsNames.KIT_INSTALL_ACTION_NAME_LIST,
170                         new Object JavaDoc[] { BookmarksKitInstallAction.INSTANCE.getValue(Action.NAME) }
171                 );
172             }
173         }
174         
175     }
176     
177     public static synchronized void addChangeListener(ChangeListener JavaDoc l) {
178         listeners.add(l);
179     }
180     
181     public static synchronized void removeChangeListener(ChangeListener JavaDoc l) {
182         listeners.remove(l);
183     }
184     
185     private static void fireChange() {
186         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(EditorBookmarksModule.class);
187         ChangeListener JavaDoc[] ls;
188         synchronized (EditorBookmarksModule.class) {
189             ls = (ChangeListener JavaDoc[]) listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
190         }
191         for (int i = 0; i < ls.length; i++) {
192             ls[i].stateChanged(ev);
193         }
194     }
195 }
196
Popular Tags