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.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.jface.action.Action;
56 import org.eclipse.jface.action.IAction;
57 import org.eclipse.jface.action.IMenuListener;
58 import org.eclipse.jface.action.IMenuManager;
59 import org.eclipse.jface.action.IStatusLineManager;
60 import org.eclipse.jface.action.IToolBarManager;
61 import org.eclipse.jface.action.MenuManager;
62 import org.eclipse.jface.action.Separator;
63 import org.eclipse.jface.preference.IPreferenceStore;
64 import org.eclipse.jface.text.Assert;
65 import org.eclipse.jface.text.ITextSelection;
66 import org.eclipse.jface.util.IPropertyChangeListener;
67 import org.eclipse.jface.util.ListenerList;
68 import org.eclipse.jface.util.PropertyChangeEvent;
69 import org.eclipse.jface.viewers.IBaseLabelProvider;
70 import org.eclipse.jface.viewers.IPostSelectionProvider;
71 import org.eclipse.jface.viewers.ISelection;
72 import org.eclipse.jface.viewers.ISelectionChangedListener;
73 import org.eclipse.jface.viewers.IStructuredSelection;
74 import org.eclipse.jface.viewers.ITreeContentProvider;
75 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
76 import org.eclipse.jface.viewers.SelectionChangedEvent;
77 import org.eclipse.jface.viewers.StructuredSelection;
78 import org.eclipse.jface.viewers.TreeViewer;
79 import org.eclipse.jface.viewers.Viewer;
80 import org.eclipse.jface.viewers.ViewerFilter;
81 import org.eclipse.swt.SWT;
82 import org.eclipse.swt.custom.BusyIndicator;
83 import org.eclipse.swt.dnd.DND;
84 import org.eclipse.swt.dnd.Transfer;
85 import org.eclipse.swt.widgets.Composite;
86 import org.eclipse.swt.widgets.Control;
87 import org.eclipse.swt.widgets.Display;
88 import org.eclipse.swt.widgets.Item;
89 import org.eclipse.swt.widgets.Menu;
90 import org.eclipse.swt.widgets.Tree;
91 import org.eclipse.swt.widgets.Widget;
92 import org.eclipse.ui.IActionBars;
93 import org.eclipse.ui.PlatformUI;
94 import org.eclipse.ui.actions.ActionContext;
95 import org.eclipse.ui.actions.ActionFactory;
96 import org.eclipse.ui.actions.ActionGroup;
97 import org.eclipse.ui.model.IWorkbenchAdapter;
98 import org.eclipse.ui.model.WorkbenchAdapter;
99 import org.eclipse.ui.part.IPageSite;
100 import org.eclipse.ui.part.IShowInSource;
101 import org.eclipse.ui.part.IShowInTarget;
102 import org.eclipse.ui.part.IShowInTargetList;
103 import org.eclipse.ui.part.Page;
104 import org.eclipse.ui.part.ShowInContext;
105 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
106 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
107 import org.eclipse.ui.texteditor.IUpdate;
108 import org.eclipse.ui.texteditor.TextEditorAction;
109 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
110 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
114 * The content outline page of the Java editor. The viewer implements a proprietary
115 * update mechanism based on Java model deltas. It does not react on domain changes.
116 * It is specified to show the content of ICompilationUnits and IClassFiles.
117 * Publishes its context menu under <code>PHPeclipsePlugin.getDefault().getPluginId() + ".outline"</code>.
119 public class JavaOutlinePage extends Page implements IContentOutlinePage, IAdaptable , IPostSelectionProvider {
121 static Object[] NO_CHILDREN= new Object[0];
124 * The element change listener of the java outline viewer.
125 * @see IElementChangedListener
127 class ElementChangedListener implements IElementChangedListener {
129 public void elementChanged(final ElementChangedEvent e) {
131 if (getControl() == null)
134 Display d= getControl().getDisplay();
136 d.asyncExec(new Runnable() {
138 ICompilationUnit cu= (ICompilationUnit) fInput;
139 IJavaElement base= cu;
140 // if (fTopLevelTypeOnly) {
141 // base= getMainType(cu);
142 // if (base == null) {
143 if (fOutlineViewer != null)
144 fOutlineViewer.refresh(true);
148 // IJavaElementDelta delta= findElement(base, e.getDelta());
149 // if (delta != null && fOutlineViewer != null) {
150 // fOutlineViewer.reconcile(delta);
157 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
158 if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
159 return true; // add or remove
161 int flags= cuDelta.getFlags();
162 if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
165 return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
168 protected IJavaElementDelta findElement(IJavaElement unit, IJavaElementDelta delta) {
170 if (delta == null || unit == null)
173 IJavaElement element= delta.getElement();
175 if (unit.equals(element)) {
176 if (isPossibleStructuralChange(delta)) {
183 if (element.getElementType() > IJavaElement.CLASS_FILE)
186 IJavaElementDelta[] children= delta.getAffectedChildren();
187 if (children == null || children.length == 0)
190 for (int i= 0; i < children.length; i++) {
191 IJavaElementDelta d= findElement(unit, children[i]);
200 static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
202 * @see java.lang.Object#toString()
204 public String toString() {
205 return PHPEditorMessages.getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
209 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
211 public Object getAdapter(Class clas) {
212 if (clas == IWorkbenchAdapter.class)
219 * Content provider for the children of an ICompilationUnit or
221 * @see ITreeContentProvider
223 class ChildrenProvider implements ITreeContentProvider {
225 private Object[] NO_CLASS= new Object[] {new NoClassElement()};
226 private ElementChangedListener fListener;
228 protected boolean matches(IJavaElement element) {
229 if (element.getElementType() == IJavaElement.METHOD) {
230 String name= element.getElementName();
231 return (name != null && name.indexOf('<') >= 0);
236 protected IJavaElement[] filter(IJavaElement[] children) {
237 boolean initializers= false;
238 for (int i= 0; i < children.length; i++) {
239 if (matches(children[i])) {
248 Vector v= new Vector();
249 for (int i= 0; i < children.length; i++) {
250 if (matches(children[i]))
252 v.addElement(children[i]);
255 IJavaElement[] result= new IJavaElement[v.size()];
260 public Object[] getChildren(Object parent) {
261 if (parent instanceof IParent) {
262 IParent c= (IParent) parent;
264 return filter(c.getChildren());
265 } catch (JavaModelException x) {
266 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
267 // don't log NotExist exceptions as this is a valid case
268 // since we might have been posted and the element
269 // removed in the meantime.
270 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
271 PHPeclipsePlugin.log(x);
277 public Object[] getElements(Object parent) {
278 if (fTopLevelTypeOnly) {
279 if (parent instanceof ICompilationUnit) {
281 IType type= getMainType((ICompilationUnit) parent);
282 return type != null ? type.getChildren() : NO_CLASS;
283 } catch (JavaModelException e) {
284 PHPeclipsePlugin.log(e);
287 // else if (parent instanceof IClassFile) {
289 // IType type= getMainType((IClassFile) parent);
290 // return type != null ? type.getChildren() : NO_CLASS;
291 // } catch (JavaModelException e) {
292 // PHPeclipsePlugin.log(e);
296 return getChildren(parent);
299 public Object getParent(Object child) {
300 if (child instanceof IJavaElement) {
301 IJavaElement e= (IJavaElement) child;
302 return e.getParent();
307 public boolean hasChildren(Object parent) {
308 if (parent instanceof IParent) {
309 IParent c= (IParent) parent;
311 IJavaElement[] children= filter(c.getChildren());
312 return (children != null && children.length > 0);
313 } catch (JavaModelException x) {
314 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
315 // don't log NotExist exceptions as this is a valid case
316 // since we might have been posted and the element
317 // removed in the meantime.
318 if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
319 PHPeclipsePlugin.log(x);
325 public boolean isDeleted(Object o) {
329 public void dispose() {
330 if (fListener != null) {
331 JavaCore.removeElementChangedListener(fListener);
337 * @see IContentProvider#inputChanged(Viewer, Object, Object)
339 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
340 boolean isCU= (newInput instanceof ICompilationUnit);
342 if (isCU && fListener == null) {
343 fListener= new ElementChangedListener();
344 JavaCore.addElementChangedListener(fListener);
345 } else if (!isCU && fListener != null) {
346 JavaCore.removeElementChangedListener(fListener);
353 class JavaOutlineViewer extends TreeViewer {
356 * Indicates an item which has been reused. At the point of
357 * its reuse it has been expanded. This field is used to
358 * communicate between <code>internalExpandToLevel</code> and
359 * <code>reuseTreeItem</code>.
361 private Item fReusedExpandedItem;
362 private boolean fReorderedMembers;
363 private boolean fForceFireSelectionChanged;
365 public JavaOutlineViewer(Tree tree) {
367 setAutoExpandLevel(ALL_LEVELS);
368 setUseHashlookup(true);
372 * Investigates the given element change event and if affected
373 * incrementally updates the Java outline.
375 * @param delta the Java element delta used to reconcile the Java outline
377 public void reconcile(IJavaElementDelta delta) {
378 fReorderedMembers= false;
379 fForceFireSelectionChanged= false;
380 if (getSorter() == null) {
381 if (fTopLevelTypeOnly
382 && delta.getElement() instanceof IType
383 && (delta.getKind() & IJavaElementDelta.ADDED) != 0)
388 Widget w= findItem(fInput);
389 if (w != null && !w.isDisposed())
391 if (fForceFireSelectionChanged)
392 fireSelectionChanged(new SelectionChangedEvent(getSite().getSelectionProvider(), this.getSelection()));
393 if (fReorderedMembers) {
395 fReorderedMembers= false;
405 * @see TreeViewer#internalExpandToLevel
407 protected void internalExpandToLevel(Widget node, int level) {
408 if (node instanceof Item) {
410 if (i.getData() instanceof IJavaElement) {
411 IJavaElement je= (IJavaElement) i.getData();
412 if (je.getElementType() == IJavaElement.IMPORT_CONTAINER || isInnerType(je)) {
413 if (i != fReusedExpandedItem) {
414 setExpanded(i, false);
420 super.internalExpandToLevel(node, level);
423 protected void reuseTreeItem(Item item, Object element) {
426 Item[] c= getChildren(item);
427 if (c != null && c.length > 0) {
429 if (getExpanded(item))
430 fReusedExpandedItem= item;
432 for (int k= 0; k < c.length; k++) {
433 if (c[k].getData() != null)
439 updateItem(item, element);
440 updatePlus(item, element);
441 internalExpandToLevel(item, ALL_LEVELS);
443 fReusedExpandedItem= null;
444 fForceFireSelectionChanged= true;
447 protected boolean mustUpdateParent(IJavaElementDelta delta, IJavaElement element) {
448 if (element instanceof IMethod) {
449 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
451 return ((IMethod)element).isMainMethod();
452 } catch (JavaModelException e) {
453 PHPeclipsePlugin.log(e.getStatus());
456 return "main".equals(element.getElementName()); //$NON-NLS-1$
462 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
464 public boolean isExpandable(Object element) {
466 return getFilteredChildren(element).length > 0;
468 return super.isExpandable(element);
471 protected ISourceRange getSourceRange(IJavaElement element) throws JavaModelException {
472 if (element instanceof ISourceReference)
473 return ((ISourceReference) element).getSourceRange();
474 if (element instanceof IMember)// && !(element instanceof IInitializer))
475 return ((IMember) element).getNameRange();
479 protected boolean overlaps(ISourceRange range, int start, int end) {
480 return start <= (range.getOffset() + range.getLength() - 1) && range.getOffset() <= end;
483 protected boolean filtered(IJavaElement parent, IJavaElement child) {
485 Object[] result= new Object[] { child };
486 ViewerFilter[] filters= getFilters();
487 for (int i= 0; i < filters.length; i++) {
488 result= filters[i].filter(this, parent, result);
489 if (result.length == 0)
496 protected void update(Widget w, IJavaElementDelta delta) {
500 IJavaElement parent= delta.getElement();
501 IJavaElementDelta[] affected= delta.getAffectedChildren();
502 Item[] children= getChildren(w);
504 boolean doUpdateParent= false;
505 boolean doUpdateParentsPlus= false;
507 Vector deletions= new Vector();
508 Vector additions= new Vector();
510 for (int i= 0; i < affected.length; i++) {
511 IJavaElementDelta affectedDelta= affected[i];
512 IJavaElement affectedElement= affectedDelta.getElement();
513 int status= affected[i].getKind();
515 // find tree item with affected element
517 for (j= 0; j < children.length; j++)
518 if (affectedElement.equals(children[j].getData()))
521 if (j == children.length) {
522 // remove from collapsed parent
523 if ((status & IJavaElementDelta.REMOVED) != 0) {
524 doUpdateParentsPlus= true;
528 if ((status & IJavaElementDelta.CHANGED) != 0 &&
529 (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0 &&
530 !filtered(parent, affectedElement))
532 additions.addElement(affectedDelta);
540 if ((status & IJavaElementDelta.REMOVED) != 0) {
541 deletions.addElement(item);
542 doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
545 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
546 int change= affectedDelta.getFlags();
547 doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
549 if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
550 if (filtered(parent, affectedElement))
551 deletions.addElement(item);
553 updateItem(item, affectedElement);
556 if ((change & IJavaElementDelta.F_CONTENT) != 0)
557 updateItem(item, affectedElement);
559 if ((change & IJavaElementDelta.F_CHILDREN) != 0)
560 update(item, affectedDelta);
562 if ((change & IJavaElementDelta.F_REORDER) != 0)
563 fReorderedMembers= true;
567 // find all elements to add
568 IJavaElementDelta[] add= delta.getAddedChildren();
569 if (additions.size() > 0) {
570 IJavaElementDelta[] tmp= new IJavaElementDelta[add.length + additions.size()];
571 System.arraycopy(add, 0, tmp, 0, add.length);
572 for (int i= 0; i < additions.size(); i++)
573 tmp[i + add.length]= (IJavaElementDelta) additions.elementAt(i);
577 // add at the right position
578 go2: for (int i= 0; i < add.length; i++) {
582 IJavaElement e= add[i].getElement();
583 if (filtered(parent, e))
586 doUpdateParent= doUpdateParent || mustUpdateParent(add[i], e);
587 ISourceRange rng= getSourceRange(e);
588 int start= rng.getOffset();
589 int end= start + rng.getLength() - 1;
590 int nameOffset= Integer.MAX_VALUE;
591 if (e instanceof IField) {
592 ISourceRange nameRange= ((IField) e).getNameRange();
593 if (nameRange != null)
594 nameOffset= nameRange.getOffset();
599 children= getChildren(w);
601 for (int j= 0; j < children.length; j++) {
603 IJavaElement r= (IJavaElement) item.getData();
606 // parent node collapsed and not be opened before -> do nothing
612 rng= getSourceRange(r);
614 // multi-field declarations always start at
615 // the same offset. They also have the same
616 // end offset if the field sequence is terminated
617 // with a semicolon. If not, the source range
618 // ends behind the identifier / initializer
619 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=51851
620 boolean multiFieldDeclaration=
621 r.getElementType() == IJavaElement.FIELD
622 && e.getElementType() == IJavaElement.FIELD
623 && rng.getOffset() == start;
625 // elements are inserted by occurrence
626 // however, multi-field declarations have
627 // equal source ranges offsets, therefore we
628 // compare name-range offsets.
629 boolean multiFieldOrderBefore= false;
630 if (multiFieldDeclaration) {
631 if (r instanceof IField) {
632 ISourceRange nameRange= ((IField) r).getNameRange();
633 if (nameRange != null) {
634 if (nameRange.getOffset() > nameOffset)
635 multiFieldOrderBefore= true;
640 if (!multiFieldDeclaration && overlaps(rng, start, end)) {
642 // be tolerant if the delta is not correct, or if
643 // the tree has been updated other than by a delta
644 reuseTreeItem(item, e);
647 } else if (multiFieldOrderBefore || rng.getOffset() > start) {
649 if (last != null && deletions.contains(last)) {
651 deletions.removeElement(last);
652 reuseTreeItem(last, e);
655 createTreeItem(w, e, j);
660 } catch (JavaModelException x) {
661 // stumbled over deleted element
667 // add at the end of the list
668 if (last != null && deletions.contains(last)) {
670 deletions.removeElement(last);
671 reuseTreeItem(last, e);
674 createTreeItem(w, e, -1);
677 } catch (JavaModelException x) {
678 // the element to be added is not present -> don't add it
683 // remove items which haven't been reused
684 Enumeration e= deletions.elements();
685 while (e.hasMoreElements()) {
686 item= (Item) e.nextElement();
692 updateItem(w, delta.getElement());
693 if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
694 updatePlus((Item)w, delta.getElement());
700 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
702 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
703 Object input= getInput();
704 if (event instanceof ProblemsLabelChangedEvent) {
705 ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event;
706 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
707 return; // marker changes can be ignored
710 // look if the underlying resource changed
711 Object[] changed= event.getElements();
712 if (changed != null) {
713 IResource resource= getUnderlyingResource();
714 if (resource != null) {
715 for (int i= 0; i < changed.length; i++) {
716 if (changed[i] != null && changed[i].equals(resource)) {
717 // change event to a full refresh
718 event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource());
724 super.handleLabelProviderChanged(event);
727 private IResource getUnderlyingResource() {
728 Object input= getInput();
729 if (input instanceof ICompilationUnit) {
730 ICompilationUnit cu= (ICompilationUnit) input;
731 cu= JavaModelUtil.toOriginal(cu);
732 return cu.getResource();
734 // else if (input instanceof IClassFile) {
735 // return ((IClassFile) input).getResource();
743 class LexicalSortingAction extends Action {
745 private JavaElementSorter fSorter= new JavaElementSorter();
747 public LexicalSortingAction() {
749 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
750 setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
751 PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
752 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
753 setDescription(PHPEditorMessages.getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
755 boolean checked= PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean("LexicalSortingAction.isChecked"); //$NON-NLS-1$
756 valueChanged(checked, false);
760 valueChanged(isChecked(), true);
763 private void valueChanged(final boolean on, boolean store) {
765 BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(), new Runnable() {
767 fOutlineViewer.setSorter(on ? fSorter : null); }
771 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue("LexicalSortingAction.isChecked", on); //$NON-NLS-1$
775 class ClassOnlyAction extends Action {
777 public ClassOnlyAction() {
779 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
780 setText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
781 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
782 setDescription(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
783 PHPUiImages.setLocalImageDescriptors(this, "gointo_toplevel_type.gif"); //$NON-NLS-1$
785 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
786 boolean showclass= preferenceStore.getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
787 setTopLevelTypeOnly(showclass);
791 * @see org.eclipse.jface.action.Action#run()
794 setTopLevelTypeOnly(!fTopLevelTypeOnly);
797 private void setTopLevelTypeOnly(boolean show) {
798 fTopLevelTypeOnly= show;
800 fOutlineViewer.refresh(false);
802 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
803 preferenceStore.setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
808 * This action toggles whether this Java Outline page links
809 * its selection to the active editor.
813 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
815 JavaOutlinePage fJavaOutlinePage;
818 * Constructs a new action.
820 * @param outlinePage the Java outline page
822 public ToggleLinkingAction(JavaOutlinePage outlinePage) {
823 boolean isLinkingEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
824 setChecked(isLinkingEnabled);
825 fJavaOutlinePage= outlinePage;
832 PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE, isChecked());
833 if (isChecked() && fEditor != null)
834 fEditor.synchronizeOutlinePage(fEditor.computeHighlightRangeSourceReference(), false);
840 /** A flag to show contents of top level type only */
841 private boolean fTopLevelTypeOnly;
843 private IJavaElement fInput;
844 private String fContextMenuID;
846 private JavaOutlineViewer fOutlineViewer;
847 private PHPEditor fEditor;
849 private MemberFilterActionGroup fMemberFilterActionGroup;
851 private ListenerList fSelectionChangedListeners= new ListenerList();
852 private ListenerList fPostSelectionChangedListeners= new ListenerList();
853 private Hashtable fActions= new Hashtable();
855 private TogglePresentationAction fTogglePresentation;
856 private GotoAnnotationAction fPreviousAnnotation;
857 private GotoAnnotationAction fNextAnnotation;
858 private TextEditorAction fShowJavadoc;
859 private IAction fUndo;
860 private IAction fRedo;
862 private ToggleLinkingAction fToggleLinkingAction;
864 private CompositeActionGroup fActionGroups;
866 private IPropertyChangeListener fPropertyChangeListener;
868 * Custom filter action group.
871 private CustomFiltersActionGroup fCustomFiltersActionGroup;
873 public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
876 Assert.isNotNull(editor);
878 fContextMenuID= contextMenuID;
880 fTogglePresentation= new TogglePresentationAction();
881 fPreviousAnnotation= new GotoAnnotationAction("PreviousAnnotation.", false); //$NON-NLS-1$
882 fNextAnnotation= new GotoAnnotationAction("NextAnnotation.", true); //$NON-NLS-1$
883 fShowJavadoc= (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
884 fUndo= fEditor.getAction(ITextEditorActionConstants.UNDO);
885 fRedo= fEditor.getAction(ITextEditorActionConstants.REDO);
887 fTogglePresentation.setEditor(editor);
888 fPreviousAnnotation.setEditor(editor);
889 fNextAnnotation.setEditor(editor);
891 fPropertyChangeListener= new IPropertyChangeListener() {
892 public void propertyChange(PropertyChangeEvent event) {
893 doPropertyChange(event);
896 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
900 * Returns the primary type of a compilation unit (has the same
901 * name as the compilation unit).
903 * @param compilationUnit the compilation unit
904 * @return returns the primary type of the compilation unit, or
905 * <code>null</code> if is does not have one
907 protected IType getMainType(ICompilationUnit compilationUnit) {
909 if (compilationUnit == null)
912 String name= compilationUnit.getElementName();
913 int index= name.indexOf('.');
915 name= name.substring(0, index);
916 IType type= compilationUnit.getType(name);
917 return type.exists() ? type : null;
921 * Returns the primary type of a class file.
923 * @param classFile the class file
924 * @return returns the primary type of the class file, or <code>null</code>
925 * if is does not have one
927 // protected IType getMainType(IClassFile classFile) {
929 // IType type= classFile.getType();
930 // return type != null && type.exists() ? type : null;
931 // } catch (JavaModelException e) {
937 * Method declared on Page
939 public void init(IPageSite pageSite) {
940 super.init(pageSite);
943 private void doPropertyChange(PropertyChangeEvent event) {
944 if (fOutlineViewer != null) {
945 if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) {
946 fOutlineViewer.refresh(false);
952 * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
954 public void addSelectionChangedListener(ISelectionChangedListener listener) {
955 if (fOutlineViewer != null)
956 fOutlineViewer.addSelectionChangedListener(listener);
958 fSelectionChangedListeners.add(listener);
962 * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
964 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
965 if (fOutlineViewer != null)
966 fOutlineViewer.removeSelectionChangedListener(listener);
968 fSelectionChangedListeners.remove(listener);
972 * @see ISelectionProvider#setSelection(ISelection)
974 public void setSelection(ISelection selection) {
975 if (fOutlineViewer != null)
976 fOutlineViewer.setSelection(selection);
980 * @see ISelectionProvider#getSelection()
982 public ISelection getSelection() {
983 if (fOutlineViewer == null)
984 return StructuredSelection.EMPTY;
985 return fOutlineViewer.getSelection();
989 * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
991 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
992 if (fOutlineViewer != null)
993 fOutlineViewer.addPostSelectionChangedListener(listener);
995 fPostSelectionChangedListeners.add(listener);
999 * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1001 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
1002 if (fOutlineViewer != null)
1003 fOutlineViewer.removePostSelectionChangedListener(listener);
1005 fPostSelectionChangedListeners.remove(listener);
1008 private void registerToolbarActions(IActionBars actionBars) {
1010 IToolBarManager toolBarManager= actionBars.getToolBarManager();
1011 if (toolBarManager != null) {
1012 toolBarManager.add(new LexicalSortingAction());
1014 fMemberFilterActionGroup= new MemberFilterActionGroup(fOutlineViewer, "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1015 fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1017 fCustomFiltersActionGroup.fillActionBars(actionBars);
1019 IMenuManager menu= actionBars.getMenuManager();
1020 menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1022 fToggleLinkingAction= new ToggleLinkingAction(this);
1023 menu.add(new ClassOnlyAction());
1024 menu.add(fToggleLinkingAction);
1029 * @see IPage#createControl
1031 public void createControl(Composite parent) {
1033 Tree tree= new Tree(parent, SWT.MULTI);
1035 AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider(
1036 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_APP_TYPE_SIGNATURE,
1037 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
1040 fOutlineViewer= new JavaOutlineViewer(tree);
1042 fOutlineViewer.setContentProvider(new ChildrenProvider());
1043 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
1045 Object[] listeners= fSelectionChangedListeners.getListeners();
1046 for (int i= 0; i < listeners.length; i++) {
1047 fSelectionChangedListeners.remove(listeners[i]);
1048 fOutlineViewer.addSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1051 listeners= fPostSelectionChangedListeners.getListeners();
1052 for (int i= 0; i < listeners.length; i++) {
1053 fPostSelectionChangedListeners.remove(listeners[i]);
1054 fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1057 MenuManager manager= new MenuManager(fContextMenuID, fContextMenuID);
1058 manager.setRemoveAllWhenShown(true);
1059 manager.addMenuListener(new IMenuListener() {
1060 public void menuAboutToShow(IMenuManager m) {
1061 contextMenuAboutToShow(m);
1064 fMenu= manager.createContextMenu(tree);
1065 tree.setMenu(fMenu);
1067 IPageSite site= getSite();
1068 site.registerContextMenu(PHPeclipsePlugin.getPluginId() + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1069 site.setSelectionProvider(fOutlineViewer);
1071 // we must create the groups after we have set the selection provider to the site
1072 fActionGroups= new CompositeActionGroup(new ActionGroup[] {
1073 // new OpenViewActionGroup(this),
1074 // new CCPActionGroup(this),
1075 new GenerateActionGroup(this)});
1076 // new RefactorActionGroup(this),
1077 // new JavaSearchActionGroup(this)});
1079 // register global actions
1080 IActionBars bars= site.getActionBars();
1082 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1083 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1084 bars.setGlobalActionHandler(ActionFactory.PREVIOUS.getId(), fPreviousAnnotation);
1085 bars.setGlobalActionHandler(ActionFactory.NEXT.getId(), fNextAnnotation);
1086 bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC, fShowJavadoc);
1087 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY, fTogglePresentation);
1088 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, fNextAnnotation);
1089 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, fPreviousAnnotation);
1092 fActionGroups.fillActionBars(bars);
1094 IStatusLineManager statusLineManager= bars.getStatusLineManager();
1095 if (statusLineManager != null) {
1096 StatusBarUpdater updater= new StatusBarUpdater(statusLineManager);
1097 fOutlineViewer.addPostSelectionChangedListener(updater);
1099 // Custom filter group
1100 fCustomFiltersActionGroup= new CustomFiltersActionGroup("net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1102 registerToolbarActions(bars);
1104 fOutlineViewer.setInput(fInput);
1107 public void dispose() {
1109 if (fEditor == null)
1112 if (fMemberFilterActionGroup != null) {
1113 fMemberFilterActionGroup.dispose();
1114 fMemberFilterActionGroup= null;
1117 if (fCustomFiltersActionGroup != null) {
1118 fCustomFiltersActionGroup.dispose();
1119 fCustomFiltersActionGroup= null;
1123 fEditor.outlinePageClosed();
1126 fSelectionChangedListeners.clear();
1127 fSelectionChangedListeners= null;
1129 fPostSelectionChangedListeners.clear();
1130 fPostSelectionChangedListeners= null;
1132 if (fPropertyChangeListener != null) {
1133 PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1134 fPropertyChangeListener= null;
1137 if (fMenu != null && !fMenu.isDisposed()) {
1142 if (fActionGroups != null)
1143 fActionGroups.dispose();
1145 fTogglePresentation.setEditor(null);
1146 fPreviousAnnotation.setEditor(null);
1147 fNextAnnotation.setEditor(null);
1149 fOutlineViewer= null;
1154 public Control getControl() {
1155 if (fOutlineViewer != null)
1156 return fOutlineViewer.getControl();
1160 public void setInput(IJavaElement inputElement) {
1161 fInput= inputElement;
1162 if (fOutlineViewer != null)
1163 fOutlineViewer.setInput(fInput);
1166 public void select(ISourceReference reference) {
1167 if (fOutlineViewer != null) {
1169 ISelection s= fOutlineViewer.getSelection();
1170 if (s instanceof IStructuredSelection) {
1171 IStructuredSelection ss= (IStructuredSelection) s;
1172 List elements= ss.toList();
1173 if (!elements.contains(reference)) {
1174 s= (reference == null ? StructuredSelection.EMPTY : new StructuredSelection(reference));
1175 fOutlineViewer.setSelection(s, true);
1181 public void setAction(String actionID, IAction action) {
1182 Assert.isNotNull(actionID);
1184 fActions.remove(actionID);
1186 fActions.put(actionID, action);
1189 public IAction getAction(String actionID) {
1190 Assert.isNotNull(actionID);
1191 return (IAction) fActions.get(actionID);
1195 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1197 public Object getAdapter(Class key) {
1198 if (key == IShowInSource.class) {
1199 return getShowInSource();
1201 if (key == IShowInTargetList.class) {
1202 return new IShowInTargetList() {
1203 public String[] getShowInTargetIds() {
1204 return new String[] { JavaUI.ID_PACKAGES };
1209 if (key == IShowInTarget.class) {
1210 return getShowInTarget();
1217 * Convenience method to add the action installed under the given actionID to the
1218 * specified group of the menu.
1220 * @param menu the menu manager
1221 * @param group the group to which to add the action
1222 * @param actionID the ID of the new action
1224 protected void addAction(IMenuManager menu, String group, String actionID) {
1225 IAction action= getAction(actionID);
1226 if (action != null) {
1227 if (action instanceof IUpdate)
1228 ((IUpdate) action).update();
1230 if (action.isEnabled()) {
1231 IMenuManager subMenu= menu.findMenuUsingPath(group);
1232 if (subMenu != null)
1233 subMenu.add(action);
1235 menu.appendToGroup(group, action);
1240 protected void contextMenuAboutToShow(IMenuManager menu) {
1242 PHPeclipsePlugin.createStandardGroups(menu);
1244 IStructuredSelection selection= (IStructuredSelection)getSelection();
1245 fActionGroups.setContext(new ActionContext(selection));
1246 fActionGroups.fillContextMenu(menu);
1250 * @see Page#setFocus()
1252 public void setFocus() {
1253 if (fOutlineViewer != null)
1254 fOutlineViewer.getControl().setFocus();
1258 * Checks whether a given Java element is an inner type.
1260 * @param element the java element
1261 * @return <code>true</code> iff the given element is an inner type
1263 private boolean isInnerType(IJavaElement element) {
1265 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1266 IType type= (IType)element;
1268 return type.isMember();
1269 } catch (JavaModelException e) {
1270 IJavaElement parent= type.getParent();
1271 if (parent != null) {
1272 int parentElementType= parent.getElementType();
1273 return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
1282 * Returns the <code>IShowInSource</code> for this view.
1284 * @return the {@link IShowInSource}
1286 protected IShowInSource getShowInSource() {
1287 return new IShowInSource() {
1288 public ShowInContext getShowInContext() {
1289 return new ShowInContext(
1291 getSite().getSelectionProvider().getSelection());
1297 * Returns the <code>IShowInTarget</code> for this view.
1299 * @return the {@link IShowInTarget}
1301 protected IShowInTarget getShowInTarget() {
1302 return new IShowInTarget() {
1303 public boolean show(ShowInContext context) {
1304 ISelection sel= context.getSelection();
1305 if (sel instanceof ITextSelection) {
1306 ITextSelection tsel= (ITextSelection) sel;
1307 int offset= tsel.getOffset();
1308 IJavaElement element= fEditor.getElementAt(offset);
1309 if (element != null) {
1310 setSelection(new StructuredSelection(element));
1319 private void initDragAndDrop() {
1320 int ops= DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1321 Transfer[] transfers= new Transfer[] {
1322 LocalSelectionTransfer.getInstance()
1326 // TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
1327 // new SelectionTransferDropAdapter(fOutlineViewer)
1329 // fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new DelegatingDropAdapter(dropListeners));
1332 // TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] {
1333 // new SelectionTransferDragAdapter(fOutlineViewer)
1335 // fOutlineViewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(fOutlineViewer, dragListeners));