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;
53 import org.eclipse.core.resources.IResource;
54 import org.eclipse.core.runtime.IAdaptable;
55 import org.eclipse.core.runtime.ListenerList;
56 import org.eclipse.jface.action.Action;
57 import org.eclipse.jface.action.IAction;
58 import org.eclipse.jface.action.IMenuListener;
59 import org.eclipse.jface.action.IMenuManager;
60 import org.eclipse.jface.action.IStatusLineManager;
61 import org.eclipse.jface.action.IToolBarManager;
62 import org.eclipse.jface.action.MenuManager;
63 import org.eclipse.jface.action.Separator;
64 import org.eclipse.jface.preference.IPreferenceStore;
66 //import org.eclipse.jface.text.Assert;
67 import org.eclipse.core.runtime.Assert;
68 import org.eclipse.jface.text.ITextSelection;
69 import org.eclipse.jface.util.IPropertyChangeListener;
70 import org.eclipse.jface.util.PropertyChangeEvent;
71 import org.eclipse.jface.viewers.IBaseLabelProvider;
72 import org.eclipse.jface.viewers.IPostSelectionProvider;
73 import org.eclipse.jface.viewers.ISelection;
74 import org.eclipse.jface.viewers.ISelectionChangedListener;
75 import org.eclipse.jface.viewers.IStructuredSelection;
76 import org.eclipse.jface.viewers.ITreeContentProvider;
77 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
78 import org.eclipse.jface.viewers.SelectionChangedEvent;
79 import org.eclipse.jface.viewers.StructuredSelection;
80 import org.eclipse.jface.viewers.TreeViewer;
81 import org.eclipse.jface.viewers.Viewer;
82 import org.eclipse.jface.viewers.ViewerFilter;
83 import org.eclipse.swt.SWT;
84 import org.eclipse.swt.custom.BusyIndicator;
85 //import org.eclipse.swt.dnd.DND;
86 //import org.eclipse.swt.dnd.Transfer;
87 import org.eclipse.swt.widgets.Composite;
88 import org.eclipse.swt.widgets.Control;
89 import org.eclipse.swt.widgets.Display;
90 import org.eclipse.swt.widgets.Item;
91 import org.eclipse.swt.widgets.Menu;
92 import org.eclipse.swt.widgets.Tree;
93 import org.eclipse.swt.widgets.Widget;
94 import org.eclipse.ui.IActionBars;
95 import org.eclipse.ui.PlatformUI;
96 import org.eclipse.ui.actions.ActionContext;
97 import org.eclipse.ui.actions.ActionFactory;
98 import org.eclipse.ui.actions.ActionGroup;
99 import org.eclipse.ui.model.IWorkbenchAdapter;
100 import org.eclipse.ui.model.WorkbenchAdapter;
101 import org.eclipse.ui.part.IPageSite;
102 import org.eclipse.ui.part.IShowInSource;
103 import org.eclipse.ui.part.IShowInTarget;
104 import org.eclipse.ui.part.IShowInTargetList;
105 import org.eclipse.ui.part.Page;
106 import org.eclipse.ui.part.ShowInContext;
107 import org.eclipse.ui.texteditor.GotoAnnotationAction;
108 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
109 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
110 import org.eclipse.ui.texteditor.IUpdate;
111 import org.eclipse.ui.texteditor.TextEditorAction;
112 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
113 //import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
116 * The content outline page of the Java editor. The viewer implements a
117 * proprietary update mechanism based on Java model deltas. It does not react on
118 * domain changes. It is specified to show the content of ICompilationUnits and
119 * IClassFiles. Publishes its context menu under
120 * <code>PHPeclipsePlugin.getDefault().getPluginId() + ".outline"</code>.
122 public class JavaOutlinePage extends Page implements IContentOutlinePage,
123 IAdaptable, IPostSelectionProvider {
125 static Object[] NO_CHILDREN = new Object[0];
128 * The element change listener of the java outline viewer.
130 * @see IElementChangedListener
132 class ElementChangedListener implements IElementChangedListener {
134 public void elementChanged(final ElementChangedEvent e) {
136 if (getControl() == null)
139 Display d = getControl().getDisplay();
141 d.asyncExec(new Runnable() {
143 //ICompilationUnit cu = (ICompilationUnit) fInput;
144 //IJavaElement base = cu;
145 // if (fTopLevelTypeOnly) {
146 // base= getMainType(cu);
147 // if (base == null) {
148 if (fOutlineViewer != null)
149 fOutlineViewer.refresh(true);
153 // IJavaElementDelta delta= findElement(base,
155 // if (delta != null && fOutlineViewer != null) {
156 // fOutlineViewer.reconcile(delta);
163 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
164 if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
165 return true; // add or remove
167 int flags = cuDelta.getFlags();
168 if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
171 return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
174 protected IJavaElementDelta findElement(IJavaElement unit,
175 IJavaElementDelta delta) {
177 if (delta == null || unit == null)
180 IJavaElement element = delta.getElement();
182 if (unit.equals(element)) {
183 if (isPossibleStructuralChange(delta)) {
189 if (element.getElementType() > IJavaElement.CLASS_FILE)
192 IJavaElementDelta[] children = delta.getAffectedChildren();
193 if (children == null || children.length == 0)
196 for (int i = 0; i < children.length; i++) {
197 IJavaElementDelta d = findElement(unit, children[i]);
206 static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
208 * @see java.lang.Object#toString()
210 public String toString() {
211 return PHPEditorMessages
212 .getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
216 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
218 public Object getAdapter(Class clas) {
219 if (clas == IWorkbenchAdapter.class)
226 * Content provider for the children of an ICompilationUnit or an IClassFile
228 * @see ITreeContentProvider
230 class ChildrenProvider implements ITreeContentProvider {
232 private Object[] NO_CLASS = new Object[] { new NoClassElement() };
234 private ElementChangedListener fListener;
236 protected boolean matches(IJavaElement element) {
237 if (element.getElementType() == IJavaElement.METHOD) {
238 String name = element.getElementName();
239 return (name != null && name.indexOf('<') >= 0);
244 protected IJavaElement[] filter(IJavaElement[] children) {
245 boolean initializers = false;
246 for (int i = 0; i < children.length; i++) {
247 if (matches(children[i])) {
256 Vector v = new Vector();
257 for (int i = 0; i < children.length; i++) {
258 if (matches(children[i]))
260 v.addElement(children[i]);
263 IJavaElement[] result = new IJavaElement[v.size()];
268 public Object[] getChildren(Object parent) {
269 if (parent instanceof IParent) {
270 IParent c = (IParent) parent;
272 return filter(c.getChildren());
273 } catch (JavaModelException x) {
274 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
275 // don't log NotExist exceptions as this is a valid case
276 // since we might have been posted and the element
277 // removed in the meantime.
278 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
279 PHPeclipsePlugin.log(x);
285 public Object[] getElements(Object parent) {
286 if (fTopLevelTypeOnly) {
287 if (parent instanceof ICompilationUnit) {
289 IType type = getMainType((ICompilationUnit) parent);
290 return type != null ? type.getChildren() : NO_CLASS;
291 } catch (JavaModelException e) {
292 PHPeclipsePlugin.log(e);
295 // else if (parent instanceof IClassFile) {
297 // IType type= getMainType((IClassFile) parent);
298 // return type != null ? type.getChildren() : NO_CLASS;
299 // } catch (JavaModelException e) {
300 // PHPeclipsePlugin.log(e);
304 return getChildren(parent);
307 public Object getParent(Object child) {
308 if (child instanceof IJavaElement) {
309 IJavaElement e = (IJavaElement) child;
310 return e.getParent();
315 public boolean hasChildren(Object parent) {
316 if (parent instanceof IParent) {
317 IParent c = (IParent) parent;
319 IJavaElement[] children = filter(c.getChildren());
320 return (children != null && children.length > 0);
321 } catch (JavaModelException x) {
322 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
323 // don't log NotExist exceptions as this is a valid case
324 // since we might have been posted and the element
325 // removed in the meantime.
326 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
327 PHPeclipsePlugin.log(x);
333 public boolean isDeleted(Object o) {
337 public void dispose() {
338 if (fListener != null) {
339 JavaCore.removeElementChangedListener(fListener);
345 * @see IContentProvider#inputChanged(Viewer, Object, Object)
347 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
348 boolean isCU = (newInput instanceof ICompilationUnit);
350 if (isCU && fListener == null) {
351 fListener = new ElementChangedListener();
352 JavaCore.addElementChangedListener(fListener);
353 } else if (!isCU && fListener != null) {
354 JavaCore.removeElementChangedListener(fListener);
360 class JavaOutlineViewer extends TreeViewer {
363 * Indicates an item which has been reused. At the point of its reuse it
364 * has been expanded. This field is used to communicate between
365 * <code>internalExpandToLevel</code> and <code>reuseTreeItem</code>.
367 private Item fReusedExpandedItem;
369 private boolean fReorderedMembers;
371 private boolean fForceFireSelectionChanged;
373 public JavaOutlineViewer(Tree tree) {
375 setAutoExpandLevel(ALL_LEVELS);
376 setUseHashlookup(true);
380 * Investigates the given element change event and if affected
381 * incrementally updates the Java outline.
384 * the Java element delta used to reconcile the Java outline
386 public void reconcile(IJavaElementDelta delta) {
387 fReorderedMembers = false;
388 fForceFireSelectionChanged = false;
389 if (getSorter() == null) {
390 if (fTopLevelTypeOnly && delta.getElement() instanceof IType
391 && (delta.getKind() & IJavaElementDelta.ADDED) != 0) {
395 Widget w = findItem(fInput);
396 if (w != null && !w.isDisposed())
398 if (fForceFireSelectionChanged)
399 fireSelectionChanged(new SelectionChangedEvent(
400 getSite().getSelectionProvider(), this
402 if (fReorderedMembers) {
404 fReorderedMembers = false;
414 * @see TreeViewer#internalExpandToLevel
416 protected void internalExpandToLevel(Widget node, int level) {
417 if (node instanceof Item) {
418 Item i = (Item) node;
419 if (i.getData() instanceof IJavaElement) {
420 IJavaElement je = (IJavaElement) i.getData();
421 if (je.getElementType() == IJavaElement.IMPORT_CONTAINER
422 || isInnerType(je)) {
423 if (i != fReusedExpandedItem) {
424 setExpanded(i, false);
430 super.internalExpandToLevel(node, level);
433 protected void reuseTreeItem(Item item, Object element) {
436 Item[] c = getChildren(item);
437 if (c != null && c.length > 0) {
439 if (getExpanded(item))
440 fReusedExpandedItem = item;
442 for (int k = 0; k < c.length; k++) {
443 if (c[k].getData() != null)
449 updateItem(item, element);
450 updatePlus(item, element);
451 internalExpandToLevel(item, ALL_LEVELS);
453 fReusedExpandedItem = null;
454 fForceFireSelectionChanged = true;
457 protected boolean mustUpdateParent(IJavaElementDelta delta,
458 IJavaElement element) {
459 if (element instanceof IMethod) {
460 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
462 return ((IMethod) element).isMainMethod();
463 } catch (JavaModelException e) {
464 PHPeclipsePlugin.log(e.getStatus());
467 return "main".equals(element.getElementName()); //$NON-NLS-1$
473 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
475 public boolean isExpandable(Object element) {
477 return getFilteredChildren(element).length > 0;
479 return super.isExpandable(element);
482 protected ISourceRange getSourceRange(IJavaElement element)
483 throws JavaModelException {
484 if (element instanceof ISourceReference)
485 return ((ISourceReference) element).getSourceRange();
486 if (element instanceof IMember)// && !(element instanceof
488 return ((IMember) element).getNameRange();
492 protected boolean overlaps(ISourceRange range, int start, int end) {
493 return start <= (range.getOffset() + range.getLength() - 1)
494 && range.getOffset() <= end;
497 protected boolean filtered(IJavaElement parent, IJavaElement child) {
499 Object[] result = new Object[] { child };
500 ViewerFilter[] filters = getFilters();
501 for (int i = 0; i < filters.length; i++) {
502 result = filters[i].filter(this, parent, result);
503 if (result.length == 0)
510 protected void update(Widget w, IJavaElementDelta delta) {
514 IJavaElement parent = delta.getElement();
515 IJavaElementDelta[] affected = delta.getAffectedChildren();
516 Item[] children = getChildren(w);
518 boolean doUpdateParent = false;
519 boolean doUpdateParentsPlus = false;
521 Vector deletions = new Vector();
522 Vector additions = new Vector();
524 for (int i = 0; i < affected.length; i++) {
525 IJavaElementDelta affectedDelta = affected[i];
526 IJavaElement affectedElement = affectedDelta.getElement();
527 int status = affected[i].getKind();
529 // find tree item with affected element
531 for (j = 0; j < children.length; j++)
532 if (affectedElement.equals(children[j].getData()))
535 if (j == children.length) {
536 // remove from collapsed parent
537 if ((status & IJavaElementDelta.REMOVED) != 0) {
538 doUpdateParentsPlus = true;
542 if ((status & IJavaElementDelta.CHANGED) != 0
543 && (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0
544 && !filtered(parent, affectedElement)) {
545 additions.addElement(affectedDelta);
553 if ((status & IJavaElementDelta.REMOVED) != 0) {
554 deletions.addElement(item);
555 doUpdateParent = doUpdateParent
556 || mustUpdateParent(affectedDelta, affectedElement);
559 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
560 int change = affectedDelta.getFlags();
561 doUpdateParent = doUpdateParent
562 || mustUpdateParent(affectedDelta, affectedElement);
564 if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
565 if (filtered(parent, affectedElement))
566 deletions.addElement(item);
568 updateItem(item, affectedElement);
571 if ((change & IJavaElementDelta.F_CONTENT) != 0)
572 updateItem(item, affectedElement);
574 if ((change & IJavaElementDelta.F_CHILDREN) != 0)
575 update(item, affectedDelta);
577 if ((change & IJavaElementDelta.F_REORDER) != 0)
578 fReorderedMembers = true;
582 // find all elements to add
583 IJavaElementDelta[] add = delta.getAddedChildren();
584 if (additions.size() > 0) {
585 IJavaElementDelta[] tmp = new IJavaElementDelta[add.length
587 System.arraycopy(add, 0, tmp, 0, add.length);
588 for (int i = 0; i < additions.size(); i++)
589 tmp[i + add.length] = (IJavaElementDelta) additions
594 // add at the right position
595 go2: for (int i = 0; i < add.length; i++) {
599 IJavaElement e = add[i].getElement();
600 if (filtered(parent, e))
603 doUpdateParent = doUpdateParent
604 || mustUpdateParent(add[i], e);
605 ISourceRange rng = getSourceRange(e);
606 int start = rng.getOffset();
607 int end = start + rng.getLength() - 1;
608 int nameOffset = Integer.MAX_VALUE;
609 if (e instanceof IField) {
610 ISourceRange nameRange = ((IField) e).getNameRange();
611 if (nameRange != null)
612 nameOffset = nameRange.getOffset();
617 children = getChildren(w);
619 for (int j = 0; j < children.length; j++) {
621 IJavaElement r = (IJavaElement) item.getData();
624 // parent node collapsed and not be opened before ->
630 rng = getSourceRange(r);
632 // multi-field declarations always start at
633 // the same offset. They also have the same
634 // end offset if the field sequence is terminated
635 // with a semicolon. If not, the source range
636 // ends behind the identifier / initializer
638 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=51851
639 boolean multiFieldDeclaration = r.getElementType() == IJavaElement.FIELD
640 && e.getElementType() == IJavaElement.FIELD
641 && rng.getOffset() == start;
643 // elements are inserted by occurrence
644 // however, multi-field declarations have
645 // equal source ranges offsets, therefore we
646 // compare name-range offsets.
647 boolean multiFieldOrderBefore = false;
648 if (multiFieldDeclaration) {
649 if (r instanceof IField) {
650 ISourceRange nameRange = ((IField) r)
652 if (nameRange != null) {
653 if (nameRange.getOffset() > nameOffset)
654 multiFieldOrderBefore = true;
659 if (!multiFieldDeclaration
660 && overlaps(rng, start, end)) {
662 // be tolerant if the delta is not correct, or
664 // the tree has been updated other than by a
666 reuseTreeItem(item, e);
669 } else if (multiFieldOrderBefore
670 || rng.getOffset() > start) {
672 if (last != null && deletions.contains(last)) {
674 deletions.removeElement(last);
675 reuseTreeItem(last, e);
678 createTreeItem(w, e, j);
683 } catch (JavaModelException x) {
684 // stumbled over deleted element
690 // add at the end of the list
691 if (last != null && deletions.contains(last)) {
693 deletions.removeElement(last);
694 reuseTreeItem(last, e);
697 createTreeItem(w, e, -1);
700 } catch (JavaModelException x) {
701 // the element to be added is not present -> don't add it
705 // remove items which haven't been reused
706 Enumeration e = deletions.elements();
707 while (e.hasMoreElements()) {
708 item = (Item) e.nextElement();
714 updateItem(w, delta.getElement());
715 if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
716 updatePlus((Item) w, delta.getElement());
720 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
722 protected void handleLabelProviderChanged(
723 LabelProviderChangedEvent event) {
724 Object input = getInput();
725 if (event instanceof ProblemsLabelChangedEvent) {
726 ProblemsLabelChangedEvent e = (ProblemsLabelChangedEvent) event;
727 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
728 return; // marker changes can be ignored
731 // look if the underlying resource changed
732 Object[] changed = event.getElements();
733 if (changed != null) {
734 IResource resource = getUnderlyingResource();
735 if (resource != null) {
736 for (int i = 0; i < changed.length; i++) {
737 if (changed[i] != null && changed[i].equals(resource)) {
738 // change event to a full refresh
739 event = new LabelProviderChangedEvent(
740 (IBaseLabelProvider) event.getSource());
746 super.handleLabelProviderChanged(event);
749 private IResource getUnderlyingResource() {
750 Object input = getInput();
751 if (input instanceof ICompilationUnit) {
752 ICompilationUnit cu = (ICompilationUnit) input;
753 cu = JavaModelUtil.toOriginal(cu);
754 return cu.getResource();
756 // else if (input instanceof IClassFile) {
757 // return ((IClassFile) input).getResource();
764 class LexicalSortingAction extends Action {
766 private JavaElementSorter fSorter = new JavaElementSorter();
768 public LexicalSortingAction() {
770 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
771 IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
772 setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
773 PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
774 setToolTipText(PHPEditorMessages
775 .getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
776 setDescription(PHPEditorMessages
777 .getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
779 boolean checked = PHPeclipsePlugin.getDefault()
780 .getPreferenceStore().getBoolean(
781 "LexicalSortingAction.isChecked"); //$NON-NLS-1$
782 valueChanged(checked, false);
786 valueChanged(isChecked(), true);
789 private void valueChanged(final boolean on, boolean store) {
791 BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(),
794 fOutlineViewer.setSorter(on ? fSorter : null);
799 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue(
800 "LexicalSortingAction.isChecked", on); //$NON-NLS-1$
804 class ClassOnlyAction extends Action {
806 public ClassOnlyAction() {
808 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
809 IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
810 setText(PHPEditorMessages
811 .getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
812 setToolTipText(PHPEditorMessages
813 .getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
814 setDescription(PHPEditorMessages
815 .getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
816 PHPUiImages.setLocalImageDescriptors(this,
817 "gointo_toplevel_type.gif"); //$NON-NLS-1$
819 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault()
820 .getPreferenceStore();
821 boolean showclass = preferenceStore
822 .getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
823 setTopLevelTypeOnly(showclass);
827 * @see org.eclipse.jface.action.Action#run()
830 setTopLevelTypeOnly(!fTopLevelTypeOnly);
833 private void setTopLevelTypeOnly(boolean show) {
834 fTopLevelTypeOnly = show;
836 fOutlineViewer.refresh(false);
838 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault()
839 .getPreferenceStore();
841 .setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
846 * This action toggles whether this Java Outline page links its selection to
851 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
853 JavaOutlinePage fJavaOutlinePage;
856 * Constructs a new action.
859 * the Java outline page
861 public ToggleLinkingAction(JavaOutlinePage outlinePage) {
862 boolean isLinkingEnabled = PreferenceConstants
863 .getPreferenceStore()
865 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
866 setChecked(isLinkingEnabled);
867 fJavaOutlinePage = outlinePage;
874 PreferenceConstants.getPreferenceStore().setValue(
875 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE,
877 if (isChecked() && fEditor != null)
878 fEditor.synchronizeOutlinePage(fEditor
879 .computeHighlightRangeSourceReference(), false);
884 /** A flag to show contents of top level type only */
885 private boolean fTopLevelTypeOnly;
887 private IJavaElement fInput;
889 private String fContextMenuID;
893 private JavaOutlineViewer fOutlineViewer;
895 private PHPEditor fEditor;
897 private MemberFilterActionGroup fMemberFilterActionGroup;
899 private ListenerList fSelectionChangedListeners = new ListenerList();
901 private ListenerList fPostSelectionChangedListeners = new ListenerList();
903 private Hashtable fActions = new Hashtable();
905 private TogglePresentationAction fTogglePresentation;
907 private GotoAnnotationAction fPreviousAnnotation;
909 private GotoAnnotationAction fNextAnnotation;
911 private TextEditorAction fShowJavadoc;
913 private IAction fUndo;
915 private IAction fRedo;
917 private ToggleLinkingAction fToggleLinkingAction;
919 private CompositeActionGroup fActionGroups;
921 private IPropertyChangeListener fPropertyChangeListener;
924 * Custom filter action group.
928 private CustomFiltersActionGroup fCustomFiltersActionGroup;
930 public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
933 Assert.isNotNull(editor);
935 fContextMenuID = contextMenuID;
937 fTogglePresentation = new TogglePresentationAction();
938 ResourceBundle bundle = PHPEditorMessages.getResourceBundle();
939 fPreviousAnnotation = new GotoAnnotationAction(bundle,
940 "PreviousAnnotation.", null, false); //$NON-NLS-1$
941 fNextAnnotation = new GotoAnnotationAction(bundle,
942 "NextAnnotation.", null, true); //$NON-NLS-1$
943 fShowJavadoc = (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
944 fUndo = fEditor.getAction(ITextEditorActionConstants.UNDO);
945 fRedo = fEditor.getAction(ITextEditorActionConstants.REDO);
947 fTogglePresentation.setEditor(editor);
948 fPreviousAnnotation.setEditor(editor);
949 fNextAnnotation.setEditor(editor);
951 fPropertyChangeListener = new IPropertyChangeListener() {
952 public void propertyChange(PropertyChangeEvent event) {
953 doPropertyChange(event);
956 PHPeclipsePlugin.getDefault().getPreferenceStore()
957 .addPropertyChangeListener(fPropertyChangeListener);
961 * Returns the primary type of a compilation unit (has the same name as the
964 * @param compilationUnit
965 * the compilation unit
966 * @return returns the primary type of the compilation unit, or
967 * <code>null</code> if is does not have one
969 protected IType getMainType(ICompilationUnit compilationUnit) {
971 if (compilationUnit == null)
974 String name = compilationUnit.getElementName();
975 int index = name.indexOf('.');
977 name = name.substring(0, index);
978 IType type = compilationUnit.getType(name);
979 return type.exists() ? type : null;
983 * Returns the primary type of a class file.
987 * @return returns the primary type of the class file, or <code>null</code>
988 * if is does not have one
990 // protected IType getMainType(IClassFile classFile) {
992 // IType type= classFile.getType();
993 // return type != null && type.exists() ? type : null;
994 // } catch (JavaModelException e) {
999 * (non-Javadoc) Method declared on Page
1001 public void init(IPageSite pageSite) {
1002 super.init(pageSite);
1005 private void doPropertyChange(PropertyChangeEvent event) {
1006 if (fOutlineViewer != null) {
1007 if (MembersOrderPreferenceCache.isMemberOrderProperty(event
1009 fOutlineViewer.refresh(false);
1015 * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
1017 public void addSelectionChangedListener(ISelectionChangedListener listener) {
1018 if (fOutlineViewer != null)
1019 fOutlineViewer.addSelectionChangedListener(listener);
1021 fSelectionChangedListeners.add(listener);
1025 * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
1027 public void removeSelectionChangedListener(
1028 ISelectionChangedListener listener) {
1029 if (fOutlineViewer != null)
1030 fOutlineViewer.removeSelectionChangedListener(listener);
1032 fSelectionChangedListeners.remove(listener);
1036 * @see ISelectionProvider#setSelection(ISelection)
1038 public void setSelection(ISelection selection) {
1039 if (fOutlineViewer != null)
1040 fOutlineViewer.setSelection(selection);
1044 * @see ISelectionProvider#getSelection()
1046 public ISelection getSelection() {
1047 if (fOutlineViewer == null)
1048 return StructuredSelection.EMPTY;
1049 return fOutlineViewer.getSelection();
1053 * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1055 public void addPostSelectionChangedListener(
1056 ISelectionChangedListener listener) {
1057 if (fOutlineViewer != null)
1058 fOutlineViewer.addPostSelectionChangedListener(listener);
1060 fPostSelectionChangedListeners.add(listener);
1064 * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1066 public void removePostSelectionChangedListener(
1067 ISelectionChangedListener listener) {
1068 if (fOutlineViewer != null)
1069 fOutlineViewer.removePostSelectionChangedListener(listener);
1071 fPostSelectionChangedListeners.remove(listener);
1074 private void registerToolbarActions(IActionBars actionBars) {
1076 IToolBarManager toolBarManager = actionBars.getToolBarManager();
1077 if (toolBarManager != null) {
1078 toolBarManager.add(new LexicalSortingAction());
1080 fMemberFilterActionGroup = new MemberFilterActionGroup(
1082 "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1083 fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1085 fCustomFiltersActionGroup.fillActionBars(actionBars);
1087 IMenuManager menu = actionBars.getMenuManager();
1088 menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1090 fToggleLinkingAction = new ToggleLinkingAction(this);
1091 menu.add(new ClassOnlyAction());
1092 menu.add(fToggleLinkingAction);
1097 * @see IPage#createControl
1099 public void createControl(Composite parent) {
1101 Tree tree = new Tree(parent, SWT.MULTI);
1103 AppearanceAwareLabelProvider lprovider = new AppearanceAwareLabelProvider(
1104 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
1105 | JavaElementLabels.F_APP_TYPE_SIGNATURE,
1106 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
1108 fOutlineViewer = new JavaOutlineViewer(tree);
1110 fOutlineViewer.setContentProvider(new ChildrenProvider());
1111 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(
1114 Object[] listeners = fSelectionChangedListeners.getListeners();
1115 for (int i = 0; i < listeners.length; i++) {
1116 fSelectionChangedListeners.remove(listeners[i]);
1118 .addSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1121 listeners = fPostSelectionChangedListeners.getListeners();
1122 for (int i = 0; i < listeners.length; i++) {
1123 fPostSelectionChangedListeners.remove(listeners[i]);
1125 .addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1128 MenuManager manager = new MenuManager(fContextMenuID, fContextMenuID);
1129 manager.setRemoveAllWhenShown(true);
1130 manager.addMenuListener(new IMenuListener() {
1131 public void menuAboutToShow(IMenuManager m) {
1132 contextMenuAboutToShow(m);
1135 fMenu = manager.createContextMenu(tree);
1136 tree.setMenu(fMenu);
1138 IPageSite site = getSite();
1140 .registerContextMenu(PHPeclipsePlugin.getPluginId()
1141 + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1142 site.setSelectionProvider(fOutlineViewer);
1144 // we must create the groups after we have set the selection provider to
1146 fActionGroups = new CompositeActionGroup(new ActionGroup[] {
1147 // new OpenViewActionGroup(this),
1148 // new CCPActionGroup(this),
1149 new GenerateActionGroup(this) });
1150 // new RefactorActionGroup(this),
1151 // new JavaSearchActionGroup(this)});
1153 // register global actions
1154 IActionBars bars = site.getActionBars();
1156 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1157 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1158 bars.setGlobalActionHandler(ActionFactory.PREVIOUS.getId(),
1159 fPreviousAnnotation);
1161 .setGlobalActionHandler(ActionFactory.NEXT.getId(),
1163 bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC,
1166 .setGlobalActionHandler(
1167 ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY,
1168 fTogglePresentation);
1169 bars.setGlobalActionHandler(
1170 ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION,
1172 bars.setGlobalActionHandler(
1173 ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION,
1174 fPreviousAnnotation);
1176 fActionGroups.fillActionBars(bars);
1178 IStatusLineManager statusLineManager = bars.getStatusLineManager();
1179 if (statusLineManager != null) {
1180 StatusBarUpdater updater = new StatusBarUpdater(statusLineManager);
1181 fOutlineViewer.addPostSelectionChangedListener(updater);
1183 // Custom filter group
1184 fCustomFiltersActionGroup = new CustomFiltersActionGroup(
1185 "net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1187 registerToolbarActions(bars);
1189 fOutlineViewer.setInput(fInput);
1192 public void dispose() {
1194 if (fEditor == null)
1197 if (fMemberFilterActionGroup != null) {
1198 fMemberFilterActionGroup.dispose();
1199 fMemberFilterActionGroup = null;
1202 if (fCustomFiltersActionGroup != null) {
1203 fCustomFiltersActionGroup.dispose();
1204 fCustomFiltersActionGroup = null;
1207 fEditor.outlinePageClosed();
1210 fSelectionChangedListeners.clear();
1211 fSelectionChangedListeners = null;
1213 fPostSelectionChangedListeners.clear();
1214 fPostSelectionChangedListeners = null;
1216 if (fPropertyChangeListener != null) {
1217 PHPeclipsePlugin.getDefault().getPreferenceStore()
1218 .removePropertyChangeListener(fPropertyChangeListener);
1219 fPropertyChangeListener = null;
1222 if (fMenu != null && !fMenu.isDisposed()) {
1227 if (fActionGroups != null)
1228 fActionGroups.dispose();
1230 fTogglePresentation.setEditor(null);
1231 fPreviousAnnotation.setEditor(null);
1232 fNextAnnotation.setEditor(null);
1234 fOutlineViewer = null;
1239 public Control getControl() {
1240 if (fOutlineViewer != null)
1241 return fOutlineViewer.getControl();
1245 public void setInput(IJavaElement inputElement) {
1246 fInput = inputElement;
1247 if (fOutlineViewer != null)
1248 fOutlineViewer.setInput(fInput);
1251 public void select(ISourceReference reference) {
1252 if (fOutlineViewer != null) {
1254 ISelection s = fOutlineViewer.getSelection();
1255 if (s instanceof IStructuredSelection) {
1256 IStructuredSelection ss = (IStructuredSelection) s;
1257 List elements = ss.toList();
1258 if (!elements.contains(reference)) {
1259 s = (reference == null ? StructuredSelection.EMPTY
1260 : new StructuredSelection(reference));
1261 fOutlineViewer.setSelection(s, true);
1267 public void setAction(String actionID, IAction action) {
1268 Assert.isNotNull(actionID);
1270 fActions.remove(actionID);
1272 fActions.put(actionID, action);
1275 public IAction getAction(String actionID) {
1276 Assert.isNotNull(actionID);
1277 return (IAction) fActions.get(actionID);
1281 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1283 public Object getAdapter(Class key) {
1284 if (key == IShowInSource.class) {
1285 return getShowInSource();
1287 if (key == IShowInTargetList.class) {
1288 return new IShowInTargetList() {
1289 public String[] getShowInTargetIds() {
1290 return new String[] { JavaUI.ID_PACKAGES };
1295 if (key == IShowInTarget.class) {
1296 return getShowInTarget();
1303 * Convenience method to add the action installed under the given actionID
1304 * to the specified group of the menu.
1309 * the group to which to add the action
1311 * the ID of the new action
1313 protected void addAction(IMenuManager menu, String group, String actionID) {
1314 IAction action = getAction(actionID);
1315 if (action != null) {
1316 if (action instanceof IUpdate)
1317 ((IUpdate) action).update();
1319 if (action.isEnabled()) {
1320 IMenuManager subMenu = menu.findMenuUsingPath(group);
1321 if (subMenu != null)
1322 subMenu.add(action);
1324 menu.appendToGroup(group, action);
1329 protected void contextMenuAboutToShow(IMenuManager menu) {
1331 PHPeclipsePlugin.createStandardGroups(menu);
1333 IStructuredSelection selection = (IStructuredSelection) getSelection();
1334 fActionGroups.setContext(new ActionContext(selection));
1335 fActionGroups.fillContextMenu(menu);
1339 * @see Page#setFocus()
1341 public void setFocus() {
1342 if (fOutlineViewer != null)
1343 fOutlineViewer.getControl().setFocus();
1347 * Checks whether a given Java element is an inner type.
1351 * @return <code>true</code> iff the given element is an inner type
1353 private boolean isInnerType(IJavaElement element) {
1355 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1356 IType type = (IType) element;
1358 return type.isMember();
1359 } catch (JavaModelException e) {
1360 IJavaElement parent = type.getParent();
1361 if (parent != null) {
1362 int parentElementType = parent.getElementType();
1363 return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
1372 * Returns the <code>IShowInSource</code> for this view.
1374 * @return the {@link IShowInSource}
1376 protected IShowInSource getShowInSource() {
1377 return new IShowInSource() {
1378 public ShowInContext getShowInContext() {
1379 return new ShowInContext(null, getSite().getSelectionProvider()
1386 * Returns the <code>IShowInTarget</code> for this view.
1388 * @return the {@link IShowInTarget}
1390 protected IShowInTarget getShowInTarget() {
1391 return new IShowInTarget() {
1392 public boolean show(ShowInContext context) {
1393 ISelection sel = context.getSelection();
1394 if (sel instanceof ITextSelection) {
1395 ITextSelection tsel = (ITextSelection) sel;
1396 int offset = tsel.getOffset();
1397 IJavaElement element = fEditor.getElementAt(offset);
1398 if (element != null) {
1399 setSelection(new StructuredSelection(element));
1408 private void initDragAndDrop() {
1409 // int ops = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1410 // Transfer[] transfers = new Transfer[] { LocalSelectionTransfer
1411 // .getInstance() };
1414 // TransferDropTargetListener[] dropListeners= new
1415 // TransferDropTargetListener[] {
1416 // new SelectionTransferDropAdapter(fOutlineViewer)
1418 // fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new
1419 // DelegatingDropAdapter(dropListeners));
1422 // TransferDragSourceListener[] dragListeners= new
1423 // TransferDragSourceListener[] {
1424 // new SelectionTransferDragAdapter(fOutlineViewer)
1426 // fOutlineViewer.addDragSupport(ops, transfers, new
1427 // JdtViewerDragAdapter(fOutlineViewer, dragListeners));