1 11 12 package org.eclipse.pde.internal.ui.editor.outline; 13 14 import org.eclipse.jface.action.IMenuManager; 15 import org.eclipse.jface.action.Separator; 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.PopupDialog; 18 import org.eclipse.jface.text.IInformationControl; 19 import org.eclipse.jface.text.IInformationControlExtension; 20 import org.eclipse.jface.text.IInformationControlExtension2; 21 import org.eclipse.jface.viewers.AbstractTreeViewer; 22 import org.eclipse.jface.viewers.ILabelProvider; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.ITreeContentProvider; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.jface.viewers.TreeViewer; 27 import org.eclipse.jface.viewers.ViewerComparator; 28 import org.eclipse.pde.internal.ui.PDEUIMessages; 29 import org.eclipse.pde.internal.ui.editor.actions.SortAction; 30 import org.eclipse.pde.internal.ui.util.StringMatcher; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.events.DisposeEvent; 33 import org.eclipse.swt.events.DisposeListener; 34 import org.eclipse.swt.events.FocusListener; 35 import org.eclipse.swt.events.KeyEvent; 36 import org.eclipse.swt.events.KeyListener; 37 import org.eclipse.swt.events.ModifyEvent; 38 import org.eclipse.swt.events.ModifyListener; 39 import org.eclipse.swt.events.MouseAdapter; 40 import org.eclipse.swt.events.MouseEvent; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.events.SelectionListener; 43 import org.eclipse.swt.graphics.Color; 44 import org.eclipse.swt.graphics.FontMetrics; 45 import org.eclipse.swt.graphics.GC; 46 import org.eclipse.swt.graphics.Point; 47 import org.eclipse.swt.layout.GridData; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Control; 50 import org.eclipse.swt.widgets.Shell; 51 import org.eclipse.swt.widgets.Text; 52 import org.eclipse.swt.widgets.Tree; 53 import org.eclipse.swt.widgets.TreeItem; 54 55 59 public class QuickOutlinePopupDialog extends PopupDialog implements 60 IInformationControl, IInformationControlExtension, 61 IInformationControlExtension2, DisposeListener { 62 63 private TreeViewer fTreeViewer; 64 65 private IOutlineContentCreator fOutlineContentCreator; 66 67 private IOutlineSelectionHandler fOutlineSelectionHandler; 68 69 private Text fFilterText; 70 71 private StringMatcher fStringMatcher; 72 73 private QuickOutlineNamePatternFilter fNamePatternFilter; 74 75 private SortAction fSortAction; 76 77 private ITreeContentProvider fTreeContentProvider; 78 79 private ILabelProvider fTreeLabelProvider; 80 81 private ViewerComparator fTreeViewerComparator; 82 83 private ViewerComparator fTreeViewerDefaultComparator; 84 85 public QuickOutlinePopupDialog(Shell parent, int shellStyle, 86 IOutlineContentCreator creator, 87 IOutlineSelectionHandler handler) { 88 super(parent, shellStyle, true, true, true, true, null, null); 89 fOutlineContentCreator = creator; 91 fOutlineSelectionHandler = handler; 93 initialize(); 95 create(); 98 } 99 100 103 private void initialize() { 104 setInfoText(PDEUIMessages.QuickOutlinePopupDialog_infoTextPressEscToExit); 105 106 fFilterText = null; 107 fTreeViewer = null; 108 fStringMatcher = null; 109 fNamePatternFilter = null; 110 fSortAction = null; 111 fTreeContentProvider = null; 112 fTreeLabelProvider = null; 113 fTreeViewerComparator = null; 114 fTreeViewerDefaultComparator = null; 115 } 116 117 120 protected Control createDialogArea(Composite parent) { 121 if ((fOutlineContentCreator == null) || 124 (fOutlineSelectionHandler == null)) { 125 return super.createDialogArea(parent); 126 } 127 createUIWidgetTreeViewer(parent); 129 createUIListenersTreeViewer(); 131 createUIActions(); 133 addDisposeListener(this); 135 return fTreeViewer.getControl(); 137 } 138 139 142 private void createUIActions() { 143 fSortAction = new SortAction(fTreeViewer, 145 PDEUIMessages.PDEMultiPageContentOutline_SortingAction_tooltip, 146 fTreeViewerComparator, fTreeViewerDefaultComparator, null); 147 } 148 149 152 protected void fillDialogMenu(IMenuManager dialogMenu) { 153 dialogMenu.add(fSortAction); 155 dialogMenu.add(new Separator()); 157 super.fillDialogMenu(dialogMenu); 159 } 160 161 164 private void createUIWidgetTreeViewer(Composite parent) { 165 166 174 int style = SWT.H_SCROLL | SWT.V_SCROLL; 175 Tree widget = new Tree(parent, style); 177 GridData data = new GridData(GridData.FILL_BOTH); 179 data.heightHint = widget.getItemHeight() * 12; 180 widget.setLayoutData(data); 181 fTreeViewer = new TreeViewer(widget); 183 fNamePatternFilter = new QuickOutlineNamePatternFilter(); 185 fTreeViewer.addFilter(fNamePatternFilter); 186 fTreeContentProvider = fOutlineContentCreator.createOutlineContentProvider(); 188 fTreeViewer.setContentProvider(fTreeContentProvider); 189 fTreeLabelProvider = fOutlineContentCreator.createOutlineLabelProvider(); 191 fTreeViewer.setLabelProvider(fTreeLabelProvider); 192 fTreeViewerComparator = fOutlineContentCreator.createOutlineComparator(); 194 fTreeViewerDefaultComparator = 199 fOutlineContentCreator.createDefaultOutlineComparator(); 200 fTreeViewer.setComparator(fTreeViewerDefaultComparator); 201 fTreeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS); 202 fTreeViewer.setUseHashlookup(true); 203 fTreeViewer.setInput(fOutlineContentCreator.getOutlineInput()); 204 } 205 206 209 private void createUIListenersTreeViewer() { 210 final Tree tree = fTreeViewer.getTree(); 212 tree.addKeyListener(new KeyListener() { 214 public void keyPressed(KeyEvent e) { 215 if (e.character == 0x1B) { 216 dispose(); 218 } 219 } 220 public void keyReleased(KeyEvent e) { 221 } 223 }); 224 tree.addMouseListener(new MouseAdapter() { 226 public void mouseUp(MouseEvent e) { 227 handleTreeViewerMouseUp(tree, e); 228 } 229 }); 230 tree.addMouseMoveListener(new QuickOutlineMouseMoveListener(fTreeViewer)); 232 tree.addSelectionListener(new SelectionListener() { 234 public void widgetSelected(SelectionEvent e) { 235 } 237 public void widgetDefaultSelected(SelectionEvent e) { 238 gotoSelectedElement(); 239 } 240 }); 241 } 242 243 247 private void handleTreeViewerMouseUp(final Tree tree, MouseEvent e) { 248 if ((tree.getSelectionCount() < 1) || 251 (e.button != 1) || 252 (tree.equals(e.getSource()) == false)) { 253 return; 254 } 255 Object object = tree.getItem(new Point(e.x, e.y)); 257 TreeItem selection = tree.getSelection()[0]; 258 if (selection.equals(object)) { 259 gotoSelectedElement(); 260 } 261 } 262 263 266 private Object getSelectedElement() { 267 if (fTreeViewer == null) { 268 return null; 269 } 270 return ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement(); 271 } 272 273 276 public void addDisposeListener(DisposeListener listener) { 277 getShell().addDisposeListener(listener); 278 } 279 280 283 public void addFocusListener(FocusListener listener) { 284 getShell().addFocusListener(listener); 285 } 286 287 290 public Point computeSizeHint() { 291 return getShell().getSize(); 294 } 295 296 299 public void dispose() { 300 close(); 301 } 302 303 306 public boolean isFocusControl() { 307 if (fTreeViewer.getControl().isFocusControl() || 308 fFilterText.isFocusControl()) { 309 return true; 310 } 311 return false; 312 } 313 314 317 public void removeDisposeListener(DisposeListener listener) { 318 getShell().removeDisposeListener(listener); 319 } 320 321 324 public void removeFocusListener(FocusListener listener) { 325 getShell().removeFocusListener(listener); 326 } 327 328 331 public void setBackgroundColor(Color background) { 332 applyBackgroundColor(background, getContents()); 333 } 334 335 338 public void setFocus() { 339 getShell().forceFocus(); 340 fFilterText.setFocus(); 341 } 342 343 346 public void setForegroundColor(Color foreground) { 347 applyForegroundColor(foreground, getContents()); 348 } 349 350 353 public void setInformation(String information) { 354 } 357 358 361 public void setLocation(Point location) { 362 373 if ((getPersistBounds() == false) || 374 (getDialogSettings() == null)) { 375 getShell().setLocation(location); 376 } 377 } 378 379 382 public void setSize(int width, int height) { 383 getShell().setSize(width, height); 384 } 385 386 389 public void setSizeConstraints(int maxWidth, int maxHeight) { 390 } 392 393 396 public void setVisible(boolean visible) { 397 if (visible) { 398 open(); 399 } else { 400 saveDialogBounds(getShell()); 401 getShell().setVisible(false); 402 } 403 } 404 405 408 public boolean hasContents() { 409 if ((fTreeViewer == null) || 410 (fTreeViewer.getInput() == null)) { 411 return false; 412 } 413 return true; 414 } 415 416 419 public void setInput(Object input) { 420 if (input != null) { 424 fTreeViewer.setSelection(new StructuredSelection(input)); 425 } 426 } 427 428 431 public void widgetDisposed(DisposeEvent e) { 432 fTreeViewer= null; 434 fFilterText= null; 435 } 436 437 440 protected Control createTitleControl(Composite parent) { 441 createUIWidgetFilterText(parent); 444 createUIListenersFilterText(); 446 return fFilterText; 448 } 449 450 454 private void createUIWidgetFilterText(Composite parent) { 455 fFilterText = new Text(parent, SWT.NONE); 457 GC gc = new GC(parent); 459 gc.setFont(parent.getFont()); 460 FontMetrics fontMetrics = gc.getFontMetrics(); 461 gc.dispose(); 462 GridData data = new GridData(GridData.FILL_HORIZONTAL); 464 data.heightHint= Dialog.convertHeightInCharsToPixels(fontMetrics, 1); 465 data.horizontalAlignment = GridData.FILL; 466 data.verticalAlignment = GridData.CENTER; 467 fFilterText.setLayoutData(data); 468 } 469 470 473 private void gotoSelectedElement() { 474 Object selectedElement = getSelectedElement(); 475 if (selectedElement == null) { 476 return; 477 } 478 dispose(); 479 fOutlineSelectionHandler.getContentOutline().setSelection( 483 new StructuredSelection(selectedElement)); 484 } 485 486 489 private void createUIListenersFilterText() { 490 fFilterText.addKeyListener(new KeyListener() { 492 public void keyPressed(KeyEvent e) { 493 if (e.keyCode == 0x0D) { 494 gotoSelectedElement(); 496 } else if (e.keyCode == SWT.ARROW_DOWN) { 497 fTreeViewer.getTree().setFocus(); 499 } else if (e.keyCode == SWT.ARROW_UP) { 500 fTreeViewer.getTree().setFocus(); 502 } else if (e.character == 0x1B) { 503 dispose(); 505 } 506 } 507 public void keyReleased(KeyEvent e) { 508 } 510 }); 511 fFilterText.addModifyListener(new ModifyListener() { 513 public void modifyText(ModifyEvent e) { 514 String text = ((Text)e.widget).getText(); 515 int length = text.length(); 516 if (length > 0) { 517 if (text.charAt(length - 1) != '*') { 520 text = text + '*'; 521 } 522 if (text.charAt(0) != '*') { 525 text = '*' + text; 526 } 527 } 528 setMatcherString(text, true); 530 } 531 }); 532 } 533 534 545 private void setMatcherString(String pattern, boolean update) { 546 if (pattern.length() == 0) { 547 fStringMatcher = null; 548 } else { 549 fStringMatcher = new StringMatcher(pattern, true, false); 550 } 551 fNamePatternFilter.setStringMatcher(fStringMatcher); 553 if (update) { 555 stringMatcherUpdated(); 556 } 557 } 558 559 563 private void stringMatcherUpdated() { 564 fTreeViewer.getControl().setRedraw(false); 566 fTreeViewer.refresh(); 567 fTreeViewer.expandAll(); 568 selectFirstMatch(); 569 fTreeViewer.getControl().setRedraw(true); 570 } 571 572 576 private void selectFirstMatch() { 577 Tree tree = fTreeViewer.getTree(); 578 Object element = findFirstMatchToPattern(tree.getItems()); 579 if (element != null) { 580 fTreeViewer.setSelection(new StructuredSelection(element), true); 581 } else { 582 fTreeViewer.setSelection(StructuredSelection.EMPTY); 583 } 584 } 585 586 590 private Object findFirstMatchToPattern(TreeItem[] items) { 591 ILabelProvider labelProvider = 593 (ILabelProvider)fTreeViewer.getLabelProvider(); 594 for (int i = 0; i < items.length; i++) { 596 Object element = items[i].getData(); 597 if (fStringMatcher == null) { 599 return element; 600 } 601 if (element != null) { 603 String label = labelProvider.getText(element); 604 if (fStringMatcher.match(label)) { 605 return element; 606 } 607 } 608 element = findFirstMatchToPattern(items[i].getItems()); 610 if (element != null) { 612 return element; 613 } 614 } 615 return null; 617 } 618 619 } 620 | Popular Tags |