KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > BookmarksPart


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.ui.internal.views;
12
13 import java.util.Observable JavaDoc;
14 import java.util.Observer JavaDoc;
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 JavaDoc {
31     private Action deleteAction;
32
33     class BookmarksProvider implements ITreeContentProvider {
34         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
35             if (parentElement == BookmarksPart.this)
36                 return new Object JavaDoc[] { BaseHelpSystem.getBookmarkManager() };
37             if (parentElement instanceof BookmarkManager)
38                 return ((BookmarkManager) parentElement).getBookmarks();
39             return new Object JavaDoc[0];
40         }
41
42         /*
43          * (non-Javadoc)
44          *
45          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
46          */

47         public Object JavaDoc getParent(Object JavaDoc element) {
48             return null;
49         }
50
51         /*
52          * (non-Javadoc)
53          *
54          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
55          */

56         public boolean hasChildren(Object JavaDoc element) {
57             return getChildren(element).length > 0;
58         }
59
60         /*
61          * (non-Javadoc)
62          *
63          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
64          */

65         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
66             return getChildren(inputElement);
67         }
68
69         /*
70          * (non-Javadoc)
71          *
72          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
73          */

74         public void dispose() {
75         }
76
77         /*
78          * (non-Javadoc)
79          *
80          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
81          * java.lang.Object, java.lang.Object)
82          */

83         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
84         }
85     }
86
87     class BookmarksLabelProvider extends LabelProvider {
88         public String JavaDoc getText(Object JavaDoc 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 JavaDoc 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     /**
108      * @param parent
109      * @param toolkit
110      * @param style
111      */

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("") { //$NON-NLS-1$
128
public void run() {
129                 Object JavaDoc 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 JavaDoc 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("") { //$NON-NLS-1$
161
public void run() {
162                     BusyIndicator.showWhile(getControl().getDisplay(), new Runnable JavaDoc() {
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 JavaDoc obj = ssel.getFirstElement();
180         return obj instanceof BookmarkManager.Bookmark;
181     }
182
183     protected void doOpen(Object JavaDoc 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 JavaDoc o, final Object JavaDoc arg) {
194         treeViewer.getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
195             public void run() {
196                 asyncUpdate(o, arg);
197             }
198         });
199     }
200
201     private void asyncUpdate(Observable JavaDoc o, Object JavaDoc 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 JavaDoc 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