1 11 package org.eclipse.help.internal.base; 12 13 import java.util.ArrayList ; 14 import java.util.Observable ; 15 import java.util.StringTokenizer ; 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 30 public class BookmarkManager extends Observable implements 31 Preferences.IPropertyChangeListener { 32 public static final int REMOVE_ALL = 1; 34 35 public static final int ADD = 2; 37 38 public static final int REMOVE = 3; 40 41 public static final int CHANGE = 4; 43 44 public static final int WORLD_CHANGED = 5; 46 47 private boolean ignoreNotification; 48 49 private ArrayList bookmarks; 50 51 public static class Bookmark implements IHelpResource { 52 private String label; 53 54 private String href; 55 56 public Bookmark(String label, String href) { 57 this.label = label; 58 this.href = href; 59 } 60 61 public String getHref() { 62 return href; 63 } 64 65 public String getLabel() { 66 return label; 67 } 68 69 public boolean equals(Object 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 bookmarkURL, String title) { 112 if (bookmarkURL != null && bookmarkURL.length() > 0 113 && !bookmarkURL.equals("about:blank")) { if (title == null) { 115 return; 116 } 117 Preferences prefs = HelpBasePlugin.getDefault() 118 .getPluginPreferences(); 119 String bookmarks = prefs.getString(BaseHelpSystem.BOOKMARKS); 120 121 123 if (bookmarks.indexOf("," + encode(bookmarkURL) + "|") != -1) return; 126 bookmarks = bookmarks 127 + "," + encode(bookmarkURL) + "|" + encode(title); 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 bookmarkURL, String title) { 141 removeBookmark(new Bookmark(title, bookmarkURL)); 142 } 143 144 public void removeBookmark(Bookmark bookmark) { 145 String bookmarkURL = bookmark.getHref(); 146 String title = bookmark.getLabel(); 147 if (bookmarkURL != null && bookmarkURL.length() > 0 148 && !bookmarkURL.equals("about:blank")) { if (title == null) { 150 return; 151 } 152 Preferences prefs = HelpBasePlugin.getDefault() 153 .getPluginPreferences(); 154 String bookmarks = prefs.getString(BaseHelpSystem.BOOKMARKS); 155 String removeString = "," + encode(bookmarkURL) + "|" + encode(title); 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, ""); 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 value = prefs.getString(BaseHelpSystem.BOOKMARKS); 188 StringTokenizer tokenizer = new StringTokenizer (value, ","); bookmarks = new ArrayList (); 190 for (int i = 0; tokenizer.hasMoreTokens(); i++) { 191 String bookmark = tokenizer.nextToken(); 192 int separator = bookmark.indexOf('|'); 194 String label = decode(bookmark.substring(separator + 1)); 195 String href = separator < 0 ? "" : 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 209 private static String encode(String s) { 210 s = TString.change(s, "\\", "\\escape"); s = TString.change(s, ",", "\\comma"); return TString.change(s, "|", "\\pipe"); } 214 215 private static String decode(String s) { 216 s = TString.change(s, "\\pipe", "|"); s = TString.change(s, "\\comma", ","); return TString.change(s, "\\escape", "\\"); } 220 221 public void propertyChange(PropertyChangeEvent event) { 222 if (ignoreNotification) 223 return; 224 if (event.getProperty().equals(BaseHelpSystem.BOOKMARKS)) { 225 bookmarks=null; 229 setChanged(); 230 notifyObservers(new BookmarkEvent(WORLD_CHANGED, null)); 231 } 232 } 233 } 234 | Popular Tags |