KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > PackagesView


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.browsing;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IFolder;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.GroupMarker;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.action.IContributionItem;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.action.IStatusLineManager;
29 import org.eclipse.jface.action.MenuManager;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.viewers.IContentProvider;
32 import org.eclipse.jface.viewers.ILabelDecorator;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.StructuredViewer;
37 import org.eclipse.jface.viewers.TableViewer;
38 import org.eclipse.jface.viewers.Viewer;
39
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.IPageLayout;
43 import org.eclipse.ui.IViewSite;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.ActionFactory;
47 import org.eclipse.ui.part.IShowInTargetList;
48
49 import org.eclipse.jdt.core.IClassFile;
50 import org.eclipse.jdt.core.ICompilationUnit;
51 import org.eclipse.jdt.core.IJavaElement;
52 import org.eclipse.jdt.core.IJavaProject;
53 import org.eclipse.jdt.core.IPackageFragment;
54 import org.eclipse.jdt.core.IPackageFragmentRoot;
55 import org.eclipse.jdt.core.IType;
56 import org.eclipse.jdt.core.JavaCore;
57
58 import org.eclipse.jdt.internal.corext.util.Messages;
59
60 import org.eclipse.jdt.ui.JavaElementComparator;
61 import org.eclipse.jdt.ui.JavaElementLabels;
62 import org.eclipse.jdt.ui.JavaUI;
63 import org.eclipse.jdt.ui.PreferenceConstants;
64
65 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
66 import org.eclipse.jdt.internal.ui.JavaPlugin;
67 import org.eclipse.jdt.internal.ui.JavaPluginImages;
68 import org.eclipse.jdt.internal.ui.actions.MultiActionGroup;
69 import org.eclipse.jdt.internal.ui.actions.SelectAllAction;
70 import org.eclipse.jdt.internal.ui.filters.NonJavaElementFilter;
71 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
72 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
73 import org.eclipse.jdt.internal.ui.viewsupport.LibraryFilter;
74 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer;
75 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer;
76 import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater;
77
78
79 public class PackagesView extends JavaBrowsingPart{
80
81     private static final String JavaDoc TAG_VIEW_STATE= ".viewState"; //$NON-NLS-1$
82
private static final int LIST_VIEW_STATE= 0;
83     private static final int TREE_VIEW_STATE= 1;
84
85
86     private static class StatusBarUpdater4LogicalPackage extends StatusBarUpdater {
87
88         private StatusBarUpdater4LogicalPackage(IStatusLineManager statusLineManager) {
89             super(statusLineManager);
90         }
91
92         protected String JavaDoc formatMessage(ISelection sel) {
93             if (sel instanceof IStructuredSelection) {
94                 IStructuredSelection selection= (IStructuredSelection)sel;
95                 int nElements= selection.size();
96                 Object JavaDoc elem= selection.getFirstElement();
97                 if (nElements == 1 && (elem instanceof LogicalPackage))
98                     return formatLogicalPackageMessage((LogicalPackage) elem);
99             }
100             return super.formatMessage(sel);
101         }
102
103         private String JavaDoc formatLogicalPackageMessage(LogicalPackage logicalPackage) {
104             IPackageFragment[] fragments= logicalPackage.getFragments();
105             StringBuffer JavaDoc buf= new StringBuffer JavaDoc(logicalPackage.getElementName());
106             buf.append(JavaElementLabels.CONCAT_STRING);
107             String JavaDoc message= ""; //$NON-NLS-1$
108
boolean firstTime= true;
109             for (int i= 0; i < fragments.length; i++) {
110                 IPackageFragment fragment= fragments[i];
111                 IJavaElement element= fragment.getParent();
112                 if (element instanceof IPackageFragmentRoot) {
113                     IPackageFragmentRoot root= (IPackageFragmentRoot) element;
114                     String JavaDoc label= JavaElementLabels.getElementLabel(root, JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_QUALIFIED);
115                     if (firstTime) {
116                         buf.append(label);
117                         firstTime= false;
118                     }
119                     else
120                         message= Messages.format(JavaBrowsingMessages.StatusBar_concat, new String JavaDoc[] {message, label});
121                 }
122             }
123             buf.append(message);
124             return buf.toString();
125         }
126     }
127
128
129     private SelectAllAction fSelectAllAction;
130
131     private int fCurrViewState;
132
133     private PackageViewerWrapper fWrappedViewer;
134
135     private MultiActionGroup fSwitchActionGroup;
136     private boolean fLastInputWasProject;
137
138     /**
139      * Adds filters the viewer of this part.
140      */

141     protected void addFilters() {
142         super.addFilters();
143         getViewer().addFilter(createNonJavaElementFilter());
144         getViewer().addFilter(new LibraryFilter());
145     }
146
147
148     /**
149      * Creates new NonJavaElementFilter and overrides method select to allow for
150      * LogicalPackages.
151      * @return NonJavaElementFilter
152      */

153     protected NonJavaElementFilter createNonJavaElementFilter() {
154         return new NonJavaElementFilter(){
155             public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element){
156                 return ((element instanceof IJavaElement) || (element instanceof LogicalPackage) || (element instanceof IFolder));
157             }
158         };
159     }
160
161     public void init(IViewSite site, IMemento memento) throws PartInitException {
162         super.init(site, memento);
163         //this must be created before all actions and filters
164
fWrappedViewer= new PackageViewerWrapper();
165         restoreLayoutState(memento);
166     }
167
168     private void restoreLayoutState(IMemento memento) {
169         if (memento == null) {
170             //read state from the preference store
171
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
172             fCurrViewState= store.getInt(this.getViewSite().getId() + TAG_VIEW_STATE);
173         } else {
174             //restore from memento
175
Integer JavaDoc integer= memento.getInteger(this.getViewSite().getId() + TAG_VIEW_STATE);
176             if ((integer == null) || !isValidState(integer.intValue())) {
177                 fCurrViewState= LIST_VIEW_STATE;
178             } else fCurrViewState= integer.intValue();
179         }
180     }
181
182     private boolean isValidState(int state) {
183         return (state==LIST_VIEW_STATE) || (state==TREE_VIEW_STATE);
184     }
185
186
187
188     /*
189      * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
190      */

191     public void saveState(IMemento memento) {
192         super.saveState(memento);
193         memento.putInteger(this.getViewSite().getId()+TAG_VIEW_STATE,fCurrViewState);
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createViewer(org.eclipse.swt.widgets.Composite)
198      */

199     protected StructuredViewer createViewer(Composite parent) {
200         //Creates the viewer of this part dependent on the current layout.
201
StructuredViewer viewer;
202         if(isInListState())
203             viewer= createTableViewer(parent);
204         else
205             viewer= createTreeViewer(parent);
206
207         fWrappedViewer.setViewer(viewer);
208         return fWrappedViewer;
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class)
213      */

214     public Object JavaDoc getAdapter(Class JavaDoc key) {
215         if (key == IShowInTargetList.class) {
216             return new IShowInTargetList() {
217                 public String JavaDoc[] getShowInTargetIds() {
218                     return new String JavaDoc[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
219                 }
220             };
221         }
222         return super.getAdapter(key);
223     }
224
225     protected boolean isInListState() {
226         return fCurrViewState== LIST_VIEW_STATE;
227     }
228
229     private ProblemTableViewer createTableViewer(Composite parent) {
230         return new PackagesViewTableViewer(parent, SWT.MULTI);
231     }
232
233     private ProblemTreeViewer createTreeViewer(Composite parent) {
234         return new PackagesViewTreeViewer(parent, SWT.MULTI);
235     }
236
237     /**
238      * Overrides the createContentProvider from JavaBrowsingPart
239      * Creates the content provider of this part.
240      */

241     protected IContentProvider createContentProvider() {
242         if(isInListState())
243             return new PackagesViewFlatContentProvider(fWrappedViewer.getViewer());
244         else return new PackagesViewHierarchicalContentProvider(fWrappedViewer.getViewer());
245     }
246
247     protected JavaUILabelProvider createLabelProvider() {
248         if(isInListState())
249             return createListLabelProvider();
250         else return createTreeLabelProvider();
251     }
252
253     private JavaUILabelProvider createTreeLabelProvider() {
254         return new PackagesViewLabelProvider(PackagesViewLabelProvider.HIERARCHICAL_VIEW_STATE);
255     }
256
257     private JavaUILabelProvider createListLabelProvider() {
258         return new PackagesViewLabelProvider(PackagesViewLabelProvider.FLAT_VIEW_STATE);
259     }
260
261     /**
262      * Returns the context ID for the Help system
263      *
264      * @return the string used as ID for the Help context
265      */

266     protected String JavaDoc getHelpContextId() {
267         return IJavaHelpContextIds.PACKAGES_BROWSING_VIEW;
268     }
269
270     protected String JavaDoc getLinkToEditorKey() {
271         return PreferenceConstants.LINK_BROWSING_PACKAGES_TO_EDITOR;
272     }
273
274     /**
275      * Answers if the given <code>element</code> is a valid
276      * input for this part.
277      *
278      * @param element the object to test
279      * @return <true> if the given element is a valid input
280      */

281     protected boolean isValidInput(Object JavaDoc element) {
282         if (element instanceof IJavaProject || (element instanceof IPackageFragmentRoot && ((IJavaElement)element).getElementName() != IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH))
283             try {
284                 IJavaProject jProject= ((IJavaElement)element).getJavaProject();
285                 if (jProject != null)
286                     return jProject.getProject().hasNature(JavaCore.NATURE_ID);
287             } catch (CoreException ex) {
288                 return false;
289             }
290         return false;
291     }
292
293     /**
294      * Answers if the given <code>element</code> is a valid
295      * element for this part.
296      *
297      * @param element the object to test
298      * @return <true> if the given element is a valid element
299      */

300     protected boolean isValidElement(Object JavaDoc element) {
301         if (element instanceof IPackageFragment) {
302             IJavaElement parent= ((IPackageFragment)element).getParent();
303             if (parent != null)
304                 return super.isValidElement(parent) || super.isValidElement(parent.getJavaProject());
305         }
306         return false;
307     }
308
309     /* (non-Javadoc)
310      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#findElementToSelect(org.eclipse.jdt.core.IJavaElement)
311      */

312     protected IJavaElement findElementToSelect(IJavaElement je) {
313         if (je == null)
314             return null;
315
316         switch (je.getElementType()) {
317             case IJavaElement.PACKAGE_FRAGMENT:
318                 return je;
319             case IJavaElement.COMPILATION_UNIT:
320                 return ((ICompilationUnit)je).getParent();
321             case IJavaElement.CLASS_FILE:
322                 return ((IClassFile)je).getParent();
323             case IJavaElement.TYPE:
324                 return ((IType)je).getPackageFragment();
325             default:
326                 return findElementToSelect(je.getParent());
327         }
328     }
329
330     /*
331      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#setInput(java.lang.Object)
332      */

333     protected void setInput(Object JavaDoc input) {
334         setViewerWrapperInput(input);
335         super.updateTitle();
336     }
337
338     private void setViewerWrapperInput(Object JavaDoc input) {
339         fWrappedViewer.setViewerInput(input);
340     }
341
342     /**
343      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#fillActionBars(org.eclipse.ui.IActionBars)
344      */

345     protected void fillActionBars(IActionBars actionBars) {
346         super.fillActionBars(actionBars);
347         fSwitchActionGroup.fillActionBars(actionBars);
348     }
349
350
351
352     private void setUpViewer(StructuredViewer viewer){
353         Assert.isTrue(viewer != null);
354
355         JavaUILabelProvider labelProvider= createLabelProvider();
356         viewer.setLabelProvider(createDecoratingLabelProvider(labelProvider));
357
358         viewer.setComparator(createJavaElementComparator());
359         viewer.setUseHashlookup(true);
360
361         createContextMenu();
362
363         //disapears when control disposed
364
addKeyListener();
365
366         //this methods only adds listeners to the viewer,
367
//these listenters disapear when the viewer is disposed
368
hookViewerListeners();
369
370         // Set content provider
371
viewer.setContentProvider(createContentProvider());
372         //Disposed when viewer's Control is disposed
373
initDragAndDrop();
374
375     }
376
377     //alter sorter to include LogicalPackages
378
protected JavaElementComparator createJavaElementComparator() {
379         return new JavaElementComparator(){
380             public int category(Object JavaDoc element) {
381                 if (element instanceof LogicalPackage) {
382                     LogicalPackage cp= (LogicalPackage) element;
383                     return super.category(cp.getFragments()[0]);
384                 } else return super.category(element);
385             }
386             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2){
387                 if (e1 instanceof LogicalPackage) {
388                     LogicalPackage cp= (LogicalPackage) e1;
389                     e1= cp.getFragments()[0];
390                 }
391                 if (e2 instanceof LogicalPackage) {
392                     LogicalPackage cp= (LogicalPackage) e2;
393                     e2= cp.getFragments()[0];
394                 }
395                 return super.compare(viewer, e1, e2);
396             }
397         };
398     }
399
400     protected StatusBarUpdater createStatusBarUpdater(IStatusLineManager slManager) {
401         return new StatusBarUpdater4LogicalPackage(slManager);
402     }
403
404     protected void setSiteSelectionProvider(){
405         getSite().setSelectionProvider(fWrappedViewer);
406     }
407
408     void adjustInputAndSetSelection(Object JavaDoc o) {
409         if (!(o instanceof LogicalPackage)) {
410             super.adjustInputAndSetSelection(o);
411             return;
412         }
413
414         LogicalPackage lp= (LogicalPackage)o;
415         if (!lp.getJavaProject().equals(getInput()))
416             setInput(lp.getJavaProject());
417
418         setSelection(new StructuredSelection(lp), true);
419     }
420
421     //do the same thing as the JavaBrowsingPart but with wrapper
422
protected void createActions() {
423         super.createActions();
424
425         createSelectAllAction();
426
427         //create the switch action group
428
fSwitchActionGroup= createSwitchActionGroup();
429     }
430
431     private MultiActionGroup createSwitchActionGroup(){
432
433         LayoutAction switchToFlatViewAction= new LayoutAction(JavaBrowsingMessages.PackagesView_flatLayoutAction_label,LIST_VIEW_STATE);
434         LayoutAction switchToHierarchicalViewAction= new LayoutAction(JavaBrowsingMessages.PackagesView_HierarchicalLayoutAction_label, TREE_VIEW_STATE);
435         JavaPluginImages.setLocalImageDescriptors(switchToFlatViewAction, "flatLayout.gif"); //$NON-NLS-1$
436
JavaPluginImages.setLocalImageDescriptors(switchToHierarchicalViewAction, "hierarchicalLayout.gif"); //$NON-NLS-1$
437

438         return new LayoutActionGroup(new IAction[]{switchToFlatViewAction,switchToHierarchicalViewAction}, fCurrViewState);
439     }
440
441     private static class LayoutActionGroup extends MultiActionGroup {
442
443         LayoutActionGroup(IAction[] actions, int index) {
444             super(actions, index);
445         }
446
447         public void fillActionBars(IActionBars actionBars) {
448             //create new layout group
449
IMenuManager manager= actionBars.getMenuManager();
450             final IContributionItem groupMarker= new GroupMarker("layout"); //$NON-NLS-1$
451
manager.add(groupMarker);
452             IMenuManager newManager= new MenuManager(JavaBrowsingMessages.PackagesView_LayoutActionGroup_layout_label);
453             manager.appendToGroup("layout", newManager); //$NON-NLS-1$
454
super.addActions(newManager);
455         }
456     }
457
458
459     /**
460      * Switches between flat and hierarchical state.
461      */

462     private class LayoutAction extends Action {
463
464         private int fState;
465
466         public LayoutAction(String JavaDoc text, int state) {
467             super(text, IAction.AS_RADIO_BUTTON);
468             fState= state;
469             if (state == PackagesView.LIST_VIEW_STATE)
470                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_FLAT_ACTION);
471             else
472                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_HIERARCHICAL_ACTION);
473         }
474
475         public int getState() {
476             return fState;
477         }
478
479         public void setRunnable(Runnable JavaDoc runnable) {
480             Assert.isNotNull(runnable);
481         }
482
483         /*
484          * @see org.eclipse.jface.action.IAction#run()
485          */

486         public void run() {
487             switchViewer(fState);
488         }
489     }
490
491     private void switchViewer(int state) {
492         //Indicate which viewer is to be used
493
if (fCurrViewState == state)
494             return;
495         else {
496             fCurrViewState= state;
497             IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
498             store.setValue(getViewSite().getId() + TAG_VIEW_STATE, state);
499         }
500
501         //get the information from the existing viewer
502
StructuredViewer viewer= fWrappedViewer.getViewer();
503         Object JavaDoc object= viewer.getInput();
504         ISelection selection= viewer.getSelection();
505
506         // create and set up the new viewer
507
Control control= createViewer(fWrappedViewer.getControl().getParent()).getControl();
508
509         setUpViewer(fWrappedViewer);
510
511         createSelectAllAction();
512
513         // add the selection information from old viewer
514
fWrappedViewer.setViewerInput(object);
515         fWrappedViewer.getControl().setFocus();
516         fWrappedViewer.setSelection(selection, true);
517
518         // dispose old viewer
519
viewer.getContentProvider().dispose();
520         viewer.getControl().dispose();
521
522         // layout the new viewer
523
if (control != null && !control.isDisposed()) {
524             control.setVisible(true);
525             control.getParent().layout(true);
526         }
527     }
528
529     private void createSelectAllAction() {
530         IActionBars actionBars= getViewSite().getActionBars();
531         if (isInListState()) {
532             fSelectAllAction= new SelectAllAction((TableViewer)fWrappedViewer.getViewer());
533             actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction);
534         } else {
535             actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), null);
536             fSelectAllAction= null;
537         }
538         actionBars.updateActionBars();
539     }
540
541     protected IJavaElement findInputForJavaElement(IJavaElement je) {
542         // null check has to take place here as well (not only in
543
// findInputForJavaElement(IJavaElement, boolean) since we
544
// are accessing the Java element
545
if (je == null)
546             return null;
547         if(je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT || je.getElementType() == IJavaElement.JAVA_PROJECT)
548             return findInputForJavaElement(je, true);
549         else
550             return findInputForJavaElement(je, false);
551
552     }
553
554     protected IJavaElement findInputForJavaElement(IJavaElement je, boolean canChangeInputType) {
555         if (je == null || !je.exists())
556             return null;
557
558         if (isValidInput(je)) {
559
560             //don't update if input must be project (i.e. project is used as source folder)
561
if (canChangeInputType)
562                 fLastInputWasProject= je.getElementType() == IJavaElement.JAVA_PROJECT;
563             return je;
564         } else if (fLastInputWasProject) {
565             IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot)je.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
566             if (!packageFragmentRoot.isExternal())
567                 return je.getJavaProject();
568         }
569
570         return findInputForJavaElement(je.getParent(), canChangeInputType);
571     }
572
573     /**
574      * Override the getText and getImage methods for the DecoratingLabelProvider
575      * to handel the decoration of logical packages.
576      *
577      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createDecoratingLabelProvider(JavaUILabelProvider)
578      */

579     protected DecoratingJavaLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) {
580         return new DecoratingJavaLabelProvider(provider, false, isInListState()) {
581
582             public String JavaDoc getText(Object JavaDoc element){
583                 if (element instanceof LogicalPackage) {
584                     LogicalPackage el= (LogicalPackage) element;
585                     return super.getText(el.getFragments()[0]);
586                 } else return super.getText(element);
587             }
588
589             public Image getImage(Object JavaDoc element) {
590                 if(element instanceof LogicalPackage){
591                     LogicalPackage el= (LogicalPackage) element;
592                     ILabelDecorator decorator= getLabelDecorator();
593                     IPackageFragment[] fragments= el.getFragments();
594
595                     Image image= super.getImage(el);
596                     for (int i= 0; i < fragments.length; i++) {
597                         IPackageFragment fragment= fragments[i];
598                         Image decoratedImage= decorator.decorateImage(image, fragment);
599                         if(decoratedImage != null)
600                             image= decoratedImage;
601                     }
602                     return image;
603                 } else return super.getImage(element);
604             }
605
606         };
607     }
608
609     /*
610      * Overridden from JavaBrowsingPart to handel LogicalPackages and tree
611      * structure.
612      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#adjustInputAndSetSelection(org.eclipse.jdt.core.IJavaElement)
613      */

614     void adjustInputAndSetSelection(IJavaElement je) {
615
616         IJavaElement jElementToSelect= findElementToSelect(je);
617         LogicalPackagesProvider p= (LogicalPackagesProvider) fWrappedViewer.getContentProvider();
618
619         Object JavaDoc elementToSelect= jElementToSelect;
620         if (jElementToSelect != null && jElementToSelect.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
621             IPackageFragment pkgFragment= (IPackageFragment)jElementToSelect;
622             elementToSelect= p.findLogicalPackage(pkgFragment);
623             if (elementToSelect == null)
624                 elementToSelect= pkgFragment;
625         }
626
627         IJavaElement newInput= findInputForJavaElement(je);
628         if (elementToSelect == null && !isValidInput(newInput))
629             setInput(null);
630         else if (elementToSelect == null || getViewer().testFindItem(elementToSelect) == null) {
631
632             //optimization, if you are in the same project but expansion hasn't happened
633
Object JavaDoc input= getViewer().getInput();
634             if (elementToSelect != null && newInput != null) {
635                 if (newInput.equals(input)) {
636                     getViewer().reveal(elementToSelect);
637                 // Adjust input to selection
638
} else {
639                     setInput(newInput);
640                     getViewer().reveal(elementToSelect);
641                 }
642             } else
643                 setInput(newInput);
644
645             if (elementToSelect instanceof IPackageFragment) {
646                 IPackageFragment pkgFragment= (IPackageFragment)elementToSelect;
647                 elementToSelect= p.findLogicalPackage(pkgFragment);
648                 if (elementToSelect == null)
649                     elementToSelect= pkgFragment;
650             }
651         }
652
653         ISelection selection;
654         if (elementToSelect != null)
655             selection= new StructuredSelection(elementToSelect);
656         else
657             selection= StructuredSelection.EMPTY;
658         setSelection(selection, true);
659     }
660
661 }
662
Popular Tags