Source Code Cross Referenced for JavaBrowsingPart.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » internal » ui » browsing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » jdt » org.eclipse.jdt.internal.ui.browsing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Copyright (c) 2000, 2007 IBM Corporation and others.
0003:         * All rights reserved. This program and the accompanying materials
0004:         * are made available under the terms of the Eclipse Public License v1.0
0005:         * which accompanies this distribution, and is available at
0006:         * http://www.eclipse.org/legal/epl-v10.html
0007:         *
0008:         * Contributors:
0009:         *     IBM Corporation - initial API and implementation
0010:         *******************************************************************************/package org.eclipse.jdt.internal.ui.browsing;
0011:
0012:        import java.util.ArrayList;
0013:        import java.util.Collection;
0014:        import java.util.Comparator;
0015:        import java.util.Iterator;
0016:        import java.util.List;
0017:
0018:        import org.eclipse.core.runtime.Assert;
0019:        import org.eclipse.core.runtime.IAdaptable;
0020:        import org.eclipse.core.runtime.IPath;
0021:
0022:        import org.eclipse.core.resources.IContainer;
0023:        import org.eclipse.core.resources.IFile;
0024:        import org.eclipse.core.resources.IMarker;
0025:        import org.eclipse.core.resources.IResource;
0026:
0027:        import org.eclipse.swt.SWT;
0028:        import org.eclipse.swt.events.KeyAdapter;
0029:        import org.eclipse.swt.events.KeyEvent;
0030:        import org.eclipse.swt.widgets.Composite;
0031:        import org.eclipse.swt.widgets.Display;
0032:        import org.eclipse.swt.widgets.Menu;
0033:        import org.eclipse.swt.widgets.Shell;
0034:
0035:        import org.eclipse.help.IContextProvider;
0036:
0037:        import org.eclipse.jface.action.IAction;
0038:        import org.eclipse.jface.action.IMenuListener;
0039:        import org.eclipse.jface.action.IMenuManager;
0040:        import org.eclipse.jface.action.IStatusLineManager;
0041:        import org.eclipse.jface.action.IToolBarManager;
0042:        import org.eclipse.jface.action.MenuManager;
0043:        import org.eclipse.jface.util.IPropertyChangeListener;
0044:        import org.eclipse.jface.util.PropertyChangeEvent;
0045:        import org.eclipse.jface.viewers.IContentProvider;
0046:        import org.eclipse.jface.viewers.ILabelProvider;
0047:        import org.eclipse.jface.viewers.IOpenListener;
0048:        import org.eclipse.jface.viewers.ISelection;
0049:        import org.eclipse.jface.viewers.ISelectionChangedListener;
0050:        import org.eclipse.jface.viewers.ISelectionProvider;
0051:        import org.eclipse.jface.viewers.IStructuredSelection;
0052:        import org.eclipse.jface.viewers.OpenEvent;
0053:        import org.eclipse.jface.viewers.SelectionChangedEvent;
0054:        import org.eclipse.jface.viewers.StructuredSelection;
0055:        import org.eclipse.jface.viewers.StructuredViewer;
0056:
0057:        import org.eclipse.jface.text.ITextSelection;
0058:
0059:        import org.eclipse.ui.IActionBars;
0060:        import org.eclipse.ui.IEditorInput;
0061:        import org.eclipse.ui.IEditorPart;
0062:        import org.eclipse.ui.IFileEditorInput;
0063:        import org.eclipse.ui.IMemento;
0064:        import org.eclipse.ui.IPartListener2;
0065:        import org.eclipse.ui.ISelectionListener;
0066:        import org.eclipse.ui.IViewSite;
0067:        import org.eclipse.ui.IWorkbenchPage;
0068:        import org.eclipse.ui.IWorkbenchPart;
0069:        import org.eclipse.ui.IWorkbenchPartReference;
0070:        import org.eclipse.ui.IWorkbenchPartSite;
0071:        import org.eclipse.ui.IWorkingSet;
0072:        import org.eclipse.ui.IWorkingSetManager;
0073:        import org.eclipse.ui.PartInitException;
0074:        import org.eclipse.ui.PlatformUI;
0075:        import org.eclipse.ui.actions.ActionContext;
0076:        import org.eclipse.ui.actions.ActionGroup;
0077:        import org.eclipse.ui.part.IShowInSource;
0078:        import org.eclipse.ui.part.ShowInContext;
0079:        import org.eclipse.ui.part.ViewPart;
0080:        import org.eclipse.ui.texteditor.ITextEditor;
0081:
0082:        import org.eclipse.search.ui.ISearchResultViewPart;
0083:
0084:        import org.eclipse.jdt.core.IClassFile;
0085:        import org.eclipse.jdt.core.ICompilationUnit;
0086:        import org.eclipse.jdt.core.IJavaElement;
0087:        import org.eclipse.jdt.core.IPackageDeclaration;
0088:        import org.eclipse.jdt.core.IPackageFragment;
0089:        import org.eclipse.jdt.core.IType;
0090:        import org.eclipse.jdt.core.JavaCore;
0091:        import org.eclipse.jdt.core.JavaModelException;
0092:
0093:        import org.eclipse.jdt.internal.corext.util.Messages;
0094:
0095:        import org.eclipse.jdt.ui.IContextMenuConstants;
0096:        import org.eclipse.jdt.ui.IWorkingCopyManager;
0097:        import org.eclipse.jdt.ui.JavaElementComparator;
0098:        import org.eclipse.jdt.ui.JavaElementLabelProvider;
0099:        import org.eclipse.jdt.ui.JavaElementLabels;
0100:        import org.eclipse.jdt.ui.JavaUI;
0101:        import org.eclipse.jdt.ui.PreferenceConstants;
0102:        import org.eclipse.jdt.ui.actions.BuildActionGroup;
0103:        import org.eclipse.jdt.ui.actions.CCPActionGroup;
0104:        import org.eclipse.jdt.ui.actions.CustomFiltersActionGroup;
0105:        import org.eclipse.jdt.ui.actions.GenerateActionGroup;
0106:        import org.eclipse.jdt.ui.actions.ImportActionGroup;
0107:        import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
0108:        import org.eclipse.jdt.ui.actions.OpenEditorActionGroup;
0109:        import org.eclipse.jdt.ui.actions.OpenViewActionGroup;
0110:        import org.eclipse.jdt.ui.actions.RefactorActionGroup;
0111:
0112:        import org.eclipse.jdt.internal.ui.JavaPlugin;
0113:        import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup;
0114:        import org.eclipse.jdt.internal.ui.actions.NewWizardsActionGroup;
0115:        import org.eclipse.jdt.internal.ui.dnd.JdtViewerDragSupport;
0116:        import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropSupport;
0117:        import org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView;
0118:        import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
0119:        import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
0120:        import org.eclipse.jdt.internal.ui.search.SearchUtil;
0121:        import org.eclipse.jdt.internal.ui.util.JavaUIHelp;
0122:        import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
0123:        import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
0124:        import org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider;
0125:        import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
0126:        import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
0127:        import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer;
0128:        import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater;
0129:        import org.eclipse.jdt.internal.ui.workingsets.WorkingSetFilterActionGroup;
0130:
0131:        abstract class JavaBrowsingPart extends ViewPart implements 
0132:                IMenuListener, ISelectionListener, IViewPartInputProvider {
0133:
0134:            private static final String TAG_SELECTED_ELEMENTS = "selectedElements"; //$NON-NLS-1$
0135:            private static final String TAG_SELECTED_ELEMENT = "selectedElement"; //$NON-NLS-1$
0136:            private static final String TAG_LOGICAL_PACKAGE = "logicalPackage"; //$NON-NLS-1$
0137:            private static final String TAG_SELECTED_ELEMENT_PATH = "selectedElementPath"; //$NON-NLS-1$
0138:
0139:            private JavaUILabelProvider fLabelProvider;
0140:            private ILabelProvider fTitleProvider;
0141:            private StructuredViewer fViewer;
0142:            private IMemento fMemento;
0143:            private JavaElementTypeComparator fTypeComparator;
0144:            private JdtViewerDropSupport fDropSupport;
0145:
0146:            // Actions
0147:            private WorkingSetFilterActionGroup fWorkingSetFilterActionGroup;
0148:            private boolean fHasWorkingSetFilter = true;
0149:            private boolean fHasCustomFilter = true;
0150:            private OpenEditorActionGroup fOpenEditorGroup;
0151:            private CCPActionGroup fCCPActionGroup;
0152:            private BuildActionGroup fBuildActionGroup;
0153:            private ToggleLinkingAction fToggleLinkingAction;
0154:            protected CompositeActionGroup fActionGroups;
0155:
0156:            // Filters
0157:            private CustomFiltersActionGroup fCustomFiltersActionGroup;
0158:
0159:            protected IWorkbenchPart fPreviousSelectionProvider;
0160:            protected Object fPreviousSelectedElement;
0161:
0162:            // Linking
0163:            private boolean fLinkingEnabled;
0164:
0165:            /*
0166:             * Ensure selection changed events being processed only if
0167:             * initiated by user interaction with this part.
0168:             */
0169:            private boolean fProcessSelectionEvents = true;
0170:
0171:            private IPartListener2 fPartListener = new IPartListener2() {
0172:                public void partActivated(IWorkbenchPartReference ref) {
0173:                }
0174:
0175:                public void partBroughtToTop(IWorkbenchPartReference ref) {
0176:                }
0177:
0178:                public void partInputChanged(IWorkbenchPartReference ref) {
0179:                }
0180:
0181:                public void partClosed(IWorkbenchPartReference ref) {
0182:                }
0183:
0184:                public void partDeactivated(IWorkbenchPartReference ref) {
0185:                }
0186:
0187:                public void partOpened(IWorkbenchPartReference ref) {
0188:                }
0189:
0190:                public void partVisible(IWorkbenchPartReference ref) {
0191:                    if (ref != null && ref.getId() == getSite().getId()) {
0192:                        fProcessSelectionEvents = true;
0193:                        IWorkbenchPage page = getSite().getWorkbenchWindow()
0194:                                .getActivePage();
0195:                        if (page != null)
0196:                            selectionChanged(page.getActivePart(), page
0197:                                    .getSelection());
0198:                    }
0199:                }
0200:
0201:                public void partHidden(IWorkbenchPartReference ref) {
0202:                    if (ref != null && ref.getId() == getSite().getId())
0203:                        fProcessSelectionEvents = false;
0204:                }
0205:            };
0206:
0207:            public JavaBrowsingPart() {
0208:                super ();
0209:                initLinkingEnabled();
0210:            }
0211:
0212:            /*
0213:             * Implements method from IViewPart.
0214:             */
0215:            public void init(IViewSite site, IMemento memento)
0216:                    throws PartInitException {
0217:                super .init(site, memento);
0218:                fMemento = memento;
0219:            }
0220:
0221:            /*
0222:             * Implements method from IViewPart.
0223:             */
0224:            public void saveState(IMemento memento) {
0225:                if (fViewer == null) {
0226:                    // part has not been created
0227:                    if (fMemento != null) //Keep the old state;
0228:                        memento.putMemento(fMemento);
0229:                    return;
0230:                }
0231:                if (fHasWorkingSetFilter)
0232:                    fWorkingSetFilterActionGroup.saveState(memento);
0233:                if (fHasCustomFilter)
0234:                    fCustomFiltersActionGroup.saveState(memento);
0235:                saveSelectionState(memento);
0236:                saveLinkingEnabled(memento);
0237:            }
0238:
0239:            private void saveLinkingEnabled(IMemento memento) {
0240:                memento.putInteger(getLinkToEditorKey(), fLinkingEnabled ? 1
0241:                        : 0);
0242:            }
0243:
0244:            private void saveSelectionState(IMemento memento) {
0245:                Object elements[] = ((IStructuredSelection) fViewer
0246:                        .getSelection()).toArray();
0247:                if (elements.length > 0) {
0248:                    IMemento selectionMem = memento
0249:                            .createChild(TAG_SELECTED_ELEMENTS);
0250:                    for (int i = 0; i < elements.length; i++) {
0251:                        IMemento elementMem = selectionMem
0252:                                .createChild(TAG_SELECTED_ELEMENT);
0253:                        Object o = elements[i];
0254:                        if (o instanceof  IJavaElement)
0255:                            elementMem.putString(TAG_SELECTED_ELEMENT_PATH,
0256:                                    ((IJavaElement) elements[i])
0257:                                            .getHandleIdentifier());
0258:                        else if (o instanceof  LogicalPackage) {
0259:                            IPackageFragment[] packages = ((LogicalPackage) o)
0260:                                    .getFragments();
0261:                            for (int j = 0; j < packages.length; j++) {
0262:                                IMemento packageMem = elementMem
0263:                                        .createChild(TAG_LOGICAL_PACKAGE);
0264:                                packageMem.putString(TAG_SELECTED_ELEMENT_PATH,
0265:                                        packages[j].getHandleIdentifier());
0266:                            }
0267:                        }
0268:                    }
0269:                }
0270:            }
0271:
0272:            protected void restoreState(IMemento memento) {
0273:                if (fHasWorkingSetFilter)
0274:                    fWorkingSetFilterActionGroup.restoreState(memento);
0275:                if (fHasCustomFilter)
0276:                    fCustomFiltersActionGroup.restoreState(memento);
0277:
0278:                if (fHasCustomFilter || fHasWorkingSetFilter) {
0279:                    fViewer.getControl().setRedraw(false);
0280:                    fViewer.refresh();
0281:                    fViewer.getControl().setRedraw(true);
0282:                }
0283:            }
0284:
0285:            private ISelection restoreSelectionState(IMemento memento) {
0286:                if (memento == null)
0287:                    return null;
0288:
0289:                IMemento childMem;
0290:                childMem = memento.getChild(TAG_SELECTED_ELEMENTS);
0291:                if (childMem != null) {
0292:                    ArrayList list = new ArrayList();
0293:                    IMemento[] elementMem = childMem
0294:                            .getChildren(TAG_SELECTED_ELEMENT);
0295:                    for (int i = 0; i < elementMem.length; i++) {
0296:                        String javaElementHandle = elementMem[i]
0297:                                .getString(TAG_SELECTED_ELEMENT_PATH);
0298:                        if (javaElementHandle == null) {
0299:                            // logical package
0300:                            IMemento[] packagesMem = elementMem[i]
0301:                                    .getChildren(TAG_LOGICAL_PACKAGE);
0302:                            LogicalPackage lp = null;
0303:                            for (int j = 0; j < packagesMem.length; j++) {
0304:                                javaElementHandle = packagesMem[j]
0305:                                        .getString(TAG_SELECTED_ELEMENT_PATH);
0306:                                Object pack = JavaCore
0307:                                        .create(javaElementHandle);
0308:                                if (pack instanceof  IPackageFragment
0309:                                        && ((IPackageFragment) pack).exists()) {
0310:                                    if (lp == null)
0311:                                        lp = new LogicalPackage(
0312:                                                (IPackageFragment) pack);
0313:                                    else
0314:                                        lp.add((IPackageFragment) pack);
0315:                                }
0316:                            }
0317:                            if (lp != null)
0318:                                list.add(lp);
0319:                        } else {
0320:                            IJavaElement element = JavaCore
0321:                                    .create(javaElementHandle);
0322:                            if (element != null && element.exists())
0323:                                list.add(element);
0324:                        }
0325:                    }
0326:                    return new StructuredSelection(list);
0327:                }
0328:                return null;
0329:            }
0330:
0331:            private void restoreLinkingEnabled(IMemento memento) {
0332:                Integer val = memento.getInteger(getLinkToEditorKey());
0333:                if (val != null) {
0334:                    fLinkingEnabled = val.intValue() != 0;
0335:                }
0336:            }
0337:
0338:            /* (non-Javadoc)
0339:             * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
0340:             */
0341:            public void createPartControl(Composite parent) {
0342:                Assert.isTrue(fViewer == null);
0343:
0344:                fTypeComparator = new JavaElementTypeComparator();
0345:
0346:                // Setup viewer
0347:                fViewer = createViewer(parent);
0348:
0349:                initDragAndDrop();
0350:
0351:                fLabelProvider = createLabelProvider();
0352:                fViewer
0353:                        .setLabelProvider(createDecoratingLabelProvider(fLabelProvider));
0354:
0355:                fViewer.setComparator(createJavaElementComparator());
0356:                fViewer.setUseHashlookup(true);
0357:                fTitleProvider = createTitleProvider();
0358:
0359:                createContextMenu();
0360:                getSite().setSelectionProvider(fViewer);
0361:
0362:                if (fMemento != null) { // initialize linking state before creating the actions
0363:                    restoreLinkingEnabled(fMemento);
0364:                }
0365:
0366:                createActions(); // call before registering for selection changes
0367:                addKeyListener();
0368:
0369:                if (fMemento != null)
0370:                    restoreState(fMemento);
0371:
0372:                getSite().setSelectionProvider(fViewer);
0373:
0374:                // Status line
0375:                IStatusLineManager slManager = getViewSite().getActionBars()
0376:                        .getStatusLineManager();
0377:                fViewer
0378:                        .addSelectionChangedListener(createStatusBarUpdater(slManager));
0379:
0380:                hookViewerListeners();
0381:
0382:                // Filters
0383:                addFilters();
0384:
0385:                // Initialize viewer input
0386:                fViewer.setContentProvider(createContentProvider());
0387:                setInitialInput();
0388:
0389:                // Initialize selection
0390:                setInitialSelection();
0391:                fMemento = null;
0392:
0393:                // Listen to page changes
0394:                getViewSite().getPage().addPostSelectionListener(this );
0395:                getViewSite().getPage().addPartListener(fPartListener);
0396:
0397:                fillActionBars(getViewSite().getActionBars());
0398:
0399:                setHelp();
0400:            }
0401:
0402:            /* (non-Javadoc)
0403:             * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
0404:             */
0405:            public Object getAdapter(Class key) {
0406:                if (key == IShowInSource.class) {
0407:                    return getShowInSource();
0408:                }
0409:                if (key == IContextProvider.class)
0410:                    return JavaUIHelp.getHelpContextProvider(this ,
0411:                            getHelpContextId());
0412:
0413:                return super .getAdapter(key);
0414:            }
0415:
0416:            /**
0417:             * Returns the <code>IShowInSource</code> for this view.
0418:             * @return returns the <code>IShowInSource</code>
0419:             */
0420:            protected IShowInSource getShowInSource() {
0421:                return new IShowInSource() {
0422:                    public ShowInContext getShowInContext() {
0423:                        return new ShowInContext(null, getSite()
0424:                                .getSelectionProvider().getSelection());
0425:                    }
0426:                };
0427:            }
0428:
0429:            protected DecoratingJavaLabelProvider createDecoratingLabelProvider(
0430:                    JavaUILabelProvider provider) {
0431:                //		XXX: Work in progress for problem decorator being a workbench decorator//
0432:                //		return new ExcludingDecoratingLabelProvider(provider, decorationMgr, "org.eclipse.jdt.ui.problem.decorator"); //$NON-NLS-1$
0433:                return new DecoratingJavaLabelProvider(provider);
0434:            }
0435:
0436:            protected JavaElementComparator createJavaElementComparator() {
0437:                return new JavaElementComparator();
0438:            }
0439:
0440:            protected StatusBarUpdater createStatusBarUpdater(
0441:                    IStatusLineManager slManager) {
0442:                return new StatusBarUpdater(slManager);
0443:            }
0444:
0445:            protected void createContextMenu() {
0446:                MenuManager menuManager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
0447:                menuManager.setRemoveAllWhenShown(true);
0448:                menuManager.addMenuListener(this );
0449:                Menu contextMenu = menuManager.createContextMenu(fViewer
0450:                        .getControl());
0451:                fViewer.getControl().setMenu(contextMenu);
0452:                getSite().registerContextMenu(menuManager, fViewer);
0453:            }
0454:
0455:            protected void initDragAndDrop() {
0456:                fDropSupport = new JdtViewerDropSupport(fViewer);
0457:                fDropSupport.start();
0458:                new JdtViewerDragSupport(fViewer).start();
0459:            }
0460:
0461:            protected JdtViewerDropSupport getDropSupport() {
0462:                return fDropSupport;
0463:            }
0464:
0465:            protected void fillActionBars(IActionBars actionBars) {
0466:                IToolBarManager toolBar = actionBars.getToolBarManager();
0467:                fillToolBar(toolBar);
0468:
0469:                if (fHasWorkingSetFilter)
0470:                    fWorkingSetFilterActionGroup.fillActionBars(getViewSite()
0471:                            .getActionBars());
0472:
0473:                actionBars.updateActionBars();
0474:
0475:                fActionGroups.fillActionBars(actionBars);
0476:
0477:                if (fHasCustomFilter)
0478:                    fCustomFiltersActionGroup.fillActionBars(actionBars);
0479:
0480:                IMenuManager menu = actionBars.getMenuManager();
0481:                menu.add(fToggleLinkingAction);
0482:            }
0483:
0484:            //---- IWorkbenchPart ------------------------------------------------------
0485:
0486:            public void setFocus() {
0487:                fViewer.getControl().setFocus();
0488:            }
0489:
0490:            public void dispose() {
0491:                if (fViewer != null) {
0492:                    getViewSite().getPage().removePostSelectionListener(this );
0493:                    getViewSite().getPage().removePartListener(fPartListener);
0494:                    fViewer = null;
0495:                }
0496:                if (fActionGroups != null)
0497:                    fActionGroups.dispose();
0498:
0499:                if (fWorkingSetFilterActionGroup != null) {
0500:                    fWorkingSetFilterActionGroup.dispose();
0501:                }
0502:
0503:                super .dispose();
0504:            }
0505:
0506:            /**
0507:             * Adds the KeyListener
0508:             */
0509:            protected void addKeyListener() {
0510:                fViewer.getControl().addKeyListener(new KeyAdapter() {
0511:                    public void keyReleased(KeyEvent event) {
0512:                        handleKeyReleased(event);
0513:                    }
0514:                });
0515:            }
0516:
0517:            protected void handleKeyReleased(KeyEvent event) {
0518:                if (event.stateMask != 0)
0519:                    return;
0520:
0521:                int key = event.keyCode;
0522:                if (key == SWT.F5) {
0523:                    IAction action = fBuildActionGroup.getRefreshAction();
0524:                    if (action.isEnabled())
0525:                        action.run();
0526:                }
0527:            }
0528:
0529:            //---- Adding Action to Toolbar -------------------------------------------
0530:
0531:            protected void fillToolBar(IToolBarManager tbm) {
0532:            }
0533:
0534:            /* (non-Javadoc)
0535:             * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
0536:             */
0537:            public void menuAboutToShow(IMenuManager menu) {
0538:                JavaPlugin.createStandardGroups(menu);
0539:
0540:                IStructuredSelection selection = (IStructuredSelection) fViewer
0541:                        .getSelection();
0542:                int size = selection.size();
0543:                Object element = selection.getFirstElement();
0544:
0545:                if (size == 1)
0546:                    addOpenNewWindowAction(menu, element);
0547:                fActionGroups.setContext(new ActionContext(selection));
0548:                fActionGroups.fillContextMenu(menu);
0549:                fActionGroups.setContext(null);
0550:            }
0551:
0552:            private void addOpenNewWindowAction(IMenuManager menu,
0553:                    Object element) {
0554:                if (element instanceof  IJavaElement) {
0555:                    element = ((IJavaElement) element).getResource();
0556:                }
0557:                if (!(element instanceof  IContainer))
0558:                    return;
0559:                menu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
0560:                        new PatchedOpenInNewWindowAction(getSite()
0561:                                .getWorkbenchWindow(), (IContainer) element));
0562:            }
0563:
0564:            protected void createActions() {
0565:                fActionGroups = new CompositeActionGroup(new ActionGroup[] {
0566:                        new NewWizardsActionGroup(this .getSite()),
0567:                        fOpenEditorGroup = new OpenEditorActionGroup(this ),
0568:                        new OpenViewActionGroup(this ),
0569:                        fCCPActionGroup = new CCPActionGroup(this ),
0570:                        new GenerateActionGroup(this ),
0571:                        new RefactorActionGroup(this ),
0572:                        new ImportActionGroup(this ),
0573:                        fBuildActionGroup = new BuildActionGroup(this ),
0574:                        new JavaSearchActionGroup(this ) });
0575:
0576:                if (fHasWorkingSetFilter) {
0577:                    String viewId = getConfigurationElement()
0578:                            .getAttribute("id"); //$NON-NLS-1$
0579:                    Assert.isNotNull(viewId);
0580:                    IPropertyChangeListener workingSetListener = new IPropertyChangeListener() {
0581:                        public void propertyChange(PropertyChangeEvent event) {
0582:                            doWorkingSetChanged(event);
0583:                        }
0584:                    };
0585:                    fWorkingSetFilterActionGroup = new WorkingSetFilterActionGroup(
0586:                            getSite(), workingSetListener);
0587:                    fViewer.addFilter(fWorkingSetFilterActionGroup
0588:                            .getWorkingSetFilter());
0589:                }
0590:
0591:                // Custom filter group
0592:                if (fHasCustomFilter)
0593:                    fCustomFiltersActionGroup = new CustomFiltersActionGroup(
0594:                            this , fViewer);
0595:
0596:                fToggleLinkingAction = new ToggleLinkingAction(this );
0597:            }
0598:
0599:            private void doWorkingSetChanged(PropertyChangeEvent event) {
0600:                String property = event.getProperty();
0601:                if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE
0602:                        .equals(property))
0603:                    updateTitle();
0604:                else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE
0605:                        .equals(property)) {
0606:                    updateTitle();
0607:                    fViewer.getControl().setRedraw(false);
0608:                    fViewer.refresh();
0609:                    fViewer.getControl().setRedraw(true);
0610:                }
0611:
0612:            }
0613:
0614:            /**
0615:             * Returns the shell to use for opening dialogs.
0616:             * Used in this class, and in the actions.
0617:             * @return returns the shell
0618:             */
0619:            Shell getShell() {
0620:                return fViewer.getControl().getShell();
0621:            }
0622:
0623:            protected final Display getDisplay() {
0624:                return fViewer.getControl().getDisplay();
0625:            }
0626:
0627:            /**
0628:             * Returns the selection provider.
0629:             * @return the selection provider
0630:             */
0631:            ISelectionProvider getSelectionProvider() {
0632:                return fViewer;
0633:            }
0634:
0635:            /**
0636:             * Answers if the given <code>element</code> is a valid
0637:             * input for this part.
0638:             *
0639:             * @param 	element	the object to test
0640:             * @return	<code>true</code> if the given element is a valid input
0641:             */
0642:            abstract protected boolean isValidInput(Object element);
0643:
0644:            /**
0645:             * Answers if the given <code>element</code> is a valid
0646:             * element for this part.
0647:             *
0648:             * @param 	element	the object to test
0649:             * @return	<code>true</code> if the given element is a valid element
0650:             */
0651:            protected boolean isValidElement(Object element) {
0652:                if (!(element instanceof  IJavaElement)) {
0653:                    return false;
0654:                }
0655:                Object input = getViewer().getInput();
0656:                if (input == null)
0657:                    return false;
0658:                if (input instanceof  Collection)
0659:                    return ((Collection) input).contains(element);
0660:                else
0661:                    return input.equals(element);
0662:
0663:            }
0664:
0665:            private boolean isInputResetBy(Object newInput, Object input,
0666:                    IWorkbenchPart part) {
0667:                if (newInput == null)
0668:                    return part == fPreviousSelectionProvider;
0669:
0670:                if (input instanceof  IJavaElement
0671:                        && newInput instanceof  IJavaElement)
0672:                    return getTypeComparator().compare(newInput, input) > 0;
0673:
0674:                if ((newInput instanceof  List)
0675:                        && (part instanceof  PackagesView))
0676:                    return true;
0677:
0678:                else
0679:                    return false;
0680:            }
0681:
0682:            private boolean isInputResetBy(IWorkbenchPart part) {
0683:                if (!(part instanceof  JavaBrowsingPart))
0684:                    return true;
0685:                Object this Input = getViewer().getInput();
0686:                Object partInput = ((JavaBrowsingPart) part).getViewer()
0687:                        .getInput();
0688:
0689:                if (this Input instanceof  Collection)
0690:                    this Input = ((Collection) this Input).iterator().next();
0691:
0692:                if (partInput instanceof  Collection)
0693:                    partInput = ((Collection) partInput).iterator().next();
0694:
0695:                if (this Input instanceof  IJavaElement
0696:                        && partInput instanceof  IJavaElement)
0697:                    return getTypeComparator().compare(partInput, this Input) > 0;
0698:                else
0699:                    return true;
0700:            }
0701:
0702:            protected boolean isAncestorOf(Object ancestor, Object element) {
0703:                if (element instanceof  IJavaElement
0704:                        && ancestor instanceof  IJavaElement)
0705:                    return !element.equals(ancestor)
0706:                            && internalIsAncestorOf((IJavaElement) ancestor,
0707:                                    (IJavaElement) element);
0708:                return false;
0709:            }
0710:
0711:            private boolean internalIsAncestorOf(IJavaElement ancestor,
0712:                    IJavaElement element) {
0713:                if (element != null)
0714:                    return element.equals(ancestor)
0715:                            || internalIsAncestorOf(ancestor, element
0716:                                    .getParent());
0717:                else
0718:                    return false;
0719:            }
0720:
0721:            private boolean isSearchResultView(IWorkbenchPart part) {
0722:                return SearchUtil.isSearchPlugInActivated()
0723:                        && part instanceof  ISearchResultViewPart;
0724:            }
0725:
0726:            protected boolean needsToProcessSelectionChanged(
0727:                    IWorkbenchPart part, ISelection selection) {
0728:                if (!fProcessSelectionEvents || part == this 
0729:                        || isSearchResultView(part)
0730:                        || part instanceof  AbstractInfoView) {
0731:                    if (part == this )
0732:                        fPreviousSelectionProvider = part;
0733:                    return false;
0734:                }
0735:                return true;
0736:            }
0737:
0738:            public void selectionChanged(IWorkbenchPart part,
0739:                    ISelection selection) {
0740:                if (!needsToProcessSelectionChanged(part, selection))
0741:                    return;
0742:
0743:                if (fToggleLinkingAction.isChecked()
0744:                        && (part instanceof  ITextEditor)) {
0745:                    setSelectionFromEditor(part, selection);
0746:                    return;
0747:                }
0748:
0749:                if (!(selection instanceof  IStructuredSelection))
0750:                    return;
0751:
0752:                // Set selection
0753:                Object selectedElement = getSingleElementFromSelection(selection);
0754:
0755:                if (selectedElement != null
0756:                        && (part == null || part
0757:                                .equals(fPreviousSelectionProvider))
0758:                        && selectedElement.equals(fPreviousSelectedElement))
0759:                    return;
0760:
0761:                fPreviousSelectedElement = selectedElement;
0762:
0763:                Object currentInput = getViewer().getInput();
0764:                if (selectedElement != null
0765:                        && selectedElement.equals(currentInput)) {
0766:                    IJavaElement elementToSelect = findElementToSelect(selectedElement);
0767:                    if (elementToSelect != null
0768:                            && getTypeComparator().compare(selectedElement,
0769:                                    elementToSelect) < 0)
0770:                        setSelection(new StructuredSelection(elementToSelect),
0771:                                true);
0772:                    else if (elementToSelect == null
0773:                            && (this  instanceof  MembersView)) {
0774:                        setSelection(StructuredSelection.EMPTY, true);
0775:                        fPreviousSelectedElement = StructuredSelection.EMPTY;
0776:                    }
0777:                    fPreviousSelectionProvider = part;
0778:                    return;
0779:                }
0780:
0781:                // Clear input if needed
0782:                if (part != fPreviousSelectionProvider
0783:                        && selectedElement != null
0784:                        && !selectedElement.equals(currentInput)
0785:                        && isInputResetBy(selectedElement, currentInput, part)) {
0786:                    if (!isAncestorOf(selectedElement, currentInput))
0787:                        setInput(null);
0788:                    fPreviousSelectionProvider = part;
0789:                    return;
0790:                } else if (selection.isEmpty() && !isInputResetBy(part)) {
0791:                    fPreviousSelectionProvider = part;
0792:                    return;
0793:                } else if (selectedElement == null
0794:                        && part == fPreviousSelectionProvider) {
0795:                    setInput(null);
0796:                    fPreviousSelectionProvider = part;
0797:                    return;
0798:                }
0799:                fPreviousSelectionProvider = part;
0800:
0801:                // Adjust input and set selection and
0802:                adjustInputAndSetSelection(selectedElement);
0803:            }
0804:
0805:            void setHasWorkingSetFilter(boolean state) {
0806:                fHasWorkingSetFilter = state;
0807:            }
0808:
0809:            void setHasCustomSetFilter(boolean state) {
0810:                fHasCustomFilter = state;
0811:            }
0812:
0813:            protected Object getInput() {
0814:                return fViewer.getInput();
0815:            }
0816:
0817:            protected void setInput(Object input) {
0818:                setViewerInput(input);
0819:                updateTitle();
0820:            }
0821:
0822:            boolean isLinkingEnabled() {
0823:                return fLinkingEnabled;
0824:            }
0825:
0826:            private void initLinkingEnabled() {
0827:                fLinkingEnabled = PreferenceConstants.getPreferenceStore()
0828:                        .getBoolean(getLinkToEditorKey());
0829:            }
0830:
0831:            private void setViewerInput(Object input) {
0832:                fProcessSelectionEvents = false;
0833:                fViewer.setInput(input);
0834:                fProcessSelectionEvents = true;
0835:            }
0836:
0837:            void updateTitle() {
0838:                setTitleToolTip(getToolTipText(fViewer.getInput()));
0839:            }
0840:
0841:            /**
0842:             * Returns the tool tip text for the given element.
0843:             * @param element the element
0844:             * @return the tooltip for the element
0845:             */
0846:            String getToolTipText(Object element) {
0847:                String result;
0848:                if (!(element instanceof  IResource)) {
0849:                    result = JavaElementLabels.getTextLabel(element,
0850:                            AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS);
0851:                } else {
0852:                    IPath path = ((IResource) element).getFullPath();
0853:                    if (path.isRoot()) {
0854:                        result = getConfigurationElement().getAttribute("name"); //$NON-NLS-1$
0855:                    } else {
0856:                        result = path.makeRelative().toString();
0857:                    }
0858:                }
0859:
0860:                if (fWorkingSetFilterActionGroup == null
0861:                        || fWorkingSetFilterActionGroup.getWorkingSet() == null)
0862:                    return result;
0863:
0864:                IWorkingSet ws = fWorkingSetFilterActionGroup.getWorkingSet();
0865:                String wsstr = Messages.format(
0866:                        JavaBrowsingMessages.JavaBrowsingPart_toolTip,
0867:                        new String[] { ws.getLabel() });
0868:                if (result.length() == 0)
0869:                    return wsstr;
0870:                return Messages.format(
0871:                        JavaBrowsingMessages.JavaBrowsingPart_toolTip2,
0872:                        new String[] { result, ws.getLabel() });
0873:            }
0874:
0875:            /* (non-Javadoc)
0876:             * @see org.eclipse.ui.part.WorkbenchPart#getTitleToolTip()
0877:             */
0878:            public String getTitleToolTip() {
0879:                if (fViewer == null)
0880:                    return super .getTitleToolTip();
0881:                return getToolTipText(fViewer.getInput());
0882:            }
0883:
0884:            protected final StructuredViewer getViewer() {
0885:                return fViewer;
0886:            }
0887:
0888:            protected final void setViewer(StructuredViewer viewer) {
0889:                fViewer = viewer;
0890:            }
0891:
0892:            protected JavaUILabelProvider createLabelProvider() {
0893:                return new AppearanceAwareLabelProvider(
0894:                        AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS,
0895:                        AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
0896:                                | JavaElementImageProvider.SMALL_ICONS);
0897:            }
0898:
0899:            protected ILabelProvider createTitleProvider() {
0900:                return new JavaElementLabelProvider(
0901:                        JavaElementLabelProvider.SHOW_BASICS
0902:                                | JavaElementLabelProvider.SHOW_SMALL_ICONS);
0903:            }
0904:
0905:            protected final ILabelProvider getLabelProvider() {
0906:                return fLabelProvider;
0907:            }
0908:
0909:            protected final ILabelProvider getTitleProvider() {
0910:                return fTitleProvider;
0911:            }
0912:
0913:            /**
0914:             * Creates the viewer of this part.
0915:             *
0916:             * @param parent	the parent for the viewer
0917:             * @return the created viewer
0918:             */
0919:            protected StructuredViewer createViewer(Composite parent) {
0920:                return new ProblemTableViewer(parent, SWT.MULTI);
0921:            }
0922:
0923:            protected int getLabelProviderFlags() {
0924:                return JavaElementLabelProvider.SHOW_BASICS
0925:                        | JavaElementLabelProvider.SHOW_OVERLAY_ICONS
0926:                        | JavaElementLabelProvider.SHOW_SMALL_ICONS
0927:                        | JavaElementLabelProvider.SHOW_VARIABLE
0928:                        | JavaElementLabelProvider.SHOW_PARAMETERS;
0929:            }
0930:
0931:            /**
0932:             * Adds filters the viewer of this part.
0933:             */
0934:            protected void addFilters() {
0935:                // default is to have no filters
0936:            }
0937:
0938:            /**
0939:             * Creates the content provider of this part.
0940:             * @return the content provider
0941:             */
0942:            protected IContentProvider createContentProvider() {
0943:                return new JavaBrowsingContentProvider(true, this );
0944:            }
0945:
0946:            protected void setInitialInput() {
0947:                // Use the selection, if any
0948:                ISelection selection = getSite().getPage().getSelection();
0949:                Object input = getSingleElementFromSelection(selection);
0950:                if (!(input instanceof  IJavaElement)) {
0951:                    // Use the input of the page
0952:                    input = getSite().getPage().getInput();
0953:                    if (!(input instanceof  IJavaElement)
0954:                            && input instanceof  IAdaptable)
0955:                        input = ((IAdaptable) input)
0956:                                .getAdapter(IJavaElement.class);
0957:                }
0958:                setInput(findInputForJavaElement((IJavaElement) input));
0959:            }
0960:
0961:            protected void setInitialSelection() {
0962:                // Use the selection, if any
0963:                Object input;
0964:                IWorkbenchPage page = getSite().getPage();
0965:                ISelection selection = null;
0966:                if (page != null)
0967:                    selection = page.getSelection();
0968:                if (selection instanceof  ITextSelection) {
0969:                    Object part = PlatformUI.getWorkbench()
0970:                            .getActiveWorkbenchWindow().getActivePage()
0971:                            .getActivePart();
0972:                    if (part instanceof  IEditorPart) {
0973:                        setSelectionFromEditor((IEditorPart) part);
0974:                        if (fViewer.getSelection() != null)
0975:                            return;
0976:                    }
0977:                }
0978:
0979:                // Use saved selection from memento
0980:                if (selection == null || selection.isEmpty())
0981:                    selection = restoreSelectionState(fMemento);
0982:
0983:                if (selection == null || selection.isEmpty()) {
0984:                    // Use the input of the page
0985:                    input = getSite().getPage().getInput();
0986:                    if (!(input instanceof  IJavaElement)) {
0987:                        if (input instanceof  IAdaptable)
0988:                            input = ((IAdaptable) input)
0989:                                    .getAdapter(IJavaElement.class);
0990:                        else
0991:                            return;
0992:                    }
0993:                    selection = new StructuredSelection(input);
0994:                }
0995:                selectionChanged(null, selection);
0996:            }
0997:
0998:            protected final void setHelp() {
0999:                JavaUIHelp.setHelp(fViewer, getHelpContextId());
1000:            }
1001:
1002:            /**
1003:             * Returns the context ID for the Help system
1004:             *
1005:             * @return	the string used as ID for the Help context
1006:             */
1007:            abstract protected String getHelpContextId();
1008:
1009:            /**
1010:             * Returns the preference key for the link to editor setting.
1011:             *
1012:             * @return	the string used as key into the preference store
1013:             */
1014:            abstract protected String getLinkToEditorKey();
1015:
1016:            /**
1017:             * Adds additional listeners to this view.
1018:             * This method can be overridden but should
1019:             * call super.
1020:             */
1021:            protected void hookViewerListeners() {
1022:                fViewer
1023:                        .addPostSelectionChangedListener(new ISelectionChangedListener() {
1024:                            public void selectionChanged(
1025:                                    SelectionChangedEvent event) {
1026:                                if (!fProcessSelectionEvents)
1027:                                    return;
1028:
1029:                                fPreviousSelectedElement = getSingleElementFromSelection(event
1030:                                        .getSelection());
1031:
1032:                                IWorkbenchPage page = getSite().getPage();
1033:                                if (page == null)
1034:                                    return;
1035:
1036:                                if (page.equals(JavaPlugin.getActivePage())
1037:                                        && JavaBrowsingPart.this .equals(page
1038:                                                .getActivePart())) {
1039:                                    linkToEditor((IStructuredSelection) event
1040:                                            .getSelection());
1041:                                }
1042:                            }
1043:                        });
1044:
1045:                fViewer.addOpenListener(new IOpenListener() {
1046:                    public void open(OpenEvent event) {
1047:                        IAction open = fOpenEditorGroup.getOpenAction();
1048:                        if (open.isEnabled()) {
1049:                            open.run();
1050:                            restoreSelection();
1051:                        }
1052:                    }
1053:                });
1054:            }
1055:
1056:            void restoreSelection() {
1057:                // Default is to do nothing
1058:            }
1059:
1060:            void adjustInputAndSetSelection(Object o) {
1061:                if (!(o instanceof  IJavaElement)) {
1062:                    if (o == null)
1063:                        setInput(null);
1064:                    setSelection(StructuredSelection.EMPTY, true);
1065:                    return;
1066:                }
1067:
1068:                IJavaElement je = (IJavaElement) o;
1069:                IJavaElement elementToSelect = findElementToSelect(je);
1070:                IJavaElement newInput = findInputForJavaElement(je);
1071:                IJavaElement oldInput = null;
1072:                if (getInput() instanceof  IJavaElement)
1073:                    oldInput = (IJavaElement) getInput();
1074:
1075:                if (elementToSelect == null && !isValidInput(newInput)
1076:                        && (newInput == null && !isAncestorOf(je, oldInput)))
1077:                    // Clear input
1078:                    setInput(null);
1079:                else if (mustSetNewInput(elementToSelect, oldInput, newInput)) {
1080:                    // Adjust input to selection
1081:                    setInput(newInput);
1082:                }
1083:
1084:                if (elementToSelect != null && elementToSelect.exists())
1085:                    setSelection(new StructuredSelection(elementToSelect), true);
1086:                else
1087:                    setSelection(StructuredSelection.EMPTY, true);
1088:            }
1089:
1090:            /**
1091:             * Compute if a new input must be set.
1092:             * 
1093:             * @param elementToSelect the element to select 
1094:             * @param oldInput old input
1095:             * @param newInput new input
1096:             * @return	<code>true</code> if the input has to be set
1097:             * @since 3.0
1098:             */
1099:            private boolean mustSetNewInput(IJavaElement elementToSelect,
1100:                    IJavaElement oldInput, IJavaElement newInput) {
1101:                return (newInput == null || !newInput.equals(oldInput))
1102:                        && (elementToSelect == null || oldInput == null || (!((elementToSelect instanceof  IPackageDeclaration)
1103:                                && (elementToSelect.getParent().equals(oldInput
1104:                                        .getParent())) && (!isAncestorOf(
1105:                                getViewPartInput(), elementToSelect)))));
1106:            }
1107:
1108:            /**
1109:             * Finds the closest Java element which can be used as input for
1110:             * this part and has the given Java element as child
1111:             *
1112:             * @param 	je 	the Java element for which to search the closest input
1113:             * @return	the closest Java element used as input for this part
1114:             */
1115:            protected IJavaElement findInputForJavaElement(IJavaElement je) {
1116:                if (je == null || !je.exists())
1117:                    return null;
1118:                if (isValidInput(je))
1119:                    return je;
1120:                return findInputForJavaElement(je.getParent());
1121:            }
1122:
1123:            protected final IJavaElement findElementToSelect(Object obj) {
1124:                if (obj instanceof  IJavaElement)
1125:                    return findElementToSelect((IJavaElement) obj);
1126:                return null;
1127:            }
1128:
1129:            /**
1130:             * Finds the element which has to be selected in this part.
1131:             *
1132:             * @param je	the Java element which has the focus
1133:             * @return returns the element to select
1134:             */
1135:            abstract protected IJavaElement findElementToSelect(IJavaElement je);
1136:
1137:            protected final Object getSingleElementFromSelection(
1138:                    ISelection selection) {
1139:                if (!(selection instanceof  IStructuredSelection)
1140:                        || selection.isEmpty())
1141:                    return null;
1142:
1143:                Iterator iter = ((IStructuredSelection) selection).iterator();
1144:                Object firstElement = iter.next();
1145:                if (!(firstElement instanceof  IJavaElement)) {
1146:                    if (firstElement instanceof  IMarker)
1147:                        firstElement = ((IMarker) firstElement).getResource();
1148:                    if (firstElement instanceof  IAdaptable) {
1149:                        IJavaElement je = (IJavaElement) ((IAdaptable) firstElement)
1150:                                .getAdapter(IJavaElement.class);
1151:                        if (je == null && firstElement instanceof  IFile) {
1152:                            IContainer parent = ((IFile) firstElement)
1153:                                    .getParent();
1154:                            if (parent != null)
1155:                                return parent.getAdapter(IJavaElement.class);
1156:                            else
1157:                                return null;
1158:                        } else
1159:                            return je;
1160:
1161:                    } else
1162:                        return firstElement;
1163:                }
1164:                Object currentInput = getViewer().getInput();
1165:                if (currentInput == null
1166:                        || !currentInput
1167:                                .equals(findInputForJavaElement((IJavaElement) firstElement)))
1168:                    if (iter.hasNext())
1169:                        // multi-selection and view is empty
1170:                        return null;
1171:                    else
1172:                        // OK: single selection and view is empty
1173:                        return firstElement;
1174:
1175:                // be nice to multi-selection
1176:                while (iter.hasNext()) {
1177:                    Object element = iter.next();
1178:                    if (!(element instanceof  IJavaElement))
1179:                        return null;
1180:                    if (!currentInput
1181:                            .equals(findInputForJavaElement((IJavaElement) element)))
1182:                        return null;
1183:                }
1184:                return firstElement;
1185:            }
1186:
1187:            /**
1188:             * Gets the typeComparator.
1189:             * @return Returns a JavaElementTypeComparator
1190:             */
1191:            protected Comparator getTypeComparator() {
1192:                return fTypeComparator;
1193:            }
1194:
1195:            /**
1196:             * Links to editor (if option enabled)
1197:             * @param selection the selection
1198:             */
1199:            private void linkToEditor(IStructuredSelection selection) {
1200:                Object obj = selection.getFirstElement();
1201:
1202:                if (selection.size() == 1) {
1203:                    IEditorPart part = EditorUtility.isOpenInEditor(obj);
1204:                    if (part != null) {
1205:                        IWorkbenchPage page = getSite().getPage();
1206:                        page.bringToTop(part);
1207:                        if (obj instanceof  IJavaElement)
1208:                            EditorUtility.revealInEditor(part,
1209:                                    (IJavaElement) obj);
1210:                    }
1211:                }
1212:            }
1213:
1214:            void setSelectionFromEditor(IWorkbenchPart part) {
1215:                if (!fProcessSelectionEvents
1216:                        || !linkBrowsingViewSelectionToEditor()
1217:                        || !(part instanceof  IEditorPart))
1218:                    return;
1219:
1220:                IWorkbenchPartSite site = part.getSite();
1221:                if (site == null)
1222:                    return;
1223:                ISelectionProvider provider = site.getSelectionProvider();
1224:                if (provider != null)
1225:                    setSelectionFromEditor(part, provider.getSelection());
1226:            }
1227:
1228:            private void setSelectionFromEditor(IWorkbenchPart part,
1229:                    ISelection selection) {
1230:                if (part instanceof  IEditorPart) {
1231:                    IJavaElement element = null;
1232:                    if (selection instanceof  IStructuredSelection) {
1233:                        Object obj = getSingleElementFromSelection(selection);
1234:                        if (obj instanceof  IJavaElement)
1235:                            element = (IJavaElement) obj;
1236:                    }
1237:                    IEditorInput ei = ((IEditorPart) part).getEditorInput();
1238:                    if (selection instanceof  ITextSelection) {
1239:                        int offset = ((ITextSelection) selection).getOffset();
1240:                        element = getElementAt(ei, offset);
1241:                    }
1242:                    if (element != null) {
1243:                        adjustInputAndSetSelection(element);
1244:                        return;
1245:                    }
1246:                    if (ei instanceof  IFileEditorInput) {
1247:                        IFile file = ((IFileEditorInput) ei).getFile();
1248:                        IJavaElement je = (IJavaElement) file
1249:                                .getAdapter(IJavaElement.class);
1250:                        if (je == null) {
1251:                            IContainer container = ((IFileEditorInput) ei)
1252:                                    .getFile().getParent();
1253:                            if (container != null)
1254:                                je = (IJavaElement) container
1255:                                        .getAdapter(IJavaElement.class);
1256:                        }
1257:                        if (je == null) {
1258:                            setSelection(null, false);
1259:                            return;
1260:                        }
1261:                        adjustInputAndSetSelection(je);
1262:                    } else if (ei instanceof  IClassFileEditorInput) {
1263:                        IClassFile cf = ((IClassFileEditorInput) ei)
1264:                                .getClassFile();
1265:                        adjustInputAndSetSelection(cf);
1266:                    }
1267:                }
1268:            }
1269:
1270:            /**
1271:             * Returns the element contained in the EditorInput
1272:             * @param input the editor input
1273:             * @return the input element
1274:             */
1275:            Object getElementOfInput(IEditorInput input) {
1276:                if (input instanceof  IFileEditorInput)
1277:                    return ((IFileEditorInput) input).getFile();
1278:                if (input != null)
1279:                    return JavaUI.getEditorInputJavaElement(input);
1280:                return null;
1281:            }
1282:
1283:            void setSelection(ISelection selection, boolean reveal) {
1284:                if (selection != null
1285:                        && selection.equals(fViewer.getSelection()))
1286:                    return;
1287:                fProcessSelectionEvents = false;
1288:                fViewer.setSelection(selection, reveal);
1289:                fProcessSelectionEvents = true;
1290:            }
1291:
1292:            /**
1293:             * @param input the editor input
1294:             * @param offset the offset in the file
1295:             * @return the element at the given offset
1296:             */
1297:            protected IJavaElement getElementAt(IEditorInput input, int offset) {
1298:                if (input instanceof  IClassFileEditorInput) {
1299:                    try {
1300:                        return ((IClassFileEditorInput) input).getClassFile()
1301:                                .getElementAt(offset);
1302:                    } catch (JavaModelException ex) {
1303:                        return null;
1304:                    }
1305:                }
1306:
1307:                IWorkingCopyManager manager = JavaPlugin.getDefault()
1308:                        .getWorkingCopyManager();
1309:                ICompilationUnit unit = manager.getWorkingCopy(input);
1310:                if (unit != null)
1311:                    try {
1312:                        if (unit.isConsistent())
1313:                            return unit.getElementAt(offset);
1314:                        else {
1315:                            /*
1316:                             * XXX: We should set the selection later when the
1317:                             *      CU is reconciled.
1318:                             *      see https://bugs.eclipse.org/bugs/show_bug.cgi?id=51290
1319:                             */
1320:                        }
1321:                    } catch (JavaModelException ex) {
1322:                        // fall through
1323:                    }
1324:                return null;
1325:            }
1326:
1327:            protected IType getTypeForCU(ICompilationUnit cu) {
1328:                // Use primary type if possible
1329:                IType primaryType = cu.findPrimaryType();
1330:                if (primaryType != null)
1331:                    return primaryType;
1332:
1333:                // Use first top-level type
1334:                try {
1335:                    IType[] types = cu.getTypes();
1336:                    if (types.length > 0)
1337:                        return types[0];
1338:                    else
1339:                        return null;
1340:                } catch (JavaModelException ex) {
1341:                    return null;
1342:                }
1343:            }
1344:
1345:            void setProcessSelectionEvents(boolean state) {
1346:                fProcessSelectionEvents = state;
1347:            }
1348:
1349:            /* (non-Javadoc)
1350:             * @see org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
1351:             */
1352:            public Object getViewPartInput() {
1353:                if (fViewer != null) {
1354:                    return fViewer.getInput();
1355:                }
1356:                return null;
1357:            }
1358:
1359:            protected void setActionGroups(CompositeActionGroup actionGroups) {
1360:                fActionGroups = actionGroups;
1361:            }
1362:
1363:            protected void setBuildActionGroup(BuildActionGroup actionGroup) {
1364:                fBuildActionGroup = actionGroup;
1365:            }
1366:
1367:            protected void setCCPActionGroup(CCPActionGroup actionGroup) {
1368:                fCCPActionGroup = actionGroup;
1369:            }
1370:
1371:            protected void setCustomFiltersActionGroup(
1372:                    CustomFiltersActionGroup customFiltersActionGroup) {
1373:                fCustomFiltersActionGroup = customFiltersActionGroup;
1374:            }
1375:
1376:            protected boolean hasCustomFilter() {
1377:                return fHasCustomFilter;
1378:            }
1379:
1380:            protected boolean hasWorkingSetFilter() {
1381:                return fHasWorkingSetFilter;
1382:            }
1383:
1384:            protected void setOpenEditorGroup(
1385:                    OpenEditorActionGroup openEditorGroup) {
1386:                fOpenEditorGroup = openEditorGroup;
1387:            }
1388:
1389:            protected OpenEditorActionGroup getOpenEditorGroup() {
1390:                return fOpenEditorGroup;
1391:            }
1392:
1393:            protected BuildActionGroup getBuildActionGroup() {
1394:                return fBuildActionGroup;
1395:            }
1396:
1397:            protected CCPActionGroup getCCPActionGroup() {
1398:                return fCCPActionGroup;
1399:            }
1400:
1401:            private boolean linkBrowsingViewSelectionToEditor() {
1402:                return isLinkingEnabled();
1403:            }
1404:
1405:            public void setLinkingEnabled(boolean enabled) {
1406:                fLinkingEnabled = enabled;
1407:                PreferenceConstants.getPreferenceStore().setValue(
1408:                        getLinkToEditorKey(), enabled);
1409:                if (enabled) {
1410:                    IEditorPart editor = getSite().getPage().getActiveEditor();
1411:                    if (editor != null) {
1412:                        setSelectionFromEditor(editor);
1413:                    }
1414:                }
1415:            }
1416:
1417:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.