1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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.phpeclipse.phpeditor;
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.List;
16 import java.util.ResourceBundle;
17 import java.util.Vector;
19 import net.sourceforge.phpdt.core.ElementChangedEvent;
20 import net.sourceforge.phpdt.core.ICompilationUnit;
21 import net.sourceforge.phpdt.core.IElementChangedListener;
22 import net.sourceforge.phpdt.core.IField;
23 import net.sourceforge.phpdt.core.IJavaElement;
24 import net.sourceforge.phpdt.core.IJavaElementDelta;
25 import net.sourceforge.phpdt.core.IMember;
26 import net.sourceforge.phpdt.core.IMethod;
27 import net.sourceforge.phpdt.core.IParent;
28 import net.sourceforge.phpdt.core.ISourceRange;
29 import net.sourceforge.phpdt.core.ISourceReference;
30 import net.sourceforge.phpdt.core.IType;
31 import net.sourceforge.phpdt.core.JavaCore;
32 import net.sourceforge.phpdt.core.JavaModelException;
33 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
34 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
35 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
36 import net.sourceforge.phpdt.internal.ui.actions.AbstractToggleLinkingAction;
37 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
38 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
39 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
40 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
41 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
42 import net.sourceforge.phpdt.internal.ui.viewsupport.StatusBarUpdater;
43 import net.sourceforge.phpdt.ui.JavaElementSorter;
44 import net.sourceforge.phpdt.ui.JavaUI;
45 import net.sourceforge.phpdt.ui.PreferenceConstants;
46 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
47 import net.sourceforge.phpdt.ui.actions.CustomFiltersActionGroup;
48 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
49 import net.sourceforge.phpdt.ui.actions.MemberFilterActionGroup;
50 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
51 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
52 import net.sourceforge.phpeclipse.ui.WebUI;
54 import org.eclipse.core.resources.IResource;
55 import org.eclipse.core.runtime.IAdaptable;
56 import org.eclipse.core.runtime.ListenerList;
57 import org.eclipse.jface.action.Action;
58 import org.eclipse.jface.action.IAction;
59 import org.eclipse.jface.action.IMenuListener;
60 import org.eclipse.jface.action.IMenuManager;
61 import org.eclipse.jface.action.IStatusLineManager;
62 import org.eclipse.jface.action.IToolBarManager;
63 import org.eclipse.jface.action.MenuManager;
64 import org.eclipse.jface.action.Separator;
65 import org.eclipse.jface.preference.IPreferenceStore;
66 import org.eclipse.jface.text.Assert;
67 import org.eclipse.jface.text.ITextSelection;
68 import org.eclipse.jface.util.IPropertyChangeListener;
69 import org.eclipse.jface.util.PropertyChangeEvent;
70 import org.eclipse.jface.viewers.IBaseLabelProvider;
71 import org.eclipse.jface.viewers.IPostSelectionProvider;
72 import org.eclipse.jface.viewers.ISelection;
73 import org.eclipse.jface.viewers.ISelectionChangedListener;
74 import org.eclipse.jface.viewers.IStructuredSelection;
75 import org.eclipse.jface.viewers.ITreeContentProvider;
76 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
77 import org.eclipse.jface.viewers.SelectionChangedEvent;
78 import org.eclipse.jface.viewers.StructuredSelection;
79 import org.eclipse.jface.viewers.TreeViewer;
80 import org.eclipse.jface.viewers.Viewer;
81 import org.eclipse.jface.viewers.ViewerFilter;
82 import org.eclipse.swt.SWT;
83 import org.eclipse.swt.custom.BusyIndicator;
84 import org.eclipse.swt.dnd.DND;
85 import org.eclipse.swt.dnd.Transfer;
86 import org.eclipse.swt.widgets.Composite;
87 import org.eclipse.swt.widgets.Control;
88 import org.eclipse.swt.widgets.Display;
89 import org.eclipse.swt.widgets.Item;
90 import org.eclipse.swt.widgets.Menu;
91 import org.eclipse.swt.widgets.Tree;
92 import org.eclipse.swt.widgets.Widget;
93 import org.eclipse.ui.IActionBars;
94 import org.eclipse.ui.PlatformUI;
95 import org.eclipse.ui.actions.ActionContext;
96 import org.eclipse.ui.actions.ActionFactory;
97 import org.eclipse.ui.actions.ActionGroup;
98 import org.eclipse.ui.model.IWorkbenchAdapter;
99 import org.eclipse.ui.model.WorkbenchAdapter;
100 import org.eclipse.ui.part.IPageSite;
101 import org.eclipse.ui.part.IShowInSource;
102 import org.eclipse.ui.part.IShowInTarget;
103 import org.eclipse.ui.part.IShowInTargetList;
104 import org.eclipse.ui.part.Page;
105 import org.eclipse.ui.part.ShowInContext;
106 import org.eclipse.ui.texteditor.GotoAnnotationAction;
107 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
108 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
109 import org.eclipse.ui.texteditor.IUpdate;
110 import org.eclipse.ui.texteditor.TextEditorAction;
111 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
112 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
115 * The content outline page of the Java editor. The viewer implements a
116 * proprietary update mechanism based on Java model deltas. It does not react on
117 * domain changes. It is specified to show the content of ICompilationUnits and
118 * IClassFiles. Publishes its context menu under
119 * <code>PHPeclipsePlugin.getDefault().getPluginId() + ".outline"</code>.
121 public class JavaOutlinePage extends Page implements IContentOutlinePage,
122 IAdaptable, IPostSelectionProvider {
124 static Object[] NO_CHILDREN = new Object[0];
127 * The element change listener of the java outline viewer.
129 * @see IElementChangedListener
131 class ElementChangedListener implements IElementChangedListener {
133 public void elementChanged(final ElementChangedEvent e) {
135 if (getControl() == null)
138 Display d = getControl().getDisplay();
140 d.asyncExec(new Runnable() {
142 ICompilationUnit cu = (ICompilationUnit) fInput;
143 IJavaElement base = cu;
144 // if (fTopLevelTypeOnly) {
145 // base= getMainType(cu);
146 // if (base == null) {
147 if (fOutlineViewer != null)
148 fOutlineViewer.refresh(true);
152 // IJavaElementDelta delta= findElement(base,
154 // if (delta != null && fOutlineViewer != null) {
155 // fOutlineViewer.reconcile(delta);
162 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
163 if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
164 return true; // add or remove
166 int flags = cuDelta.getFlags();
167 if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
170 return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
173 protected IJavaElementDelta findElement(IJavaElement unit,
174 IJavaElementDelta delta) {
176 if (delta == null || unit == null)
179 IJavaElement element = delta.getElement();
181 if (unit.equals(element)) {
182 if (isPossibleStructuralChange(delta)) {
188 if (element.getElementType() > IJavaElement.CLASS_FILE)
191 IJavaElementDelta[] children = delta.getAffectedChildren();
192 if (children == null || children.length == 0)
195 for (int i = 0; i < children.length; i++) {
196 IJavaElementDelta d = findElement(unit, children[i]);
205 static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
207 * @see java.lang.Object#toString()
209 public String toString() {
210 return PHPEditorMessages
211 .getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
215 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
217 public Object getAdapter(Class clas) {
218 if (clas == IWorkbenchAdapter.class)
225 * Content provider for the children of an ICompilationUnit or an IClassFile
227 * @see ITreeContentProvider
229 class ChildrenProvider implements ITreeContentProvider {
231 private Object[] NO_CLASS = new Object[] { new NoClassElement() };
233 private ElementChangedListener fListener;
235 protected boolean matches(IJavaElement element) {
236 if (element.getElementType() == IJavaElement.METHOD) {
237 String name = element.getElementName();
238 return (name != null && name.indexOf('<') >= 0);
243 protected IJavaElement[] filter(IJavaElement[] children) {
244 boolean initializers = false;
245 for (int i = 0; i < children.length; i++) {
246 if (matches(children[i])) {
255 Vector v = new Vector();
256 for (int i = 0; i < children.length; i++) {
257 if (matches(children[i]))
259 v.addElement(children[i]);
262 IJavaElement[] result = new IJavaElement[v.size()];
267 public Object[] getChildren(Object parent) {
268 if (parent instanceof IParent) {
269 IParent c = (IParent) parent;
271 return filter(c.getChildren());
272 } catch (JavaModelException x) {
273 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
274 // don't log NotExist exceptions as this is a valid case
275 // since we might have been posted and the element
276 // removed in the meantime.
277 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
278 PHPeclipsePlugin.log(x);
284 public Object[] getElements(Object parent) {
285 if (fTopLevelTypeOnly) {
286 if (parent instanceof ICompilationUnit) {
288 IType type = getMainType((ICompilationUnit) parent);
289 return type != null ? type.getChildren() : NO_CLASS;
290 } catch (JavaModelException e) {
291 PHPeclipsePlugin.log(e);
294 // else if (parent instanceof IClassFile) {
296 // IType type= getMainType((IClassFile) parent);
297 // return type != null ? type.getChildren() : NO_CLASS;
298 // } catch (JavaModelException e) {
299 // PHPeclipsePlugin.log(e);
303 return getChildren(parent);
306 public Object getParent(Object child) {
307 if (child instanceof IJavaElement) {
308 IJavaElement e = (IJavaElement) child;
309 return e.getParent();
314 public boolean hasChildren(Object parent) {
315 if (parent instanceof IParent) {
316 IParent c = (IParent) parent;
318 IJavaElement[] children = filter(c.getChildren());
319 return (children != null && children.length > 0);
320 } catch (JavaModelException x) {
321 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
322 // don't log NotExist exceptions as this is a valid case
323 // since we might have been posted and the element
324 // removed in the meantime.
325 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
326 PHPeclipsePlugin.log(x);
332 public boolean isDeleted(Object o) {
336 public void dispose() {
337 if (fListener != null) {
338 JavaCore.removeElementChangedListener(fListener);
344 * @see IContentProvider#inputChanged(Viewer, Object, Object)
346 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
347 boolean isCU = (newInput instanceof ICompilationUnit);
349 if (isCU && fListener == null) {
350 fListener = new ElementChangedListener();
351 JavaCore.addElementChangedListener(fListener);
352 } else if (!isCU && fListener != null) {
353 JavaCore.removeElementChangedListener(fListener);
359 class JavaOutlineViewer extends TreeViewer {
362 * Indicates an item which has been reused. At the point of its reuse it
363 * has been expanded. This field is used to communicate between
364 * <code>internalExpandToLevel</code> and <code>reuseTreeItem</code>.
366 private Item fReusedExpandedItem;
368 private boolean fReorderedMembers;
370 private boolean fForceFireSelectionChanged;
372 public JavaOutlineViewer(Tree tree) {
374 setAutoExpandLevel(ALL_LEVELS);
375 setUseHashlookup(true);
379 * Investigates the given element change event and if affected
380 * incrementally updates the Java outline.
383 * the Java element delta used to reconcile the Java outline
385 public void reconcile(IJavaElementDelta delta) {
386 fReorderedMembers = false;
387 fForceFireSelectionChanged = false;
388 if (getSorter() == null) {
389 if (fTopLevelTypeOnly && delta.getElement() instanceof IType
390 && (delta.getKind() & IJavaElementDelta.ADDED) != 0) {
394 Widget w = findItem(fInput);
395 if (w != null && !w.isDisposed())
397 if (fForceFireSelectionChanged)
398 fireSelectionChanged(new SelectionChangedEvent(
399 getSite().getSelectionProvider(), this
401 if (fReorderedMembers) {
403 fReorderedMembers = false;
413 * @see TreeViewer#internalExpandToLevel
415 protected void internalExpandToLevel(Widget node, int level) {
416 if (node instanceof Item) {
417 Item i = (Item) node;
418 if (i.getData() instanceof IJavaElement) {
419 IJavaElement je = (IJavaElement) i.getData();
420 if (je.getElementType() == IJavaElement.IMPORT_CONTAINER
421 || isInnerType(je)) {
422 if (i != fReusedExpandedItem) {
423 setExpanded(i, false);
429 super.internalExpandToLevel(node, level);
432 protected void reuseTreeItem(Item item, Object element) {
435 Item[] c = getChildren(item);
436 if (c != null && c.length > 0) {
438 if (getExpanded(item))
439 fReusedExpandedItem = item;
441 for (int k = 0; k < c.length; k++) {
442 if (c[k].getData() != null)
448 updateItem(item, element);
449 updatePlus(item, element);
450 internalExpandToLevel(item, ALL_LEVELS);
452 fReusedExpandedItem = null;
453 fForceFireSelectionChanged = true;
456 protected boolean mustUpdateParent(IJavaElementDelta delta,
457 IJavaElement element) {
458 if (element instanceof IMethod) {
459 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
461 return ((IMethod) element).isMainMethod();
462 } catch (JavaModelException e) {
463 PHPeclipsePlugin.log(e.getStatus());
466 return "main".equals(element.getElementName()); //$NON-NLS-1$
472 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
474 public boolean isExpandable(Object element) {
476 return getFilteredChildren(element).length > 0;
478 return super.isExpandable(element);
481 protected ISourceRange getSourceRange(IJavaElement element)
482 throws JavaModelException {
483 if (element instanceof ISourceReference)
484 return ((ISourceReference) element).getSourceRange();
485 if (element instanceof IMember)// && !(element instanceof
487 return ((IMember) element).getNameRange();
491 protected boolean overlaps(ISourceRange range, int start, int end) {
492 return start <= (range.getOffset() + range.getLength() - 1)
493 && range.getOffset() <= end;
496 protected boolean filtered(IJavaElement parent, IJavaElement child) {
498 Object[] result = new Object[] { child };
499 ViewerFilter[] filters = getFilters();
500 for (int i = 0; i < filters.length; i++) {
501 result = filters[i].filter(this, parent, result);
502 if (result.length == 0)
509 protected void update(Widget w, IJavaElementDelta delta) {
513 IJavaElement parent = delta.getElement();
514 IJavaElementDelta[] affected = delta.getAffectedChildren();
515 Item[] children = getChildren(w);
517 boolean doUpdateParent = false;
518 boolean doUpdateParentsPlus = false;
520 Vector deletions = new Vector();
521 Vector additions = new Vector();
523 for (int i = 0; i < affected.length; i++) {
524 IJavaElementDelta affectedDelta = affected[i];
525 IJavaElement affectedElement = affectedDelta.getElement();
526 int status = affected[i].getKind();
528 // find tree item with affected element
530 for (j = 0; j < children.length; j++)
531 if (affectedElement.equals(children[j].getData()))
534 if (j == children.length) {
535 // remove from collapsed parent
536 if ((status & IJavaElementDelta.REMOVED) != 0) {
537 doUpdateParentsPlus = true;
541 if ((status & IJavaElementDelta.CHANGED) != 0
542 && (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0
543 && !filtered(parent, affectedElement)) {
544 additions.addElement(affectedDelta);
552 if ((status & IJavaElementDelta.REMOVED) != 0) {
553 deletions.addElement(item);
554 doUpdateParent = doUpdateParent
555 || mustUpdateParent(affectedDelta, affectedElement);
558 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
559 int change = affectedDelta.getFlags();
560 doUpdateParent = doUpdateParent
561 || mustUpdateParent(affectedDelta, affectedElement);
563 if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
564 if (filtered(parent, affectedElement))
565 deletions.addElement(item);
567 updateItem(item, affectedElement);
570 if ((change & IJavaElementDelta.F_CONTENT) != 0)
571 updateItem(item, affectedElement);
573 if ((change & IJavaElementDelta.F_CHILDREN) != 0)
574 update(item, affectedDelta);
576 if ((change & IJavaElementDelta.F_REORDER) != 0)
577 fReorderedMembers = true;
581 // find all elements to add
582 IJavaElementDelta[] add = delta.getAddedChildren();
583 if (additions.size() > 0) {
584 IJavaElementDelta[] tmp = new IJavaElementDelta[add.length
586 System.arraycopy(add, 0, tmp, 0, add.length);
587 for (int i = 0; i < additions.size(); i++)
588 tmp[i + add.length] = (IJavaElementDelta) additions
593 // add at the right position
594 go2: for (int i = 0; i < add.length; i++) {
598 IJavaElement e = add[i].getElement();
599 if (filtered(parent, e))
602 doUpdateParent = doUpdateParent
603 || mustUpdateParent(add[i], e);
604 ISourceRange rng = getSourceRange(e);
605 int start = rng.getOffset();
606 int end = start + rng.getLength() - 1;
607 int nameOffset = Integer.MAX_VALUE;
608 if (e instanceof IField) {
609 ISourceRange nameRange = ((IField) e).getNameRange();
610 if (nameRange != null)
611 nameOffset = nameRange.getOffset();
616 children = getChildren(w);
618 for (int j = 0; j < children.length; j++) {
620 IJavaElement r = (IJavaElement) item.getData();
623 // parent node collapsed and not be opened before ->
629 rng = getSourceRange(r);
631 // multi-field declarations always start at
632 // the same offset. They also have the same
633 // end offset if the field sequence is terminated
634 // with a semicolon. If not, the source range
635 // ends behind the identifier / initializer
637 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=51851
638 boolean multiFieldDeclaration = r.getElementType() == IJavaElement.FIELD
639 && e.getElementType() == IJavaElement.FIELD
640 && rng.getOffset() == start;
642 // elements are inserted by occurrence
643 // however, multi-field declarations have
644 // equal source ranges offsets, therefore we
645 // compare name-range offsets.
646 boolean multiFieldOrderBefore = false;
647 if (multiFieldDeclaration) {
648 if (r instanceof IField) {
649 ISourceRange nameRange = ((IField) r)
651 if (nameRange != null) {
652 if (nameRange.getOffset() > nameOffset)
653 multiFieldOrderBefore = true;
658 if (!multiFieldDeclaration
659 && overlaps(rng, start, end)) {
661 // be tolerant if the delta is not correct, or
663 // the tree has been updated other than by a
665 reuseTreeItem(item, e);
668 } else if (multiFieldOrderBefore
669 || rng.getOffset() > start) {
671 if (last != null && deletions.contains(last)) {
673 deletions.removeElement(last);
674 reuseTreeItem(last, e);
677 createTreeItem(w, e, j);
682 } catch (JavaModelException x) {
683 // stumbled over deleted element
689 // add at the end of the list
690 if (last != null && deletions.contains(last)) {
692 deletions.removeElement(last);
693 reuseTreeItem(last, e);
696 createTreeItem(w, e, -1);
699 } catch (JavaModelException x) {
700 // the element to be added is not present -> don't add it
704 // remove items which haven't been reused
705 Enumeration e = deletions.elements();
706 while (e.hasMoreElements()) {
707 item = (Item) e.nextElement();
713 updateItem(w, delta.getElement());
714 if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
715 updatePlus((Item) w, delta.getElement());
719 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
721 protected void handleLabelProviderChanged(
722 LabelProviderChangedEvent event) {
723 Object input = getInput();
724 if (event instanceof ProblemsLabelChangedEvent) {
725 ProblemsLabelChangedEvent e = (ProblemsLabelChangedEvent) event;
726 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
727 return; // marker changes can be ignored
730 // look if the underlying resource changed
731 Object[] changed = event.getElements();
732 if (changed != null) {
733 IResource resource = getUnderlyingResource();
734 if (resource != null) {
735 for (int i = 0; i < changed.length; i++) {
736 if (changed[i] != null && changed[i].equals(resource)) {
737 // change event to a full refresh
738 event = new LabelProviderChangedEvent(
739 (IBaseLabelProvider) event.getSource());
745 super.handleLabelProviderChanged(event);
748 private IResource getUnderlyingResource() {
749 Object input = getInput();
750 if (input instanceof ICompilationUnit) {
751 ICompilationUnit cu = (ICompilationUnit) input;
752 cu = JavaModelUtil.toOriginal(cu);
753 return cu.getResource();
755 // else if (input instanceof IClassFile) {
756 // return ((IClassFile) input).getResource();
763 class LexicalSortingAction extends Action {
765 private JavaElementSorter fSorter = new JavaElementSorter();
767 public LexicalSortingAction() {
769 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
770 IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
771 setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
772 PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
773 setToolTipText(PHPEditorMessages
774 .getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
775 setDescription(PHPEditorMessages
776 .getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
778 boolean checked = WebUI.getDefault()
779 .getPreferenceStore().getBoolean(
780 "LexicalSortingAction.isChecked"); //$NON-NLS-1$
781 valueChanged(checked, false);
785 valueChanged(isChecked(), true);
788 private void valueChanged(final boolean on, boolean store) {
790 BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(),
793 fOutlineViewer.setSorter(on ? fSorter : null);
798 WebUI.getDefault().getPreferenceStore().setValue(
799 "LexicalSortingAction.isChecked", on); //$NON-NLS-1$
803 class ClassOnlyAction extends Action {
805 public ClassOnlyAction() {
807 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
808 IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
809 setText(PHPEditorMessages
810 .getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
811 setToolTipText(PHPEditorMessages
812 .getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
813 setDescription(PHPEditorMessages
814 .getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
815 PHPUiImages.setLocalImageDescriptors(this,
816 "gointo_toplevel_type.gif"); //$NON-NLS-1$
818 IPreferenceStore preferenceStore = WebUI.getDefault()
819 .getPreferenceStore();
820 boolean showclass = preferenceStore
821 .getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
822 setTopLevelTypeOnly(showclass);
826 * @see org.eclipse.jface.action.Action#run()
829 setTopLevelTypeOnly(!fTopLevelTypeOnly);
832 private void setTopLevelTypeOnly(boolean show) {
833 fTopLevelTypeOnly = show;
835 fOutlineViewer.refresh(false);
837 IPreferenceStore preferenceStore = WebUI.getDefault()
838 .getPreferenceStore();
840 .setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
845 * This action toggles whether this Java Outline page links its selection to
850 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
852 JavaOutlinePage fJavaOutlinePage;
855 * Constructs a new action.
858 * the Java outline page
860 public ToggleLinkingAction(JavaOutlinePage outlinePage) {
861 boolean isLinkingEnabled = PreferenceConstants
862 .getPreferenceStore()
864 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
865 setChecked(isLinkingEnabled);
866 fJavaOutlinePage = outlinePage;
873 PreferenceConstants.getPreferenceStore().setValue(
874 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE,
876 if (isChecked() && fEditor != null)
877 fEditor.synchronizeOutlinePage(fEditor
878 .computeHighlightRangeSourceReference(), false);
883 /** A flag to show contents of top level type only */
884 private boolean fTopLevelTypeOnly;
886 private IJavaElement fInput;
888 private String fContextMenuID;
892 private JavaOutlineViewer fOutlineViewer;
894 private PHPEditor fEditor;
896 private MemberFilterActionGroup fMemberFilterActionGroup;
898 private ListenerList fSelectionChangedListeners = new ListenerList();
900 private ListenerList fPostSelectionChangedListeners = new ListenerList();
902 private Hashtable fActions = new Hashtable();
904 private TogglePresentationAction fTogglePresentation;
906 private GotoAnnotationAction fPreviousAnnotation;
908 private GotoAnnotationAction fNextAnnotation;
910 private TextEditorAction fShowJavadoc;
912 private IAction fUndo;
914 private IAction fRedo;
916 private ToggleLinkingAction fToggleLinkingAction;
918 private CompositeActionGroup fActionGroups;
920 private IPropertyChangeListener fPropertyChangeListener;
923 * Custom filter action group.
927 private CustomFiltersActionGroup fCustomFiltersActionGroup;
929 public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
932 Assert.isNotNull(editor);
934 fContextMenuID = contextMenuID;
936 fTogglePresentation = new TogglePresentationAction();
937 ResourceBundle bundle = PHPEditorMessages.getResourceBundle();
938 fPreviousAnnotation = new GotoAnnotationAction(bundle,
939 "PreviousAnnotation.", null, false); //$NON-NLS-1$
940 fNextAnnotation = new GotoAnnotationAction(bundle,
941 "NextAnnotation.", null, true); //$NON-NLS-1$
942 fShowJavadoc = (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
943 fUndo = fEditor.getAction(ITextEditorActionConstants.UNDO);
944 fRedo = fEditor.getAction(ITextEditorActionConstants.REDO);
946 fTogglePresentation.setEditor(editor);
947 fPreviousAnnotation.setEditor(editor);
948 fNextAnnotation.setEditor(editor);
950 fPropertyChangeListener = new IPropertyChangeListener() {
951 public void propertyChange(PropertyChangeEvent event) {
952 doPropertyChange(event);
955 WebUI.getDefault().getPreferenceStore()
956 .addPropertyChangeListener(fPropertyChangeListener);
960 * Returns the primary type of a compilation unit (has the same name as the
963 * @param compilationUnit
964 * the compilation unit
965 * @return returns the primary type of the compilation unit, or
966 * <code>null</code> if is does not have one
968 protected IType getMainType(ICompilationUnit compilationUnit) {
970 if (compilationUnit == null)
973 String name = compilationUnit.getElementName();
974 int index = name.indexOf('.');
976 name = name.substring(0, index);
977 IType type = compilationUnit.getType(name);
978 return type.exists() ? type : null;
982 * Returns the primary type of a class file.
986 * @return returns the primary type of the class file, or <code>null</code>
987 * if is does not have one
989 // protected IType getMainType(IClassFile classFile) {
991 // IType type= classFile.getType();
992 // return type != null && type.exists() ? type : null;
993 // } catch (JavaModelException e) {
998 * (non-Javadoc) Method declared on Page
1000 public void init(IPageSite pageSite) {
1001 super.init(pageSite);
1004 private void doPropertyChange(PropertyChangeEvent event) {
1005 if (fOutlineViewer != null) {
1006 if (MembersOrderPreferenceCache.isMemberOrderProperty(event
1008 fOutlineViewer.refresh(false);
1014 * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
1016 public void addSelectionChangedListener(ISelectionChangedListener listener) {
1017 if (fOutlineViewer != null)
1018 fOutlineViewer.addSelectionChangedListener(listener);
1020 fSelectionChangedListeners.add(listener);
1024 * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
1026 public void removeSelectionChangedListener(
1027 ISelectionChangedListener listener) {
1028 if (fOutlineViewer != null)
1029 fOutlineViewer.removeSelectionChangedListener(listener);
1031 fSelectionChangedListeners.remove(listener);
1035 * @see ISelectionProvider#setSelection(ISelection)
1037 public void setSelection(ISelection selection) {
1038 if (fOutlineViewer != null)
1039 fOutlineViewer.setSelection(selection);
1043 * @see ISelectionProvider#getSelection()
1045 public ISelection getSelection() {
1046 if (fOutlineViewer == null)
1047 return StructuredSelection.EMPTY;
1048 return fOutlineViewer.getSelection();
1052 * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1054 public void addPostSelectionChangedListener(
1055 ISelectionChangedListener listener) {
1056 if (fOutlineViewer != null)
1057 fOutlineViewer.addPostSelectionChangedListener(listener);
1059 fPostSelectionChangedListeners.add(listener);
1063 * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1065 public void removePostSelectionChangedListener(
1066 ISelectionChangedListener listener) {
1067 if (fOutlineViewer != null)
1068 fOutlineViewer.removePostSelectionChangedListener(listener);
1070 fPostSelectionChangedListeners.remove(listener);
1073 private void registerToolbarActions(IActionBars actionBars) {
1075 IToolBarManager toolBarManager = actionBars.getToolBarManager();
1076 if (toolBarManager != null) {
1077 toolBarManager.add(new LexicalSortingAction());
1079 fMemberFilterActionGroup = new MemberFilterActionGroup(
1081 "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1082 fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1084 fCustomFiltersActionGroup.fillActionBars(actionBars);
1086 IMenuManager menu = actionBars.getMenuManager();
1087 menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1089 fToggleLinkingAction = new ToggleLinkingAction(this);
1090 menu.add(new ClassOnlyAction());
1091 menu.add(fToggleLinkingAction);
1096 * @see IPage#createControl
1098 public void createControl(Composite parent) {
1100 Tree tree = new Tree(parent, SWT.MULTI);
1102 AppearanceAwareLabelProvider lprovider = new AppearanceAwareLabelProvider(
1103 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
1104 | JavaElementLabels.F_APP_TYPE_SIGNATURE,
1105 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
1107 fOutlineViewer = new JavaOutlineViewer(tree);
1109 fOutlineViewer.setContentProvider(new ChildrenProvider());
1110 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(
1113 Object[] listeners = fSelectionChangedListeners.getListeners();
1114 for (int i = 0; i < listeners.length; i++) {
1115 fSelectionChangedListeners.remove(listeners[i]);
1117 .addSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1120 listeners = fPostSelectionChangedListeners.getListeners();
1121 for (int i = 0; i < listeners.length; i++) {
1122 fPostSelectionChangedListeners.remove(listeners[i]);
1124 .addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1127 MenuManager manager = new MenuManager(fContextMenuID, fContextMenuID);
1128 manager.setRemoveAllWhenShown(true);
1129 manager.addMenuListener(new IMenuListener() {
1130 public void menuAboutToShow(IMenuManager m) {
1131 contextMenuAboutToShow(m);
1134 fMenu = manager.createContextMenu(tree);
1135 tree.setMenu(fMenu);
1137 IPageSite site = getSite();
1139 .registerContextMenu(PHPeclipsePlugin.getPluginId()
1140 + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1141 site.setSelectionProvider(fOutlineViewer);
1143 // we must create the groups after we have set the selection provider to
1145 fActionGroups = new CompositeActionGroup(new ActionGroup[] {
1146 // new OpenViewActionGroup(this),
1147 // new CCPActionGroup(this),
1148 new GenerateActionGroup(this) });
1149 // new RefactorActionGroup(this),
1150 // new JavaSearchActionGroup(this)});
1152 // register global actions
1153 IActionBars bars = site.getActionBars();
1155 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1156 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1157 bars.setGlobalActionHandler(ActionFactory.PREVIOUS.getId(),
1158 fPreviousAnnotation);
1160 .setGlobalActionHandler(ActionFactory.NEXT.getId(),
1162 bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC,
1165 .setGlobalActionHandler(
1166 ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY,
1167 fTogglePresentation);
1168 bars.setGlobalActionHandler(
1169 ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION,
1171 bars.setGlobalActionHandler(
1172 ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION,
1173 fPreviousAnnotation);
1175 fActionGroups.fillActionBars(bars);
1177 IStatusLineManager statusLineManager = bars.getStatusLineManager();
1178 if (statusLineManager != null) {
1179 StatusBarUpdater updater = new StatusBarUpdater(statusLineManager);
1180 fOutlineViewer.addPostSelectionChangedListener(updater);
1182 // Custom filter group
1183 fCustomFiltersActionGroup = new CustomFiltersActionGroup(
1184 "net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1186 registerToolbarActions(bars);
1188 fOutlineViewer.setInput(fInput);
1191 public void dispose() {
1193 if (fEditor == null)
1196 if (fMemberFilterActionGroup != null) {
1197 fMemberFilterActionGroup.dispose();
1198 fMemberFilterActionGroup = null;
1201 if (fCustomFiltersActionGroup != null) {
1202 fCustomFiltersActionGroup.dispose();
1203 fCustomFiltersActionGroup = null;
1206 fEditor.outlinePageClosed();
1209 fSelectionChangedListeners.clear();
1210 fSelectionChangedListeners = null;
1212 fPostSelectionChangedListeners.clear();
1213 fPostSelectionChangedListeners = null;
1215 if (fPropertyChangeListener != null) {
1216 WebUI.getDefault().getPreferenceStore()
1217 .removePropertyChangeListener(fPropertyChangeListener);
1218 fPropertyChangeListener = null;
1221 if (fMenu != null && !fMenu.isDisposed()) {
1226 if (fActionGroups != null)
1227 fActionGroups.dispose();
1229 fTogglePresentation.setEditor(null);
1230 fPreviousAnnotation.setEditor(null);
1231 fNextAnnotation.setEditor(null);
1233 fOutlineViewer = null;
1238 public Control getControl() {
1239 if (fOutlineViewer != null)
1240 return fOutlineViewer.getControl();
1244 public void setInput(IJavaElement inputElement) {
1245 fInput = inputElement;
1246 if (fOutlineViewer != null)
1247 fOutlineViewer.setInput(fInput);
1250 public void select(ISourceReference reference) {
1251 if (fOutlineViewer != null) {
1253 ISelection s = fOutlineViewer.getSelection();
1254 if (s instanceof IStructuredSelection) {
1255 IStructuredSelection ss = (IStructuredSelection) s;
1256 List elements = ss.toList();
1257 if (!elements.contains(reference)) {
1258 s = (reference == null ? StructuredSelection.EMPTY
1259 : new StructuredSelection(reference));
1260 fOutlineViewer.setSelection(s, true);
1266 public void setAction(String actionID, IAction action) {
1267 Assert.isNotNull(actionID);
1269 fActions.remove(actionID);
1271 fActions.put(actionID, action);
1274 public IAction getAction(String actionID) {
1275 Assert.isNotNull(actionID);
1276 return (IAction) fActions.get(actionID);
1280 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1282 public Object getAdapter(Class key) {
1283 if (key == IShowInSource.class) {
1284 return getShowInSource();
1286 if (key == IShowInTargetList.class) {
1287 return new IShowInTargetList() {
1288 public String[] getShowInTargetIds() {
1289 return new String[] { JavaUI.ID_PACKAGES };
1294 if (key == IShowInTarget.class) {
1295 return getShowInTarget();
1302 * Convenience method to add the action installed under the given actionID
1303 * to the specified group of the menu.
1308 * the group to which to add the action
1310 * the ID of the new action
1312 protected void addAction(IMenuManager menu, String group, String actionID) {
1313 IAction action = getAction(actionID);
1314 if (action != null) {
1315 if (action instanceof IUpdate)
1316 ((IUpdate) action).update();
1318 if (action.isEnabled()) {
1319 IMenuManager subMenu = menu.findMenuUsingPath(group);
1320 if (subMenu != null)
1321 subMenu.add(action);
1323 menu.appendToGroup(group, action);
1328 protected void contextMenuAboutToShow(IMenuManager menu) {
1330 WebUI.createStandardGroups(menu);
1332 IStructuredSelection selection = (IStructuredSelection) getSelection();
1333 fActionGroups.setContext(new ActionContext(selection));
1334 fActionGroups.fillContextMenu(menu);
1338 * @see Page#setFocus()
1340 public void setFocus() {
1341 if (fOutlineViewer != null)
1342 fOutlineViewer.getControl().setFocus();
1346 * Checks whether a given Java element is an inner type.
1350 * @return <code>true</code> iff the given element is an inner type
1352 private boolean isInnerType(IJavaElement element) {
1354 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1355 IType type = (IType) element;
1357 return type.isMember();
1358 } catch (JavaModelException e) {
1359 IJavaElement parent = type.getParent();
1360 if (parent != null) {
1361 int parentElementType = parent.getElementType();
1362 return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
1371 * Returns the <code>IShowInSource</code> for this view.
1373 * @return the {@link IShowInSource}
1375 protected IShowInSource getShowInSource() {
1376 return new IShowInSource() {
1377 public ShowInContext getShowInContext() {
1378 return new ShowInContext(null, getSite().getSelectionProvider()
1385 * Returns the <code>IShowInTarget</code> for this view.
1387 * @return the {@link IShowInTarget}
1389 protected IShowInTarget getShowInTarget() {
1390 return new IShowInTarget() {
1391 public boolean show(ShowInContext context) {
1392 ISelection sel = context.getSelection();
1393 if (sel instanceof ITextSelection) {
1394 ITextSelection tsel = (ITextSelection) sel;
1395 int offset = tsel.getOffset();
1396 IJavaElement element = fEditor.getElementAt(offset);
1397 if (element != null) {
1398 setSelection(new StructuredSelection(element));
1407 private void initDragAndDrop() {
1408 int ops = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1409 Transfer[] transfers = new Transfer[] { LocalSelectionTransfer
1413 // TransferDropTargetListener[] dropListeners= new
1414 // TransferDropTargetListener[] {
1415 // new SelectionTransferDropAdapter(fOutlineViewer)
1417 // fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new
1418 // DelegatingDropAdapter(dropListeners));
1421 // TransferDragSourceListener[] dragListeners= new
1422 // TransferDragSourceListener[] {
1423 // new SelectionTransferDragAdapter(fOutlineViewer)
1425 // fOutlineViewer.addDragSupport(ops, transfers, new
1426 // JdtViewerDragAdapter(fOutlineViewer, dragListeners));