1 11 package org.eclipse.help.ui.internal.views; 12 13 import org.eclipse.help.*; 14 import org.eclipse.help.ui.internal.*; 15 import org.eclipse.jface.action.*; 16 import org.eclipse.jface.viewers.*; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.BusyIndicator; 19 import org.eclipse.swt.events.*; 20 import org.eclipse.swt.graphics.*; 21 import org.eclipse.swt.layout.*; 22 import org.eclipse.swt.widgets.*; 23 import org.eclipse.ui.IMemento; 24 import org.eclipse.ui.actions.ActionFactory; 25 import org.eclipse.ui.forms.AbstractFormPart; 26 import org.eclipse.ui.forms.events.HyperlinkEvent; 27 import org.eclipse.ui.forms.widgets.FormToolkit; 28 29 public abstract class HyperlinkTreePart extends AbstractFormPart implements 30 IHelpPart { 31 protected ReusableHelpPart parent; 32 33 private String id; 34 35 37 protected TreeViewer treeViewer; 38 39 private TreeItem lastItem; 40 41 private Cursor handCursor; 42 43 48 public HyperlinkTreePart(Composite parent, final FormToolkit toolkit, 49 IToolBarManager tbm) { 50 handCursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND); 51 65 treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL); 66 treeViewer.getTree().setMenu(parent.getMenu()); 67 treeViewer.getTree().setForeground( 68 toolkit.getHyperlinkGroup().getForeground()); 69 configureTreeViewer(); 70 treeViewer.setInput(this); 71 treeViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); 72 treeViewer.addOpenListener(new IOpenListener() { 73 public void open(OpenEvent event) { 74 doOpenSelection((IStructuredSelection) event.getSelection()); 75 } 76 }); 77 treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 78 public void selectionChanged(SelectionChangedEvent event) { 79 handleSelectionChanged((IStructuredSelection) event 80 .getSelection()); 81 } 82 }); 83 treeViewer.getTree().addMouseListener(new MouseAdapter() { 84 long lastTime; 85 86 public void mouseUp(MouseEvent e) { 87 long eventTime = e.time & 0xFFFFFFFFL; 88 if (eventTime - lastTime <= e.display.getDoubleClickTime()) 89 return; 90 if (e.button != 1) 91 return; 92 lastTime = eventTime; 93 Point p = new Point(e.x, e.y); 94 TreeItem item = treeViewer.getTree().getItem(p); 95 if (item != null) { 96 Object obj = item.getData(); 97 if (obj != null) { 98 doOpen(obj); 99 } 100 } 101 } 102 }); 103 104 treeViewer.getTree().addPaintListener(new PaintListener() { 105 public void paintControl(PaintEvent e) { 106 validateLastItem(); 107 if (lastItem == null) 108 return; 109 Rectangle bounds = lastItem.getBounds(); 110 boolean selected = false; 111 TreeItem[] items = lastItem.getParent().getSelection(); 112 for (int i = 0; i < items.length; i++) { 113 if (items[i].equals(lastItem)) { 114 selected = true; 115 break; 116 } 117 } 118 if (selected) 119 e.gc.setForeground(e.display.getSystemColor( 120 SWT.COLOR_LIST_SELECTION_TEXT)); 121 else 122 e.gc.setForeground(toolkit.getHyperlinkGroup() 123 .getActiveForeground()); 124 FontMetrics fm = e.gc.getFontMetrics(); 125 int height = fm.getHeight(); 126 int lineY = bounds.y + height; 127 e.gc.drawLine(bounds.x, lineY, bounds.x + bounds.width - 1, 128 lineY); 129 } 130 }); 131 132 treeViewer.getTree().addMouseTrackListener(new MouseTrackAdapter() { 133 public void mouseExit(MouseEvent e) { 134 validateLastItem(); 135 if (lastItem != null) { 136 TreeItem item = lastItem; 137 lastItem = null; 138 item.setForeground(null); 139 } 140 } 141 }); 142 143 treeViewer.getTree().addMouseMoveListener(new MouseMoveListener() { 144 public void mouseMove(MouseEvent e) { 145 Point p = new Point(e.x, e.y); 146 TreeItem item = treeViewer.getTree().getItem(p); 147 validateLastItem(); 148 if (item != null) { 149 if (lastItem != null) { 150 if (!lastItem.equals(item)) { 151 lastItem.setForeground(null); 152 repaintItem(lastItem); 153 updateStatus(null); 154 lastItem = null; 155 } else 156 return; 157 } 158 Object obj = item.getData(); 159 treeViewer.getTree().setCursor(handCursor); 160 IStructuredSelection ssel = (IStructuredSelection) treeViewer 161 .getSelection(); 162 if (ssel.getFirstElement() == obj) 163 item.setForeground(e.display 164 .getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT)); 165 else 166 item.setForeground(toolkit.getHyperlinkGroup() 167 .getActiveForeground()); 168 lastItem = item; 169 repaintItem(lastItem); 170 if (obj instanceof IHelpResource) 171 updateStatus((IHelpResource) obj); 172 else 173 updateStatus(null); 174 return; 175 } else if (lastItem != null) { 176 lastItem.setForeground(null); 177 repaintItem(lastItem); 178 lastItem = null; 179 updateStatus(null); 180 } 181 treeViewer.getTree().setCursor(null); 182 } 183 }); 184 contributeToToolBar(tbm); 185 } 186 187 public void dispose() { 188 handCursor.dispose(); 189 super.dispose(); 190 } 191 192 private void repaintItem(TreeItem item) { 193 Rectangle bounds = item.getBounds(); 194 item.getParent().redraw(bounds.x, bounds.y, bounds.width, 195 bounds.height, false); 196 } 197 198 protected void contributeToToolBar(IToolBarManager tbm) { 199 Action collapseAllAction = new Action() { 200 public void run() { 201 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable () { 202 public void run() { 203 doCollapseAll(); 204 } 205 }); 206 } 207 }; 208 collapseAllAction.setImageDescriptor(HelpUIResources 209 .getImageDescriptor(IHelpUIConstants.IMAGE_COLLAPSE_ALL)); 210 collapseAllAction 211 .setToolTipText(Messages.AllTopicsPart_collapseAll_tooltip); 212 tbm.insertBefore("back", collapseAllAction); tbm.insertBefore("back", new Separator()); } 215 216 private void doCollapseAll() { 217 Object [] expanded = treeViewer.getExpandedElements(); 218 treeViewer.collapseAll(); 219 treeViewer.update(expanded, null); 220 } 221 222 227 public Control getControl() { 228 return treeViewer.getControl(); 229 } 230 231 236 public void init(ReusableHelpPart parent, String id, IMemento memento) { 237 this.parent = parent; 238 this.id = id; 239 } 240 241 public String getId() { 242 return id; 243 } 244 245 250 public void setVisible(boolean visible) { 251 getControl().setVisible(visible); 252 if (visible) 253 treeViewer.refresh(); 254 } 255 256 private void doOpenSelection(IStructuredSelection sel) { 257 Object obj = sel.getFirstElement(); 258 if (obj != null) { 259 doOpen(obj); 260 } 261 } 262 263 protected void handleSelectionChanged(IStructuredSelection sel) { 264 Object obj = sel.getFirstElement(); 265 if (lastItem != null && !lastItem.isDisposed()) { 266 Object lastObj = lastItem.getData(); 267 if (lastObj==obj) 268 lastItem.setForeground(getControl().getDisplay() 269 .getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT)); 270 else 271 lastItem.setForeground(parent.getForm().getToolkit().getHyperlinkGroup() 272 .getActiveForeground()); 273 repaintItem(lastItem); 274 } 275 if (obj instanceof IHelpResource) { 276 IHelpResource res = (IHelpResource) obj; 277 updateStatus(res, false); 278 } else 279 updateStatus(null, false); 280 } 281 282 private void updateStatus(IHelpResource res) { 283 updateStatus(res, true); 284 } 285 286 private void updateStatus(IHelpResource res, boolean defaultToSelection) { 287 if (defaultToSelection && res == null) { 288 IStructuredSelection ssel = (IStructuredSelection) treeViewer 289 .getSelection(); 290 Object obj = ssel.getFirstElement(); 291 if (obj instanceof IHelpResource) 292 res = (IHelpResource) obj; 293 } 294 if (res != null) { 295 String label = res.getLabel(); 296 String href = getHref(res); 297 HyperlinkTreePart.this.parent.handleLinkEntered(new HyperlinkEvent( 298 treeViewer.getTree(), href, label, SWT.NULL)); 299 } else { 300 HyperlinkTreePart.this.parent.handleLinkExited(null); 301 } 302 } 303 304 protected String getHref(IHelpResource res) { 305 return res.getHref(); 306 } 307 308 protected abstract void configureTreeViewer(); 309 310 protected abstract void doOpen(Object obj); 311 312 protected void postUpdate(final Object obj) { 313 treeViewer.getControl().getDisplay().asyncExec(new Runnable () { 314 public void run() { 315 treeViewer.update(obj, null); 316 } 317 }); 318 } 319 320 325 public boolean fillContextMenu(IMenuManager manager) { 326 return parent.fillSelectionProviderMenu(treeViewer, manager, 327 canAddBookmarks()); 328 } 329 330 protected abstract boolean canAddBookmarks(); 331 332 337 public boolean hasFocusControl(Control focusControl) { 338 return treeViewer.getControl().equals(focusControl); 339 } 340 341 public void setFocus() { 342 if (treeViewer != null) 343 treeViewer.getTree().setFocus(); 344 } 345 346 public IAction getGlobalAction(String id) { 347 if (id.equals(ActionFactory.COPY.getId())) 348 return parent.getCopyAction(); 349 return null; 350 } 351 352 public void stop() { 353 } 354 355 private void validateLastItem() { 356 if (lastItem != null && lastItem.isDisposed()) 357 lastItem = null; 358 } 359 } 360 | Popular Tags |