1 11 package org.eclipse.help.ui.internal.views; 12 13 import java.util.Observable ; 14 import java.util.Observer ; 15 16 import org.eclipse.help.IHelpResource; 17 import org.eclipse.help.internal.base.*; 18 import org.eclipse.help.ui.internal.*; 19 import org.eclipse.jface.action.*; 20 import org.eclipse.jface.action.IToolBarManager; 21 import org.eclipse.jface.viewers.*; 22 import org.eclipse.swt.custom.BusyIndicator; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.ui.IMemento; 26 import org.eclipse.ui.actions.ActionFactory; 27 import org.eclipse.ui.forms.widgets.FormToolkit; 28 29 public class BookmarksPart extends HyperlinkTreePart implements IHelpPart, 30 Observer { 31 private Action deleteAction; 32 33 class BookmarksProvider implements ITreeContentProvider { 34 public Object [] getChildren(Object parentElement) { 35 if (parentElement == BookmarksPart.this) 36 return new Object [] { BaseHelpSystem.getBookmarkManager() }; 37 if (parentElement instanceof BookmarkManager) 38 return ((BookmarkManager) parentElement).getBookmarks(); 39 return new Object [0]; 40 } 41 42 47 public Object getParent(Object element) { 48 return null; 49 } 50 51 56 public boolean hasChildren(Object element) { 57 return getChildren(element).length > 0; 58 } 59 60 65 public Object [] getElements(Object inputElement) { 66 return getChildren(inputElement); 67 } 68 69 74 public void dispose() { 75 } 76 77 83 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 84 } 85 } 86 87 class BookmarksLabelProvider extends LabelProvider { 88 public String getText(Object obj) { 89 if (obj instanceof BookmarkManager) 90 return Messages.BookmarksPart_savedTopics; 91 if (obj instanceof IHelpResource) 92 return ((IHelpResource) obj).getLabel(); 93 return super.getText(obj); 94 } 95 96 public Image getImage(Object obj) { 97 if (obj instanceof BookmarkManager) 98 return HelpUIResources 99 .getImage(IHelpUIConstants.IMAGE_BOOKMARKS); 100 if (obj instanceof IHelpResource) 101 return HelpUIResources 102 .getImage(IHelpUIConstants.IMAGE_BOOKMARK); 103 return super.getImage(obj); 104 } 105 } 106 107 112 public BookmarksPart(Composite parent, final FormToolkit toolkit, 113 IToolBarManager tbm) { 114 super(parent, toolkit, tbm); 115 BaseHelpSystem.getBookmarkManager().addObserver(this); 116 } 117 118 public void dispose() { 119 BaseHelpSystem.getBookmarkManager().deleteObserver(this); 120 super.dispose(); 121 } 122 123 protected void configureTreeViewer() { 124 treeViewer.setContentProvider(new BookmarksProvider()); 125 treeViewer.setLabelProvider(new BookmarksLabelProvider()); 126 treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); 127 deleteAction = new Action("") { public void run() { 129 Object obj = ((IStructuredSelection)treeViewer.getSelection()).getFirstElement(); 130 if (obj instanceof BookmarkManager.Bookmark) { 131 BookmarkManager.Bookmark b = (BookmarkManager.Bookmark)obj; 132 BaseHelpSystem.getBookmarkManager().removeBookmark(b); 133 } 134 } 135 }; 136 deleteAction.setText(Messages.BookmarksPart_delete); 137 deleteAction.setEnabled(false); 138 } 139 140 protected void handleSelectionChanged(IStructuredSelection sel) { 141 Object obj = sel.getFirstElement(); 142 deleteAction.setEnabled(obj!=null && obj instanceof BookmarkManager.Bookmark); 143 super.handleSelectionChanged(sel); 144 } 145 146 public boolean fillContextMenu(IMenuManager manager) { 147 boolean value = super.fillContextMenu(manager); 148 ISelection selection = treeViewer.getSelection(); 149 boolean canDeleteAll=false; 150 int count = BaseHelpSystem.getBookmarkManager().getBookmarks().length; 151 canDeleteAll = count>0; 152 153 if (canDelete((IStructuredSelection) selection)) { 154 if (value) 155 manager.add(new Separator()); 156 manager.add(deleteAction); 157 value=true; 158 } 159 if (canDeleteAll) { 160 Action action = new Action("") { public void run() { 162 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable () { 163 public void run() { 164 BaseHelpSystem.getBookmarkManager().removeAllBookmarks(); 165 } 166 }); 167 } 168 }; 169 action.setText(Messages.BookmarksPart_deleteAll); 170 manager.add(action); 171 value=true; 172 } 173 if (value==true) 174 manager.add(new Separator()); 175 return value; 176 } 177 178 private boolean canDelete(IStructuredSelection ssel) { 179 Object obj = ssel.getFirstElement(); 180 return obj instanceof BookmarkManager.Bookmark; 181 } 182 183 protected void doOpen(Object obj) { 184 if (obj instanceof BookmarkManager) { 185 treeViewer.setExpandedState(obj, !treeViewer.getExpandedState(obj)); 186 } else if (obj instanceof IHelpResource) { 187 IHelpResource res = (IHelpResource) obj; 188 if (res.getHref() != null) 189 parent.showURL(res.getHref()); 190 } 191 } 192 193 public void update(final Observable o, final Object arg) { 194 treeViewer.getControl().getDisplay().asyncExec(new Runnable () { 195 public void run() { 196 asyncUpdate(o, arg); 197 } 198 }); 199 } 200 201 private void asyncUpdate(Observable o, Object arg) { 202 BookmarkManager.BookmarkEvent event = (BookmarkManager.BookmarkEvent) arg; 203 switch (event.getType()) { 204 case BookmarkManager.ADD: 205 treeViewer.add(BaseHelpSystem.getBookmarkManager(), event 206 .getBookmark()); 207 break; 208 case BookmarkManager.REMOVE: 209 treeViewer.remove(event.getBookmark()); 210 break; 211 case BookmarkManager.REMOVE_ALL: 212 case BookmarkManager.WORLD_CHANGED: 213 treeViewer.refresh(); 214 break; 215 } 216 } 217 public IAction getGlobalAction(String id) { 218 if (id.equals(ActionFactory.DELETE.getId())) 219 return deleteAction; 220 return super.getGlobalAction(id); 221 } 222 223 protected boolean canAddBookmarks() { 224 return false; 225 } 226 227 public void toggleRoleFilter() { 228 } 229 230 public void refilter() { 231 } 232 233 public void saveState(IMemento memento) { 234 } 235 } 236 | Popular Tags |