KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 Intel 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  * Intel Corporation - initial API and implementation
10  * IBM Corporation - 163558 Dynamic content support for all UA
11  *******************************************************************************/

12 package org.eclipse.help.ui.internal.views;
13
14 import org.eclipse.help.HelpSystem;
15 import org.eclipse.help.IHelpResource;
16 import org.eclipse.help.IIndexEntry;
17 import org.eclipse.help.UAContentFilter;
18 import org.eclipse.help.internal.base.HelpBasePlugin;
19 import org.eclipse.help.internal.base.HelpEvaluationContext;
20 import org.eclipse.help.ui.internal.IHelpUIConstants;
21 import org.eclipse.jface.action.IToolBarManager;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.jface.viewers.ViewerFilter;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Tree;
29 import org.eclipse.ui.IMemento;
30 import org.eclipse.ui.forms.widgets.FormToolkit;
31
32 public class IndexPart extends HyperlinkTreePart implements IHelpUIConstants {
33     private RoleFilter roleFilter;
34
35     class IndexProvider implements ITreeContentProvider {
36         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
37             if (parentElement == IndexPart.this) {
38                 return HelpSystem.getIndex().getEntries();
39             }
40             if (parentElement instanceof IIndexEntry) {
41                 return IndexPart.this.getChildren((IIndexEntry) parentElement);
42             }
43             return new Object JavaDoc[0];
44         }
45
46         public Object JavaDoc getParent(Object JavaDoc element) {
47             return null;
48         }
49
50         public boolean hasChildren(Object JavaDoc element) {
51             return getChildren(element).length > 0;
52         }
53
54         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
55             return getChildren(inputElement);
56         }
57
58         public void dispose() {}
59         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
60     }
61
62     class IndexLabelProvider extends LabelProvider {
63         public String JavaDoc getText(Object JavaDoc obj) {
64             if (obj instanceof IIndexEntry)
65                 return ((IIndexEntry) obj).getKeyword();
66             if (obj instanceof IHelpResource)
67                 return ((IHelpResource) obj).getLabel();
68             return super.getText(obj);
69         }
70
71         public Image getImage(Object JavaDoc obj) {
72             return super.getImage(obj);
73         }
74     }
75
76     class RoleFilter extends ViewerFilter {
77         public boolean select(Viewer viewer, Object JavaDoc parentElement,
78                 Object JavaDoc element) {
79             if (element instanceof IIndexEntry) {
80                 return isEnabled((IIndexEntry) element);
81             } else if (element instanceof IHelpResource) {
82                 return isEnabled((IHelpResource) element);
83             }
84             return false;
85         }
86
87         private boolean isEnabled(IIndexEntry entry) {
88             if (!UAContentFilter.isFiltered(entry, HelpEvaluationContext.getContext())) {
89                 IHelpResource[] topics = entry.getTopics();
90                 for (int i = 0; i < topics.length; i++) {
91                     if (isEnabled(topics[i]))
92                         return true;
93                 }
94                 IIndexEntry[] subentries = entry.getSubentries();
95                 for (int i = 0; i < subentries.length; i++) {
96                     if (isEnabled(subentries[i]))
97                         return true;
98                 }
99             }
100             return false;
101         }
102
103         private boolean isEnabled(Object JavaDoc obj) {
104             return !UAContentFilter.isFiltered(obj, HelpEvaluationContext.getContext());
105         }
106
107         private boolean isEnabled(IHelpResource topic) {
108             return isEnabled((Object JavaDoc)topic) && HelpBasePlugin.getActivitySupport().isEnabled(topic.getHref());
109         }
110     }
111
112     public IndexPart(Composite parent, FormToolkit toolkit, IToolBarManager tbm) {
113         super(parent, toolkit, tbm);
114         roleFilter = new RoleFilter();
115     }
116
117     protected void configureTreeViewer() {
118         treeViewer.setContentProvider(new IndexProvider());
119         treeViewer.setLabelProvider(new IndexLabelProvider());
120     }
121
122     public void init(ReusableHelpPart parent, String JavaDoc id, IMemento memento) {
123         super.init(parent, id, memento);
124         if (parent.isFilteredByRoles())
125             treeViewer.addFilter(roleFilter);
126     }
127
128     protected void doOpen(Object JavaDoc obj) {
129         if (obj instanceof IHelpResource) {
130             parent.showURL(((IHelpResource) obj).getHref());
131         } else if (obj instanceof IIndexEntry) {
132             IIndexEntry entry = (IIndexEntry) obj;
133             if (getChildren(entry).length > 0) {
134                 treeViewer.setExpandedState(obj, !treeViewer.getExpandedState(obj));
135             }
136             IHelpResource[] topics = entry.getTopics();
137             if (topics.length == 1) {
138                 parent.showURL(topics[0].getHref());
139             }
140         }
141     }
142
143     protected boolean canAddBookmarks() {
144         return true;
145     }
146
147     public void saveState(IMemento memento) {
148     }
149
150     public void toggleRoleFilter() {
151         if (parent.isFilteredByRoles())
152             treeViewer.addFilter(roleFilter);
153         else
154             treeViewer.removeFilter(roleFilter);
155     }
156
157     public void refilter() {
158         treeViewer.refresh();
159     }
160
161     private Object JavaDoc[] getChildren(IIndexEntry entry) {
162         /*
163          * Index entry has two types of children: topics and subentries.
164          *
165          * The method returns topics among children only if number of the topics
166          * more than 1.
167          *
168          * In case when the entry owns only one topic, this topic is not returned
169          * as child because the entry will represent this topic by its keyword.
170          */

171         IHelpResource[] topics = entry.getTopics();
172         IIndexEntry[] subentries = entry.getSubentries();
173
174         if (topics.length <= 1) {
175             return subentries;
176         }
177
178         Object JavaDoc[] childrens = new Object JavaDoc[topics.length + subentries.length];
179         System.arraycopy(topics, 0, childrens, 0, topics.length);
180         System.arraycopy(subentries, 0, childrens, topics.length, subentries.length);
181
182         return childrens;
183     }
184
185     protected Tree getTreeWidget() {
186         return treeViewer.getTree();
187     }
188 }
189
Popular Tags