KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.lib.editor.bookmarks.BookmarksApiPackageAccessor;
28 import org.netbeans.lib.editor.bookmarks.api.BookmarkList;
29 import org.netbeans.lib.editor.bookmarks.spi.BookmarkManager;
30 import org.netbeans.modules.editor.NbEditorUtilities;
31 import org.openide.cookies.EditorCookie;
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataObject;
34 import org.openide.loaders.DataObjectNotFoundException;
35
36 /**
37  * Listening on when the document becomes unmodified
38  * and notification to bookmark manager.
39  *
40  * @author Miloslav Metelka
41  * @version 1.00
42  */

43
44 class DocumentUnmodifiedListener implements PropertyChangeListener JavaDoc {
45     
46     private static final Map JavaDoc eco2listener = new WeakHashMap JavaDoc();
47     
48     private static final DocumentUnmodifiedListener INSTANCE = new DocumentUnmodifiedListener();
49     
50     /**
51      * Initialize listening for document unmodification
52      * for the given document.
53      */

54     public static void init(Document JavaDoc doc) {
55         FileObject fo = NbEditorUtilities.getFileObject(doc);
56         if (fo != null) {
57             EditorCookie.Observable eco;
58             try {
59                 DataObject dob = DataObject.find(fo);
60                 eco = (EditorCookie.Observable)dob.getCookie(EditorCookie.Observable.class);
61             } catch (DataObjectNotFoundException e) {
62                 eco = null;
63             }
64             if (eco != null && eco2listener.get(eco) == null) { // not listening yet
65
eco.addPropertyChangeListener(INSTANCE);
66                 eco2listener.put(eco, INSTANCE); // adding to weak hash map
67
}
68         }
69     }
70             
71     private DocumentUnmodifiedListener() {
72         // just a private singleton instance allowed
73
}
74     
75     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
76         if ("modified".equals(evt.getPropertyName())) { // become unmodified
77
if (!(Boolean.TRUE.equals(evt.getNewValue()))) {
78                 EditorCookie.Observable eco = (EditorCookie.Observable)evt.getSource();
79                 Document JavaDoc doc = eco.getDocument();
80                 if (doc != null) {
81                     // Document is being saved
82
BookmarkList bookmarkList = BookmarkList.get(doc);
83                     BookmarkManager manager = BookmarksApiPackageAccessor.get().getBookmarkManager(bookmarkList);
84                     manager.saveBookmarks();
85                 }
86             }
87         }
88     }
89     
90 }
91
92
Popular Tags