1 11 package org.eclipse.help.ui.internal.views; 12 13 import org.eclipse.help.HelpSystem; 14 import org.eclipse.help.IHelpResource; 15 import org.eclipse.help.IToc; 16 import org.eclipse.help.ITopic; 17 import org.eclipse.help.internal.UAElement; 18 import org.eclipse.help.ui.internal.HelpUIResources; 19 import org.eclipse.help.ui.internal.IHelpUIConstants; 20 import org.eclipse.help.ui.internal.util.OverlayIcon; 21 import org.eclipse.jface.action.IToolBarManager; 22 import org.eclipse.jface.resource.ImageDescriptor; 23 import org.eclipse.jface.viewers.ITreeContentProvider; 24 import org.eclipse.jface.viewers.ITreeViewerListener; 25 import org.eclipse.jface.viewers.LabelProvider; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.viewers.TreeExpansionEvent; 28 import org.eclipse.jface.viewers.Viewer; 29 import org.eclipse.jface.viewers.ViewerFilter; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.ui.IMemento; 33 import org.eclipse.ui.forms.widgets.FormToolkit; 34 35 public class AllTopicsPart extends HyperlinkTreePart implements IHelpPart { 36 37 private Image containerWithTopicImage; 38 39 41 class TopicsProvider implements ITreeContentProvider { 42 public Object [] getChildren(Object parentElement) { 43 if (parentElement == AllTopicsPart.this) 44 return HelpSystem.getTocs(); 45 if (parentElement instanceof IToc) 46 return ((IToc) parentElement).getTopics(); 47 if (parentElement instanceof ITopic) 48 return ((ITopic) parentElement).getSubtopics(); 49 return new Object [0]; 50 } 51 52 57 public Object getParent(Object element) { 58 if (element instanceof IToc) { 59 return AllTopicsPart.this; 60 } 61 else if (element instanceof UAElement) { 62 return ((UAElement)element).getParentElement(); 63 } 64 return null; 65 } 66 67 72 public boolean hasChildren(Object element) { 73 return getChildren(element).length > 0; 74 } 75 76 81 public Object [] getElements(Object inputElement) { 82 return getChildren(inputElement); 83 } 84 85 90 public void dispose() { 91 } 92 93 99 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 100 } 101 } 102 103 class TopicsLabelProvider extends LabelProvider { 104 public String getText(Object obj) { 105 if (obj instanceof IHelpResource) 106 return ((IHelpResource) obj).getLabel(); 107 return super.getText(obj); 108 } 109 110 public Image getImage(Object obj) { 111 if (obj instanceof IToc) { 112 boolean expanded = treeViewer.getExpandedState(obj); 113 String key = expanded ? IHelpUIConstants.IMAGE_TOC_OPEN 114 : IHelpUIConstants.IMAGE_TOC_CLOSED; 115 return HelpUIResources.getImage(key); 116 } 117 if (obj instanceof ITopic) { 118 boolean expandable = treeViewer.isExpandable(obj); 119 if (expandable) { 120 ITopic topic = (ITopic) obj; 121 if (topic.getHref() != null) 122 return containerWithTopicImage; 123 } 124 String key = expandable ? IHelpUIConstants.IMAGE_CONTAINER 125 : IHelpUIConstants.IMAGE_FILE_F1TOPIC; 126 return HelpUIResources.getImage(key); 127 } 128 return super.getImage(obj); 129 } 130 } 131 132 class EmptyContainerFilter extends ViewerFilter { 133 public boolean select(Viewer viewer, Object parentElement, 134 Object element) { 135 if (element instanceof IToc) { 136 return isNotEmpty((IToc) element); 137 } else if (element instanceof ITopic) { 138 return isNotEmpty((ITopic) element); 139 } 140 return false; 141 } 142 143 private boolean isNotEmpty(IToc toc) { 144 ITopic[] topics = toc.getTopics(); 145 return isNotEmpty(topics); 146 } 147 148 private boolean isNotEmpty(ITopic topic) { 149 String href = topic.getHref(); 150 ITopic[] topics = topic.getSubtopics(); 151 return href != null || isNotEmpty(topics); 152 } 153 154 private boolean isNotEmpty(ITopic[] topics) { 155 for (int i = 0; i < topics.length; i++) { 156 if (isNotEmpty(topics[i])) 157 return true; 158 } 159 return false; 160 } 161 } 162 163 168 public AllTopicsPart(Composite parent, final FormToolkit toolkit, 169 IToolBarManager tbm) { 170 super(parent, toolkit, tbm); 171 } 172 173 protected void configureTreeViewer() { 174 initializeImages(); 175 treeViewer.setContentProvider(new TopicsProvider()); 176 treeViewer.setLabelProvider(new TopicsLabelProvider()); 177 treeViewer.addTreeListener(new ITreeViewerListener() { 178 public void treeCollapsed(TreeExpansionEvent event) { 179 Object obj = event.getElement(); 180 if (obj instanceof IToc) { 181 postUpdate(obj); 182 } 183 } 184 185 public void treeExpanded(TreeExpansionEvent event) { 186 Object obj = event.getElement(); 187 188 if (obj instanceof IToc) { 189 postUpdate(event.getElement()); 190 } 191 } 192 }); 193 } 194 195 public void init(ReusableHelpPart parent, String id, IMemento memento) { 196 super.init(parent, id, memento); 197 if (parent.isFilteredByRoles()) 198 treeViewer.addFilter(parent.getRoleFilter()); 199 treeViewer.addFilter(parent.getUAFilter()); 200 treeViewer.addFilter(new EmptyContainerFilter()); 201 } 202 203 private void initializeImages() { 204 ImageDescriptor base = HelpUIResources 205 .getImageDescriptor(IHelpUIConstants.IMAGE_CONTAINER); 206 ImageDescriptor ovr = HelpUIResources 207 .getImageDescriptor(IHelpUIConstants.IMAGE_DOC_OVR); 208 ImageDescriptor desc = new OverlayIcon(base, 209 new ImageDescriptor[][] { { ovr } }); 210 containerWithTopicImage = desc.createImage(); 211 } 212 213 public void dispose() { 214 containerWithTopicImage.dispose(); 215 super.dispose(); 216 } 217 218 protected void doOpen(Object obj) { 219 if (!(obj instanceof IHelpResource)) 220 return; 221 IHelpResource res = (IHelpResource) obj; 222 if (res instanceof IToc 223 || (res instanceof ITopic 224 && ((ITopic) obj).getSubtopics().length > 0 && res 225 .getHref() == null)) 226 treeViewer.setExpandedState(obj, !treeViewer.getExpandedState(res)); 227 if (res instanceof IToc) 228 postUpdate(res); 229 else if (res.getHref() != null) 230 parent.showURL(res.getHref()); 231 } 232 233 protected String getHref(IHelpResource res) { 234 return (res instanceof ITopic) ? res.getHref() : null; 235 } 236 237 public void selectReveal(String href) { 238 IToc[] tocs = HelpSystem.getTocs(); 239 for (int i = 0; i < tocs.length; i++) { 240 IToc toc = tocs[i]; 241 ITopic topic = toc.getTopic(href); 242 if (topic != null) { 243 selectReveal(topic); 244 return; 245 } 246 } 247 } 248 249 public void selectReveal(IHelpResource res) { 250 treeViewer.setSelection(new StructuredSelection(res), true); 251 treeViewer.expandToLevel(res, 1); 252 treeViewer.getControl().setFocus(); 253 } 254 255 protected boolean canAddBookmarks() { 256 return true; 257 } 258 259 public void toggleRoleFilter() { 260 if (parent.isFilteredByRoles()) 261 treeViewer.addFilter(parent.getRoleFilter()); 262 else 263 treeViewer.removeFilter(parent.getRoleFilter()); 264 } 265 266 public void refilter() { 267 treeViewer.refresh(); 268 } 269 270 public void saveState(IMemento memento) { 271 } 272 } | Popular Tags |