avoid NullPointerException in debug
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaOutlinePage.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
12
13
14 import java.util.Enumeration;
15 import java.util.Hashtable;
16 import java.util.List;
17 import java.util.Vector;
18
19 import net.sourceforge.phpdt.core.ElementChangedEvent;
20 import net.sourceforge.phpdt.core.ICompilationUnit;
21 import net.sourceforge.phpdt.core.IElementChangedListener;
22 import net.sourceforge.phpdt.core.IField;
23 import net.sourceforge.phpdt.core.IJavaElement;
24 import net.sourceforge.phpdt.core.IJavaElementDelta;
25 import net.sourceforge.phpdt.core.IMember;
26 import net.sourceforge.phpdt.core.IMethod;
27 import net.sourceforge.phpdt.core.IParent;
28 import net.sourceforge.phpdt.core.ISourceRange;
29 import net.sourceforge.phpdt.core.ISourceReference;
30 import net.sourceforge.phpdt.core.IType;
31 import net.sourceforge.phpdt.core.JavaCore;
32 import net.sourceforge.phpdt.core.JavaModelException;
33 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
34 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
35 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
36 import net.sourceforge.phpdt.internal.ui.actions.AbstractToggleLinkingAction;
37 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
38 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
39 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
40 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
41 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
42 import net.sourceforge.phpdt.internal.ui.viewsupport.StatusBarUpdater;
43 import net.sourceforge.phpdt.ui.JavaElementSorter;
44 import net.sourceforge.phpdt.ui.JavaUI;
45 import net.sourceforge.phpdt.ui.PreferenceConstants;
46 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
47 import net.sourceforge.phpdt.ui.actions.CustomFiltersActionGroup;
48 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
49 import net.sourceforge.phpdt.ui.actions.MemberFilterActionGroup;
50 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
51 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
52
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;
111
112
113 /**
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>.
118  */
119 public class JavaOutlinePage extends Page implements IContentOutlinePage, IAdaptable , IPostSelectionProvider {
120
121                         static Object[] NO_CHILDREN= new Object[0];
122
123                         /**
124                          * The element change listener of the java outline viewer.
125                          * @see IElementChangedListener
126                          */
127                         class ElementChangedListener implements IElementChangedListener {
128
129                                 public void elementChanged(final ElementChangedEvent e) {
130
131                                         if (getControl() == null)
132                                                 return;
133
134                                         Display d= getControl().getDisplay();
135                                         if (d != null) {
136                                                 d.asyncExec(new Runnable() {
137                                                         public void run() {
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);
145                                                                                 return;
146 //                                                                      }
147 //                                                              }
148 //                                                              IJavaElementDelta delta= findElement(base, e.getDelta());
149 //                                                              if (delta != null && fOutlineViewer != null) {
150 //                                                                      fOutlineViewer.reconcile(delta);
151 //                                                              }
152                                                         }
153                                                 });
154                                         }
155                                 }
156
157                                 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
158                                         if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
159                                                 return true; // add or remove
160                                         }
161                                         int flags= cuDelta.getFlags();
162                                         if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
163                                                 return true;
164                                         }
165                                         return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
166                                 }
167
168                                 protected IJavaElementDelta findElement(IJavaElement unit, IJavaElementDelta delta) {
169
170                                         if (delta == null || unit == null)
171                                                 return null;
172
173                                         IJavaElement element= delta.getElement();
174
175                                         if (unit.equals(element)) {
176                                                 if (isPossibleStructuralChange(delta)) {
177                                                         return delta;
178                                                 }
179                                                 return null;
180                                         }
181
182
183                                         if (element.getElementType() > IJavaElement.CLASS_FILE)
184                                                 return null;
185
186                                         IJavaElementDelta[] children= delta.getAffectedChildren();
187                                         if (children == null || children.length == 0)
188                                                 return null;
189
190                                         for (int i= 0; i < children.length; i++) {
191                                                 IJavaElementDelta d= findElement(unit, children[i]);
192                                                 if (d != null)
193                                                         return d;
194                                         }
195
196                                         return null;
197                                 }
198                         }
199
200                         static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
201                                 /*
202                                  * @see java.lang.Object#toString()
203                                  */
204                                 public String toString() {
205                                         return PHPEditorMessages.getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
206                                 }
207
208                                 /*
209                                  * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
210                                  */
211                                 public Object getAdapter(Class clas) {
212                                         if (clas == IWorkbenchAdapter.class)
213                                                 return this;
214                                         return null;
215                                 }
216                         }
217
218                         /**
219                          * Content provider for the children of an ICompilationUnit or
220                          * an IClassFile
221                          * @see ITreeContentProvider
222                          */
223                         class ChildrenProvider implements ITreeContentProvider {
224
225                                 private Object[] NO_CLASS= new Object[] {new NoClassElement()};
226                                 private ElementChangedListener fListener;
227
228                                 protected boolean matches(IJavaElement element) {
229                                         if (element.getElementType() == IJavaElement.METHOD) {
230                                                 String name= element.getElementName();
231                                                 return (name != null && name.indexOf('<') >= 0);
232                                         }
233                                         return false;
234                                 }
235
236                                 protected IJavaElement[] filter(IJavaElement[] children) {
237                                         boolean initializers= false;
238                                         for (int i= 0; i < children.length; i++) {
239                                                 if (matches(children[i])) {
240                                                         initializers= true;
241                                                         break;
242                                                 }
243                                         }
244
245                                         if (!initializers)
246                                                 return children;
247
248                                         Vector v= new Vector();
249                                         for (int i= 0; i < children.length; i++) {
250                                                 if (matches(children[i]))
251                                                         continue;
252                                                 v.addElement(children[i]);
253                                         }
254
255                                         IJavaElement[] result= new IJavaElement[v.size()];
256                                         v.copyInto(result);
257                                         return result;
258                                 }
259
260                                 public Object[] getChildren(Object parent) {
261                                         if (parent instanceof IParent) {
262                                                 IParent c= (IParent) parent;
263                                                 try {
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);
272                                                 }
273                                         }
274                                         return NO_CHILDREN;
275                                 }
276
277                                 public Object[] getElements(Object parent) {
278                                         if (fTopLevelTypeOnly) {
279                                                 if (parent instanceof ICompilationUnit) {
280                                                         try {
281                                                                 IType type= getMainType((ICompilationUnit) parent);
282                                                                 return type != null ? type.getChildren() : NO_CLASS;
283                                                         } catch (JavaModelException e) {
284                                                                 PHPeclipsePlugin.log(e);
285                                                         }
286                                                 }
287 //                                              else if (parent instanceof IClassFile) {
288 //                                                      try {
289 //                                                              IType type= getMainType((IClassFile) parent);
290 //                                                              return type != null ? type.getChildren() : NO_CLASS;
291 //                                                      } catch (JavaModelException e) {
292 //                                                              PHPeclipsePlugin.log(e);
293 //                                                      }
294 //                                              }
295                                         }
296                                         return getChildren(parent);
297                                 }
298
299                                 public Object getParent(Object child) {
300                                         if (child instanceof IJavaElement) {
301                                                 IJavaElement e= (IJavaElement) child;
302                                                 return e.getParent();
303                                         }
304                                         return null;
305                                 }
306
307                                 public boolean hasChildren(Object parent) {
308                                         if (parent instanceof IParent) {
309                                                 IParent c= (IParent) parent;
310                                                 try {
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);
320                                                 }
321                                         }
322                                         return false;
323                                 }
324
325                                 public boolean isDeleted(Object o) {
326                                         return false;
327                                 }
328
329                                 public void dispose() {
330                                         if (fListener != null) {
331                                                 JavaCore.removeElementChangedListener(fListener);
332                                                 fListener= null;
333                                         }
334                                 }
335
336                                 /*
337                                  * @see IContentProvider#inputChanged(Viewer, Object, Object)
338                                  */
339                                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
340                                         boolean isCU= (newInput instanceof ICompilationUnit);
341
342                                         if (isCU && fListener == null) {
343                                                 fListener= new ElementChangedListener();
344                                                 JavaCore.addElementChangedListener(fListener);
345                                         } else if (!isCU && fListener != null) {
346                                                 JavaCore.removeElementChangedListener(fListener);
347                                                 fListener= null;
348                                         }
349                                 }
350                         }
351
352
353                         class JavaOutlineViewer extends TreeViewer {
354
355                                 /**
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>.
360                                  */
361                                 private Item fReusedExpandedItem;
362                                 private boolean fReorderedMembers;
363                                 private boolean fForceFireSelectionChanged;
364
365                                 public JavaOutlineViewer(Tree tree) {
366                                         super(tree);
367                                         setAutoExpandLevel(ALL_LEVELS);
368                                         setUseHashlookup(true);
369                                 }
370
371                                 /**
372                                  * Investigates the given element change event and if affected
373                                  * incrementally updates the Java outline.
374                                  *
375                                  * @param delta the Java element delta used to reconcile the Java outline
376                                  */
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)
384                                                 {
385                                                         refresh(true);
386
387                                                 } else {
388                                                         Widget w= findItem(fInput);
389                                                         if (w != null && !w.isDisposed())
390                                                                 update(w, delta);
391                                                         if (fForceFireSelectionChanged)
392                                                                 fireSelectionChanged(new SelectionChangedEvent(getSite().getSelectionProvider(), this.getSelection()));
393                                                         if (fReorderedMembers) {
394                                                                 refresh(false);
395                                                                 fReorderedMembers= false;
396                                                 }
397                                                 }
398                                         } else {
399                                                 // just for now
400                                                 refresh(true);
401                                         }
402                                 }
403
404                                 /*
405                                  * @see TreeViewer#internalExpandToLevel
406                                  */
407                                 protected void internalExpandToLevel(Widget node, int level) {
408                                         if (node instanceof Item) {
409                                                 Item i= (Item) node;
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);
415                                                                         return;
416                                                                 }
417                                                         }
418                                                 }
419                                         }
420                                         super.internalExpandToLevel(node, level);
421                                 }
422
423                                 protected void reuseTreeItem(Item item, Object element) {
424
425                                         // remove children
426                                         Item[] c= getChildren(item);
427                                         if (c != null && c.length > 0) {
428
429                                                 if (getExpanded(item))
430                                                         fReusedExpandedItem= item;
431
432                                                 for (int k= 0; k < c.length; k++) {
433                                                         if (c[k].getData() != null)
434                                                                 disassociate(c[k]);
435                                                         c[k].dispose();
436                                                 }
437                                         }
438
439                                         updateItem(item, element);
440                                         updatePlus(item, element);
441                                         internalExpandToLevel(item, ALL_LEVELS);
442
443                                         fReusedExpandedItem= null;
444                                         fForceFireSelectionChanged= true;
445                                 }
446
447                                 protected boolean mustUpdateParent(IJavaElementDelta delta, IJavaElement element) {
448                                         if (element instanceof IMethod) {
449                                                 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
450                                                         try {
451                                                                 return ((IMethod)element).isMainMethod();
452                                                         } catch (JavaModelException e) {
453                                                                 PHPeclipsePlugin.log(e.getStatus());
454                                                         }
455                                                 }
456                                                 return "main".equals(element.getElementName()); //$NON-NLS-1$
457                                         }
458                                         return false;
459                                 }
460
461                                 /*
462                                  * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
463                                  */
464                                 public boolean isExpandable(Object element) {
465                                         if (hasFilters()) {
466                                                 return getFilteredChildren(element).length > 0;
467                                         }
468                                         return super.isExpandable(element);
469                                 }
470
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();
476                                         return null;
477                                 }
478
479                                 protected boolean overlaps(ISourceRange range, int start, int end) {
480                                         return start <= (range.getOffset() + range.getLength() - 1) && range.getOffset() <= end;
481                                 }
482
483                                 protected boolean filtered(IJavaElement parent, IJavaElement child) {
484
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)
490                                                         return true;
491                                         }
492
493                                         return false;
494                                 }
495
496                                 protected void update(Widget w, IJavaElementDelta delta) {
497
498                                         Item item;
499
500                                         IJavaElement parent= delta.getElement();
501                                         IJavaElementDelta[] affected= delta.getAffectedChildren();
502                                         Item[] children= getChildren(w);
503
504                                         boolean doUpdateParent= false;
505                                         boolean doUpdateParentsPlus= false;
506
507                                         Vector deletions= new Vector();
508                                         Vector additions= new Vector();
509
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();
514
515                                                 // find tree item with affected element
516                                                 int j;
517                                                 for (j= 0; j < children.length; j++)
518                                                     if (affectedElement.equals(children[j].getData()))
519                                                         break;
520
521                                                 if (j == children.length) {
522                                                         // remove from collapsed parent
523                                                         if ((status & IJavaElementDelta.REMOVED) != 0) {
524                                                                 doUpdateParentsPlus= true;
525                                                                 continue;
526                                                         }
527                                                         // addition
528                                                         if ((status & IJavaElementDelta.CHANGED) != 0 &&
529                                                                 (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0 &&
530                                                                 !filtered(parent, affectedElement))
531                                                         {
532                                                                 additions.addElement(affectedDelta);
533                                                         }
534                                                         continue;
535                                                 }
536
537                                                 item= children[j];
538
539                                                 // removed
540                                                 if ((status & IJavaElementDelta.REMOVED) != 0) {
541                                                         deletions.addElement(item);
542                                                         doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
543
544                                                 // changed
545                                                 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
546                                                         int change= affectedDelta.getFlags();
547                                                         doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
548
549                                                         if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
550                                                                 if (filtered(parent, affectedElement))
551                                                                         deletions.addElement(item);
552                                                                 else
553                                                                         updateItem(item, affectedElement);
554                                                         }
555
556                                                         if ((change & IJavaElementDelta.F_CONTENT) != 0)
557                                                                 updateItem(item, affectedElement);
558
559                                                         if ((change & IJavaElementDelta.F_CHILDREN) != 0)
560                                                                 update(item, affectedDelta);
561
562                                                         if ((change & IJavaElementDelta.F_REORDER) != 0)
563                                                                 fReorderedMembers= true;
564                                                 }
565                                         }
566
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);
574                                                 add= tmp;
575                                         }
576
577                                         // add at the right position
578                                         go2: for (int i= 0; i < add.length; i++) {
579
580                                                 try {
581
582                                                         IJavaElement e= add[i].getElement();
583                                                         if (filtered(parent, e))
584                                                                 continue go2;
585
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();
595                                                         }
596
597                                                         Item last= null;
598                                                         item= null;
599                                                         children= getChildren(w);
600
601                                                         for (int j= 0; j < children.length; j++) {
602                                                                 item= children[j];
603                                                                 IJavaElement r= (IJavaElement) item.getData();
604
605                                                                 if (r == null) {
606                                                                         // parent node collapsed and not be opened before -> do nothing
607                                                                         continue go2;
608                                                                 }
609
610
611                                                                 try {
612                                                                         rng= getSourceRange(r);
613
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;
624
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;
636                                                                                         }
637                                                                                 }
638                                                                         }
639
640                                                                         if (!multiFieldDeclaration && overlaps(rng, start, end)) {
641
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);
645                                                                                 continue go2;
646
647                                                                         } else if (multiFieldOrderBefore || rng.getOffset() > start) {
648
649                                                                                 if (last != null && deletions.contains(last)) {
650                                                                                         // reuse item
651                                                                                         deletions.removeElement(last);
652                                                                                         reuseTreeItem(last, e);
653                                                                                 } else {
654                                                                                         // nothing to reuse
655                                                                                         createTreeItem(w, e, j);
656                                                                                 }
657                                                                                 continue go2;
658                                                                         }
659
660                                                                 } catch (JavaModelException x) {
661                                                                         // stumbled over deleted element
662                                                                 }
663
664                                                                 last= item;
665                                                         }
666
667                                                         // add at the end of the list
668                                                         if (last != null && deletions.contains(last)) {
669                                                                 // reuse item
670                                                                 deletions.removeElement(last);
671                                                                 reuseTreeItem(last, e);
672                                                         } else {
673                                                                 // nothing to reuse
674                                                                 createTreeItem(w, e, -1);
675                                                         }
676
677                                                 } catch (JavaModelException x) {
678                                                         // the element to be added is not present -> don't add it
679                                                 }
680                                         }
681
682
683                                         // remove items which haven't been reused
684                                         Enumeration e= deletions.elements();
685                                         while (e.hasMoreElements()) {
686                                                 item= (Item) e.nextElement();
687                                                 disassociate(item);
688                                                 item.dispose();
689                                         }
690
691                                         if (doUpdateParent)
692                                                 updateItem(w, delta.getElement());
693                                         if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
694                                                 updatePlus((Item)w, delta.getElement());
695                                 }
696
697
698
699                                 /*
700                                  * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
701                                  */
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
708                                                 }
709                                         }
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());
719                                                                         break;
720                                                                 }
721                                                         }
722                                                 }
723                                         }
724                                         super.handleLabelProviderChanged(event);
725                                 }
726
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();
733                                         }
734 //                                      else if (input instanceof IClassFile) {
735 //                                              return ((IClassFile) input).getResource();
736 //                                      }
737                                         return null;
738                                 }
739
740
741                         }
742
743                         class LexicalSortingAction extends Action {
744
745                                 private JavaElementSorter fSorter= new JavaElementSorter();
746
747                                 public LexicalSortingAction() {
748                                         super();
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$
754
755                                         boolean checked= PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean("LexicalSortingAction.isChecked"); //$NON-NLS-1$
756                                         valueChanged(checked, false);
757                                 }
758
759                                 public void run() {
760                                         valueChanged(isChecked(), true);
761                                 }
762
763                                 private void valueChanged(final boolean on, boolean store) {
764                                         setChecked(on);
765                                         BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(), new Runnable() {
766                                                 public void run() {
767                                                         fOutlineViewer.setSorter(on ? fSorter : null);                                          }
768                                         });
769
770                                         if (store)
771                                                 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue("LexicalSortingAction.isChecked", on); //$NON-NLS-1$
772                                 }
773                         }
774
775                 class ClassOnlyAction extends Action {
776
777                         public ClassOnlyAction() {
778                                 super();
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$
784
785                                 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
786                                 boolean showclass= preferenceStore.getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
787                                 setTopLevelTypeOnly(showclass);
788                         }
789
790                         /*
791                          * @see org.eclipse.jface.action.Action#run()
792                          */
793                         public void run() {
794                                 setTopLevelTypeOnly(!fTopLevelTypeOnly);
795                         }
796
797                         private void setTopLevelTypeOnly(boolean show) {
798                                 fTopLevelTypeOnly= show;
799                                 setChecked(show);
800                                 fOutlineViewer.refresh(false);
801
802                                 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
803                                 preferenceStore.setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
804                         }
805                 }
806
807                 /**
808                  * This action toggles whether this Java Outline page links
809                  * its selection to the active editor.
810                  *
811                  * @since 3.0
812                  */
813                 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
814
815                         JavaOutlinePage fJavaOutlinePage;
816
817                         /**
818                          * Constructs a new action.
819                          *
820                          * @param outlinePage the Java outline page
821                          */
822                         public ToggleLinkingAction(JavaOutlinePage outlinePage) {
823                                 boolean isLinkingEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
824                                 setChecked(isLinkingEnabled);
825                                 fJavaOutlinePage= outlinePage;
826                         }
827
828                         /**
829                          * Runs the action.
830                          */
831                         public void run() {
832                                 PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE, isChecked());
833                                 if (isChecked() && fEditor != null)
834                                         fEditor.synchronizeOutlinePage(fEditor.computeHighlightRangeSourceReference(), false);
835                         }
836
837                 }
838
839
840         /** A flag to show contents of top level type only */
841         private boolean fTopLevelTypeOnly;
842
843         private IJavaElement fInput;
844         private String fContextMenuID;
845         private Menu fMenu;
846         private JavaOutlineViewer fOutlineViewer;
847         private PHPEditor fEditor;
848
849         private MemberFilterActionGroup fMemberFilterActionGroup;
850
851         private ListenerList fSelectionChangedListeners= new ListenerList();
852         private ListenerList fPostSelectionChangedListeners= new ListenerList();
853         private Hashtable fActions= new Hashtable();
854
855         private TogglePresentationAction fTogglePresentation;
856         private GotoAnnotationAction fPreviousAnnotation;
857         private GotoAnnotationAction fNextAnnotation;
858         private TextEditorAction fShowJavadoc;
859         private IAction fUndo;
860         private IAction fRedo;
861
862         private ToggleLinkingAction fToggleLinkingAction;
863
864         private CompositeActionGroup fActionGroups;
865
866         private IPropertyChangeListener fPropertyChangeListener;
867         /**
868          * Custom filter action group.
869          * @since 3.0
870          */
871         private CustomFiltersActionGroup fCustomFiltersActionGroup;
872
873         public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
874                 super();
875
876                 Assert.isNotNull(editor);
877
878                 fContextMenuID= contextMenuID;
879                 fEditor= editor;
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);
886
887                 fTogglePresentation.setEditor(editor);
888                 fPreviousAnnotation.setEditor(editor);
889                 fNextAnnotation.setEditor(editor);
890
891                 fPropertyChangeListener= new IPropertyChangeListener() {
892                         public void propertyChange(PropertyChangeEvent event) {
893                                 doPropertyChange(event);
894                         }
895                 };
896                 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
897         }
898
899         /**
900          * Returns the primary type of a compilation unit (has the same
901          * name as the compilation unit).
902          *
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
906          */
907         protected IType getMainType(ICompilationUnit compilationUnit) {
908
909                 if (compilationUnit == null)
910                         return null;
911
912                 String name= compilationUnit.getElementName();
913                 int index= name.indexOf('.');
914                 if (index != -1)
915                         name= name.substring(0, index);
916                 IType type= compilationUnit.getType(name);
917                 return type.exists() ? type : null;
918         }
919
920         /**
921          * Returns the primary type of a class file.
922          *
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
926          */
927 //      protected IType getMainType(IClassFile classFile) {
928 //              try {
929 //                      IType type= classFile.getType();
930 //                      return type != null && type.exists() ? type : null;
931 //              } catch (JavaModelException e) {
932 //                      return null;
933 //              }
934 //      }
935
936         /* (non-Javadoc)
937          * Method declared on Page
938          */
939         public void init(IPageSite pageSite) {
940                 super.init(pageSite);
941         }
942
943         private void doPropertyChange(PropertyChangeEvent event) {
944                 if (fOutlineViewer != null) {
945                         if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) {
946                                 fOutlineViewer.refresh(false);
947                         }
948                 }
949         }
950
951         /*
952          * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
953          */
954         public void addSelectionChangedListener(ISelectionChangedListener listener) {
955                 if (fOutlineViewer != null)
956                         fOutlineViewer.addSelectionChangedListener(listener);
957                 else
958                         fSelectionChangedListeners.add(listener);
959         }
960
961         /*
962          * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
963          */
964         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
965                 if (fOutlineViewer != null)
966                         fOutlineViewer.removeSelectionChangedListener(listener);
967                 else
968                         fSelectionChangedListeners.remove(listener);
969         }
970
971         /*
972          * @see ISelectionProvider#setSelection(ISelection)
973          */
974         public void setSelection(ISelection selection) {
975                 if (fOutlineViewer != null)
976                         fOutlineViewer.setSelection(selection);
977         }
978
979         /*
980          * @see ISelectionProvider#getSelection()
981          */
982         public ISelection getSelection() {
983                 if (fOutlineViewer == null)
984                         return StructuredSelection.EMPTY;
985                 return fOutlineViewer.getSelection();
986         }
987
988         /*
989          * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
990          */
991         public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
992                 if (fOutlineViewer != null)
993                         fOutlineViewer.addPostSelectionChangedListener(listener);
994                 else
995                         fPostSelectionChangedListeners.add(listener);
996         }
997
998         /*
999          * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1000          */
1001         public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
1002                 if (fOutlineViewer != null)
1003                         fOutlineViewer.removePostSelectionChangedListener(listener);
1004                 else
1005                         fPostSelectionChangedListeners.remove(listener);
1006         }
1007
1008         private void registerToolbarActions(IActionBars actionBars) {
1009
1010                 IToolBarManager toolBarManager= actionBars.getToolBarManager();
1011                 if (toolBarManager != null) {
1012                         toolBarManager.add(new LexicalSortingAction());
1013
1014                         fMemberFilterActionGroup= new MemberFilterActionGroup(fOutlineViewer, "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1015                         fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1016
1017                         fCustomFiltersActionGroup.fillActionBars(actionBars);
1018
1019                         IMenuManager menu= actionBars.getMenuManager();
1020                         menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1021
1022                         fToggleLinkingAction= new ToggleLinkingAction(this);
1023                         menu.add(new ClassOnlyAction());
1024                         menu.add(fToggleLinkingAction);
1025                 }
1026         }
1027
1028         /*
1029          * @see IPage#createControl
1030          */
1031         public void createControl(Composite parent) {
1032
1033                 Tree tree= new Tree(parent, SWT.MULTI);
1034
1035                 AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider(
1036                         AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS |  JavaElementLabels.F_APP_TYPE_SIGNATURE,
1037                         AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
1038                 );
1039
1040                 fOutlineViewer= new JavaOutlineViewer(tree);
1041                 initDragAndDrop();
1042                 fOutlineViewer.setContentProvider(new ChildrenProvider());
1043                 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
1044
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]);
1049                 }
1050
1051                 listeners= fPostSelectionChangedListeners.getListeners();
1052                 for (int i= 0; i < listeners.length; i++) {
1053                         fPostSelectionChangedListeners.remove(listeners[i]);
1054                         fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1055                 }
1056
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);
1062                         }
1063                 });
1064                 fMenu= manager.createContextMenu(tree);
1065                 tree.setMenu(fMenu);
1066
1067                 IPageSite site= getSite();
1068                 site.registerContextMenu(PHPeclipsePlugin.getPluginId() + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1069                 site.setSelectionProvider(fOutlineViewer);
1070
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)});
1078
1079                 // register global actions
1080                 IActionBars bars= site.getActionBars();
1081
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);
1090
1091
1092                 fActionGroups.fillActionBars(bars);
1093
1094                 IStatusLineManager statusLineManager= bars.getStatusLineManager();
1095                 if (statusLineManager != null) {
1096                         StatusBarUpdater updater= new StatusBarUpdater(statusLineManager);
1097                         fOutlineViewer.addPostSelectionChangedListener(updater);
1098                 }
1099                 // Custom filter group
1100                 fCustomFiltersActionGroup= new CustomFiltersActionGroup("net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1101
1102                 registerToolbarActions(bars);
1103
1104                 fOutlineViewer.setInput(fInput);
1105         }
1106
1107         public void dispose() {
1108
1109                 if (fEditor == null)
1110                         return;
1111
1112                 if (fMemberFilterActionGroup != null) {
1113                         fMemberFilterActionGroup.dispose();
1114                         fMemberFilterActionGroup= null;
1115                 }
1116
1117                 if (fCustomFiltersActionGroup != null) {
1118                         fCustomFiltersActionGroup.dispose();
1119                         fCustomFiltersActionGroup= null;
1120                 }
1121
1122
1123                 fEditor.outlinePageClosed();
1124                 fEditor= null;
1125
1126                 fSelectionChangedListeners.clear();
1127                 fSelectionChangedListeners= null;
1128
1129                 fPostSelectionChangedListeners.clear();
1130                 fPostSelectionChangedListeners= null;
1131
1132                 if (fPropertyChangeListener != null) {
1133                         PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1134                         fPropertyChangeListener= null;
1135                 }
1136
1137                 if (fMenu != null && !fMenu.isDisposed()) {
1138                         fMenu.dispose();
1139                         fMenu= null;
1140                 }
1141
1142                 if (fActionGroups != null)
1143                         fActionGroups.dispose();
1144
1145                 fTogglePresentation.setEditor(null);
1146                 fPreviousAnnotation.setEditor(null);
1147                 fNextAnnotation.setEditor(null);
1148
1149                 fOutlineViewer= null;
1150
1151                 super.dispose();
1152         }
1153
1154         public Control getControl() {
1155                 if (fOutlineViewer != null)
1156                         return fOutlineViewer.getControl();
1157                 return null;
1158         }
1159
1160         public void setInput(IJavaElement inputElement) {
1161                 fInput= inputElement;
1162                 if (fOutlineViewer != null)
1163                         fOutlineViewer.setInput(fInput);
1164         }
1165
1166         public void select(ISourceReference reference) {
1167                 if (fOutlineViewer != null) {
1168
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);
1176                                 }
1177                         }
1178                 }
1179         }
1180
1181         public void setAction(String actionID, IAction action) {
1182                 Assert.isNotNull(actionID);
1183                 if (action == null)
1184                         fActions.remove(actionID);
1185                 else
1186                         fActions.put(actionID, action);
1187         }
1188
1189         public IAction getAction(String actionID) {
1190                 Assert.isNotNull(actionID);
1191                 return (IAction) fActions.get(actionID);
1192         }
1193
1194         /*
1195          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1196          */
1197         public Object getAdapter(Class key) {
1198                 if (key == IShowInSource.class) {
1199                         return getShowInSource();
1200                 }
1201                 if (key == IShowInTargetList.class) {
1202                         return new IShowInTargetList() {
1203                                 public String[] getShowInTargetIds() {
1204                                         return new String[] { JavaUI.ID_PACKAGES };
1205                                 }
1206
1207                         };
1208                 }
1209                 if (key == IShowInTarget.class) {
1210                         return getShowInTarget();
1211                 }
1212
1213                 return null;
1214         }
1215
1216         /**
1217          * Convenience method to add the action installed under the given actionID to the
1218          * specified group of the menu.
1219          *
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
1223          */
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();
1229
1230                         if (action.isEnabled()) {
1231                                 IMenuManager subMenu= menu.findMenuUsingPath(group);
1232                                 if (subMenu != null)
1233                                         subMenu.add(action);
1234                                 else
1235                                         menu.appendToGroup(group, action);
1236                         }
1237                 }
1238         }
1239
1240         protected void contextMenuAboutToShow(IMenuManager menu) {
1241
1242                 PHPeclipsePlugin.createStandardGroups(menu);
1243
1244                 IStructuredSelection selection= (IStructuredSelection)getSelection();
1245                 fActionGroups.setContext(new ActionContext(selection));
1246                 fActionGroups.fillContextMenu(menu);
1247         }
1248
1249         /*
1250          * @see Page#setFocus()
1251          */
1252         public void setFocus() {
1253                 if (fOutlineViewer != null)
1254                         fOutlineViewer.getControl().setFocus();
1255         }
1256
1257         /**
1258          * Checks whether a given Java element is an inner type.
1259          *
1260          * @param element the java element
1261          * @return <code>true</code> iff the given element is an inner type
1262          */
1263         private boolean isInnerType(IJavaElement element) {
1264
1265                 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1266                         IType type= (IType)element;
1267                         try {
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);
1274                                 }
1275                         }
1276                 }
1277
1278                 return false;
1279         }
1280
1281         /**
1282          * Returns the <code>IShowInSource</code> for this view.
1283          *
1284          * @return the {@link IShowInSource}
1285          */
1286         protected IShowInSource getShowInSource() {
1287                 return new IShowInSource() {
1288                         public ShowInContext getShowInContext() {
1289                                 return new ShowInContext(
1290                                         null,
1291                                         getSite().getSelectionProvider().getSelection());
1292                         }
1293                 };
1294         }
1295
1296         /**
1297          * Returns the <code>IShowInTarget</code> for this view.
1298          *
1299          * @return the {@link IShowInTarget}
1300          */
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));
1311                                                 return true;
1312                                         }
1313                                 }
1314                                 return false;
1315                         }
1316                 };
1317         }
1318
1319         private void initDragAndDrop() {
1320                 int ops= DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1321                 Transfer[] transfers= new Transfer[] {
1322                         LocalSelectionTransfer.getInstance()
1323                         };
1324
1325                 // Drop Adapter
1326 //              TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
1327 //                      new SelectionTransferDropAdapter(fOutlineViewer)
1328 //              };
1329 //              fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new DelegatingDropAdapter(dropListeners));
1330
1331                 // Drag Adapter
1332 //              TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] {
1333 //                      new SelectionTransferDragAdapter(fOutlineViewer)
1334 //              };
1335 //              fOutlineViewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(fOutlineViewer, dragListeners));
1336         }
1337 }