1 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 [] getChildren(Object 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 [0]; 44 } 45 46 public Object getParent(Object element) { 47 return null; 48 } 49 50 public boolean hasChildren(Object element) { 51 return getChildren(element).length > 0; 52 } 53 54 public Object [] getElements(Object inputElement) { 55 return getChildren(inputElement); 56 } 57 58 public void dispose() {} 59 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 60 } 61 62 class IndexLabelProvider extends LabelProvider { 63 public String getText(Object 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 obj) { 72 return super.getImage(obj); 73 } 74 } 75 76 class RoleFilter extends ViewerFilter { 77 public boolean select(Viewer viewer, Object parentElement, 78 Object 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 obj) { 104 return !UAContentFilter.isFiltered(obj, HelpEvaluationContext.getContext()); 105 } 106 107 private boolean isEnabled(IHelpResource topic) { 108 return isEnabled((Object )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 id, IMemento memento) { 123 super.init(parent, id, memento); 124 if (parent.isFilteredByRoles()) 125 treeViewer.addFilter(roleFilter); 126 } 127 128 protected void doOpen(Object 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 [] getChildren(IIndexEntry entry) { 162 171 IHelpResource[] topics = entry.getTopics(); 172 IIndexEntry[] subentries = entry.getSubentries(); 173 174 if (topics.length <= 1) { 175 return subentries; 176 } 177 178 Object [] childrens = new Object [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 |