1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.List;
16 import java.util.Vector;
18 import net.sourceforge.phpdt.core.ElementChangedEvent;
19 import net.sourceforge.phpdt.core.ICompilationUnit;
20 import net.sourceforge.phpdt.core.IElementChangedListener;
21 import net.sourceforge.phpdt.core.IJavaElement;
22 import net.sourceforge.phpdt.core.IJavaElementDelta;
23 import net.sourceforge.phpdt.core.IMember;
24 import net.sourceforge.phpdt.core.IParent;
25 import net.sourceforge.phpdt.core.ISourceRange;
26 import net.sourceforge.phpdt.core.ISourceReference;
27 import net.sourceforge.phpdt.core.IType;
28 import net.sourceforge.phpdt.core.JavaCore;
29 import net.sourceforge.phpdt.core.JavaModelException;
30 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
31 import net.sourceforge.phpdt.internal.ui.actions.AbstractToggleLinkingAction;
32 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
33 import net.sourceforge.phpdt.internal.ui.dnd.JdtViewerDragAdapter;
34 import net.sourceforge.phpdt.internal.ui.dnd.TransferDragSourceListener;
35 import net.sourceforge.phpdt.internal.ui.packageview.SelectionTransferDragAdapter;
36 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
37 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
38 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
39 import net.sourceforge.phpdt.internal.ui.viewsupport.StatusBarUpdater;
40 import net.sourceforge.phpdt.ui.JavaElementSorter;
41 import net.sourceforge.phpdt.ui.JavaUI;
42 import net.sourceforge.phpdt.ui.PreferenceConstants;
43 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
44 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
45 import net.sourceforge.phpdt.ui.actions.MemberFilterActionGroup;
46 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
48 import org.eclipse.core.resources.IResource;
49 import org.eclipse.core.runtime.IAdaptable;
50 import org.eclipse.jface.action.Action;
51 import org.eclipse.jface.action.IAction;
52 import org.eclipse.jface.action.IMenuListener;
53 import org.eclipse.jface.action.IMenuManager;
54 import org.eclipse.jface.action.IStatusLineManager;
55 import org.eclipse.jface.action.IToolBarManager;
56 import org.eclipse.jface.action.MenuManager;
57 import org.eclipse.jface.action.Separator;
58 import org.eclipse.jface.preference.IPreferenceStore;
59 import org.eclipse.jface.text.Assert;
60 import org.eclipse.jface.text.ITextSelection;
61 import org.eclipse.jface.util.IPropertyChangeListener;
62 import org.eclipse.jface.util.ListenerList;
63 import org.eclipse.jface.util.PropertyChangeEvent;
64 import org.eclipse.jface.viewers.IBaseLabelProvider;
65 import org.eclipse.jface.viewers.IPostSelectionProvider;
66 import org.eclipse.jface.viewers.ISelection;
67 import org.eclipse.jface.viewers.ISelectionChangedListener;
68 import org.eclipse.jface.viewers.IStructuredSelection;
69 import org.eclipse.jface.viewers.ITreeContentProvider;
70 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
71 import org.eclipse.jface.viewers.SelectionChangedEvent;
72 import org.eclipse.jface.viewers.StructuredSelection;
73 import org.eclipse.jface.viewers.TreeViewer;
74 import org.eclipse.jface.viewers.Viewer;
75 import org.eclipse.jface.viewers.ViewerFilter;
76 import org.eclipse.swt.SWT;
77 import org.eclipse.swt.custom.BusyIndicator;
78 import org.eclipse.swt.dnd.DND;
79 import org.eclipse.swt.dnd.Transfer;
80 import org.eclipse.swt.events.KeyEvent;
81 import org.eclipse.swt.widgets.Composite;
82 import org.eclipse.swt.widgets.Control;
83 import org.eclipse.swt.widgets.Display;
84 import org.eclipse.swt.widgets.Item;
85 import org.eclipse.swt.widgets.Menu;
86 import org.eclipse.swt.widgets.Tree;
87 import org.eclipse.swt.widgets.Widget;
88 import org.eclipse.ui.IActionBars;
89 import org.eclipse.ui.actions.ActionContext;
90 import org.eclipse.ui.actions.ActionGroup;
91 import org.eclipse.ui.model.IWorkbenchAdapter;
92 import org.eclipse.ui.model.WorkbenchAdapter;
93 import org.eclipse.ui.part.IPageSite;
94 import org.eclipse.ui.part.IShowInSource;
95 import org.eclipse.ui.part.IShowInTarget;
96 import org.eclipse.ui.part.IShowInTargetList;
97 import org.eclipse.ui.part.Page;
98 import org.eclipse.ui.part.ShowInContext;
99 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
100 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
101 import org.eclipse.ui.texteditor.IUpdate;
102 import org.eclipse.ui.texteditor.TextEditorAction;
103 import org.eclipse.ui.texteditor.TextOperationAction;
104 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
105 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
108 * The content outline page of the Java editor. The viewer implements a proprietary update mechanism based on Java model deltas. It
109 * does not react on domain changes. It is specified to show the content of ICompilationUnits and IClassFiles. Pulishes its context
110 * menu under <code>JavaPlugin.getDefault().getPluginId() + ".outline"</code>.
112 public class JavaOutlinePage extends Page implements IContentOutlinePage, IAdaptable, IPostSelectionProvider {
114 static Object[] NO_CHILDREN = new Object[0];
117 * The element change listener of the java outline viewer.
119 * @see IElementChangedListener
121 class ElementChangedListener implements IElementChangedListener {
123 public void elementChanged(final ElementChangedEvent e) {
125 if (getControl() == null)
128 Display d = getControl().getDisplay();
130 d.asyncExec(new Runnable() {
132 ICompilationUnit cu = (ICompilationUnit) fInput;
133 IJavaElement base = cu;
134 if (fTopLevelTypeOnly) {
135 base = getMainType(cu);
137 if (fOutlineViewer != null)
138 fOutlineViewer.refresh(true);
142 IJavaElementDelta delta = findElement(base, e.getDelta());
143 if (delta != null && fOutlineViewer != null) {
144 fOutlineViewer.reconcile(delta);
151 protected IJavaElementDelta findElement(IJavaElement unit, IJavaElementDelta delta) {
153 if (delta == null || unit == null)
156 IJavaElement element = delta.getElement();
158 if (unit.equals(element))
161 if (element.getElementType() > IJavaElement.CLASS_FILE)
164 IJavaElementDelta[] children = delta.getAffectedChildren();
165 if (children == null || children.length == 0)
168 for (int i = 0; i < children.length; i++) {
169 IJavaElementDelta d = findElement(unit, children[i]);
178 static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
180 * @see java.lang.Object#toString()
182 public String toString() {
183 return PHPEditorMessages.getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
187 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
189 public Object getAdapter(Class clas) {
190 if (clas == IWorkbenchAdapter.class)
197 * Content provider for the children of an ICompilationUnit or an IClassFile
199 * @see ITreeContentProvider
201 class ChildrenProvider implements ITreeContentProvider {
203 private Object[] NO_CLASS = new Object[] { new NoClassElement() };
205 private ElementChangedListener fListener;
207 protected boolean matches(IJavaElement element) {
208 if (element.getElementType() == IJavaElement.METHOD) {
209 String name = element.getElementName();
210 return (name != null && name.indexOf('<') >= 0);
215 protected IJavaElement[] filter(IJavaElement[] children) {
216 boolean initializers = false;
217 for (int i = 0; i < children.length; i++) {
218 if (matches(children[i])) {
227 Vector v = new Vector();
228 for (int i = 0; i < children.length; i++) {
229 if (matches(children[i]))
231 v.addElement(children[i]);
234 IJavaElement[] result = new IJavaElement[v.size()];
239 public Object[] getChildren(Object parent) {
240 if (parent instanceof IParent) {
241 IParent c = (IParent) parent;
243 return filter(c.getChildren());
244 } catch (JavaModelException x) {
245 PHPeclipsePlugin.log(x);
251 public Object[] getElements(Object parent) {
252 if (fTopLevelTypeOnly) {
253 if (parent instanceof ICompilationUnit) {
255 IType type = getMainType((ICompilationUnit) parent);
256 return type != null ? type.getChildren() : NO_CLASS;
257 } catch (JavaModelException e) {
258 PHPeclipsePlugin.log(e);
261 // else if (parent instanceof IClassFile) {
263 // IType type= getMainType((IClassFile) parent);
264 // return type != null ? type.getChildren() : NO_CLASS;
265 // } catch (JavaModelException e) {
266 // JavaPlugin.log(e);
270 return getChildren(parent);
273 public Object getParent(Object child) {
274 if (child instanceof IJavaElement) {
275 IJavaElement e = (IJavaElement) child;
276 return e.getParent();
281 public boolean hasChildren(Object parent) {
282 if (parent instanceof IParent) {
283 IParent c = (IParent) parent;
285 IJavaElement[] children = filter(c.getChildren());
286 return (children != null && children.length > 0);
287 } catch (JavaModelException x) {
288 PHPeclipsePlugin.log(x);
294 public boolean isDeleted(Object o) {
298 public void dispose() {
299 if (fListener != null) {
300 JavaCore.removeElementChangedListener(fListener);
306 * @see IContentProvider#inputChanged(Viewer, Object, Object)
308 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
309 boolean isCU = (newInput instanceof ICompilationUnit);
311 if (isCU && fListener == null) {
312 fListener = new ElementChangedListener();
313 JavaCore.addElementChangedListener(fListener);
314 } else if (!isCU && fListener != null) {
315 JavaCore.removeElementChangedListener(fListener);
321 class JavaOutlineViewer extends TreeViewer {
324 * Indicates an item which has been reused. At the point of its reuse it has been expanded. This field is used to communicate
325 * between <code>internalExpandToLevel</code> and <code>reuseTreeItem</code>.
327 private Item fReusedExpandedItem;
329 private boolean fReorderedMembers;
330 private boolean fForceFireSelectionChanged;
332 public JavaOutlineViewer(Tree tree) {
334 setAutoExpandLevel(ALL_LEVELS);
338 * Investigates the given element change event and if affected
339 * incrementally updates the Java outline.
341 * @param delta the Java element delta used to reconcile the Java outline
343 public void reconcile(IJavaElementDelta delta) {
344 fReorderedMembers= false;
345 fForceFireSelectionChanged= false;
346 if (getSorter() == null) {
347 if (fTopLevelTypeOnly
348 && delta.getElement() instanceof IType
349 && (delta.getKind() & IJavaElementDelta.ADDED) != 0)
354 Widget w= findItem(fInput);
355 if (w != null && !w.isDisposed())
357 if (fForceFireSelectionChanged)
358 fireSelectionChanged(new SelectionChangedEvent(getSite().getSelectionProvider(), this.getSelection()));
359 if (fReorderedMembers) {
361 fReorderedMembers= false;
369 // public void reconcile(IJavaElementDelta delta) {
370 // fReorderedMembers = false;
371 // if (getSorter() == null) {
372 // if (fTopLevelTypeOnly && delta.getElement() instanceof IType && (delta.getKind() & IJavaElementDelta.ADDED) != 0) {
376 // Widget w = findItem(fInput);
377 // if (w != null && !w.isDisposed())
379 // if (fReorderedMembers) {
381 // fReorderedMembers = false;
391 * @see TreeViewer#internalExpandToLevel
393 protected void internalExpandToLevel(Widget node, int level) {
394 if (node instanceof Item) {
395 Item i = (Item) node;
396 if (i.getData() instanceof IJavaElement) {
397 IJavaElement je = (IJavaElement) i.getData();
398 if (je.getElementType() == IJavaElement.IMPORT_CONTAINER || isInnerType(je)) {
399 if (i != fReusedExpandedItem) {
400 setExpanded(i, false);
406 super.internalExpandToLevel(node, level);
409 protected void reuseTreeItem(Item item, Object element) {
412 Item[] c = getChildren(item);
413 if (c != null && c.length > 0) {
415 if (getExpanded(item))
416 fReusedExpandedItem = item;
418 for (int k = 0; k < c.length; k++) {
419 if (c[k].getData() != null)
425 updateItem(item, element);
426 updatePlus(item, element);
427 internalExpandToLevel(item, ALL_LEVELS);
429 fReusedExpandedItem = null;
430 fForceFireSelectionChanged= true;
433 protected boolean mustUpdateParent(IJavaElementDelta delta, IJavaElement element) {
434 // if (element instanceof IMethod) {
435 // if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
437 // return ((IMethod) element).isMainMethod();
438 // } catch (JavaModelException e) {
439 // PHPeclipsePlugin.log(e.getStatus());
442 // return "main".equals(element.getElementName()); //$NON-NLS-1$
447 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
449 public boolean isExpandable(Object element) {
451 return getFilteredChildren(element).length > 0;
453 return super.isExpandable(element);
455 protected ISourceRange getSourceRange(IJavaElement element) throws JavaModelException {
456 if (element instanceof IMember)// && !(element instanceof IInitializer))
457 return ((IMember) element).getNameRange();
458 if (element instanceof ISourceReference)
459 return ((ISourceReference) element).getSourceRange();
463 protected boolean overlaps(ISourceRange range, int start, int end) {
464 return start <= (range.getOffset() + range.getLength() - 1) && range.getOffset() <= end;
467 protected boolean filtered(IJavaElement parent, IJavaElement child) {
469 Object[] result = new Object[] { child };
470 ViewerFilter[] filters = getFilters();
471 for (int i = 0; i < filters.length; i++) {
472 result = filters[i].filter(this, parent, result);
473 if (result.length == 0)
480 protected void update(Widget w, IJavaElementDelta delta) {
484 IJavaElement parent = delta.getElement();
485 IJavaElementDelta[] affected = delta.getAffectedChildren();
486 Item[] children = getChildren(w);
488 boolean doUpdateParent = false;
489 boolean doUpdateParentsPlus = false;
491 Vector deletions = new Vector();
492 Vector additions = new Vector();
494 for (int i = 0; i < affected.length; i++) {
495 IJavaElementDelta affectedDelta = affected[i];
496 IJavaElement affectedElement = affectedDelta.getElement();
497 int status = affected[i].getKind();
499 // find tree item with affected element
501 for (j = 0; j < children.length; j++)
502 if (affectedElement.equals(children[j].getData()))
505 if (j == children.length) {
506 // remove from collapsed parent
507 if ((status & IJavaElementDelta.REMOVED) != 0) {
508 doUpdateParentsPlus = true;
512 if ((status & IJavaElementDelta.CHANGED) != 0 && (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0
513 && !filtered(parent, affectedElement)) {
514 additions.addElement(affectedDelta);
522 if ((status & IJavaElementDelta.REMOVED) != 0) {
523 deletions.addElement(item);
524 doUpdateParent = doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
527 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
528 int change = affectedDelta.getFlags();
529 doUpdateParent = doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
531 if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
532 if (filtered(parent, affectedElement))
533 deletions.addElement(item);
535 updateItem(item, affectedElement);
538 if ((change & IJavaElementDelta.F_CONTENT) != 0)
539 updateItem(item, affectedElement);
541 if ((change & IJavaElementDelta.F_CHILDREN) != 0)
542 update(item, affectedDelta);
544 if ((change & IJavaElementDelta.F_REORDER) != 0)
545 fReorderedMembers = true;
549 // find all elements to add
550 IJavaElementDelta[] add = delta.getAddedChildren();
551 if (additions.size() > 0) {
552 IJavaElementDelta[] tmp = new IJavaElementDelta[add.length + additions.size()];
553 System.arraycopy(add, 0, tmp, 0, add.length);
554 for (int i = 0; i < additions.size(); i++)
555 tmp[i + add.length] = (IJavaElementDelta) additions.elementAt(i);
559 // add at the right position
560 go2: for (int i = 0; i < add.length; i++) {
564 IJavaElement e = add[i].getElement();
565 if (filtered(parent, e))
568 doUpdateParent = doUpdateParent || mustUpdateParent(add[i], e);
569 ISourceRange rng = getSourceRange(e);
570 int start = rng.getOffset();
571 int end = start + rng.getLength() - 1;
575 children = getChildren(w);
577 for (int j = 0; j < children.length; j++) {
579 IJavaElement r = (IJavaElement) item.getData();
582 // parent node collapsed and not be opened before -> do nothing
587 rng = getSourceRange(r);
588 if (overlaps(rng, start, end)) {
590 // be tolerant if the delta is not correct, or if
591 // the tree has been updated other than by a delta
592 reuseTreeItem(item, e);
595 } else if (rng.getOffset() > start) {
597 if (last != null && deletions.contains(last)) {
599 deletions.removeElement(last);
600 reuseTreeItem(last, (Object) e);
603 createTreeItem(w, (Object) e, j);
608 } catch (JavaModelException x) {
609 // stumbled over deleted element
615 // add at the end of the list
616 if (last != null && deletions.contains(last)) {
618 deletions.removeElement(last);
619 reuseTreeItem(last, e);
622 createTreeItem(w, e, -1);
625 } catch (JavaModelException x) {
626 // the element to be added is not present -> don't add it
630 // remove items which haven't been reused
631 Enumeration e = deletions.elements();
632 while (e.hasMoreElements()) {
633 item = (Item) e.nextElement();
639 updateItem(w, delta.getElement());
640 if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
641 updatePlus((Item) w, delta.getElement());
645 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
647 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
648 Object input = getInput();
649 if (event instanceof ProblemsLabelChangedEvent) {
650 ProblemsLabelChangedEvent e = (ProblemsLabelChangedEvent) event;
651 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
652 return; // marker changes can be ignored
655 // look if the underlying resource changed
656 Object[] changed = event.getElements();
657 if (changed != null) {
658 IResource resource = getUnderlyingResource();
659 if (resource != null) {
660 for (int i = 0; i < changed.length; i++) {
661 if (changed[i] != null && changed[i].equals(resource)) {
662 // change event to a full refresh
663 event = new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource());
669 super.handleLabelProviderChanged(event);
672 private IResource getUnderlyingResource() {
673 Object input = getInput();
674 if (input instanceof ICompilationUnit) {
675 ICompilationUnit cu = (ICompilationUnit) input;
676 if (cu.isWorkingCopy()) {
677 return cu.getOriginalElement().getResource();
679 return cu.getResource();
682 // else if (input instanceof IClassFile) {
683 // return ((IClassFile) input).getResource();
690 class LexicalSortingAction extends Action {
692 private JavaElementSorter fSorter = new JavaElementSorter();
694 public LexicalSortingAction() {
696 // WorkbenchHelp.setHelp(this, IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
697 setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
698 PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
699 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
700 setDescription(PHPEditorMessages.getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
702 boolean checked = PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean("LexicalSortingAction.isChecked"); //$NON-NLS-1$
703 valueChanged(checked, false);
707 valueChanged(isChecked(), true);
710 private void valueChanged(final boolean on, boolean store) {
712 BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(), new Runnable() {
714 fOutlineViewer.setSorter(on ? fSorter : null);
719 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue("LexicalSortingAction.isChecked", on); //$NON-NLS-1$
723 class ClassOnlyAction extends Action {
725 public ClassOnlyAction() {
727 // WorkbenchHelp.setHelp(this, IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
728 setText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
729 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
730 setDescription(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
731 PHPUiImages.setLocalImageDescriptors(this, "gointo_toplevel_type.gif"); //$NON-NLS-1$
733 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
734 boolean showclass = preferenceStore.getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
735 setTopLevelTypeOnly(showclass);
739 * @see org.eclipse.jface.action.Action#run()
742 setTopLevelTypeOnly(!fTopLevelTypeOnly);
745 private void setTopLevelTypeOnly(boolean show) {
746 fTopLevelTypeOnly = show;
748 fOutlineViewer.refresh(false);
750 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
751 preferenceStore.setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
756 * This action toggles whether this Java Outline page links its selection to the active editor.
760 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
762 JavaOutlinePage fJavaOutlinePage;
765 * Constructs a new action.
768 * the Java outline page
770 public ToggleLinkingAction(JavaOutlinePage outlinePage) {
771 boolean isLinkingEnabled = PreferenceConstants.getPreferenceStore().getBoolean(
772 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
773 setChecked(isLinkingEnabled);
774 fJavaOutlinePage = outlinePage;
781 PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE, isChecked());
782 if (isChecked() && fEditor != null)
783 fEditor.synchronizeOutlinePage(fEditor.computeHighlightRangeSourceReference(), false);
788 /** A flag to show contents of top level type only */
789 private boolean fTopLevelTypeOnly;
791 private IJavaElement fInput;
793 private String fContextMenuID;
797 private JavaOutlineViewer fOutlineViewer;
799 private PHPEditor fEditor;
801 private MemberFilterActionGroup fMemberFilterActionGroup;
803 private ListenerList fSelectionChangedListeners = new ListenerList();
805 private ListenerList fPostSelectionChangedListeners = new ListenerList();
807 private Hashtable fActions = new Hashtable();
809 private TogglePresentationAction fTogglePresentation;
811 private GotoErrorAction fPreviousError;
813 private GotoErrorAction fNextError;
815 private TextEditorAction fShowJavadoc;
817 private TextOperationAction fUndo;
819 private TextOperationAction fRedo;
821 private ToggleLinkingAction fToggleLinkingAction;
823 private CompositeActionGroup fActionGroups;
825 // private CCPActionGroup fCCPActionGroup;
827 private IPropertyChangeListener fPropertyChangeListener;
829 * Custom filter action group.
832 // private CustomFiltersActionGroup fCustomFiltersActionGroup;
834 public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
837 Assert.isNotNull(editor);
839 fContextMenuID = contextMenuID;
842 fTogglePresentation = new TogglePresentationAction();
843 fPreviousError = new GotoErrorAction("PreviousError.", false); //$NON-NLS-1$
844 fPreviousError.setImageDescriptor(PHPUiImages.DESC_TOOL_GOTO_PREV_ERROR);
845 fNextError = new GotoErrorAction("NextError.", true); //$NON-NLS-1$
846 fNextError.setImageDescriptor(PHPUiImages.DESC_TOOL_GOTO_NEXT_ERROR);
847 fShowJavadoc = (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
848 fUndo = (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.UNDO);
849 fRedo = (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.REDO);
851 fTogglePresentation.setEditor(editor);
852 fPreviousError.setEditor(editor);
853 fNextError.setEditor(editor);
855 fPropertyChangeListener = new IPropertyChangeListener() {
856 public void propertyChange(PropertyChangeEvent event) {
857 doPropertyChange(event);
860 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
864 * Returns the primary type of a compilation unit (has the same name as the compilation unit).
866 * @param compilationUnit
867 * the compilation unit
868 * @return returns the primary type of the compilation unit, or <code>null</code> if is does not have one
870 protected IType getMainType(ICompilationUnit compilationUnit) {
871 String name = compilationUnit.getElementName();
872 int index = name.indexOf('.');
874 name = name.substring(0, index);
875 IType type = compilationUnit.getType(name);
876 return type.exists() ? type : null;
880 * Returns the primary type of a class file.
884 * @return returns the primary type of the class file, or <code>null</code> if is does not have one
886 // protected IType getMainType(IClassFile classFile) {
888 // IType type= classFile.getType();
889 // return type != null && type.exists() ? type : null;
890 // } catch (JavaModelException e) {
895 * (non-Javadoc) Method declared on Page
897 public void init(IPageSite pageSite) {
898 super.init(pageSite);
901 private void doPropertyChange(PropertyChangeEvent event) {
902 if (fOutlineViewer != null) {
903 if (PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER.equals(event.getProperty())) {
904 fOutlineViewer.refresh(false);
910 * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
912 public void addSelectionChangedListener(ISelectionChangedListener listener) {
913 if (fOutlineViewer != null)
914 fOutlineViewer.addPostSelectionChangedListener(listener);
916 fSelectionChangedListeners.add(listener);
920 * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
922 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
923 if (fOutlineViewer != null)
924 fOutlineViewer.removePostSelectionChangedListener(listener);
926 fSelectionChangedListeners.remove(listener);
930 * @see ISelectionProvider#setSelection(ISelection)
932 public void setSelection(ISelection selection) {
933 if (fOutlineViewer != null)
934 fOutlineViewer.setSelection(selection);
938 * @see ISelectionProvider#getSelection()
940 public ISelection getSelection() {
941 if (fOutlineViewer == null)
942 return StructuredSelection.EMPTY;
943 return fOutlineViewer.getSelection();
946 // private void registerToolbarActions() {
948 // IToolBarManager toolBarManager = getSite().getActionBars().getToolBarManager();
949 // if (toolBarManager != null) {
950 // toolBarManager.add(new ClassOnlyAction());
951 // toolBarManager.add(new LexicalSortingAction());
953 // fMemberFilterActionGroup = new MemberFilterActionGroup(fOutlineViewer, "JavaOutlineViewer"); //$NON-NLS-1$
954 // fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
959 private void registerToolbarActions(IActionBars actionBars) {
961 IToolBarManager toolBarManager = actionBars.getToolBarManager();
962 if (toolBarManager != null) {
963 toolBarManager.add(new LexicalSortingAction());
965 fMemberFilterActionGroup = new MemberFilterActionGroup(fOutlineViewer, "org.eclipse.jdt.ui.JavaOutlinePage"); //$NON-NLS-1$
966 fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
968 // fCustomFiltersActionGroup.fillActionBars(actionBars);
970 IMenuManager menu = actionBars.getMenuManager();
971 menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
973 fToggleLinkingAction = new ToggleLinkingAction(this);
974 menu.add(new ClassOnlyAction());
975 menu.add(fToggleLinkingAction);
980 * @see IPage#createControl
982 public void createControl(Composite parent) {
984 Tree tree = new Tree(parent, SWT.MULTI);
986 AppearanceAwareLabelProvider lprovider = new AppearanceAwareLabelProvider(AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
987 | JavaElementLabels.F_APP_TYPE_SIGNATURE, AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
989 fOutlineViewer = new JavaOutlineViewer(tree);
990 fOutlineViewer.setContentProvider(new ChildrenProvider());
991 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
993 Object[] listeners = fSelectionChangedListeners.getListeners();
994 for (int i = 0; i < listeners.length; i++) {
995 fSelectionChangedListeners.remove(listeners[i]);
996 fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
999 listeners = fPostSelectionChangedListeners.getListeners();
1000 for (int i = 0; i < listeners.length; i++) {
1001 fPostSelectionChangedListeners.remove(listeners[i]);
1002 fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1005 MenuManager manager = new MenuManager(fContextMenuID, fContextMenuID);
1006 manager.setRemoveAllWhenShown(true);
1007 manager.addMenuListener(new IMenuListener() {
1008 public void menuAboutToShow(IMenuManager manager) {
1009 contextMenuAboutToShow(manager);
1012 fMenu = manager.createContextMenu(tree);
1013 tree.setMenu(fMenu);
1015 IPageSite site = getSite();
1016 site.registerContextMenu(PHPeclipsePlugin.getPluginId() + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1017 site.setSelectionProvider(fOutlineViewer);
1019 // we must create the groups after we have set the selection provider to the site
1020 fActionGroups = new CompositeActionGroup(new ActionGroup[] {
1021 // new OpenViewActionGroup(this),
1022 // fCCPActionGroup= new CCPActionGroup(this),
1023 new GenerateActionGroup(this) });
1024 // new RefactorActionGroup(this),
1025 // new JavaSearchActionGroup(this)});
1027 // register global actions
1028 IActionBars bars = site.getActionBars();
1030 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1031 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1032 bars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, fPreviousError);
1033 bars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, fNextError);
1034 // bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_PHP_DOC, fShowJavadoc);
1035 bars.setGlobalActionHandler(IJavaEditorActionConstants.TOGGLE_PRESENTATION, fTogglePresentation);
1036 // http://dev.eclipse.org/bugs/show_bug.cgi?id=18968
1037 bars.setGlobalActionHandler(IJavaEditorActionConstants.PREVIOUS_ERROR, fPreviousError);
1038 bars.setGlobalActionHandler(IJavaEditorActionConstants.NEXT_ERROR, fNextError);
1040 fActionGroups.fillActionBars(bars);
1042 IStatusLineManager statusLineManager = site.getActionBars().getStatusLineManager();
1043 if (statusLineManager != null) {
1044 StatusBarUpdater updater = new StatusBarUpdater(statusLineManager);
1045 fOutlineViewer.addPostSelectionChangedListener(updater);
1048 registerToolbarActions(bars);
1050 fOutlineViewer.setInput(fInput);
1051 // fOutlineViewer.getControl().addKeyListener(new KeyAdapter() {
1052 // public void keyPressed(KeyEvent e) {
1053 // handleKeyReleased(e);
1057 // initDragAndDrop();
1060 public void dispose() {
1062 if (fEditor == null)
1065 if (fMemberFilterActionGroup != null) {
1066 fMemberFilterActionGroup.dispose();
1067 fMemberFilterActionGroup = null;
1070 fEditor.outlinePageClosed();
1073 fSelectionChangedListeners.clear();
1074 fSelectionChangedListeners = null;
1076 fPostSelectionChangedListeners.clear();
1077 fPostSelectionChangedListeners = null;
1079 if (fPropertyChangeListener != null) {
1080 PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1081 fPropertyChangeListener = null;
1084 if (fMenu != null && !fMenu.isDisposed()) {
1089 if (fActionGroups != null)
1090 fActionGroups.dispose();
1092 fTogglePresentation.setEditor(null);
1093 fPreviousError.setEditor(null);
1094 fNextError.setEditor(null);
1096 fOutlineViewer = null;
1101 public Control getControl() {
1102 if (fOutlineViewer != null)
1103 return fOutlineViewer.getControl();
1107 public void setInput(IJavaElement inputElement) {
1108 fInput = inputElement;
1109 if (fOutlineViewer != null)
1110 fOutlineViewer.setInput(fInput);
1113 public void select(ISourceReference reference) {
1114 if (fOutlineViewer != null) {
1116 ISelection s = fOutlineViewer.getSelection();
1117 if (s instanceof IStructuredSelection) {
1118 IStructuredSelection ss = (IStructuredSelection) s;
1119 List elements = ss.toList();
1120 if (!elements.contains(reference)) {
1121 s = (reference == null ? StructuredSelection.EMPTY : new StructuredSelection(reference));
1122 fOutlineViewer.setSelection(s, true);
1128 public void setAction(String actionID, IAction action) {
1129 Assert.isNotNull(actionID);
1131 fActions.remove(actionID);
1133 fActions.put(actionID, action);
1136 public IAction getAction(String actionID) {
1137 Assert.isNotNull(actionID);
1138 return (IAction) fActions.get(actionID);
1142 * Answer the property defined by key.
1144 public Object getAdapter(Class key) {
1145 if (key == IShowInSource.class) {
1146 return getShowInSource();
1148 if (key == IShowInTargetList.class) {
1149 return new IShowInTargetList() {
1150 public String[] getShowInTargetIds() {
1151 return new String[] { JavaUI.ID_PACKAGES };
1156 if (key == IShowInTarget.class) {
1157 return getShowInTarget();
1164 * Convenience method to add the action installed under the given actionID to the specified group of the menu.
1166 protected void addAction(IMenuManager menu, String group, String actionID) {
1167 IAction action = getAction(actionID);
1168 if (action != null) {
1169 if (action instanceof IUpdate)
1170 ((IUpdate) action).update();
1172 if (action.isEnabled()) {
1173 IMenuManager subMenu = menu.findMenuUsingPath(group);
1174 if (subMenu != null)
1175 subMenu.add(action);
1177 menu.appendToGroup(group, action);
1182 protected void contextMenuAboutToShow(IMenuManager menu) {
1184 PHPeclipsePlugin.createStandardGroups(menu);
1186 IStructuredSelection selection = (IStructuredSelection) getSelection();
1187 fActionGroups.setContext(new ActionContext(selection));
1188 fActionGroups.fillContextMenu(menu);
1192 * @see Page#setFocus()
1194 public void setFocus() {
1195 if (fOutlineViewer != null)
1196 fOutlineViewer.getControl().setFocus();
1200 * Checkes whether a given Java element is an inner type.
1202 private boolean isInnerType(IJavaElement element) {
1204 if (element.getElementType() == IJavaElement.TYPE) {
1205 IJavaElement parent = element.getParent();
1206 int type = parent.getElementType();
1207 return (type != IJavaElement.COMPILATION_UNIT && type != IJavaElement.CLASS_FILE);
1214 * Handles key events in viewer.
1216 private void handleKeyReleased(KeyEvent event) {
1218 if (event.stateMask != 0)
1221 IAction action = null;
1222 // if (event.character == SWT.DEL) {
1223 // action= fCCPActionGroup.getDeleteAction();
1226 if (action != null && action.isEnabled())
1231 * Returns the <code>IShowInSource</code> for this view.
1233 protected IShowInSource getShowInSource() {
1234 return new IShowInSource() {
1235 public ShowInContext getShowInContext() {
1236 return new ShowInContext(null, getSite().getSelectionProvider().getSelection());
1242 * Returns the <code>IShowInTarget</code> for this view.
1244 protected IShowInTarget getShowInTarget() {
1245 return new IShowInTarget() {
1246 public boolean show(ShowInContext context) {
1247 ISelection sel = context.getSelection();
1248 if (sel instanceof ITextSelection) {
1249 ITextSelection tsel = (ITextSelection) sel;
1250 int offset = tsel.getOffset();
1251 IJavaElement element = fEditor.getElementAt(offset);
1252 if (element != null) {
1253 setSelection(new StructuredSelection(element));
1262 private void initDragAndDrop() {
1263 int ops = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1264 Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getInstance() };
1267 // TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
1268 // new SelectionTransferDropAdapter(fOutlineViewer)
1270 // fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new DelegatingDropAdapter(dropListeners));
1273 TransferDragSourceListener[] dragListeners = new TransferDragSourceListener[] { new SelectionTransferDragAdapter(fOutlineViewer) };
1274 fOutlineViewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(fOutlineViewer, dragListeners));
1278 * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1280 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
1281 if (fOutlineViewer != null)
1282 fOutlineViewer.addPostSelectionChangedListener(listener);
1284 fPostSelectionChangedListeners.add(listener);
1288 * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1290 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
1291 if (fOutlineViewer != null)
1292 fOutlineViewer.removePostSelectionChangedListener(listener);
1294 fPostSelectionChangedListeners.remove(listener);