1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Map ; 25 import java.util.WeakHashMap ; 26 import javax.swing.text.Document ; 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 43 44 class DocumentUnmodifiedListener implements PropertyChangeListener { 45 46 private static final Map eco2listener = new WeakHashMap (); 47 48 private static final DocumentUnmodifiedListener INSTANCE = new DocumentUnmodifiedListener(); 49 50 54 public static void init(Document 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) { eco.addPropertyChangeListener(INSTANCE); 66 eco2listener.put(eco, INSTANCE); } 68 } 69 } 70 71 private DocumentUnmodifiedListener() { 72 } 74 75 public void propertyChange(PropertyChangeEvent evt) { 76 if ("modified".equals(evt.getPropertyName())) { if (!(Boolean.TRUE.equals(evt.getNewValue()))) { 78 EditorCookie.Observable eco = (EditorCookie.Observable)evt.getSource(); 79 Document doc = eco.getDocument(); 80 if (doc != null) { 81 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 |