KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > BookmarkManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Observable JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
19 import org.eclipse.help.IHelpResource;
20 import org.eclipse.help.internal.base.util.TString;
21
22 /**
23  * Code for bookmark management has been moved here so that it can be shared
24  * between the web app and the help view. The manager implements Observable so
25  * that views can be notified on bookmark changes. The webapp does not use this
26  * feature.
27  *
28  * @since 3.1
29  */

30 public class BookmarkManager extends Observable JavaDoc implements
31         Preferences.IPropertyChangeListener {
32     // all bookmarks removed
33
public static final int REMOVE_ALL = 1;
34
35     // bookmark added
36
public static final int ADD = 2;
37
38     // bookmark removed
39
public static final int REMOVE = 3;
40
41     // bookmark changed
42
public static final int CHANGE = 4;
43
44     // everything changed (by the webapp)
45
public static final int WORLD_CHANGED = 5;
46
47     private boolean ignoreNotification;
48     
49     private ArrayList JavaDoc bookmarks;
50
51     public static class Bookmark implements IHelpResource {
52         private String JavaDoc label;
53
54         private String JavaDoc href;
55
56         public Bookmark(String JavaDoc label, String JavaDoc href) {
57             this.label = label;
58             this.href = href;
59         }
60
61         public String JavaDoc getHref() {
62             return href;
63         }
64
65         public String JavaDoc getLabel() {
66             return label;
67         }
68
69         public boolean equals(Object JavaDoc object) {
70             if (object == null)
71                 return false;
72             if (object == this)
73                 return true;
74             if (object instanceof Bookmark) {
75                 Bookmark b = (Bookmark) object;
76                 return b.href.equals(href) && b.label.equals(label);
77             }
78             return false;
79         }
80     }
81
82     public static class BookmarkEvent {
83         private int type;
84
85         private Bookmark bookmark;
86
87         public BookmarkEvent(int type, Bookmark bookmark) {
88             this.type = type;
89             this.bookmark = bookmark;
90         }
91
92         public int getType() {
93             return type;
94         }
95
96         public Bookmark getBookmark() {
97             return bookmark;
98         }
99     }
100
101     public BookmarkManager() {
102         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
103         prefs.addPropertyChangeListener(this);
104     }
105
106     public void close() {
107         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
108         prefs.removePropertyChangeListener(this);
109     }
110
111     public void addBookmark(String JavaDoc bookmarkURL, String JavaDoc title) {
112         if (bookmarkURL != null && bookmarkURL.length() > 0
113                 && !bookmarkURL.equals("about:blank")) { //$NON-NLS-1$
114
if (title == null) {
115                 return;
116             }
117             Preferences prefs = HelpBasePlugin.getDefault()
118                     .getPluginPreferences();
119             String JavaDoc bookmarks = prefs.getString(BaseHelpSystem.BOOKMARKS);
120
121             // separate the url and title by vertical bar
122

123             // check for duplicates
124
if (bookmarks.indexOf("," + encode(bookmarkURL) + "|") != -1) //$NON-NLS-1$ //$NON-NLS-2$
125
return;
126             bookmarks = bookmarks
127                     + "," + encode(bookmarkURL) + "|" + encode(title); //$NON-NLS-1$ //$NON-NLS-2$
128
ignoreNotification = true;
129             prefs.setValue(BaseHelpSystem.BOOKMARKS, bookmarks);
130             HelpBasePlugin.getDefault().savePluginPreferences();
131             Bookmark bookmark = new Bookmark(title, bookmarkURL);
132             if (this.bookmarks!=null)
133                 this.bookmarks.add(bookmark);
134             setChanged();
135             notifyObservers(new BookmarkEvent(ADD, bookmark));
136             ignoreNotification = false;
137         }
138     }
139
140     public void removeBookmark(String JavaDoc bookmarkURL, String JavaDoc title) {
141         removeBookmark(new Bookmark(title, bookmarkURL));
142     }
143
144     public void removeBookmark(Bookmark bookmark) {
145         String JavaDoc bookmarkURL = bookmark.getHref();
146         String JavaDoc title = bookmark.getLabel();
147         if (bookmarkURL != null && bookmarkURL.length() > 0
148                 && !bookmarkURL.equals("about:blank")) { //$NON-NLS-1$
149
if (title == null) {
150                 return;
151             }
152             Preferences prefs = HelpBasePlugin.getDefault()
153                     .getPluginPreferences();
154             String JavaDoc bookmarks = prefs.getString(BaseHelpSystem.BOOKMARKS);
155             String JavaDoc removeString = "," + encode(bookmarkURL) + "|" + encode(title); //$NON-NLS-1$ //$NON-NLS-2$
156
int i = bookmarks.indexOf(removeString);
157             if (i == -1)
158                 return;
159             bookmarks = bookmarks.substring(0, i)
160                     + bookmarks.substring(i + removeString.length());
161             ignoreNotification = true;
162             prefs.setValue(BaseHelpSystem.BOOKMARKS, bookmarks);
163             HelpBasePlugin.getDefault().savePluginPreferences();
164             if (this.bookmarks!=null)
165                 this.bookmarks.remove(bookmark);
166             setChanged();
167             notifyObservers(new BookmarkEvent(REMOVE, bookmark));
168             ignoreNotification = false;
169         }
170     }
171
172     public void removeAllBookmarks() {
173         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
174         ignoreNotification = true;
175         prefs.setValue(BaseHelpSystem.BOOKMARKS, ""); //$NON-NLS-1$
176
HelpBasePlugin.getDefault().savePluginPreferences();
177         if (bookmarks!=null)
178             bookmarks.clear();
179         setChanged();
180         notifyObservers(new BookmarkEvent(REMOVE_ALL, null));
181         ignoreNotification = false;
182     }
183
184     public IHelpResource[] getBookmarks() {
185         if (bookmarks==null) {
186             Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
187             String JavaDoc value = prefs.getString(BaseHelpSystem.BOOKMARKS);
188             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
189
bookmarks = new ArrayList JavaDoc();
190             for (int i = 0; tokenizer.hasMoreTokens(); i++) {
191                 String JavaDoc bookmark = tokenizer.nextToken();
192                 // url and title are separated by vertical bar
193
int separator = bookmark.indexOf('|');
194                 String JavaDoc label = decode(bookmark.substring(separator + 1));
195                 String JavaDoc href = separator < 0 ? "" //$NON-NLS-1$
196
: decode(bookmark.substring(0, separator));
197                 bookmarks.add(new Bookmark(label, href));
198             }
199         }
200         return (IHelpResource[])bookmarks.toArray(new IHelpResource[bookmarks.size()]);
201     }
202
203     /**
204      * Ensures that string does not contains ',' or '|' characters.
205      *
206      * @param s
207      * @return String
208      */

209     private static String JavaDoc encode(String JavaDoc s) {
210         s = TString.change(s, "\\", "\\escape"); //$NON-NLS-1$ //$NON-NLS-2$
211
s = TString.change(s, ",", "\\comma"); //$NON-NLS-1$ //$NON-NLS-2$
212
return TString.change(s, "|", "\\pipe"); //$NON-NLS-1$ //$NON-NLS-2$
213
}
214
215     private static String JavaDoc decode(String JavaDoc s) {
216         s = TString.change(s, "\\pipe", "|"); //$NON-NLS-1$ //$NON-NLS-2$
217
s = TString.change(s, "\\comma", ","); //$NON-NLS-1$ //$NON-NLS-2$
218
return TString.change(s, "\\escape", "\\"); //$NON-NLS-1$ //$NON-NLS-2$
219
}
220
221     public void propertyChange(PropertyChangeEvent event) {
222         if (ignoreNotification)
223             return;
224         if (event.getProperty().equals(BaseHelpSystem.BOOKMARKS)) {
225             // notify observers about the changes in the preferences
226
// made elsewhere (by the webapp)
227
// Also clear the cache.
228
bookmarks=null;
229             setChanged();
230             notifyObservers(new BookmarkEvent(WORLD_CHANGED, null));
231         }
232     }
233 }
234
Popular Tags