improved PHP parser
[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.dnd.JdtViewerDragAdapter;
39 import net.sourceforge.phpdt.internal.ui.dnd.TransferDragSourceListener;
40 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
41 import net.sourceforge.phpdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
42 import net.sourceforge.phpdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
43 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
44 import net.sourceforge.phpdt.internal.ui.viewsupport.StatusBarUpdater;
45 import net.sourceforge.phpdt.ui.JavaElementSorter;
46 import net.sourceforge.phpdt.ui.JavaUI;
47 import net.sourceforge.phpdt.ui.PreferenceConstants;
48 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
49 import net.sourceforge.phpdt.ui.actions.CustomFiltersActionGroup;
50 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
51 import net.sourceforge.phpdt.ui.actions.MemberFilterActionGroup;
52 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
53 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
54
55 import org.eclipse.core.resources.IResource;
56 import org.eclipse.core.runtime.IAdaptable;
57 import org.eclipse.jface.action.Action;
58 import org.eclipse.jface.action.IAction;
59 import org.eclipse.jface.action.IMenuListener;
60 import org.eclipse.jface.action.IMenuManager;
61 import org.eclipse.jface.action.IStatusLineManager;
62 import org.eclipse.jface.action.IToolBarManager;
63 import org.eclipse.jface.action.MenuManager;
64 import org.eclipse.jface.action.Separator;
65 import org.eclipse.jface.preference.IPreferenceStore;
66 import org.eclipse.jface.text.Assert;
67 import org.eclipse.jface.text.ITextSelection;
68 import org.eclipse.jface.util.IPropertyChangeListener;
69 import org.eclipse.jface.util.ListenerList;
70 import org.eclipse.jface.util.PropertyChangeEvent;
71 import org.eclipse.jface.viewers.IBaseLabelProvider;
72 import org.eclipse.jface.viewers.IPostSelectionProvider;
73 import org.eclipse.jface.viewers.ISelection;
74 import org.eclipse.jface.viewers.ISelectionChangedListener;
75 import org.eclipse.jface.viewers.IStructuredSelection;
76 import org.eclipse.jface.viewers.ITreeContentProvider;
77 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
78 import org.eclipse.jface.viewers.SelectionChangedEvent;
79 import org.eclipse.jface.viewers.StructuredSelection;
80 import org.eclipse.jface.viewers.TreeViewer;
81 import org.eclipse.jface.viewers.Viewer;
82 import org.eclipse.jface.viewers.ViewerFilter;
83 import org.eclipse.swt.SWT;
84 import org.eclipse.swt.custom.BusyIndicator;
85 import org.eclipse.swt.dnd.DND;
86 import org.eclipse.swt.dnd.Transfer;
87 import org.eclipse.swt.widgets.Composite;
88 import org.eclipse.swt.widgets.Control;
89 import org.eclipse.swt.widgets.Display;
90 import org.eclipse.swt.widgets.Item;
91 import org.eclipse.swt.widgets.Menu;
92 import org.eclipse.swt.widgets.Tree;
93 import org.eclipse.swt.widgets.Widget;
94 import org.eclipse.ui.IActionBars;
95 import org.eclipse.ui.actions.ActionContext;
96 import org.eclipse.ui.actions.ActionGroup;
97 import org.eclipse.ui.help.WorkbenchHelp;
98 import org.eclipse.ui.model.IWorkbenchAdapter;
99 import org.eclipse.ui.model.WorkbenchAdapter;
100 import org.eclipse.ui.part.IPageSite;
101 import org.eclipse.ui.part.IShowInSource;
102 import org.eclipse.ui.part.IShowInTarget;
103 import org.eclipse.ui.part.IShowInTargetList;
104 import org.eclipse.ui.part.Page;
105 import org.eclipse.ui.part.ShowInContext;
106 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
107 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
108 import org.eclipse.ui.texteditor.IUpdate;
109 import org.eclipse.ui.texteditor.TextEditorAction;
110 import org.eclipse.ui.texteditor.TextOperationAction;
111 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
112 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
113 import org.eclipse.ui.views.navigator.RefactorActionGroup;
114
115
116 /**
117  * The content outline page of the Java editor. The viewer implements a proprietary
118  * update mechanism based on Java model deltas. It does not react on domain changes.
119  * It is specified to show the content of ICompilationUnits and IClassFiles.
120  * Publishes its context menu under <code>PHPeclipsePlugin.getDefault().getPluginId() + ".outline"</code>.
121  */
122 public class JavaOutlinePage extends Page implements IContentOutlinePage, IAdaptable , IPostSelectionProvider {
123
124                         static Object[] NO_CHILDREN= new Object[0];
125    
126                         /**
127                          * The element change listener of the java outline viewer.
128                          * @see IElementChangedListener
129                          */
130                         class ElementChangedListener implements IElementChangedListener {
131                                 
132                                 public void elementChanged(final ElementChangedEvent e) {
133                                         
134                                         if (getControl() == null)
135                                                 return;
136                                                 
137                                         Display d= getControl().getDisplay();
138                                         if (d != null) {
139                                                 d.asyncExec(new Runnable() {
140                                                         public void run() {
141                                                                 ICompilationUnit cu= (ICompilationUnit) fInput;
142                                                                 IJavaElement base= cu;
143 //                                                              if (fTopLevelTypeOnly) {
144 //                                                                      base= getMainType(cu);
145 //                                                                      if (base == null) {
146                                                                                 if (fOutlineViewer != null)
147                                                                                         fOutlineViewer.refresh(true);
148                                                                                 return;
149 //                                                                      }
150 //                                                              }
151 //                                                              IJavaElementDelta delta= findElement(base, e.getDelta());
152 //                                                              if (delta != null && fOutlineViewer != null) {
153 //                                                                      fOutlineViewer.reconcile(delta);
154 //                                                              }
155                                                         }
156                                                 });
157                                         }
158                                 }
159                                 
160                                 private boolean isPossibleStructuralChange(IJavaElementDelta cuDelta) {
161                                         if (cuDelta.getKind() != IJavaElementDelta.CHANGED) {
162                                                 return true; // add or remove
163                                         }
164                                         int flags= cuDelta.getFlags();
165                                         if ((flags & IJavaElementDelta.F_CHILDREN) != 0) {
166                                                 return true;
167                                         }
168                                         return (flags & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_FINE_GRAINED)) == IJavaElementDelta.F_CONTENT;
169                                 }
170                                 
171                                 protected IJavaElementDelta findElement(IJavaElement unit, IJavaElementDelta delta) {
172                                         
173                                         if (delta == null || unit == null)
174                                                 return null;
175                                         
176                                         IJavaElement element= delta.getElement();
177                                         
178                                         if (unit.equals(element)) {
179                                                 if (isPossibleStructuralChange(delta)) {
180                                                         return delta;
181                                                 }
182                                                 return null;
183                                         }
184                                                 
185                                         
186                                         if (element.getElementType() > IJavaElement.CLASS_FILE)
187                                                 return null;
188                                                 
189                                         IJavaElementDelta[] children= delta.getAffectedChildren();
190                                         if (children == null || children.length == 0)
191                                                 return null;
192                                                 
193                                         for (int i= 0; i < children.length; i++) {
194                                                 IJavaElementDelta d= findElement(unit, children[i]);
195                                                 if (d != null)
196                                                         return d;
197                                         }
198                                         
199                                         return null;
200                                 }
201                         }
202          
203                         static class NoClassElement extends WorkbenchAdapter implements IAdaptable {
204                                 /*
205                                  * @see java.lang.Object#toString()
206                                  */
207                                 public String toString() {
208                                         return PHPEditorMessages.getString("JavaOutlinePage.error.NoTopLevelType"); //$NON-NLS-1$
209                                 }
210                 
211                                 /*
212                                  * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
213                                  */
214                                 public Object getAdapter(Class clas) {
215                                         if (clas == IWorkbenchAdapter.class)
216                                                 return this;
217                                         return null;
218                                 }
219                         }
220                         
221                         /**
222                          * Content provider for the children of an ICompilationUnit or
223                          * an IClassFile
224                          * @see ITreeContentProvider
225                          */
226                         class ChildrenProvider implements ITreeContentProvider {
227             
228                                 private Object[] NO_CLASS= new Object[] {new NoClassElement()};
229                                 private ElementChangedListener fListener;
230                                 
231                                 protected boolean matches(IJavaElement element) {
232                                         if (element.getElementType() == IJavaElement.METHOD) {
233                                                 String name= element.getElementName();
234                                                 return (name != null && name.indexOf('<') >= 0);
235                                         }
236                                         return false;
237                                 }
238                                 
239                                 protected IJavaElement[] filter(IJavaElement[] children) {
240                                         boolean initializers= false;
241                                         for (int i= 0; i < children.length; i++) {
242                                                 if (matches(children[i])) {
243                                                         initializers= true;
244                                                         break;
245                                                 }
246                                         }
247                                                         
248                                         if (!initializers)
249                                                 return children;
250                                                 
251                                         Vector v= new Vector();
252                                         for (int i= 0; i < children.length; i++) {
253                                                 if (matches(children[i]))
254                                                         continue;
255                                                 v.addElement(children[i]);
256                                         }
257                                         
258                                         IJavaElement[] result= new IJavaElement[v.size()];
259                                         v.copyInto(result);
260                                         return result;
261                                 }
262                                 
263                                 public Object[] getChildren(Object parent) {
264                                         if (parent instanceof IParent) {
265                                                 IParent c= (IParent) parent;
266                                                 try {
267                                                         return filter(c.getChildren());
268                                                 } catch (JavaModelException x) {
269                                                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
270                                                         // don't log NotExist exceptions as this is a valid case
271                                                         // since we might have been posted and the element
272                                                         // removed in the meantime.
273                                                         if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
274                                                                 PHPeclipsePlugin.log(x);
275                                                 }
276                                         }
277                                         return NO_CHILDREN;
278                                 }
279                                 
280                                 public Object[] getElements(Object parent) {
281                                         if (fTopLevelTypeOnly) {
282                                                 if (parent instanceof ICompilationUnit) {
283                                                         try {
284                                                                 IType type= getMainType((ICompilationUnit) parent);
285                                                                 return type != null ? type.getChildren() : NO_CLASS;
286                                                         } catch (JavaModelException e) {
287                                                                 PHPeclipsePlugin.log(e);
288                                                         }
289                                                 } 
290 //                                              else if (parent instanceof IClassFile) {
291 //                                                      try {
292 //                                                              IType type= getMainType((IClassFile) parent);
293 //                                                              return type != null ? type.getChildren() : NO_CLASS;
294 //                                                      } catch (JavaModelException e) {
295 //                                                              PHPeclipsePlugin.log(e);
296 //                                                      }                                                       
297 //                                              }
298                                         }
299                                         return getChildren(parent);
300                                 }
301                                 
302                                 public Object getParent(Object child) {
303                                         if (child instanceof IJavaElement) {
304                                                 IJavaElement e= (IJavaElement) child;
305                                                 return e.getParent();
306                                         }
307                                         return null;
308                                 }
309                                 
310                                 public boolean hasChildren(Object parent) {
311                                         if (parent instanceof IParent) {
312                                                 IParent c= (IParent) parent;
313                                                 try {
314                                                         IJavaElement[] children= filter(c.getChildren());
315                                                         return (children != null && children.length > 0);
316                                                 } catch (JavaModelException x) {
317                                                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
318                                                         // don't log NotExist exceptions as this is a valid case
319                                                         // since we might have been posted and the element
320                                                         // removed in the meantime.
321                                                         if (PHPeclipsePlugin.isDebug() || !x.isDoesNotExist())
322                                                                 PHPeclipsePlugin.log(x);
323                                                 }
324                                         }
325                                         return false;
326                                 }
327                                 
328                                 public boolean isDeleted(Object o) {
329                                         return false;
330                                 }
331                                 
332                                 public void dispose() {
333                                         if (fListener != null) {
334                                                 JavaCore.removeElementChangedListener(fListener);
335                                                 fListener= null;
336                                         }               
337                                 }
338                                 
339                                 /*
340                                  * @see IContentProvider#inputChanged(Viewer, Object, Object)
341                                  */
342                                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
343                                         boolean isCU= (newInput instanceof ICompilationUnit);
344                                                                         
345                                         if (isCU && fListener == null) {
346                                                 fListener= new ElementChangedListener();
347                                                 JavaCore.addElementChangedListener(fListener);
348                                         } else if (!isCU && fListener != null) {
349                                                 JavaCore.removeElementChangedListener(fListener);
350                                                 fListener= null;
351                                         }
352                                 }
353                         }
354                         
355                         
356                         class JavaOutlineViewer extends TreeViewer {
357                                 
358                                 /**
359                                  * Indicates an item which has been reused. At the point of
360                                  * its reuse it has been expanded. This field is used to
361                                  * communicate between <code>internalExpandToLevel</code> and
362                                  * <code>reuseTreeItem</code>.
363                                  */
364                                 private Item fReusedExpandedItem;
365                                 private boolean fReorderedMembers;
366                                 private boolean fForceFireSelectionChanged;
367                                 
368                                 public JavaOutlineViewer(Tree tree) {
369                                         super(tree);
370                                         setAutoExpandLevel(ALL_LEVELS);
371                                         setUseHashlookup(true);
372                                 }
373                                 
374                                 /**
375                                  * Investigates the given element change event and if affected
376                                  * incrementally updates the Java outline.
377                                  * 
378                                  * @param delta the Java element delta used to reconcile the Java outline
379                                  */
380                                 public void reconcile(IJavaElementDelta delta) {
381                                         fReorderedMembers= false;
382                                         fForceFireSelectionChanged= false;
383                                         if (getSorter() == null) {
384                                                 if (fTopLevelTypeOnly
385                                                         && delta.getElement() instanceof IType
386                                                         && (delta.getKind() & IJavaElementDelta.ADDED) != 0)
387                                                 {
388                                                         refresh(true);
389
390                                                 } else {
391                                                         Widget w= findItem(fInput);
392                                                         if (w != null && !w.isDisposed())
393                                                                 update(w, delta);
394                                                         if (fForceFireSelectionChanged)
395                                                                 fireSelectionChanged(new SelectionChangedEvent(getSite().getSelectionProvider(), this.getSelection()));
396                                                         if (fReorderedMembers) {
397                                                                 refresh(false);
398                                                                 fReorderedMembers= false;
399                                                 }
400                                                 }
401                                         } else {
402                                                 // just for now
403                                                 refresh(true);
404                                         }
405                                 }
406                                 
407                                 /*
408                                  * @see TreeViewer#internalExpandToLevel
409                                  */
410                                 protected void internalExpandToLevel(Widget node, int level) {
411                                         if (node instanceof Item) {
412                                                 Item i= (Item) node;
413                                                 if (i.getData() instanceof IJavaElement) {
414                                                         IJavaElement je= (IJavaElement) i.getData();
415                                                         if (je.getElementType() == IJavaElement.IMPORT_CONTAINER || isInnerType(je)) {
416                                                                 if (i != fReusedExpandedItem) {
417                                                                         setExpanded(i, false);
418                                                                         return;
419                                                                 }
420                                                         }
421                                                 }
422                                         }
423                                         super.internalExpandToLevel(node, level);
424                                 }
425                                                                 
426                                 protected void reuseTreeItem(Item item, Object element) {
427                                         
428                                         // remove children
429                                         Item[] c= getChildren(item);
430                                         if (c != null && c.length > 0) {
431                                                 
432                                                 if (getExpanded(item))
433                                                         fReusedExpandedItem= item;
434                                                 
435                                                 for (int k= 0; k < c.length; k++) {
436                                                         if (c[k].getData() != null)
437                                                                 disassociate(c[k]);
438                                                         c[k].dispose();
439                                                 }
440                                         }
441                                         
442                                         updateItem(item, element);
443                                         updatePlus(item, element);
444                                         internalExpandToLevel(item, ALL_LEVELS);
445                                         
446                                         fReusedExpandedItem= null;
447                                         fForceFireSelectionChanged= true;
448                                 }
449                                 
450                                 protected boolean mustUpdateParent(IJavaElementDelta delta, IJavaElement element) {
451                                         if (element instanceof IMethod) {
452                                                 if ((delta.getKind() & IJavaElementDelta.ADDED) != 0) {
453                                                         try {
454                                                                 return ((IMethod)element).isMainMethod();
455                                                         } catch (JavaModelException e) {
456                                                                 PHPeclipsePlugin.log(e.getStatus());
457                                                         }
458                                                 }
459                                                 return "main".equals(element.getElementName()); //$NON-NLS-1$
460                                         }
461                                         return false;
462                                 }
463                                 
464                                 /*
465                                  * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
466                                  */
467                                 public boolean isExpandable(Object element) {
468                                         if (hasFilters()) {
469                                                 return getFilteredChildren(element).length > 0;
470                                         }
471                                         return super.isExpandable(element);
472                                 }
473                                 
474                                 protected ISourceRange getSourceRange(IJavaElement element) throws JavaModelException {
475                                         if (element instanceof ISourceReference)
476                                                 return ((ISourceReference) element).getSourceRange();
477                                         if (element instanceof IMember)// && !(element instanceof IInitializer))
478                                                 return ((IMember) element).getNameRange();
479                                         return null;
480                                 }
481                                 
482                                 protected boolean overlaps(ISourceRange range, int start, int end) {
483                                         return start <= (range.getOffset() + range.getLength() - 1) && range.getOffset() <= end;
484                                 }
485                                 
486                                 protected boolean filtered(IJavaElement parent, IJavaElement child) {
487                                         
488                                         Object[] result= new Object[] { child };
489                                         ViewerFilter[] filters= getFilters();
490                                         for (int i= 0; i < filters.length; i++) {
491                                                 result= filters[i].filter(this, parent, result);
492                                                 if (result.length == 0)
493                                                         return true;
494                                         }
495                                         
496                                         return false;
497                                 }
498                                 
499                                 protected void update(Widget w, IJavaElementDelta delta) {
500                                         
501                                         Item item;
502                                         
503                                         IJavaElement parent= delta.getElement();
504                                         IJavaElementDelta[] affected= delta.getAffectedChildren();
505                                         Item[] children= getChildren(w);
506
507                                         boolean doUpdateParent= false;
508                                         boolean doUpdateParentsPlus= false;
509                                                                                 
510                                         Vector deletions= new Vector();
511                                         Vector additions= new Vector();                         
512
513                                         for (int i= 0; i < affected.length; i++) {
514                                             IJavaElementDelta affectedDelta= affected[i];
515                                                 IJavaElement affectedElement= affectedDelta.getElement();
516                                                 int status= affected[i].getKind();
517
518                                                 // find tree item with affected element
519                                                 int j;
520                                                 for (j= 0; j < children.length; j++)
521                                                     if (affectedElement.equals(children[j].getData()))
522                                                         break;
523                                                 
524                                                 if (j == children.length) {
525                                                         // remove from collapsed parent
526                                                         if ((status & IJavaElementDelta.REMOVED) != 0) {
527                                                                 doUpdateParentsPlus= true;
528                                                                 continue;
529                                                         }                                                       
530                                                         // addition
531                                                         if ((status & IJavaElementDelta.CHANGED) != 0 &&                                                        
532                                                                 (affectedDelta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0 &&
533                                                                 !filtered(parent, affectedElement))
534                                                         {
535                                                                 additions.addElement(affectedDelta);
536                                                         }
537                                                         continue;
538                                                 }
539
540                                                 item= children[j];
541
542                                                 // removed                                                  
543                                                 if ((status & IJavaElementDelta.REMOVED) != 0) {
544                                                         deletions.addElement(item);
545                                                         doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
546
547                                                 // changed                                                  
548                                                 } else if ((status & IJavaElementDelta.CHANGED) != 0) {
549                                                         int change= affectedDelta.getFlags();
550                                                         doUpdateParent= doUpdateParent || mustUpdateParent(affectedDelta, affectedElement);
551                                                         
552                                                         if ((change & IJavaElementDelta.F_MODIFIERS) != 0) {
553                                                                 if (filtered(parent, affectedElement))
554                                                                         deletions.addElement(item);
555                                                                 else
556                                                                         updateItem(item, affectedElement);
557                                                         }
558                                                         
559                                                         if ((change & IJavaElementDelta.F_CONTENT) != 0)
560                                                                 updateItem(item, affectedElement);
561                                                                 
562                                                         if ((change & IJavaElementDelta.F_CHILDREN) != 0)
563                                                                 update(item, affectedDelta);                                                                                                                        
564                                                         
565                                                         if ((change & IJavaElementDelta.F_REORDER) != 0)
566                                                                 fReorderedMembers= true;
567                                                 }
568                                         }
569                                         
570                                         // find all elements to add
571                                         IJavaElementDelta[] add= delta.getAddedChildren();
572                                         if (additions.size() > 0) {
573                                                 IJavaElementDelta[] tmp= new IJavaElementDelta[add.length + additions.size()];
574                                                 System.arraycopy(add, 0, tmp, 0, add.length);
575                                                 for (int i= 0; i < additions.size(); i++)
576                                                         tmp[i + add.length]= (IJavaElementDelta) additions.elementAt(i);
577                                                 add= tmp;
578                                         }
579                                         
580                                         // add at the right position
581                                         go2: for (int i= 0; i < add.length; i++) {
582                                                 
583                                                 try {
584                                                         
585                                                         IJavaElement e= add[i].getElement();
586                                                         if (filtered(parent, e))
587                                                                 continue go2;
588                                                                 
589                                                         doUpdateParent= doUpdateParent || mustUpdateParent(add[i], e);
590                                                         ISourceRange rng= getSourceRange(e);
591                                                         int start= rng.getOffset();
592                                                         int end= start + rng.getLength() - 1;
593                                                         int nameOffset= Integer.MAX_VALUE;
594                                                         if (e instanceof IField) {
595                                                                 ISourceRange nameRange= ((IField) e).getNameRange();
596                                                                 if (nameRange != null)
597                                                                         nameOffset= nameRange.getOffset();
598                                                         }
599                                                         
600                                                         Item last= null;
601                                                         item= null;
602                                                         children= getChildren(w);
603                                                         
604                                                         for (int j= 0; j < children.length; j++) {
605                                                                 item= children[j];
606                                                                 IJavaElement r= (IJavaElement) item.getData();
607                                                                 
608                                                                 if (r == null) {
609                                                                         // parent node collapsed and not be opened before -> do nothing
610                                                                         continue go2;
611                                                                 }
612                                                                 
613                                                                         
614                                                                 try {
615                                                                         rng= getSourceRange(r);
616
617                                                                         // multi-field declarations always start at 
618                                                                         // the same offset. They also have the same
619                                                                         // end offset if the field sequence is terminated
620                                                                         // with a semicolon. If not, the source range
621                                                                         // ends behind the identifier / initializer
622                                                                         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=51851
623                                                                         boolean multiFieldDeclaration= 
624                                                                                 r.getElementType() == IJavaElement.FIELD 
625                                                                                         && e.getElementType() == IJavaElement.FIELD
626                                                                                         && rng.getOffset() == start;
627
628                                                                         // elements are inserted by occurrence
629                                                                         // however, multi-field declarations have
630                                                                         // equal source ranges offsets, therefore we
631                                                                         // compare name-range offsets.
632                                                                         boolean multiFieldOrderBefore= false;
633                                                                         if (multiFieldDeclaration) {
634                                                                                 if (r instanceof IField) {
635                                                                                         ISourceRange nameRange= ((IField) r).getNameRange();
636                                                                                         if (nameRange != null) {
637                                                                                                 if (nameRange.getOffset() > nameOffset)
638                                                                                                         multiFieldOrderBefore= true;
639                                                                                         }
640                                                                                 }
641                                                                         }
642                                                                         
643                                                                         if (!multiFieldDeclaration && overlaps(rng, start, end)) {
644                                                                                 
645                                                                                 // be tolerant if the delta is not correct, or if 
646                                                                                 // the tree has been updated other than by a delta
647                                                                                 reuseTreeItem(item, e);
648                                                                                 continue go2;
649                                                                                 
650                                                                         } else if (multiFieldOrderBefore || rng.getOffset() > start) {
651                                                                                 
652                                                                                 if (last != null && deletions.contains(last)) {
653                                                                                         // reuse item
654                                                                                         deletions.removeElement(last);
655                                                                                         reuseTreeItem(last, e);
656                                                                                 } else {
657                                                                                         // nothing to reuse
658                                                                                         createTreeItem(w, e, j);
659                                                                                 }
660                                                                                 continue go2;
661                                                                         }
662                                                                         
663                                                                 } catch (JavaModelException x) {
664                                                                         // stumbled over deleted element
665                                                                 }
666                                                                 
667                                                                 last= item;
668                                                         }
669                                                 
670                                                         // add at the end of the list
671                                                         if (last != null && deletions.contains(last)) {
672                                                                 // reuse item
673                                                                 deletions.removeElement(last);
674                                                                 reuseTreeItem(last, e);
675                                                         } else {
676                                                                 // nothing to reuse
677                                                                 createTreeItem(w, e, -1);
678                                                         }
679                                                 
680                                                 } catch (JavaModelException x) {
681                                                         // the element to be added is not present -> don't add it
682                                                 }
683                                         }
684                                         
685                                         
686                                         // remove items which haven't been reused
687                                         Enumeration e= deletions.elements();
688                                         while (e.hasMoreElements()) {
689                                                 item= (Item) e.nextElement();
690                                                 disassociate(item);
691                                                 item.dispose();
692                                         }
693                                         
694                                         if (doUpdateParent)
695                                                 updateItem(w, delta.getElement());
696                                         if (!doUpdateParent && doUpdateParentsPlus && w instanceof Item)
697                                                 updatePlus((Item)w, delta.getElement());
698                                 }
699                                 
700
701                                                                 
702                                 /*
703                                  * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
704                                  */
705                                 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
706                                         Object input= getInput();
707                                         if (event instanceof ProblemsLabelChangedEvent) {
708                                                 ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event;
709                                                 if (e.isMarkerChange() && input instanceof ICompilationUnit) {
710                                                         return; // marker changes can be ignored
711                                                 }
712                                         }
713                                         // look if the underlying resource changed
714                                         Object[] changed= event.getElements();
715                                         if (changed != null) {
716                                                 IResource resource= getUnderlyingResource();
717                                                 if (resource != null) {
718                                                         for (int i= 0; i < changed.length; i++) {
719                                                                 if (changed[i] != null && changed[i].equals(resource)) {
720                                                                         // change event to a full refresh
721                                                                         event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource());
722                                                                         break;
723                                                                 }
724                                                         }
725                                                 }
726                                         }
727                                         super.handleLabelProviderChanged(event);
728                                 }
729                                 
730                                 private IResource getUnderlyingResource() {
731                                         Object input= getInput();
732                                         if (input instanceof ICompilationUnit) {
733                                                 ICompilationUnit cu= (ICompilationUnit) input;
734                                                 cu= JavaModelUtil.toOriginal(cu);
735                                                 return cu.getResource();                
736                                         } 
737 //                                      else if (input instanceof IClassFile) {
738 //                                              return ((IClassFile) input).getResource();
739 //                                      }
740                                         return null;
741                                 }                               
742                                 
743
744                         }
745                                 
746                         class LexicalSortingAction extends Action {
747                                 
748                                 private JavaElementSorter fSorter= new JavaElementSorter();                     
749
750                                 public LexicalSortingAction() {
751                                         super();
752                                         WorkbenchHelp.setHelp(this, IJavaHelpContextIds.LEXICAL_SORTING_OUTLINE_ACTION);
753                                         setText(PHPEditorMessages.getString("JavaOutlinePage.Sort.label")); //$NON-NLS-1$
754                                         PHPUiImages.setLocalImageDescriptors(this, "alphab_sort_co.gif"); //$NON-NLS-1$
755                                         setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.Sort.tooltip")); //$NON-NLS-1$
756                                         setDescription(PHPEditorMessages.getString("JavaOutlinePage.Sort.description")); //$NON-NLS-1$
757                                         
758                                         boolean checked= PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean("LexicalSortingAction.isChecked"); //$NON-NLS-1$
759                                         valueChanged(checked, false);
760                                 }
761                                 
762                                 public void run() {
763                                         valueChanged(isChecked(), true);
764                                 }
765                                 
766                                 private void valueChanged(final boolean on, boolean store) {
767                                         setChecked(on);
768                                         BusyIndicator.showWhile(fOutlineViewer.getControl().getDisplay(), new Runnable() {
769                                                 public void run() {
770                                                         fOutlineViewer.setSorter(on ? fSorter : null);                                          }
771                                         });
772
773                                         if (store)
774                                                 PHPeclipsePlugin.getDefault().getPreferenceStore().setValue("LexicalSortingAction.isChecked", on); //$NON-NLS-1$
775                                 }
776                         }
777
778                 class ClassOnlyAction extends Action {
779
780                         public ClassOnlyAction() {
781                                 super();
782                                 WorkbenchHelp.setHelp(this, IJavaHelpContextIds.GO_INTO_TOP_LEVEL_TYPE_ACTION);
783                                 setText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.label")); //$NON-NLS-1$
784                                 setToolTipText(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.tooltip")); //$NON-NLS-1$
785                                 setDescription(PHPEditorMessages.getString("JavaOutlinePage.GoIntoTopLevelType.description")); //$NON-NLS-1$
786                                 PHPUiImages.setLocalImageDescriptors(this, "gointo_toplevel_type.gif"); //$NON-NLS-1$
787
788                                 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
789                                 boolean showclass= preferenceStore.getBoolean("GoIntoTopLevelTypeAction.isChecked"); //$NON-NLS-1$
790                                 setTopLevelTypeOnly(showclass);
791                         }
792
793                         /*
794                          * @see org.eclipse.jface.action.Action#run()
795                          */
796                         public void run() {
797                                 setTopLevelTypeOnly(!fTopLevelTypeOnly);
798                         }
799
800                         private void setTopLevelTypeOnly(boolean show) {
801                                 fTopLevelTypeOnly= show;
802                                 setChecked(show);
803                                 fOutlineViewer.refresh(false);
804                                 
805                                 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore(); 
806                                 preferenceStore.setValue("GoIntoTopLevelTypeAction.isChecked", show); //$NON-NLS-1$
807                         }
808                 }
809
810                 /**
811                  * This action toggles whether this Java Outline page links
812                  * its selection to the active editor.
813                  * 
814                  * @since 3.0
815                  */
816                 public class ToggleLinkingAction extends AbstractToggleLinkingAction {
817                 
818                         JavaOutlinePage fJavaOutlinePage;
819                 
820                         /**
821                          * Constructs a new action.
822                          * 
823                          * @param outlinePage the Java outline page
824                          */
825                         public ToggleLinkingAction(JavaOutlinePage outlinePage) {
826                                 boolean isLinkingEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
827                                 setChecked(isLinkingEnabled);
828                                 fJavaOutlinePage= outlinePage;
829                         }
830         
831                         /**
832                          * Runs the action.
833                          */
834                         public void run() {
835                                 PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE, isChecked());
836                                 if (isChecked() && fEditor != null)
837                                         fEditor.synchronizeOutlinePage(fEditor.computeHighlightRangeSourceReference(), false);
838                         }
839         
840                 }
841
842
843         /** A flag to show contents of top level type only */
844         private boolean fTopLevelTypeOnly;
845                         
846         private IJavaElement fInput;
847         private String fContextMenuID;
848         private Menu fMenu;
849         private JavaOutlineViewer fOutlineViewer;
850         private PHPEditor fEditor;
851         
852         private MemberFilterActionGroup fMemberFilterActionGroup;
853                 
854         private ListenerList fSelectionChangedListeners= new ListenerList();
855         private ListenerList fPostSelectionChangedListeners= new ListenerList();
856         private Hashtable fActions= new Hashtable();
857         
858         private TogglePresentationAction fTogglePresentation;
859         private GotoAnnotationAction fPreviousAnnotation;
860         private GotoAnnotationAction fNextAnnotation;
861         private TextEditorAction fShowJavadoc;
862         private TextOperationAction fUndo;
863         private TextOperationAction fRedo;
864         
865         private ToggleLinkingAction fToggleLinkingAction;
866         
867         private CompositeActionGroup fActionGroups;
868
869         private IPropertyChangeListener fPropertyChangeListener;
870         /**
871          * Custom filter action group.
872          * @since 3.0
873          */
874         private CustomFiltersActionGroup fCustomFiltersActionGroup;
875         
876         public JavaOutlinePage(String contextMenuID, PHPEditor editor) {
877                 super();
878                 
879                 Assert.isNotNull(editor);
880                 
881                 fContextMenuID= contextMenuID;
882                 fEditor= editor;
883                 
884                 fTogglePresentation= new TogglePresentationAction();
885                 fPreviousAnnotation= new GotoAnnotationAction("PreviousAnnotation.", false); //$NON-NLS-1$
886                 fNextAnnotation= new GotoAnnotationAction("NextAnnotation.", true); //$NON-NLS-1$
887                 fShowJavadoc= (TextEditorAction) fEditor.getAction("ShowJavaDoc"); //$NON-NLS-1$
888                 fUndo= (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.UNDO);
889                 fRedo= (TextOperationAction) fEditor.getAction(ITextEditorActionConstants.REDO);
890                 
891                 fTogglePresentation.setEditor(editor);
892                 fPreviousAnnotation.setEditor(editor);
893                 fNextAnnotation.setEditor(editor);      
894                 
895                 fPropertyChangeListener= new IPropertyChangeListener() {
896                         public void propertyChange(PropertyChangeEvent event) {
897                                 doPropertyChange(event);
898                         }
899                 };
900                 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
901         }
902    
903         /**
904          * Returns the primary type of a compilation unit (has the same
905          * name as the compilation unit).
906          * 
907          * @param compilationUnit the compilation unit
908          * @return returns the primary type of the compilation unit, or
909          * <code>null</code> if is does not have one
910          */
911         protected IType getMainType(ICompilationUnit compilationUnit) {
912                 
913                 if (compilationUnit == null)
914                         return null;
915                 
916                 String name= compilationUnit.getElementName();
917                 int index= name.indexOf('.');
918                 if (index != -1)
919                         name= name.substring(0, index);
920                 IType type= compilationUnit.getType(name);
921                 return type.exists() ? type : null;
922         }
923
924         /**
925          * Returns the primary type of a class file.
926          * 
927          * @param classFile the class file
928          * @return returns the primary type of the class file, or <code>null</code>
929          * if is does not have one
930          */
931 //      protected IType getMainType(IClassFile classFile) {
932 //              try {
933 //                      IType type= classFile.getType();
934 //                      return type != null && type.exists() ? type : null;
935 //              } catch (JavaModelException e) {
936 //                      return null;    
937 //              }
938 //      }
939         
940         /* (non-Javadoc)
941          * Method declared on Page
942          */
943         public void init(IPageSite pageSite) {
944                 super.init(pageSite);
945         }
946         
947         private void doPropertyChange(PropertyChangeEvent event) {
948                 if (fOutlineViewer != null) {
949                         if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) {
950                                 fOutlineViewer.refresh(false);
951                         }
952                 }
953         }       
954         
955         /*
956          * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
957          */
958         public void addSelectionChangedListener(ISelectionChangedListener listener) {
959                 if (fOutlineViewer != null)
960                         fOutlineViewer.addSelectionChangedListener(listener);
961                 else
962                         fSelectionChangedListeners.add(listener);
963         }
964         
965         /*
966          * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
967          */
968         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
969                 if (fOutlineViewer != null)
970                         fOutlineViewer.removeSelectionChangedListener(listener);
971                 else
972                         fSelectionChangedListeners.remove(listener);
973         }
974         
975         /*
976          * @see ISelectionProvider#setSelection(ISelection)
977          */
978         public void setSelection(ISelection selection) {
979                 if (fOutlineViewer != null)
980                         fOutlineViewer.setSelection(selection);         
981         }       
982         
983         /*
984          * @see ISelectionProvider#getSelection()
985          */
986         public ISelection getSelection() {
987                 if (fOutlineViewer == null)
988                         return StructuredSelection.EMPTY;
989                 return fOutlineViewer.getSelection();
990         }
991         
992         /*
993          * @see org.eclipse.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
994          */
995         public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
996                 if (fOutlineViewer != null)
997                         fOutlineViewer.addPostSelectionChangedListener(listener);
998                 else
999                         fPostSelectionChangedListeners.add(listener);
1000         }
1001         
1002         /*
1003          * @see org.eclipse.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1004          */
1005         public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
1006                 if (fOutlineViewer != null)
1007                         fOutlineViewer.removePostSelectionChangedListener(listener);
1008                 else
1009                         fPostSelectionChangedListeners.remove(listener);        
1010         }
1011         
1012         private void registerToolbarActions(IActionBars actionBars) {
1013                 
1014                 IToolBarManager toolBarManager= actionBars.getToolBarManager();
1015                 if (toolBarManager != null) {   
1016                         toolBarManager.add(new LexicalSortingAction());
1017                         
1018                         fMemberFilterActionGroup= new MemberFilterActionGroup(fOutlineViewer, "net.sourceforge.phpeclipse.JavaOutlinePage"); //$NON-NLS-1$
1019                         fMemberFilterActionGroup.contributeToToolBar(toolBarManager);
1020
1021                         fCustomFiltersActionGroup.fillActionBars(actionBars);
1022                         
1023                         IMenuManager menu= actionBars.getMenuManager();
1024                         menu.add(new Separator("EndFilterGroup")); //$NON-NLS-1$
1025                         
1026                         fToggleLinkingAction= new ToggleLinkingAction(this);
1027                         menu.add(new ClassOnlyAction());                
1028                         menu.add(fToggleLinkingAction);
1029                 }
1030         }
1031         
1032         /*
1033          * @see IPage#createControl
1034          */
1035         public void createControl(Composite parent) {
1036                 
1037                 Tree tree= new Tree(parent, SWT.MULTI);
1038
1039                 AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider(
1040                         AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS |  JavaElementLabels.F_APP_TYPE_SIGNATURE,
1041                         AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
1042                 );
1043
1044                 fOutlineViewer= new JavaOutlineViewer(tree);            
1045                 initDragAndDrop();
1046                 fOutlineViewer.setContentProvider(new ChildrenProvider());
1047                 fOutlineViewer.setLabelProvider(new DecoratingJavaLabelProvider(lprovider));
1048                 
1049                 Object[] listeners= fSelectionChangedListeners.getListeners();
1050                 for (int i= 0; i < listeners.length; i++) {
1051                         fSelectionChangedListeners.remove(listeners[i]);
1052                         fOutlineViewer.addSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1053                 }
1054                 
1055                 listeners= fPostSelectionChangedListeners.getListeners();
1056                 for (int i= 0; i < listeners.length; i++) {
1057                         fPostSelectionChangedListeners.remove(listeners[i]);
1058                         fOutlineViewer.addPostSelectionChangedListener((ISelectionChangedListener) listeners[i]);
1059                 }
1060                                                 
1061                 MenuManager manager= new MenuManager(fContextMenuID, fContextMenuID);
1062                 manager.setRemoveAllWhenShown(true);
1063                 manager.addMenuListener(new IMenuListener() {
1064                         public void menuAboutToShow(IMenuManager m) {
1065                                 contextMenuAboutToShow(m);
1066                         }
1067                 });
1068                 fMenu= manager.createContextMenu(tree);
1069                 tree.setMenu(fMenu);
1070                 
1071                 IPageSite site= getSite();
1072                 site.registerContextMenu(PHPeclipsePlugin.getPluginId() + ".outline", manager, fOutlineViewer); //$NON-NLS-1$
1073                 site.setSelectionProvider(fOutlineViewer);
1074
1075                 // we must create the groups after we have set the selection provider to the site
1076                 fActionGroups= new CompositeActionGroup(new ActionGroup[] {
1077 //                              new OpenViewActionGroup(this), 
1078 //                              new CCPActionGroup(this),
1079                                 new GenerateActionGroup(this)});
1080 //                              new RefactorActionGroup(this), 
1081 //                              new JavaSearchActionGroup(this)});
1082                                 
1083                 // register global actions
1084                 IActionBars bars= site.getActionBars();
1085                 
1086                 bars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, fUndo);
1087                 bars.setGlobalActionHandler(ITextEditorActionConstants.REDO, fRedo);
1088                 bars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, fPreviousAnnotation);
1089                 bars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, fNextAnnotation);
1090                 bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC, fShowJavadoc);
1091                 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY, fTogglePresentation);
1092                 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, fNextAnnotation);
1093                 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, fPreviousAnnotation);
1094                 
1095                 
1096                 fActionGroups.fillActionBars(bars);
1097
1098                 IStatusLineManager statusLineManager= bars.getStatusLineManager();
1099                 if (statusLineManager != null) {
1100                         StatusBarUpdater updater= new StatusBarUpdater(statusLineManager);
1101                         fOutlineViewer.addPostSelectionChangedListener(updater);
1102                 }
1103                 // Custom filter group
1104                 fCustomFiltersActionGroup= new CustomFiltersActionGroup("net.sourceforge.phpdt.ui.JavaOutlinePage", fOutlineViewer); //$NON-NLS-1$
1105
1106                 registerToolbarActions(bars);
1107                                 
1108                 fOutlineViewer.setInput(fInput);        
1109         }
1110
1111         public void dispose() {
1112                 
1113                 if (fEditor == null)
1114                         return;
1115                         
1116                 if (fMemberFilterActionGroup != null) {
1117                         fMemberFilterActionGroup.dispose();
1118                         fMemberFilterActionGroup= null;
1119                 }
1120                 
1121                 if (fCustomFiltersActionGroup != null) {
1122                         fCustomFiltersActionGroup.dispose();
1123                         fCustomFiltersActionGroup= null;
1124                 }
1125                         
1126                         
1127                 fEditor.outlinePageClosed();
1128                 fEditor= null;
1129
1130                 fSelectionChangedListeners.clear();
1131                 fSelectionChangedListeners= null;
1132                 
1133                 fPostSelectionChangedListeners.clear();
1134                 fPostSelectionChangedListeners= null;
1135
1136                 if (fPropertyChangeListener != null) {
1137                         PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1138                         fPropertyChangeListener= null;
1139                 }
1140                 
1141                 if (fMenu != null && !fMenu.isDisposed()) {
1142                         fMenu.dispose();
1143                         fMenu= null;
1144                 }
1145                 
1146                 if (fActionGroups != null)
1147                         fActionGroups.dispose();
1148                         
1149                 fTogglePresentation.setEditor(null);
1150                 fPreviousAnnotation.setEditor(null);
1151                 fNextAnnotation.setEditor(null);        
1152                 
1153                 fOutlineViewer= null;
1154                 
1155                 super.dispose();
1156         }
1157         
1158         public Control getControl() {
1159                 if (fOutlineViewer != null)
1160                         return fOutlineViewer.getControl();
1161                 return null;
1162         }
1163         
1164         public void setInput(IJavaElement inputElement) {
1165                 fInput= inputElement;   
1166                 if (fOutlineViewer != null)
1167                         fOutlineViewer.setInput(fInput);
1168         }
1169                 
1170         public void select(ISourceReference reference) {
1171                 if (fOutlineViewer != null) {
1172                         
1173                         ISelection s= fOutlineViewer.getSelection();
1174                         if (s instanceof IStructuredSelection) {
1175                                 IStructuredSelection ss= (IStructuredSelection) s;
1176                                 List elements= ss.toList();
1177                                 if (!elements.contains(reference)) {
1178                                         s= (reference == null ? StructuredSelection.EMPTY : new StructuredSelection(reference));
1179                                         fOutlineViewer.setSelection(s, true);
1180                                 }
1181                         }
1182                 }
1183         }
1184         
1185         public void setAction(String actionID, IAction action) {
1186                 Assert.isNotNull(actionID);
1187                 if (action == null)
1188                         fActions.remove(actionID);
1189                 else
1190                         fActions.put(actionID, action);
1191         }
1192         
1193         public IAction getAction(String actionID) {
1194                 Assert.isNotNull(actionID);
1195                 return (IAction) fActions.get(actionID);
1196         }
1197
1198         /*
1199          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1200          */
1201         public Object getAdapter(Class key) {
1202                 if (key == IShowInSource.class) {
1203                         return getShowInSource();
1204                 }
1205                 if (key == IShowInTargetList.class) {
1206                         return new IShowInTargetList() {
1207                                 public String[] getShowInTargetIds() {
1208                                         return new String[] { JavaUI.ID_PACKAGES };
1209                                 }
1210
1211                         };
1212                 }
1213                 if (key == IShowInTarget.class) {
1214                         return getShowInTarget();
1215                 }
1216
1217                 return null;
1218         }
1219
1220         /**
1221          * Convenience method to add the action installed under the given actionID to the
1222          * specified group of the menu.
1223          * 
1224          * @param menu          the menu manager
1225          * @param group         the group to which to add the action
1226          * @param actionID      the ID of the new action
1227          */
1228         protected void addAction(IMenuManager menu, String group, String actionID) {
1229                 IAction action= getAction(actionID);
1230                 if (action != null) {
1231                         if (action instanceof IUpdate)
1232                                 ((IUpdate) action).update();
1233                                 
1234                         if (action.isEnabled()) {
1235                                 IMenuManager subMenu= menu.findMenuUsingPath(group);
1236                                 if (subMenu != null)
1237                                         subMenu.add(action);
1238                                 else
1239                                         menu.appendToGroup(group, action);
1240                         }
1241                 }
1242         }
1243          
1244         protected void contextMenuAboutToShow(IMenuManager menu) {
1245                 
1246                 PHPeclipsePlugin.createStandardGroups(menu);
1247                                 
1248                 IStructuredSelection selection= (IStructuredSelection)getSelection();
1249                 fActionGroups.setContext(new ActionContext(selection));
1250                 fActionGroups.fillContextMenu(menu);
1251         }
1252         
1253         /*
1254          * @see Page#setFocus()
1255          */
1256         public void setFocus() {
1257                 if (fOutlineViewer != null)
1258                         fOutlineViewer.getControl().setFocus();
1259         }
1260         
1261         /**
1262          * Checks whether a given Java element is an inner type.
1263          * 
1264          * @param element the java element
1265          * @return <code>true</code> iff the given element is an inner type
1266          */
1267         private boolean isInnerType(IJavaElement element) {
1268                 
1269                 if (element != null && element.getElementType() == IJavaElement.TYPE) {
1270                         IType type= (IType)element;
1271                         try {
1272                                 return type.isMember();
1273                         } catch (JavaModelException e) {
1274                                 IJavaElement parent= type.getParent();
1275                                 if (parent != null) {
1276                                         int parentElementType= parent.getElementType();
1277                                         return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
1278                                 }
1279                         }
1280                 }
1281                 
1282                 return false;           
1283         }
1284         
1285         /**
1286          * Returns the <code>IShowInSource</code> for this view.
1287          * 
1288          * @return the {@link IShowInSource}
1289          */
1290         protected IShowInSource getShowInSource() {
1291                 return new IShowInSource() {
1292                         public ShowInContext getShowInContext() {
1293                                 return new ShowInContext(
1294                                         null,
1295                                         getSite().getSelectionProvider().getSelection());
1296                         }
1297                 };
1298         }
1299
1300         /**
1301          * Returns the <code>IShowInTarget</code> for this view.
1302          * 
1303          * @return the {@link IShowInTarget}
1304          */
1305         protected IShowInTarget getShowInTarget() {
1306                 return new IShowInTarget() {
1307                         public boolean show(ShowInContext context) {
1308                                 ISelection sel= context.getSelection();
1309                                 if (sel instanceof ITextSelection) {
1310                                         ITextSelection tsel= (ITextSelection) sel;
1311                                         int offset= tsel.getOffset();
1312                                         IJavaElement element= fEditor.getElementAt(offset);
1313                                         if (element != null) {
1314                                                 setSelection(new StructuredSelection(element));
1315                                                 return true;
1316                                         }
1317                                 }
1318                                 return false;
1319                         }
1320                 };
1321         }
1322         
1323         private void initDragAndDrop() {
1324                 int ops= DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
1325                 Transfer[] transfers= new Transfer[] {
1326                         LocalSelectionTransfer.getInstance()
1327                         };
1328                 
1329                 // Drop Adapter
1330 //              TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
1331 //                      new SelectionTransferDropAdapter(fOutlineViewer)
1332 //              };
1333 //              fOutlineViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new DelegatingDropAdapter(dropListeners));
1334                 
1335                 // Drag Adapter
1336 //              TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] {
1337 //                      new SelectionTransferDragAdapter(fOutlineViewer)
1338 //              };
1339 //              fOutlineViewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(fOutlineViewer, dragListeners));
1340         }
1341 }