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;
14 import java.util.Enumeration;
15 import java.util.Hashtable;
16 import java.util.List;
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.dnd.JdtViewerDragAdapter;
39 import net.sourceforge.phpdt.internal.ui.dnd.TransferDragSourceListener;
40 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
41 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
42 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
43 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
44 import net.sourceforge.phpdt.internal.ui.viewsupport.StatusBarUpdater;
45 import net.sourceforge.phpdt.ui.JavaElementSorter;
46 import net.sourceforge.phpdt.ui.JavaUI;
47 import net.sourceforge.phpdt.ui.PreferenceConstants;
48 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
49 import net.sourceforge.phpdt.ui.actions.CustomFiltersActionGroup;
50 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
51 import net.sourceforge.phpdt.ui.actions.MemberFilterActionGroup;
52 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
53 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
55 import org.eclipse.core.resources.IResource;
56 import org.eclipse.core.runtime.IAdaptable;
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.ListenerList;
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.actions.ActionContext;
96 import org.eclipse.ui.actions.ActionGroup;
97 import org.eclipse.ui.help.WorkbenchHelp;
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.ITextEditorActionConstants;
107 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
108 import org.eclipse.ui.texteditor.IUpdate;
109 import org.eclipse.ui.texteditor.TextEditorAction;
110 import org.eclipse.ui.texteditor.TextOperationAction;
111 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
112 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
113 import org.eclipse.ui.views.navigator.RefactorActionGroup;
117 * The content outline page of the Java editor. The viewer implements a proprietary
118 * update mechanism based on Java model deltas. It does not react on domain changes.
119 * It is specified to show the content of ICompilationUnits and IClassFiles.
120 * Publishes its context menu under <code>PHPeclipsePlugin.getDefault().getPluginId() + ".outline"</code>.
122 public class JavaOutlinePage extends Page implements IContentOutlinePage, IAdaptable , IPostSelectionProvider {
124 static Object[] NO_CHILDREN= new Object[0];
127 * The element change listener of the java outline viewer.
128 * @see IElementChangedListener
130 class ElementChangedListener implements IElementChangedListener {
132 public void elementChanged(final ElementChangedEvent e) {
134 if (getControl() == null)
137 Display d= getControl().getDisplay();
139 d.asyncExec(new Runnable() {
141 ICompilationUnit cu= (ICompilationUnit) fInput;
142 IJavaElement base= cu;
143 // if (fTopLevelTypeOnly) {
144 // base= getMainType(cu);
145 // if (base == null) {
146 if (fOutlineViewer != null)
147 fOutlineViewer.refresh(true);
151 // IJavaElementDelta delta= findElement(base, e.getDelta());
152 // if (delta != null && fOutlineViewer != null) {
153 // fOutlineViewer.reconcile(delta);
160 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
161 if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
162 return true; // add or remove
164 int flags= cuDelta.getFlags();
165 if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
168 return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
171 protected IJavaElementDelta findElement(IJavaElement unit, IJavaElementDelta delta) {
173 if (delta == null || unit == null)
176 IJavaElement element= delta.getElement();
178 if (unit.equals(element)) {
179 if (isPossibleStructuralChange(delta)) {
186 if (element.getElementType() > IJavaElement.CLASS_FILE)
189 IJavaElementDelta[] children= delta.getAffectedChildren();
190 if (children == null || children.length == 0)
193 for (int i= 0; i < children.length; i++) {
194 IJavaElementDelta d= findElement(unit, children[i]);
203 static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
205 * @see java.lang.Object#toString()
207 public String toString() {
208 return PHPEditorMessages.getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
212 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
214 public Object getAdapter(Class clas) {
215 if (clas == IWorkbenchAdapter.class)
222 * Content provider for the children of an ICompilationUnit or
224 * @see ITreeContentProvider
226 class ChildrenProvider implements ITreeContentProvider {
228 private Object[] NO_CLASS= new Object[] {new NoClassElement()};
229 private ElementChangedListener fListener;
231 protected boolean matches(IJavaElement element) {
232 if (element.getElementType() == IJavaElement.METHOD) {
233 String name= element.getElementName();
234 return (name != null && name.indexOf('<') >= 0);
239 protected IJavaElement[] filter(IJavaElement[] children) {
240 boolean initializers= false;
241 for (int i= 0; i < children.length; i++) {
242 if (matches(children[i])) {
251 Vector v= new Vector();
252 for (int i= 0; i < children.length; i++) {
253 if (matches(children[i]))
255 v.addElement(children[i]);
258 IJavaElement[] result= new IJavaElement[v.size()];
263 public Object[] getChildren(Object parent) {
264 if (parent instanceof IParent) {
265 IParent c= (IParent) parent;
267 return filter(c.getChildren());
268 } catch (JavaModelException x) {
269 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
270 // don't log NotExist exceptions as this is a valid case
271 // since we might have been posted and the element
272 // removed in the meantime.
273 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
274 PHPeclipsePlugin.log(x);
280 public Object[] getElements(Object parent) {
281 if (fTopLevelTypeOnly) {
282 if (parent instanceof ICompilationUnit) {
284 IType type= getMainType((ICompilationUnit) parent);
285 return type != null ? type.getChildren() : NO_CLASS;
286 } catch (JavaModelException e) {
287 PHPeclipsePlugin.log(e);
290 // else if (parent instanceof IClassFile) {
292 // IType type= getMainType((IClassFile) parent);
293 // return type != null ? type.getChildren() : NO_CLASS;
294 // } catch (JavaModelException e) {
295 // PHPeclipsePlugin.log(e);
299 return getChildren(parent);
302 public Object getParent(Object child) {
303 if (child instanceof IJavaElement) {
304 IJavaElement e= (IJavaElement) child;
305 return e.getParent();
310 public boolean hasChildren(Object parent) {
311 if (parent instanceof IParent) {
312 IParent c= (IParent) parent;
314 IJavaElement[] children= filter(c.getChildren());
315 return (children != null && children.length > 0);
316 } catch (JavaModelException x) {
317 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
318 // don't log NotExist exceptions as this is a valid case
319 // since we might have been posted and the element
320 // removed in the meantime.
321 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
322 PHPeclipsePlugin.log(x);
328 public boolean isDeleted(Object o) {
332 public void dispose() {
333 if (fListener != null) {
334 JavaCore.removeElementChangedListener(fListener);
340 * @see IContentProvider#inputChanged(Viewer, Object, Object)
342 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
343 boolean isCU= (newInput instanceof ICompilationUnit);
345 if (isCU && fListener == null) {
346 fListener= new ElementChangedListener();
347 JavaCore.addElementChangedListener(fListener);
348 } else if (!isCU && fListener != null) {
349 JavaCore.removeElementChangedListener(fListener);
356 class JavaOutlineViewer extends TreeViewer {
359 * Indicates an item which has been reused. At the point of
360 * its reuse it has been expanded. This field is used to
361 * communicate between <code>internalExpandToLevel</code> and
362 * <code>reuseTreeItem</code>.
364 private Item fReusedExpandedItem;
365 private boolean fReorderedMembers;
366 private boolean fForceFireSelectionChanged;
368 public JavaOutlineViewer(Tree tree) {
370 setAutoExpandLevel(ALL_LEVELS);
371 setUseHashlookup(true);
375 * Investigates the given element change event and if affected
376 * incrementally updates the Java outline.
378 * @param delta the Java element delta used to reconcile the Java outline
380 public void reconcile(IJavaElementDelta delta) {
381 fReorderedMembers= false;
382 fForceFireSelectionChanged= false;
383 if (getSorter() == null) {
384 if (fTopLevelTypeOnly
385 && delta.getElement() instanceof IType
386 && (delta.getKind() & IJavaElementDelta.ADDED) != 0)
391 Widget w= findItem(fInput);
392 if (w != null && !w.isDisposed())
394 if (fForceFireSelectionChanged)
395 fireSelectionChanged(new SelectionChangedEvent(getSite().getSelectionProvider(), this.getSelection()));
396 if (fReorderedMembers) {
398 fReorderedMembers= false;
408 * @see TreeViewer#internalExpandToLevel
410 protected void internalExpandToLevel(Widget node, int level) {
411 if (node instanceof Item) {
413 if (i.getData() instanceof IJavaElement) {
414 IJavaElement je= (IJavaElement) i.getData();
415 if (je.getElementType() == IJavaElement.IMPORT_CONTAINER || isInnerType(je)) {
416 if (i != fReusedExpandedItem) {
417 setExpanded(i, false);
423 super.internalExpandToLevel(node, level);
426 protected void reuseTreeItem(Item item, Object element) {
429 Item[] c= getChildren(item);
430 if (c != null && c.length > 0) {
432 if (getExpanded(item))
433 fReusedExpandedItem= item;
435 for (int k= 0; k < c.length; k++) {
436 if (c[k].getData() != null)
442 updateItem(item, element);
443 updatePlus(item, element);
444 internalExpandToLevel(item, ALL_LEVELS);
446 fReusedExpandedItem= null;
447 fForceFireSelectionChanged= true;
450 protected boolean mustUpdateParent(IJavaElementDelta delta, IJavaElement element) {
451 if (element instanceof IMethod) {
452 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
454 return ((IMethod)element).isMainMethod();
455 } catch (JavaModelException e) {
456 PHPeclipsePlugin.log(e.getStatus());
459 return "main".equals(element.getElementName()); //$NON-NLS-1$
465 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
467 public boolean isExpandable(Object element) {
469 return getFilteredChildren(element).length > 0;
471 return super.isExpandable(element);
474 protected ISourceRange getSourceRange(IJavaElement element) throws JavaModelException {
475 if (element instanceof ISourceReference)
476 return ((ISourceReference) element).getSourceRange();
477 if (element instanceof IMember)// && !(element instanceof IInitializer))
478 return ((IMember) element).getNameRange();
482 protected boolean overlaps(ISourceRange range, int start, int end) {
483 return start <= (range.getOffset() + range.getLength() - 1) && range.getOffset() <= end;
486 protected boolean filtered(IJavaElement parent, IJavaElement child) {
488 Object[] result= new Object[] { child };
489 ViewerFilter[] filters= getFilters();
490 for (int i= 0; i < filters.length; i++) {
491 result= filters[i].filter(this, parent, result);
492 if (result.length == 0)
499 protected void update(Widget w, IJavaElementDelta delta) {
503 IJavaElement parent= delta.getElement();
504 IJavaElementDelta[] affected= delta.getAffectedChildren();
505 Item[] children= getChildren(w);
507 boolean doUpdateParent= false;
508 boolean doUpdateParentsPlus= false;
510 Vector deletions= new Vector();
511 Vector additions= new Vector();
513 for (int i= 0; i < affected.length; i++) {
514 IJavaElementDelta affectedDelta= affected[i];
515 IJavaElement affectedElement= affectedDelta.getElement();
516 int status= affected[i].getKind();
518 // find tree item with affected element
520 for (j= 0; j < children.length; j++)
521 if (affectedElement.equals(children[j].getData()))
524 if (j == children.length) {
525 // remove from collapsed parent
526 if ((status & IJavaElementDelta.REMOVED) != 0) {
527 doUpdateParentsPlus= true;
531 if ((status & IJavaElementDelta.CHANGED) != 0 &&
532 (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0 &&
533 !filtered(parent, affectedElement))
535 additions.addElement(affectedDelta);
543 if ((status & IJavaElementDelta.REMOVED) != 0) {
544 deletions.addElement(item);
545 doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
548 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
549 int change= affectedDelta.getFlags();
550 doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
552 if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
553 if (filtered(parent, affectedElement))
554 deletions.addElement(item);
556 updateItem(item, affectedElement);
559 if ((change & IJavaElementDelta.F_CONTENT) != 0)
560 updateItem(item, affectedElement);
562 if ((change & IJavaElementDelta.F_CHILDREN) != 0)
563 update(item, affectedDelta);
565 if ((change & IJavaElementDelta.F_REORDER) != 0)
566 fReorderedMembers= true;
570 // find all elements to add
571 IJavaElementDelta[] add= delta.getAddedChildren();
572 if (additions.size() > 0) {
573 IJavaElementDelta[] tmp= new IJavaElementDelta[add.length + additions.size()];
574 System.arraycopy(add, 0, tmp, 0, add.length);
575 for (int i= 0; i < additions.size(); i++)
576 tmp[i + add.length]= (IJavaElementDelta) additions.elementAt(i);
580 // add at the right position
581 go2: for (int i= 0; i < add.length; i++) {
585 IJavaElement e= add[i].getElement();
586 if (filtered(parent, e))
589 doUpdateParent= doUpdateParent || mustUpdateParent(add[i], e);
590 ISourceRange rng= getSourceRange(e);
591 int start= rng.getOffset();
592 int end= start + rng.getLength() - 1;
593 int nameOffset= Integer.MAX_VALUE;
594 if (e instanceof IField) {
595 ISourceRange nameRange= ((IField) e).getNameRange();
596 if (nameRange != null)
597 nameOffset= nameRange.getOffset();
602 children= getChildren(w);
604 for (int j= 0; j < children.length; j++) {
606 IJavaElement r= (IJavaElement) item.getData();
609 // parent node collapsed and not be opened before -> do nothing
615 rng= getSourceRange(r);
617 // multi-field declarations always start at
618 // the same offset. They also have the same
619 // end offset if the field sequence is terminated
620 // with a semicolon. If not, the source range
621 // ends behind the identifier / initializer
622 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=51851
623 boolean multiFieldDeclaration=
624 r.getElementType() == IJavaElement.FIELD
625 && e.getElementType() == IJavaElement.FIELD
626 && rng.getOffset() == start;
628 // elements are inserted by occurrence
629 // however, multi-field declarations have
630 // equal source ranges offsets, therefore we
631 // compare name-range offsets.
632 boolean multiFieldOrderBefore= false;
633 if (multiFieldDeclaration) {
634 if (r instanceof IField) {
635 ISourceRange nameRange= ((IField) r).getNameRange();
636 if (nameRange != null) {
637 if (nameRange.getOffset() > nameOffset)
638 multiFieldOrderBefore= true;
643 if (!multiFieldDeclaration && overlaps(rng, start, end)) {
645 // be tolerant if the delta is not correct, or if
646 // the tree has been updated other than by a delta
647 reuseTreeItem(item, e);
650 } else if (multiFieldOrderBefore || rng.getOffset() > start) {
652 if (last != null && deletions.contains(last)) {
654 deletions.removeElement(last);
655 reuseTreeItem(last, e);
658 createTreeItem(w, e, j);
663 } catch (JavaModelException x) {
664 // stumbled over deleted element
670 // add at the end of the list
671 if (last != null && deletions.contains(last)) {
673 deletions.removeElement(last);
674 reuseTreeItem(last, e);
677 createTreeItem(w, e, -1);
680 } catch (JavaModelException x) {
681 // the element to be added is not present -> don't add it
686 // remove items which haven't been reused
687 Enumeration e= deletions.elements();
688 while (e.hasMoreElements()) {
689 item= (Item) e.nextElement();
695 updateItem(w, delta.getElement());
696 if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
697 updatePlus((Item)w, delta.getElement());
703 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
705 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
706 Object input= getInput();
707 if (event instanceof ProblemsLabelChangedEvent) {
708 ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event;
709 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
710 return; // marker changes can be ignored
713 // look if the underlying resource changed
714 Object[] changed= event.getElements();
715 if (changed != null) {
716 IResource resource= getUnderlyingResource();
717 if (resource != null) {
718 for (int i= 0; i < changed.length; i++) {
719 if (changed[i] != null && changed[i].equals(resource)) {
720 // change event to a full refresh
721 event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource());
727 super.handleLabelProviderChanged(event);
730 private IResource getUnderlyingResource() {
731 Object input= getInput();
732 if (input instanceof ICompilationUnit) {
733 ICompilationUnit cu= (ICompilationUnit) input;
734 cu= JavaModelUtil.toOriginal(cu);
735 return cu.getResource();
737 // else if (input instanceof IClassFile) {
738 // return ((IClassFile) input).getResource();
746 class LexicalSortingAction extends Action {
748 private JavaElementSorter fSorter= new JavaElementSorter();
750 public LexicalSortingAction() {
752 WorkbenchHelp.setHelp(this, IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
753 setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
754 PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
755 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
756 setDescription(PHPEditorMessages.getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
758 boolean checked= PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean("LexicalSortingAction.isChecked"); //$NON-NLS-1$
759 valueChanged(checked, false);
763 valueChanged(isChecked(), true);
766 private void valueChanged(final boolean on, boolean store) {
768 BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(), new Runnable() {
770 fOutlineViewer.setSorter(on ? fSorter : null); }
774 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue("LexicalSortingAction.isChecked", on); //$NON-NLS-1$
778 class ClassOnlyAction extends Action {
780 public ClassOnlyAction() {
782 WorkbenchHelp.setHelp(this, IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
783 setText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
784 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
785 setDescription(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
786 PHPUiImages.setLocalImageDescriptors(this, "gointo_toplevel_type.gif"); //$NON-NLS-1$
788 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
789 boolean showclass= preferenceStore.getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
790 setTopLevelTypeOnly(showclass);
794 * @see org.eclipse.jface.action.Action#run()
797 setTopLevelTypeOnly(!fTopLevelTypeOnly);
800 private void setTopLevelTypeOnly(boolean show) {
801 fTopLevelTypeOnly= show;
803 fOutlineViewer.refresh(false);
805 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
806 preferenceStore.setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
811 * This action toggles whether this Java Outline page links
812 * its selection to the active editor.
816 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
818 JavaOutlinePage fJavaOutlinePage;
821 * Constructs a new action.
823 * @param outlinePage the Java outline page
825 public ToggleLinkingAction(JavaOutlinePage outlinePage) {
826 boolean isLinkingEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
827 setChecked(isLinkingEnabled);
828 fJavaOutlinePage= outlinePage;
835 PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE, isChecked());
836 if (isChecked() && fEditor != null)
837 fEditor.synchronizeOutlinePage(fEditor.computeHighlightRangeSourceReference(), false);
843 /** A flag to show contents of top level type only */
844 private boolean fTopLevelTypeOnly;
846 private IJavaElement fInput;
847 private String fContextMenuID;
849 private JavaOutlineViewer fOutlineViewer;
850 private PHPEditor fEditor;
852 private MemberFilterActionGroup fMemberFilterActionGroup;
854 private ListenerList fSelectionChangedListeners= new ListenerList();
855 private ListenerList fPostSelectionChangedListeners= new ListenerList();
856 private Hashtable fActions= new Hashtable();
858 private TogglePresentationAction fTogglePresentation;
859 private GotoAnnotationAction fPreviousAnnotation;
860 private GotoAnnotationAction fNextAnnotation;
861 private TextEditorAction fShowJavadoc;
862 private TextOperationAction fUndo;
863 private TextOperationAction fRedo;
865 private ToggleLinkingAction fToggleLinkingAction;
867 private CompositeActionGroup fActionGroups;
869 private IPropertyChangeListener fPropertyChangeListener;
871 * Custom filter action group.
874 private CustomFiltersActionGroup fCustomFiltersActionGroup;
876 public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
879 Assert.isNotNull(editor);
881 fContextMenuID= contextMenuID;
884 fTogglePresentation= new TogglePresentationAction();
885 fPreviousAnnotation= new GotoAnnotationAction("PreviousAnnotation.", false); //$NON-NLS-1$
886 fNextAnnotation= new GotoAnnotationAction("NextAnnotation.", true); //$NON-NLS-1$
887 fShowJavadoc= (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
888 fUndo= (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.UNDO);
889 fRedo= (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.REDO);
891 fTogglePresentation.setEditor(editor);
892 fPreviousAnnotation.setEditor(editor);
893 fNextAnnotation.setEditor(editor);
895 fPropertyChangeListener= new IPropertyChangeListener() {
896 public void propertyChange(PropertyChangeEvent event) {
897 doPropertyChange(event);
900 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
904 * Returns the primary type of a compilation unit (has the same
905 * name as the compilation unit).
907 * @param compilationUnit the compilation unit
908 * @return returns the primary type of the compilation unit, or
909 * <code>null</code> if is does not have one
911 protected IType getMainType(ICompilationUnit compilationUnit) {
913 if (compilationUnit == null)
916 String name= compilationUnit.getElementName();
917 int index= name.indexOf('.');
919 name= name.substring(0, index);
920 IType type= compilationUnit.getType(name);
921 return type.exists() ? type : null;
925 * Returns the primary type of a class file.
927 * @param classFile the class file
928 * @return returns the primary type of the class file, or <code>null</code>
929 * if is does not have one
931 // protected IType getMainType(IClassFile classFile) {
933 // IType type= classFile.getType();
934 // return type != null && type.exists() ? type : null;
935 // } catch (JavaModelException e) {
941 * Method declared on Page
943 public void init(IPageSite pageSite) {
944 super.init(pageSite);
947 private void doPropertyChange(PropertyChangeEvent event) {
948 if (fOutlineViewer != null) {
949 if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) {
950 fOutlineViewer.refresh(false);
956 * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
958 public void addSelectionChangedListener(ISelectionChangedListener listener) {
959 if (fOutlineViewer != null)
960 fOutlineViewer.addSelectionChangedListener(listener);
962 fSelectionChangedListeners.add(listener);
966 * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
968 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
969 if (fOutlineViewer != null)
970 fOutlineViewer.removeSelectionChangedListener(listener);
972 fSelectionChangedListeners.remove(listener);
976 * @see ISelectionProvider#setSelection(ISelection)
978 public void setSelection(ISelection selection) {
979 if (fOutlineViewer != null)
980 fOutlineViewer.setSelection(selection);
984 * @see ISelectionProvider#getSelection()
986 public ISelection getSelection() {
987 if (fOutlineViewer == null)
988 return StructuredSelection.EMPTY;
989 return fOutlineViewer.getSelection();
993 * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
995 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
996 if (fOutlineViewer != null)
997 fOutlineViewer.addPostSelectionChangedListener(listener);
999 fPostSelectionChangedListeners.add(listener);
1003 * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1005 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
1006 if (fOutlineViewer != null)
1007 fOutlineViewer.removePostSelectionChangedListener(listener);
1009 fPostSelectionChangedListeners.remove(listener);
1012 private void registerToolbarActions(IActionBars actionBars) {
1014 IToolBarManager toolBarManager= actionBars.getToolBarManager();
1015 if (toolBarManager != null) {
1016 toolBarManager.add(new LexicalSortingAction());
1018 fMemberFilterActionGroup= new MemberFilterActionGroup(fOutlineViewer, "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1019 fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1021 fCustomFiltersActionGroup.fillActionBars(actionBars);
1023 IMenuManager menu= actionBars.getMenuManager();
1024 menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1026 fToggleLinkingAction= new ToggleLinkingAction(this);
1027 menu.add(new ClassOnlyAction());
1028 menu.add(fToggleLinkingAction);
1033 * @see IPage#createControl
1035 public void createControl(Composite parent) {
1037 Tree tree= new Tree(parent, SWT.MULTI);
1039 AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider(
1040 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_APP_TYPE_SIGNATURE,
1041 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
1044 fOutlineViewer= new JavaOutlineViewer(tree);
1046 fOutlineViewer.setContentProvider(new ChildrenProvider());
1047 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
1049 Object[] listeners= fSelectionChangedListeners.getListeners();
1050 for (int i= 0; i < listeners.length; i++) {
1051 fSelectionChangedListeners.remove(listeners[i]);
1052 fOutlineViewer.addSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1055 listeners= fPostSelectionChangedListeners.getListeners();
1056 for (int i= 0; i < listeners.length; i++) {
1057 fPostSelectionChangedListeners.remove(listeners[i]);
1058 fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1061 MenuManager manager= new MenuManager(fContextMenuID, fContextMenuID);
1062 manager.setRemoveAllWhenShown(true);
1063 manager.addMenuListener(new IMenuListener() {
1064 public void menuAboutToShow(IMenuManager m) {
1065 contextMenuAboutToShow(m);
1068 fMenu= manager.createContextMenu(tree);
1069 tree.setMenu(fMenu);
1071 IPageSite site= getSite();
1072 site.registerContextMenu(PHPeclipsePlugin.getPluginId() + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1073 site.setSelectionProvider(fOutlineViewer);
1075 // we must create the groups after we have set the selection provider to the site
1076 fActionGroups= new CompositeActionGroup(new ActionGroup[] {
1077 // new OpenViewActionGroup(this),
1078 // new CCPActionGroup(this),
1079 new GenerateActionGroup(this)});
1080 // new RefactorActionGroup(this),
1081 // new JavaSearchActionGroup(this)});
1083 // register global actions
1084 IActionBars bars= site.getActionBars();
1086 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1087 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1088 bars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, fPreviousAnnotation);
1089 bars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, fNextAnnotation);
1090 bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC, fShowJavadoc);
1091 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY, fTogglePresentation);
1092 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, fNextAnnotation);
1093 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, fPreviousAnnotation);
1096 fActionGroups.fillActionBars(bars);
1098 IStatusLineManager statusLineManager= bars.getStatusLineManager();
1099 if (statusLineManager != null) {
1100 StatusBarUpdater updater= new StatusBarUpdater(statusLineManager);
1101 fOutlineViewer.addPostSelectionChangedListener(updater);
1103 // Custom filter group
1104 fCustomFiltersActionGroup= new CustomFiltersActionGroup("net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1106 registerToolbarActions(bars);
1108 fOutlineViewer.setInput(fInput);
1111 public void dispose() {
1113 if (fEditor == null)
1116 if (fMemberFilterActionGroup != null) {
1117 fMemberFilterActionGroup.dispose();
1118 fMemberFilterActionGroup= null;
1121 if (fCustomFiltersActionGroup != null) {
1122 fCustomFiltersActionGroup.dispose();
1123 fCustomFiltersActionGroup= null;
1127 fEditor.outlinePageClosed();
1130 fSelectionChangedListeners.clear();
1131 fSelectionChangedListeners= null;
1133 fPostSelectionChangedListeners.clear();
1134 fPostSelectionChangedListeners= null;
1136 if (fPropertyChangeListener != null) {
1137 PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1138 fPropertyChangeListener= null;
1141 if (fMenu != null && !fMenu.isDisposed()) {
1146 if (fActionGroups != null)
1147 fActionGroups.dispose();
1149 fTogglePresentation.setEditor(null);
1150 fPreviousAnnotation.setEditor(null);
1151 fNextAnnotation.setEditor(null);
1153 fOutlineViewer= null;
1158 public Control getControl() {
1159 if (fOutlineViewer != null)
1160 return fOutlineViewer.getControl();
1164 public void setInput(IJavaElement inputElement) {
1165 fInput= inputElement;
1166 if (fOutlineViewer != null)
1167 fOutlineViewer.setInput(fInput);
1170 public void select(ISourceReference reference) {
1171 if (fOutlineViewer != null) {
1173 ISelection s= fOutlineViewer.getSelection();
1174 if (s instanceof IStructuredSelection) {
1175 IStructuredSelection ss= (IStructuredSelection) s;
1176 List elements= ss.toList();
1177 if (!elements.contains(reference)) {
1178 s= (reference == null ? StructuredSelection.EMPTY : new StructuredSelection(reference));
1179 fOutlineViewer.setSelection(s, true);
1185 public void setAction(String actionID, IAction action) {
1186 Assert.isNotNull(actionID);
1188 fActions.remove(actionID);
1190 fActions.put(actionID, action);
1193 public IAction getAction(String actionID) {
1194 Assert.isNotNull(actionID);
1195 return (IAction) fActions.get(actionID);
1199 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1201 public Object getAdapter(Class key) {
1202 if (key == IShowInSource.class) {
1203 return getShowInSource();
1205 if (key == IShowInTargetList.class) {
1206 return new IShowInTargetList() {
1207 public String[] getShowInTargetIds() {
1208 return new String[] { JavaUI.ID_PACKAGES };
1213 if (key == IShowInTarget.class) {
1214 return getShowInTarget();
1221 * Convenience method to add the action installed under the given actionID to the
1222 * specified group of the menu.
1224 * @param menu the menu manager
1225 * @param group the group to which to add the action
1226 * @param actionID the ID of the new action
1228 protected void addAction(IMenuManager menu, String group, String actionID) {
1229 IAction action= getAction(actionID);
1230 if (action != null) {
1231 if (action instanceof IUpdate)
1232 ((IUpdate) action).update();
1234 if (action.isEnabled()) {
1235 IMenuManager subMenu= menu.findMenuUsingPath(group);
1236 if (subMenu != null)
1237 subMenu.add(action);
1239 menu.appendToGroup(group, action);
1244 protected void contextMenuAboutToShow(IMenuManager menu) {
1246 PHPeclipsePlugin.createStandardGroups(menu);
1248 IStructuredSelection selection= (IStructuredSelection)getSelection();
1249 fActionGroups.setContext(new ActionContext(selection));
1250 fActionGroups.fillContextMenu(menu);
1254 * @see Page#setFocus()
1256 public void setFocus() {
1257 if (fOutlineViewer != null)
1258 fOutlineViewer.getControl().setFocus();
1262 * Checks whether a given Java element is an inner type.
1264 * @param element the java element
1265 * @return <code>true</code> iff the given element is an inner type
1267 private boolean isInnerType(IJavaElement element) {
1269 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1270 IType type= (IType)element;
1272 return type.isMember();
1273 } catch (JavaModelException e) {
1274 IJavaElement parent= type.getParent();
1275 if (parent != null) {
1276 int parentElementType= parent.getElementType();
1277 return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
1286 * Returns the <code>IShowInSource</code> for this view.
1288 * @return the {@link IShowInSource}
1290 protected IShowInSource getShowInSource() {
1291 return new IShowInSource() {
1292 public ShowInContext getShowInContext() {
1293 return new ShowInContext(
1295 getSite().getSelectionProvider().getSelection());
1301 * Returns the <code>IShowInTarget</code> for this view.
1303 * @return the {@link IShowInTarget}
1305 protected IShowInTarget getShowInTarget() {
1306 return new IShowInTarget() {
1307 public boolean show(ShowInContext context) {
1308 ISelection sel= context.getSelection();
1309 if (sel instanceof ITextSelection) {
1310 ITextSelection tsel= (ITextSelection) sel;
1311 int offset= tsel.getOffset();
1312 IJavaElement element= fEditor.getElementAt(offset);
1313 if (element != null) {
1314 setSelection(new StructuredSelection(element));
1323 private void initDragAndDrop() {
1324 int ops= DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1325 Transfer[] transfers= new Transfer[] {
1326 LocalSelectionTransfer.getInstance()
1330 // TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
1331 // new SelectionTransferDropAdapter(fOutlineViewer)
1333 // fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new DelegatingDropAdapter(dropListeners));
1336 // TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] {
1337 // new SelectionTransferDragAdapter(fOutlineViewer)
1339 // fOutlineViewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(fOutlineViewer, dragListeners));