1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IParent;
16 import net.sourceforge.phpdt.core.JavaModelException;
17 import net.sourceforge.phpdt.internal.ui.actions.OpenActionUtil;
18 import net.sourceforge.phpdt.internal.ui.util.StringMatcher;
19 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
20 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
22 import net.sourceforge.phpdt.ui.JavaElementSorter;
23 import net.sourceforge.phpdt.ui.StandardJavaElementContentProvider;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.jface.text.IInformationControl;
28 import org.eclipse.jface.text.IInformationControlExtension;
29 import org.eclipse.jface.text.IInformationControlExtension2;
30 import org.eclipse.jface.viewers.AbstractTreeViewer;
31 import org.eclipse.jface.viewers.IBaseLabelProvider;
32 import org.eclipse.jface.viewers.ILabelProvider;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.viewers.StructuredViewer;
36 import org.eclipse.jface.viewers.TreeViewer;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerFilter;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.DisposeListener;
41 import org.eclipse.swt.events.FocusListener;
42 import org.eclipse.swt.events.KeyEvent;
43 import org.eclipse.swt.events.KeyListener;
44 import org.eclipse.swt.events.ModifyEvent;
45 import org.eclipse.swt.events.ModifyListener;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.SelectionListener;
48 import org.eclipse.swt.graphics.Color;
49 import org.eclipse.swt.graphics.FontMetrics;
50 import org.eclipse.swt.graphics.GC;
51 import org.eclipse.swt.graphics.Point;
52 import org.eclipse.swt.graphics.Rectangle;
53 import org.eclipse.swt.layout.GridData;
54 import org.eclipse.swt.layout.GridLayout;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Control;
57 import org.eclipse.swt.widgets.Display;
58 import org.eclipse.swt.widgets.Label;
59 import org.eclipse.swt.widgets.Layout;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.swt.widgets.Text;
62 import org.eclipse.swt.widgets.Tree;
63 import org.eclipse.swt.widgets.TreeItem;
68 * To change this generated comment edit the template variable "typecomment":
69 * Window>Preferences>Java>Templates. To enable and disable the creation of type
70 * comments go to Window>Preferences>Java>Code Generation.
72 public class JavaOutlineInformationControl implements IInformationControl,
73 IInformationControlExtension, IInformationControlExtension2 {
76 * The NamePatternFilter selects the elements which match the given string
79 * The following characters have special meaning: ? => any character * =>
85 private static class NamePatternFilter extends ViewerFilter {
86 private String fPattern;
88 private StringMatcher fMatcher;
90 private ILabelProvider fLabelProvider;
92 private Viewer fViewer;
94 private StringMatcher getMatcher() {
99 * (non-Javadoc) Method declared on ViewerFilter.
101 public boolean select(Viewer viewer, Object parentElement,
103 if (fMatcher == null)
106 ILabelProvider labelProvider = getLabelProvider(viewer);
108 String matchName = null;
109 if (labelProvider != null)
110 matchName = ((ILabelProvider) labelProvider).getText(element);
111 else if (element instanceof IJavaElement)
112 matchName = ((IJavaElement) element).getElementName();
114 if (matchName != null && fMatcher.match(matchName))
117 return hasUnfilteredChild(viewer, element);
120 private ILabelProvider getLabelProvider(Viewer viewer) {
121 if (fViewer == viewer)
122 return fLabelProvider;
124 fLabelProvider = null;
125 IBaseLabelProvider baseLabelProvider = null;
126 if (viewer instanceof StructuredViewer)
127 baseLabelProvider = ((StructuredViewer) viewer)
130 if (baseLabelProvider instanceof ILabelProvider)
131 fLabelProvider = (ILabelProvider) baseLabelProvider;
133 return fLabelProvider;
136 private boolean hasUnfilteredChild(Viewer viewer, Object element) {
137 IJavaElement[] children;
138 if (element instanceof IParent) {
140 children = ((IParent) element).getChildren();
141 } catch (JavaModelException ex) {
144 for (int i = 0; i < children.length; i++)
145 if (select(viewer, element, children[i]))
152 * Sets the patterns to filter out for the receiver.
154 * The following characters have special meaning: ? => any character * =>
158 public void setPattern(String pattern) {
160 if (fPattern == null) {
164 boolean ignoreCase = pattern.toLowerCase().equals(pattern);
165 fMatcher = new StringMatcher(pattern, ignoreCase, false);
169 private static class BorderFillLayout extends Layout {
171 /** The border widths. */
172 final int fBorderSize;
175 * Creates a fill layout with a border.
177 public BorderFillLayout(int borderSize) {
179 throw new IllegalArgumentException();
180 fBorderSize = borderSize;
184 * Returns the border size.
186 public int getBorderSize() {
191 * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
194 protected Point computeSize(Composite composite, int wHint, int hHint,
195 boolean flushCache) {
197 Control[] children = composite.getChildren();
198 Point minSize = new Point(0, 0);
200 if (children != null) {
201 for (int i = 0; i < children.length; i++) {
202 Point size = children[i].computeSize(wHint, hHint,
204 minSize.x = Math.max(minSize.x, size.x);
205 minSize.y = Math.max(minSize.y, size.y);
209 minSize.x += fBorderSize * 2 + RIGHT_MARGIN;
210 minSize.y += fBorderSize * 2;
216 * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
219 protected void layout(Composite composite, boolean flushCache) {
221 Control[] children = composite.getChildren();
222 Point minSize = new Point(composite.getClientArea().width,
223 composite.getClientArea().height);
225 if (children != null) {
226 for (int i = 0; i < children.length; i++) {
227 Control child = children[i];
228 child.setSize(minSize.x - fBorderSize * 2, minSize.y
230 child.setLocation(fBorderSize, fBorderSize);
236 /** Border thickness in pixels. */
237 private static final int BORDER = 1;
239 /** Right margin in pixels. */
240 private static final int RIGHT_MARGIN = 3;
242 /** The control's shell */
243 private Shell fShell;
246 Composite fComposite;
248 /** The control's text widget */
249 private Text fFilterText;
251 /** The control's tree widget */
252 private TreeViewer fTreeViewer;
254 /** The control width constraint */
255 private int fMaxWidth = -1;
257 /** The control height constraint */
258 private int fMaxHeight = -1;
260 private StringMatcher fStringMatcher;
263 * Creates a tree information control with the given shell as parent. The
264 * given style is applied to the tree widget.
269 * the additional styles for the tree widget
271 public JavaOutlineInformationControl(Shell parent, int style) {
272 this(parent, SWT.RESIZE, style);
276 * Creates a tree information control with the given shell as parent. No
277 * additional styles are applied.
282 public JavaOutlineInformationControl(Shell parent) {
283 this(parent, SWT.NONE);
287 * Creates a tree information control with the given shell as parent. The
288 * given styles are applied to the shell and the tree widget.
293 * the additional styles for the shell
295 * the additional styles for the tree widget
297 public JavaOutlineInformationControl(Shell parent, int shellStyle,
299 fShell = new Shell(parent, shellStyle);
300 Display display = fShell.getDisplay();
301 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
303 // Composite for filter text and tree
304 fComposite = new Composite(fShell, SWT.RESIZE);
305 GridLayout layout = new GridLayout(1, false);
306 fComposite.setLayout(layout);
307 fComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
309 createFilterText(fComposite);
310 createTreeViewer(fComposite, treeStyle);
312 int border = ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER;
313 fShell.setLayout(new BorderFillLayout(border));
315 setInfoSystemColor();
319 private void createTreeViewer(Composite parent, int style) {
320 Tree tree = new Tree(parent, SWT.SINGLE | (style & ~SWT.MULTI));
321 GridData data = new GridData(GridData.FILL_BOTH);
322 tree.setLayoutData(data);
324 fTreeViewer = new TreeViewer(tree);
326 // Hide import declartions but show the container
327 // fTreeViewer.addFilter(new ViewerFilter() {
328 // public boolean select(Viewer viewer, Object parentElement, Object
330 // return !(element instanceof IImportDeclaration);
334 fTreeViewer.setContentProvider(new StandardJavaElementContentProvider(
336 fTreeViewer.setSorter(new JavaElementSorter());
337 fTreeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
339 AppearanceAwareLabelProvider lprovider = new AppearanceAwareLabelProvider(
340 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
341 | JavaElementLabels.F_APP_TYPE_SIGNATURE,
342 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
344 .setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
346 fTreeViewer.getTree().addKeyListener(new KeyListener() {
347 public void keyPressed(KeyEvent e) {
348 if (e.character == 0x1B) // ESC
352 public void keyReleased(KeyEvent e) {
357 fTreeViewer.getTree().addSelectionListener(new SelectionListener() {
358 public void widgetSelected(SelectionEvent e) {
362 public void widgetDefaultSelected(SelectionEvent e) {
363 gotoSelectedElement();
368 private Text createFilterText(Composite parent) {
369 fFilterText = new Text(parent, SWT.FLAT);
371 GridData data = new GridData();
372 GC gc = new GC(parent);
373 gc.setFont(parent.getFont());
374 FontMetrics fontMetrics = gc.getFontMetrics();
377 data.heightHint = org.eclipse.jface.dialogs.Dialog
378 .convertHeightInCharsToPixels(fontMetrics, 1);
379 data.horizontalAlignment = GridData.FILL;
380 data.verticalAlignment = GridData.BEGINNING;
381 fFilterText.setLayoutData(data);
383 fFilterText.addKeyListener(new KeyListener() {
384 public void keyPressed(KeyEvent e) {
385 if (e.keyCode == 0x0D) // return
386 gotoSelectedElement();
387 if (e.keyCode == SWT.ARROW_DOWN)
388 fTreeViewer.getTree().setFocus();
389 if (e.keyCode == SWT.ARROW_UP)
390 fTreeViewer.getTree().setFocus();
391 if (e.character == 0x1B) // ESC
395 public void keyReleased(KeyEvent e) {
400 // Horizonral separator line
401 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
403 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
408 private void setInfoSystemColor() {
409 Display display = fShell.getDisplay();
410 setForegroundColor(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
411 setBackgroundColor(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
414 private void installFilter() {
415 final NamePatternFilter viewerFilter = new NamePatternFilter();
416 fTreeViewer.addFilter(viewerFilter);
417 fFilterText.setText(""); //$NON-NLS-1$
419 fFilterText.addModifyListener(new ModifyListener() {
420 public void modifyText(ModifyEvent e) {
421 String pattern = fFilterText.getText();
422 if (pattern != null) {
423 int length = pattern.length();
426 else if (pattern.charAt(length - 1) != '*')
427 pattern = pattern + '*';
430 viewerFilter.setPattern(pattern);
431 fStringMatcher = viewerFilter.getMatcher();
432 fTreeViewer.getControl().setRedraw(false);
433 fTreeViewer.refresh();
434 fTreeViewer.expandAll();
436 fTreeViewer.getControl().setRedraw(true);
441 private void gotoSelectedElement() {
442 Object selectedElement = ((IStructuredSelection) fTreeViewer
443 .getSelection()).getFirstElement();
444 if (selectedElement != null) {
447 OpenActionUtil.open(selectedElement, true);
448 } catch (CoreException ex) {
449 PHPeclipsePlugin.log(ex);
455 * Selects the first element in the tree which matches the current filter
458 private void selectFirstMatch() {
459 Tree tree = fTreeViewer.getTree();
460 Object element = findElement(tree.getItems());
462 fTreeViewer.setSelection(new StructuredSelection(element), true);
464 fTreeViewer.setSelection(StructuredSelection.EMPTY);
467 private IJavaElement findElement(TreeItem[] items) {
468 ILabelProvider labelProvider = (ILabelProvider) fTreeViewer
470 for (int i = 0; i < items.length; i++) {
471 IJavaElement element = (IJavaElement) items[i].getData();
472 if (fStringMatcher == null)
475 if (element != null) {
476 String label = labelProvider.getText(element);
477 if (fStringMatcher.match(label))
481 element = findElement(items[i].getItems());
489 * @see IInformationControl#setInformation(String)
491 public void setInformation(String information) {
492 // this method is ignored, see IInformationControlExtension2
496 * @see IInformationControlExtension2#setInput(Object)
498 public void setInput(Object information) {
499 fFilterText.setText(""); //$NON-NLS-1$
500 if (information == null || information instanceof String) {
504 IJavaElement je = (IJavaElement) information;
505 IJavaElement sel = null;
506 ICompilationUnit cu = (ICompilationUnit) je
507 .getAncestor(IJavaElement.COMPILATION_UNIT);
511 sel = je.getAncestor(IJavaElement.CLASS_FILE);
512 fTreeViewer.setInput(sel);
513 fTreeViewer.setSelection(new StructuredSelection(information));
517 * @see IInformationControl#setVisible(boolean)
519 public void setVisible(boolean visible) {
520 fShell.setVisible(visible);
524 * @see IInformationControl#dispose()
526 public void dispose() {
527 if (fShell != null) {
528 if (!fShell.isDisposed())
538 * @see org.eclipse.jface.text.IInformationControlExtension#hasContents()
540 public boolean hasContents() {
541 return fTreeViewer != null && fTreeViewer.getInput() != null;
545 * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int,
548 public void setSizeConstraints(int maxWidth, int maxHeight) {
549 fMaxWidth = maxWidth;
550 fMaxHeight = maxHeight;
554 * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
556 public Point computeSizeHint() {
557 return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
561 * @see IInformationControl#setLocation(Point)
563 public void setLocation(Point location) {
564 Rectangle trim = fShell.computeTrim(0, 0, 0, 0);
565 Point textLocation = fComposite.getLocation();
566 location.x += trim.x - textLocation.x;
567 location.y += trim.y - textLocation.y;
568 fShell.setLocation(location);
572 * @see IInformationControl#setSize(int, int)
574 public void setSize(int width, int height) {
575 fShell.setSize(width, height);
579 * @see IInformationControl#addDisposeListener(DisposeListener)
581 public void addDisposeListener(DisposeListener listener) {
582 fShell.addDisposeListener(listener);
586 * @see IInformationControl#removeDisposeListener(DisposeListener)
588 public void removeDisposeListener(DisposeListener listener) {
589 fShell.removeDisposeListener(listener);
593 * @see IInformationControl#setForegroundColor(Color)
595 public void setForegroundColor(Color foreground) {
596 fTreeViewer.getTree().setForeground(foreground);
597 fFilterText.setForeground(foreground);
598 fComposite.setForeground(foreground);
602 * @see IInformationControl#setBackgroundColor(Color)
604 public void setBackgroundColor(Color background) {
605 fTreeViewer.getTree().setBackground(background);
606 fFilterText.setBackground(background);
607 fComposite.setBackground(background);
611 * @see IInformationControl#isFocusControl()
613 public boolean isFocusControl() {
614 return fTreeViewer.getControl().isFocusControl()
615 || fFilterText.isFocusControl();
619 * @see IInformationControl#setFocus()
621 public void setFocus() {
623 fFilterText.setFocus();
627 * @see IInformationControl#addFocusListener(FocusListener)
629 public void addFocusListener(FocusListener listener) {
630 fShell.addFocusListener(listener);
634 * @see IInformationControl#removeFocusListener(FocusListener)
636 public void removeFocusListener(FocusListener listener) {
637 fShell.removeFocusListener(listener);