Source Code Cross Referenced for ResourceNavigator.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » views » navigator » 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 » ui ide » org.eclipse.ui.views.navigator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Copyright (c) 2000, 2006 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:         *     Benjamin Muskalla - bug 105041
0011:         *******************************************************************************/package org.eclipse.ui.views.navigator;
0012:
0013:        import java.util.ArrayList;
0014:        import java.util.Arrays;
0015:        import java.util.Iterator;
0016:        import java.util.List;
0017:
0018:        import org.eclipse.core.resources.IContainer;
0019:        import org.eclipse.core.resources.IFile;
0020:        import org.eclipse.core.resources.IMarker;
0021:        import org.eclipse.core.resources.IResource;
0022:        import org.eclipse.core.resources.IWorkspace;
0023:        import org.eclipse.core.resources.ResourcesPlugin;
0024:        import org.eclipse.core.runtime.IAdaptable;
0025:        import org.eclipse.core.runtime.IPath;
0026:        import org.eclipse.jface.action.IMenuListener;
0027:        import org.eclipse.jface.action.IMenuManager;
0028:        import org.eclipse.jface.action.MenuManager;
0029:        import org.eclipse.jface.dialogs.IDialogSettings;
0030:        import org.eclipse.jface.preference.IPreferenceStore;
0031:        import org.eclipse.jface.util.IPropertyChangeListener;
0032:        import org.eclipse.jface.util.PropertyChangeEvent;
0033:        import org.eclipse.jface.viewers.DecoratingLabelProvider;
0034:        import org.eclipse.jface.viewers.DoubleClickEvent;
0035:        import org.eclipse.jface.viewers.IDoubleClickListener;
0036:        import org.eclipse.jface.viewers.ILabelDecorator;
0037:        import org.eclipse.jface.viewers.ILabelProvider;
0038:        import org.eclipse.jface.viewers.IOpenListener;
0039:        import org.eclipse.jface.viewers.ISelection;
0040:        import org.eclipse.jface.viewers.ISelectionChangedListener;
0041:        import org.eclipse.jface.viewers.IStructuredSelection;
0042:        import org.eclipse.jface.viewers.OpenEvent;
0043:        import org.eclipse.jface.viewers.SelectionChangedEvent;
0044:        import org.eclipse.jface.viewers.StructuredSelection;
0045:        import org.eclipse.jface.viewers.TreeViewer;
0046:        import org.eclipse.jface.viewers.ViewerComparator;
0047:        import org.eclipse.jface.viewers.ViewerSorter;
0048:        import org.eclipse.osgi.util.NLS;
0049:        import org.eclipse.swt.SWT;
0050:        import org.eclipse.swt.dnd.DND;
0051:        import org.eclipse.swt.dnd.FileTransfer;
0052:        import org.eclipse.swt.dnd.Transfer;
0053:        import org.eclipse.swt.events.KeyEvent;
0054:        import org.eclipse.swt.events.KeyListener;
0055:        import org.eclipse.swt.widgets.Composite;
0056:        import org.eclipse.swt.widgets.Control;
0057:        import org.eclipse.swt.widgets.Event;
0058:        import org.eclipse.swt.widgets.Listener;
0059:        import org.eclipse.swt.widgets.Menu;
0060:        import org.eclipse.swt.widgets.Shell;
0061:        import org.eclipse.ui.IEditorPart;
0062:        import org.eclipse.ui.IMemento;
0063:        import org.eclipse.ui.IPartListener;
0064:        import org.eclipse.ui.IViewSite;
0065:        import org.eclipse.ui.IWorkbenchPage;
0066:        import org.eclipse.ui.IWorkbenchPart;
0067:        import org.eclipse.ui.IWorkbenchPreferenceConstants;
0068:        import org.eclipse.ui.IWorkingSet;
0069:        import org.eclipse.ui.IWorkingSetManager;
0070:        import org.eclipse.ui.PartInitException;
0071:        import org.eclipse.ui.PlatformUI;
0072:        import org.eclipse.ui.ResourceWorkingSetFilter;
0073:        import org.eclipse.ui.actions.ActionContext;
0074:        import org.eclipse.ui.actions.OpenResourceAction;
0075:        import org.eclipse.ui.ide.ResourceUtil;
0076:        import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
0077:        import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
0078:        import org.eclipse.ui.model.WorkbenchContentProvider;
0079:        import org.eclipse.ui.model.WorkbenchLabelProvider;
0080:        import org.eclipse.ui.part.ISetSelectionTarget;
0081:        import org.eclipse.ui.part.IShowInSource;
0082:        import org.eclipse.ui.part.IShowInTarget;
0083:        import org.eclipse.ui.part.PluginTransfer;
0084:        import org.eclipse.ui.part.ResourceTransfer;
0085:        import org.eclipse.ui.part.ShowInContext;
0086:        import org.eclipse.ui.part.ViewPart;
0087:        import org.eclipse.ui.plugin.AbstractUIPlugin;
0088:        import org.eclipse.ui.views.framelist.FrameList;
0089:        import org.eclipse.ui.views.framelist.TreeFrame;
0090:
0091:        /**
0092:         * Implements the Resource Navigator view.
0093:         */
0094:        public class ResourceNavigator extends ViewPart implements 
0095:                ISetSelectionTarget, IResourceNavigator {
0096:
0097:            private TreeViewer viewer;
0098:
0099:            private IDialogSettings settings;
0100:
0101:            private IMemento memento;
0102:
0103:            private FrameList frameList;
0104:
0105:            private ResourceNavigatorActionGroup actionGroup;
0106:
0107:            private ResourcePatternFilter patternFilter = new ResourcePatternFilter();
0108:
0109:            private ResourceWorkingSetFilter workingSetFilter = new ResourceWorkingSetFilter();
0110:
0111:            private boolean linkingEnabled;
0112:
0113:            private boolean dragDetected;
0114:
0115:            private Listener dragDetectListener;
0116:            /**
0117:             * Remembered working set.
0118:             */
0119:            private IWorkingSet workingSet;
0120:
0121:            /**
0122:             * Marks whether the working set we're using is currently empty. In this
0123:             * event we're effectively not using a working set.
0124:             */
0125:            private boolean emptyWorkingSet = false;
0126:
0127:            /**
0128:             * Settings constant for section name (value <code>ResourceNavigator</code>).
0129:             */
0130:            private static final String STORE_SECTION = "ResourceNavigator"; //$NON-NLS-1$
0131:
0132:            /** 
0133:             * Settings constant for sort order (value <code>ResourceViewer.STORE_SORT_TYPE</code>).
0134:             */
0135:            private static final String STORE_SORT_TYPE = "ResourceViewer.STORE_SORT_TYPE"; //$NON-NLS-1$
0136:
0137:            /** 
0138:             * Settings constant for working set (value <code>ResourceWorkingSetFilter.STORE_WORKING_SET</code>).
0139:             */
0140:            private static final String STORE_WORKING_SET = "ResourceWorkingSetFilter.STORE_WORKING_SET"; //$NON-NLS-1$
0141:
0142:            /**
0143:             * @deprecated No longer used but preserved to avoid an api change.
0144:             */
0145:            public static final String NAVIGATOR_VIEW_HELP_ID = INavigatorHelpContextIds.RESOURCE_VIEW;
0146:
0147:            /**
0148:             * True iff we've already scheduled an asynchronous call to linkToEditor
0149:             */
0150:            private boolean linkScheduled = false;
0151:
0152:            // Persistance tags.
0153:            private static final String TAG_SORTER = "sorter"; //$NON-NLS-1$
0154:
0155:            private static final String TAG_FILTERS = "filters"; //$NON-NLS-1$
0156:
0157:            private static final String TAG_FILTER = "filter"; //$NON-NLS-1$
0158:
0159:            private static final String TAG_SELECTION = "selection"; //$NON-NLS-1$
0160:
0161:            private static final String TAG_EXPANDED = "expanded"; //$NON-NLS-1$
0162:
0163:            private static final String TAG_ELEMENT = "element"; //$NON-NLS-1$
0164:
0165:            private static final String TAG_IS_ENABLED = "isEnabled"; //$NON-NLS-1$
0166:
0167:            private static final String TAG_PATH = "path"; //$NON-NLS-1$
0168:
0169:            private static final String TAG_CURRENT_FRAME = "currentFrame"; //$NON-NLS-1$
0170:
0171:            private IPartListener partListener = new IPartListener() {
0172:                public void partActivated(IWorkbenchPart part) {
0173:                    if (part instanceof  IEditorPart) {
0174:                        editorActivated((IEditorPart) part);
0175:                    }
0176:                }
0177:
0178:                public void partBroughtToTop(IWorkbenchPart part) {
0179:                }
0180:
0181:                public void partClosed(IWorkbenchPart part) {
0182:                }
0183:
0184:                public void partDeactivated(IWorkbenchPart part) {
0185:                }
0186:
0187:                public void partOpened(IWorkbenchPart part) {
0188:                }
0189:            };
0190:
0191:            private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
0192:                public void propertyChange(PropertyChangeEvent event) {
0193:                    String property = event.getProperty();
0194:                    Object newValue = event.getNewValue();
0195:                    Object oldValue = event.getOldValue();
0196:
0197:                    if (IWorkingSetManager.CHANGE_WORKING_SET_REMOVE
0198:                            .equals(property)
0199:                            && oldValue == workingSet) {
0200:                        setWorkingSet(null);
0201:                    } else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE
0202:                            .equals(property)
0203:                            && newValue == workingSet) {
0204:                        updateTitle();
0205:                    } else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE
0206:                            .equals(property)
0207:                            && newValue == workingSet) {
0208:                        if (workingSet.isAggregateWorkingSet()
0209:                                && workingSet.isEmpty()) {
0210:                            // act as if the working set has been made null
0211:                            if (!emptyWorkingSet) {
0212:                                emptyWorkingSet = true;
0213:                                workingSetFilter.setWorkingSet(null);
0214:                            }
0215:                        } else {
0216:                            // we've gone from empty to non-empty on our set.
0217:                            // Restore it.
0218:                            if (emptyWorkingSet) {
0219:                                emptyWorkingSet = false;
0220:                                workingSetFilter.setWorkingSet(workingSet);
0221:                            }
0222:                        }
0223:                        getViewer().refresh();
0224:                    }
0225:                }
0226:            };
0227:
0228:            /**
0229:             * Constructs a new resource navigator view.
0230:             */
0231:            public ResourceNavigator() {
0232:                IDialogSettings viewsSettings = getPlugin().getDialogSettings();
0233:
0234:                settings = viewsSettings.getSection(STORE_SECTION);
0235:                if (settings == null) {
0236:                    settings = viewsSettings.addNewSection(STORE_SECTION);
0237:                }
0238:
0239:                initLinkingEnabled();
0240:            }
0241:
0242:            /**
0243:             * Converts the given selection into a form usable by the viewer,
0244:             * where the elements are resources.
0245:             */
0246:            private StructuredSelection convertSelection(ISelection selection) {
0247:                ArrayList list = new ArrayList();
0248:                if (selection instanceof  IStructuredSelection) {
0249:                    IStructuredSelection ssel = (IStructuredSelection) selection;
0250:                    for (Iterator i = ssel.iterator(); i.hasNext();) {
0251:                        Object o = i.next();
0252:                        IResource resource = null;
0253:                        if (o instanceof  IResource) {
0254:                            resource = (IResource) o;
0255:                        } else {
0256:                            if (o instanceof  IAdaptable) {
0257:                                resource = (IResource) ((IAdaptable) o)
0258:                                        .getAdapter(IResource.class);
0259:                            }
0260:                        }
0261:                        if (resource != null) {
0262:                            list.add(resource);
0263:                        }
0264:                    }
0265:                }
0266:                return new StructuredSelection(list);
0267:            }
0268:
0269:            /* (non-Javadoc)
0270:             * Method declared on IWorkbenchPart.
0271:             */
0272:            public void createPartControl(Composite parent) {
0273:                TreeViewer viewer = createViewer(parent);
0274:                this .viewer = viewer;
0275:
0276:                if (memento != null) {
0277:                    restoreFilters();
0278:                    restoreLinkingEnabled();
0279:                }
0280:                frameList = createFrameList();
0281:                initDragAndDrop();
0282:                updateTitle();
0283:
0284:                initContextMenu();
0285:
0286:                initResourceComparator();
0287:                initWorkingSetFilter();
0288:
0289:                // make sure input is set after sorters and filters,
0290:                // to avoid unnecessary refreshes
0291:                viewer.setInput(getInitialInput());
0292:
0293:                // make actions after setting input, because some actions
0294:                // look at the viewer for enablement (e.g. the Up action)
0295:                makeActions();
0296:
0297:                // Fill the action bars and update the global action handlers'
0298:                // enabled state to match the current selection.
0299:                getActionGroup().fillActionBars(getViewSite().getActionBars());
0300:                updateActionBars((IStructuredSelection) viewer.getSelection());
0301:
0302:                getSite().setSelectionProvider(viewer);
0303:                getSite().getPage().addPartListener(partListener);
0304:                IWorkingSetManager workingSetManager = getPlugin()
0305:                        .getWorkbench().getWorkingSetManager();
0306:                workingSetManager
0307:                        .addPropertyChangeListener(propertyChangeListener);
0308:
0309:                if (memento != null) {
0310:                    restoreState(memento);
0311:                }
0312:                memento = null;
0313:
0314:                // Set help for the view 
0315:                getSite().getWorkbenchWindow().getWorkbench().getHelpSystem()
0316:                        .setHelp(viewer.getControl(), getHelpContextId());
0317:            }
0318:
0319:            /**
0320:             * Returns the help context id to use for this view.
0321:             * 
0322:             * @since 2.0
0323:             */
0324:            protected String getHelpContextId() {
0325:                return INavigatorHelpContextIds.RESOURCE_VIEW;
0326:            }
0327:
0328:            /**
0329:             * Initializes and registers the context menu.
0330:             * 
0331:             * @since 2.0
0332:             */
0333:            protected void initContextMenu() {
0334:                MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
0335:                menuMgr.setRemoveAllWhenShown(true);
0336:                menuMgr.addMenuListener(new IMenuListener() {
0337:                    public void menuAboutToShow(IMenuManager manager) {
0338:                        ResourceNavigator.this .fillContextMenu(manager);
0339:                    }
0340:                });
0341:                TreeViewer viewer = getTreeViewer();
0342:                Menu menu = menuMgr.createContextMenu(viewer.getTree());
0343:                viewer.getTree().setMenu(menu);
0344:                getSite().registerContextMenu(menuMgr, viewer);
0345:            }
0346:
0347:            /**
0348:             * Creates the viewer.
0349:             * 
0350:             * @param parent the parent composite
0351:             * @since 2.0
0352:             */
0353:            protected TreeViewer createViewer(Composite parent) {
0354:                TreeViewer viewer = new TreeViewer(parent, SWT.MULTI
0355:                        | SWT.H_SCROLL | SWT.V_SCROLL);
0356:                viewer.setUseHashlookup(true);
0357:                initContentProvider(viewer);
0358:                initLabelProvider(viewer);
0359:                initFilters(viewer);
0360:                initListeners(viewer);
0361:
0362:                return viewer;
0363:            }
0364:
0365:            /**
0366:             * Sets the content provider for the viewer.
0367:             * 
0368:             * @param viewer the viewer
0369:             * @since 2.0
0370:             */
0371:            protected void initContentProvider(TreeViewer viewer) {
0372:                viewer.setContentProvider(new WorkbenchContentProvider());
0373:            }
0374:
0375:            /**
0376:             * Sets the label provider for the viewer.
0377:             * 
0378:             * @param viewer the viewer
0379:             * @since 2.0
0380:             */
0381:            protected void initLabelProvider(TreeViewer viewer) {
0382:                viewer.setLabelProvider(new DecoratingLabelProvider(
0383:                        new WorkbenchLabelProvider(), getPlugin()
0384:                                .getWorkbench().getDecoratorManager()
0385:                                .getLabelDecorator()));
0386:            }
0387:
0388:            /**
0389:             * Adds the filters to the viewer.
0390:             * 
0391:             * @param viewer the viewer
0392:             * @since 2.0
0393:             */
0394:            protected void initFilters(TreeViewer viewer) {
0395:                viewer.addFilter(patternFilter);
0396:                viewer.addFilter(workingSetFilter);
0397:            }
0398:
0399:            /**
0400:             * Initializes the linking enabled setting from the preference store.
0401:             */
0402:            private void initLinkingEnabled() {
0403:                // Try the dialog settings first, which remember the last choice.
0404:                String setting = settings
0405:                        .get(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR);
0406:                if (setting != null) {
0407:                    linkingEnabled = setting.equals("true"); //$NON-NLS-1$
0408:                    return;
0409:                }
0410:                // If not in the dialog settings, check the preference store for the default setting. 
0411:                // Use the UI plugin's preference store since this is a public preference.
0412:                linkingEnabled = PlatformUI.getPreferenceStore().getBoolean(
0413:                        IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR);
0414:            }
0415:
0416:            /**
0417:             * Adds the listeners to the viewer.
0418:             * 
0419:             * @param viewer the viewer
0420:             * @since 2.0
0421:             */
0422:            protected void initListeners(TreeViewer viewer) {
0423:                viewer
0424:                        .addSelectionChangedListener(new ISelectionChangedListener() {
0425:                            public void selectionChanged(
0426:                                    SelectionChangedEvent event) {
0427:                                handleSelectionChanged(event);
0428:                            }
0429:                        });
0430:                viewer.addDoubleClickListener(new IDoubleClickListener() {
0431:                    public void doubleClick(DoubleClickEvent event) {
0432:                        handleDoubleClick(event);
0433:                    }
0434:                });
0435:                viewer.addOpenListener(new IOpenListener() {
0436:                    public void open(OpenEvent event) {
0437:                        handleOpen(event);
0438:                    }
0439:                });
0440:                viewer.getControl().addKeyListener(new KeyListener() {
0441:                    public void keyPressed(KeyEvent event) {
0442:                        handleKeyPressed(event);
0443:                    }
0444:
0445:                    public void keyReleased(KeyEvent event) {
0446:                        handleKeyReleased(event);
0447:                    }
0448:                });
0449:            }
0450:
0451:            /* (non-Javadoc)
0452:             * Method declared on IWorkbenchPart.
0453:             */
0454:            public void dispose() {
0455:                getSite().getPage().removePartListener(partListener);
0456:
0457:                IWorkingSetManager workingSetManager = getPlugin()
0458:                        .getWorkbench().getWorkingSetManager();
0459:                workingSetManager
0460:                        .removePropertyChangeListener(propertyChangeListener);
0461:
0462:                if (getActionGroup() != null) {
0463:                    getActionGroup().dispose();
0464:                }
0465:                Control control = viewer.getControl();
0466:                if (dragDetectListener != null && control != null
0467:                        && control.isDisposed() == false) {
0468:                    control.removeListener(SWT.DragDetect, dragDetectListener);
0469:                }
0470:                super .dispose();
0471:            }
0472:
0473:            /**
0474:             * An editor has been activated.  Sets the selection in this navigator
0475:             * to be the editor's input, if linking is enabled.
0476:             * 
0477:             * @param editor the active editor
0478:             * @since 2.0
0479:             */
0480:            protected void editorActivated(IEditorPart editor) {
0481:                if (!isLinkingEnabled()) {
0482:                    return;
0483:                }
0484:
0485:                IFile file = ResourceUtil.getFile(editor.getEditorInput());
0486:                if (file != null) {
0487:                    ISelection newSelection = new StructuredSelection(file);
0488:                    if (getTreeViewer().getSelection().equals(newSelection)) {
0489:                        getTreeViewer().getTree().showSelection();
0490:                    } else {
0491:                        getTreeViewer().setSelection(newSelection, true);
0492:                    }
0493:                }
0494:            }
0495:
0496:            /**
0497:             * Called when the context menu is about to open.
0498:             * Delegates to the action group using the viewer's selection as the action context.
0499:             * @since 2.0
0500:             */
0501:            protected void fillContextMenu(IMenuManager menu) {
0502:                IStructuredSelection selection = (IStructuredSelection) getViewer()
0503:                        .getSelection();
0504:                getActionGroup().setContext(new ActionContext(selection));
0505:                getActionGroup().fillContextMenu(menu);
0506:            }
0507:
0508:            /*
0509:             * @see IResourceNavigatorPart
0510:             * @since 2.0
0511:             */
0512:            public FrameList getFrameList() {
0513:                return frameList;
0514:            }
0515:
0516:            /** 
0517:             * Returns the initial input for the viewer.
0518:             * Tries to convert the page input to a resource, either directly or via IAdaptable.
0519:             * If the resource is a container, it uses that.
0520:             * If the resource is a file, it uses its parent folder.
0521:             * If a resource could not be obtained, it uses the workspace root.
0522:             * 
0523:             * @since 2.0
0524:             */
0525:            protected IAdaptable getInitialInput() {
0526:                IAdaptable input = getSite().getPage().getInput();
0527:                if (input != null) {
0528:                    IResource resource = null;
0529:                    if (input instanceof  IResource) {
0530:                        resource = (IResource) input;
0531:                    } else {
0532:                        resource = (IResource) input
0533:                                .getAdapter(IResource.class);
0534:                    }
0535:                    if (resource != null) {
0536:                        switch (resource.getType()) {
0537:                        case IResource.FILE:
0538:                            return resource.getParent();
0539:                        case IResource.FOLDER:
0540:                        case IResource.PROJECT:
0541:                        case IResource.ROOT:
0542:                            return resource;
0543:                        default:
0544:                            // Unknown resource type.  Fall through.
0545:                            break;
0546:                        }
0547:                    }
0548:                }
0549:                return ResourcesPlugin.getWorkspace().getRoot();
0550:            }
0551:
0552:            /**
0553:             * Returns the pattern filter for this view.
0554:             *
0555:             * @return the pattern filter
0556:             * @since 2.0
0557:             */
0558:            public ResourcePatternFilter getPatternFilter() {
0559:                return this .patternFilter;
0560:            }
0561:
0562:            /**
0563:             * Returns the working set for this view.
0564:             *
0565:             * @return the working set
0566:             * @since 2.0
0567:             */
0568:            public IWorkingSet getWorkingSet() {
0569:                return workingSetFilter.getWorkingSet();
0570:            }
0571:
0572:            /**
0573:             * Returns the navigator's plugin.
0574:             * @return the UI plugin for this bundle
0575:             */
0576:            public AbstractUIPlugin getPlugin() {
0577:                return IDEWorkbenchPlugin.getDefault();
0578:            }
0579:
0580:            /**
0581:             * Return the sorter.
0582:             * 
0583:             * @since 2.0
0584:             * @deprecated as of 3.3, use {@link ResourceNavigator#getComparator()}
0585:             */
0586:            public ResourceSorter getSorter() {
0587:                return (ResourceSorter) getTreeViewer().getSorter();
0588:            }
0589:
0590:            /**
0591:             * Returns the comparator.
0592:             * 
0593:             * @return the <code>ResourceComparator</code>
0594:             * @since 3.3
0595:             */
0596:
0597:            public ResourceComparator getComparator() {
0598:                return (ResourceComparator) getTreeViewer().getComparator();
0599:            }
0600:
0601:            /**
0602:             * Returns the resource viewer which shows the resource hierarchy.
0603:             * @since 2.0
0604:             */
0605:            public TreeViewer getViewer() {
0606:                return viewer;
0607:            }
0608:
0609:            /**
0610:             * Returns the tree viewer which shows the resource hierarchy.
0611:             * @return the tree viewer
0612:             * @since 2.0
0613:             */
0614:            public TreeViewer getTreeViewer() {
0615:                return viewer;
0616:            }
0617:
0618:            /**
0619:             * Returns the shell to use for opening dialogs.
0620:             * Used in this class, and in the actions.
0621:             * 
0622:             * @return the shell
0623:             * @deprecated use getViewSite().getShell()
0624:             */
0625:            public Shell getShell() {
0626:                return getViewSite().getShell();
0627:            }
0628:
0629:            /**
0630:             * Returns the message to show in the status line.
0631:             *
0632:             * @param selection the current selection
0633:             * @return the status line message
0634:             * @since 2.0
0635:             */
0636:            protected String getStatusLineMessage(IStructuredSelection selection) {
0637:                if (selection.size() == 1) {
0638:                    Object o = selection.getFirstElement();
0639:                    if (o instanceof  IResource) {
0640:                        return ((IResource) o).getFullPath().makeRelative()
0641:                                .toString();
0642:                    }
0643:                    return ResourceNavigatorMessages.ResourceNavigator_oneItemSelected;
0644:                }
0645:                if (selection.size() > 1) {
0646:                    return NLS
0647:                            .bind(
0648:                                    ResourceNavigatorMessages.ResourceNavigator_statusLine,
0649:                                    String.valueOf(selection.size()));
0650:                }
0651:                return ""; //$NON-NLS-1$
0652:            }
0653:
0654:            /**
0655:             * Returns the name for the given element.
0656:             * Used as the name for the current frame. 
0657:             */
0658:            String getFrameName(Object element) {
0659:                if (element instanceof  IResource) {
0660:                    return ((IResource) element).getName();
0661:                }
0662:                String text = ((ILabelProvider) getTreeViewer()
0663:                        .getLabelProvider()).getText(element);
0664:                if (text == null) {
0665:                    return "";//$NON-NLS-1$
0666:                }
0667:                return text;
0668:            }
0669:
0670:            /**
0671:             * Returns the tool tip text for the given element.
0672:             * Used as the tool tip text for the current frame, and for the view title tooltip.
0673:             */
0674:            String getFrameToolTipText(Object element) {
0675:                if (element instanceof  IResource) {
0676:                    IPath path = ((IResource) element).getFullPath();
0677:                    if (path.isRoot()) {
0678:                        return ResourceNavigatorMessages.ResourceManager_toolTip;
0679:                    }
0680:                    return path.makeRelative().toString();
0681:                }
0682:
0683:                String text = ((ILabelProvider) getTreeViewer()
0684:                        .getLabelProvider()).getText(element);
0685:                if (text == null) {
0686:                    return "";//$NON-NLS-1$
0687:                }
0688:                return text;
0689:            }
0690:
0691:            /**
0692:             * Handles an open event from the viewer.
0693:             * Opens an editor on the selected file.
0694:             * 
0695:             * @param event the open event
0696:             * @since 2.0
0697:             */
0698:            protected void handleOpen(OpenEvent event) {
0699:                IStructuredSelection selection = (IStructuredSelection) event
0700:                        .getSelection();
0701:                getActionGroup().runDefaultAction(selection);
0702:            }
0703:
0704:            /**
0705:             * Handles a double-click event from the viewer.
0706:             * Expands or collapses a folder when double-clicked.
0707:             * 
0708:             * @param event the double-click event
0709:             * @since 2.0
0710:             */
0711:            protected void handleDoubleClick(DoubleClickEvent event) {
0712:                IStructuredSelection selection = (IStructuredSelection) event
0713:                        .getSelection();
0714:                Object element = selection.getFirstElement();
0715:
0716:                // 1GBZIA0: ITPUI:WIN2000 - Double-clicking in navigator should expand/collapse containers
0717:                TreeViewer viewer = getTreeViewer();
0718:                if (viewer.isExpandable(element)) {
0719:                    viewer.setExpandedState(element, !viewer
0720:                            .getExpandedState(element));
0721:                } else if (selection.size() == 1
0722:                        && (element instanceof  IResource)
0723:                        && ((IResource) element).getType() == IResource.PROJECT) {
0724:                    OpenResourceAction ora = new OpenResourceAction(getSite()
0725:                            .getShell());
0726:                    ora.selectionChanged((IStructuredSelection) viewer
0727:                            .getSelection());
0728:                    if (ora.isEnabled()) {
0729:                        ora.run();
0730:                    }
0731:                }
0732:
0733:            }
0734:
0735:            /**
0736:             * Handles a selection changed event from the viewer.
0737:             * Updates the status line and the action bars, and links to editor (if option enabled).
0738:             * 
0739:             * @param event the selection event
0740:             * @since 2.0
0741:             */
0742:            protected void handleSelectionChanged(SelectionChangedEvent event) {
0743:                final IStructuredSelection sel = (IStructuredSelection) event
0744:                        .getSelection();
0745:                updateStatusLine(sel);
0746:                updateActionBars(sel);
0747:                dragDetected = false;
0748:                if (isLinkingEnabled() && !linkScheduled) {
0749:                    // Ensure that if another selection change arrives while we're waiting for the *syncExec,
0750:                    // we only do this work once.
0751:                    linkScheduled = true;
0752:                    getShell().getDisplay().asyncExec(new Runnable() {
0753:                        public void run() {
0754:                            // There's no telling what might have changed since the syncExec was scheduled.
0755:                            // Check to make sure that the widgets haven't been disposed.
0756:                            linkScheduled = false;
0757:
0758:                            if (viewer == null || viewer.getControl() == null
0759:                                    || viewer.getControl().isDisposed()) {
0760:                                return;
0761:                            }
0762:
0763:                            if (dragDetected == false) {
0764:                                ISelection sel = viewer.getSelection();
0765:                                if (sel instanceof  IStructuredSelection) {
0766:                                    IStructuredSelection selection = (IStructuredSelection) sel;
0767:
0768:                                    // only synchronize with editor when the selection is not the result 
0769:                                    // of a drag. Fixes bug 22274.
0770:                                    linkToEditor(selection);
0771:                                }
0772:                            }
0773:                        }
0774:                    });
0775:                }
0776:            }
0777:
0778:            /**
0779:             * Handles a key press event from the viewer.
0780:             * Delegates to the action group.
0781:             * 
0782:             * @param event the key event
0783:             * @since 2.0
0784:             */
0785:            protected void handleKeyPressed(KeyEvent event) {
0786:                getActionGroup().handleKeyPressed(event);
0787:            }
0788:
0789:            /**
0790:             * Handles a key release in the viewer.  Does nothing by default.
0791:             * 
0792:             * @param event the key event
0793:             * @since 2.0
0794:             */
0795:            protected void handleKeyReleased(KeyEvent event) {
0796:            }
0797:
0798:            /* (non-Javadoc)
0799:             * Method declared on IViewPart.
0800:             */
0801:            public void init(IViewSite site, IMemento memento)
0802:                    throws PartInitException {
0803:                super .init(site, memento);
0804:                this .memento = memento;
0805:            }
0806:
0807:            /**
0808:             * Adds drag and drop support to the navigator.
0809:             * 
0810:             * @since 2.0
0811:             */
0812:            protected void initDragAndDrop() {
0813:                int ops = DND.DROP_COPY | DND.DROP_MOVE;
0814:                Transfer[] transfers = new Transfer[] {
0815:                        LocalSelectionTransfer.getInstance(),
0816:                        ResourceTransfer.getInstance(),
0817:                        FileTransfer.getInstance(),
0818:                        PluginTransfer.getInstance() };
0819:                TreeViewer viewer = getTreeViewer();
0820:                viewer.addDragSupport(ops, transfers, new NavigatorDragAdapter(
0821:                        viewer));
0822:                NavigatorDropAdapter adapter = new NavigatorDropAdapter(viewer);
0823:                adapter.setFeedbackEnabled(false);
0824:                viewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers,
0825:                        adapter);
0826:                dragDetectListener = new Listener() {
0827:                    public void handleEvent(Event event) {
0828:                        dragDetected = true;
0829:                    }
0830:                };
0831:                viewer.getControl().addListener(SWT.DragDetect,
0832:                        dragDetectListener);
0833:            }
0834:
0835:            /**
0836:             * Creates the frame source and frame list, and connects them.
0837:             * 
0838:             * @since 2.0
0839:             */
0840:            protected FrameList createFrameList() {
0841:                NavigatorFrameSource frameSource = new NavigatorFrameSource(
0842:                        this );
0843:                FrameList frameList = new FrameList(frameSource);
0844:                frameSource.connectTo(frameList);
0845:                return frameList;
0846:            }
0847:
0848:            /**
0849:             * Initializes the sorter.
0850:             * 
0851:             * @deprecated as of 3.3, use {@link ResourceNavigator#initResourceComparator()} instead
0852:             */
0853:            protected void initResourceSorter() {
0854:                int sortType = ResourceSorter.NAME;
0855:                try {
0856:                    int sortInt = 0;
0857:                    if (memento != null) {
0858:                        String sortStr = memento.getString(TAG_SORTER);
0859:                        if (sortStr != null) {
0860:                            sortInt = new Integer(sortStr).intValue();
0861:                        }
0862:                    } else {
0863:                        sortInt = settings.getInt(STORE_SORT_TYPE);
0864:                    }
0865:                    if (sortInt == ResourceSorter.NAME
0866:                            || sortInt == ResourceSorter.TYPE) {
0867:                        sortType = sortInt;
0868:                    }
0869:                } catch (NumberFormatException e) {
0870:                }
0871:                setSorter(new ResourceSorter(sortType));
0872:            }
0873:
0874:            /**
0875:             * Initializes the comparator.
0876:             * @since 3.3
0877:             */
0878:            protected void initResourceComparator() {
0879:                int sortType = ResourceComparator.NAME;
0880:                try {
0881:                    int sortInt = 0;
0882:                    if (memento != null) {
0883:                        String sortStr = memento.getString(TAG_SORTER);
0884:                        if (sortStr != null) {
0885:                            sortInt = new Integer(sortStr).intValue();
0886:                        }
0887:                    } else {
0888:                        sortInt = settings.getInt(STORE_SORT_TYPE);
0889:                    }
0890:                    if (sortInt == ResourceComparator.NAME
0891:                            || sortInt == ResourceComparator.TYPE) {
0892:                        sortType = sortInt;
0893:                    }
0894:                } catch (NumberFormatException e) {
0895:                }
0896:                setComparator(new ResourceComparator(sortType));
0897:            }
0898:
0899:            /**
0900:             * Restores the working set filter from the persistence store.
0901:             */
0902:            protected void initWorkingSetFilter() {
0903:                String workingSetName = settings.get(STORE_WORKING_SET);
0904:
0905:                IWorkingSet workingSet = null;
0906:
0907:                if (workingSetName != null
0908:                        && workingSetName.equals("") == false) { //$NON-NLS-1$
0909:                    IWorkingSetManager workingSetManager = getPlugin()
0910:                            .getWorkbench().getWorkingSetManager();
0911:                    workingSet = workingSetManager
0912:                            .getWorkingSet(workingSetName);
0913:                } else if (PlatformUI
0914:                        .getPreferenceStore()
0915:                        .getBoolean(
0916:                                IWorkbenchPreferenceConstants.USE_WINDOW_WORKING_SET_BY_DEFAULT)) {
0917:                    // use the window set by default if the global preference is set
0918:                    workingSet = getSite().getPage().getAggregateWorkingSet();
0919:                }
0920:
0921:                if (workingSet != null) {
0922:                    // Only initialize filter. Don't set working set into viewer.
0923:                    // Working set is set via WorkingSetFilterActionGroup
0924:                    // during action creation.
0925:                    workingSetFilter.setWorkingSet(workingSet);
0926:                    internalSetWorkingSet(workingSet);
0927:                }
0928:            }
0929:
0930:            /**
0931:             * Returns whether the navigator selection automatically tracks the active
0932:             * editor.
0933:             * 
0934:             * @return <code>true</code> if linking is enabled, <code>false</code>
0935:             *         if not
0936:             * @since 2.0 (this was protected in 2.0, but was made public in 2.1)
0937:             */
0938:            public boolean isLinkingEnabled() {
0939:                return linkingEnabled;
0940:            }
0941:
0942:            /**
0943:             * Brings the corresponding editor to top if the selected resource is open.
0944:             * 
0945:             * @since 2.0
0946:             */
0947:            protected void linkToEditor(IStructuredSelection selection) {
0948:
0949:                if (this  != this .getSite().getPage().getActivePart())
0950:                    return;
0951:
0952:                Object obj = selection.getFirstElement();
0953:                if (obj instanceof  IFile && selection.size() == 1) {
0954:                    IFile file = (IFile) obj;
0955:                    IWorkbenchPage page = getSite().getPage();
0956:                    IEditorPart editor = ResourceUtil.findEditor(page, file);
0957:                    if (editor != null) {
0958:                        page.bringToTop(editor);
0959:                        return;
0960:                    }
0961:                }
0962:            }
0963:
0964:            /**
0965:             * Creates the action group, which encapsulates all actions for the view.
0966:             */
0967:            protected void makeActions() {
0968:                setActionGroup(new MainActionGroup(this ));
0969:            }
0970:
0971:            /**
0972:             * Restores the saved filter settings.
0973:             */
0974:            private void restoreFilters() {
0975:                IMemento filtersMem = memento.getChild(TAG_FILTERS);
0976:
0977:                if (filtersMem != null) { //filters have been defined
0978:                    IMemento children[] = filtersMem.getChildren(TAG_FILTER);
0979:
0980:                    // check if first element has new tag defined, indicates new version
0981:                    if (children.length > 0
0982:                            && children[0].getString(TAG_IS_ENABLED) != null) {
0983:                        ArrayList selectedFilters = new ArrayList();
0984:                        ArrayList unSelectedFilters = new ArrayList();
0985:                        for (int i = 0; i < children.length; i++) {
0986:                            if (children[i].getString(TAG_IS_ENABLED).equals(
0987:                                    String.valueOf(true))) {
0988:                                selectedFilters.add(children[i]
0989:                                        .getString(TAG_ELEMENT));
0990:                            } else {
0991:                                //enabled == false
0992:                                unSelectedFilters.add(children[i]
0993:                                        .getString(TAG_ELEMENT));
0994:                            }
0995:                        }
0996:
0997:                        /* merge filters from Memento with selected = true filters from plugins
0998:                         * ensure there are no duplicates & don't override user preferences	 */
0999:                        List pluginFilters = FiltersContentProvider
1000:                                .getDefaultFilters();
1001:                        for (Iterator iter = pluginFilters.iterator(); iter
1002:                                .hasNext();) {
1003:                            String element = (String) iter.next();
1004:                            if (!selectedFilters.contains(element)
1005:                                    && !unSelectedFilters.contains(element)) {
1006:                                selectedFilters.add(element);
1007:                            }
1008:                        }
1009:
1010:                        //Convert to an array of Strings
1011:                        String[] patternArray = new String[selectedFilters
1012:                                .size()];
1013:                        selectedFilters.toArray(patternArray);
1014:                        getPatternFilter().setPatterns(patternArray);
1015:
1016:                    } else { //filters defined, old version: ignore filters from plugins
1017:                        String filters[] = new String[children.length];
1018:                        for (int i = 0; i < children.length; i++) {
1019:                            filters[i] = children[i].getString(TAG_ELEMENT);
1020:                        }
1021:                        getPatternFilter().setPatterns(filters);
1022:                    }
1023:                } else { //no filters defined, old version: ignore filters from plugins
1024:                    getPatternFilter().setPatterns(new String[0]);
1025:                }
1026:            }
1027:
1028:            /**
1029:             * Restores the state of the receiver to the state described in the specified memento.
1030:             *
1031:             * @param memento the memento
1032:             * @since 2.0
1033:             */
1034:            protected void restoreState(IMemento memento) {
1035:                TreeViewer viewer = getTreeViewer();
1036:                IMemento frameMemento = memento.getChild(TAG_CURRENT_FRAME);
1037:
1038:                if (frameMemento != null) {
1039:                    TreeFrame frame = new TreeFrame(viewer);
1040:                    frame.restoreState(frameMemento);
1041:                    frame.setName(getFrameName(frame.getInput()));
1042:                    frame.setToolTipText(getFrameToolTipText(frame.getInput()));
1043:                    viewer.setSelection(new StructuredSelection(frame
1044:                            .getInput()));
1045:                    frameList.gotoFrame(frame);
1046:                } else {
1047:                    IContainer container = ResourcesPlugin.getWorkspace()
1048:                            .getRoot();
1049:                    IMemento childMem = memento.getChild(TAG_EXPANDED);
1050:                    if (childMem != null) {
1051:                        ArrayList elements = new ArrayList();
1052:                        IMemento[] elementMem = childMem
1053:                                .getChildren(TAG_ELEMENT);
1054:                        for (int i = 0; i < elementMem.length; i++) {
1055:                            Object element = container.findMember(elementMem[i]
1056:                                    .getString(TAG_PATH));
1057:                            if (element != null) {
1058:                                elements.add(element);
1059:                            }
1060:                        }
1061:                        viewer.setExpandedElements(elements.toArray());
1062:                    }
1063:                    childMem = memento.getChild(TAG_SELECTION);
1064:                    if (childMem != null) {
1065:                        ArrayList list = new ArrayList();
1066:                        IMemento[] elementMem = childMem
1067:                                .getChildren(TAG_ELEMENT);
1068:                        for (int i = 0; i < elementMem.length; i++) {
1069:                            Object element = container.findMember(elementMem[i]
1070:                                    .getString(TAG_PATH));
1071:                            if (element != null) {
1072:                                list.add(element);
1073:                            }
1074:                        }
1075:                        viewer.setSelection(new StructuredSelection(list));
1076:                    }
1077:                }
1078:            }
1079:
1080:            /**
1081:             * Restores the linking enabled state.
1082:             */
1083:            private void restoreLinkingEnabled() {
1084:                Integer val = memento
1085:                        .getInteger(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR);
1086:                if (val != null) {
1087:                    linkingEnabled = val.intValue() != 0;
1088:                }
1089:            }
1090:
1091:            /**	
1092:             * @see ViewPart#saveState
1093:             */
1094:            public void saveState(IMemento memento) {
1095:                TreeViewer viewer = getTreeViewer();
1096:                if (viewer == null) {
1097:                    if (this .memento != null) {
1098:                        memento.putMemento(this .memento);
1099:                    }
1100:                    return;
1101:                }
1102:
1103:                //save sorter
1104:                memento.putInteger(TAG_SORTER, getComparator().getCriteria());
1105:
1106:                //save filters
1107:                String filters[] = getPatternFilter().getPatterns();
1108:                List selectedFilters = Arrays.asList(filters);
1109:                List allFilters = FiltersContentProvider.getDefinedFilters();
1110:                IMemento filtersMem = memento.createChild(TAG_FILTERS);
1111:                for (Iterator iter = allFilters.iterator(); iter.hasNext();) {
1112:                    String element = (String) iter.next();
1113:                    IMemento child = filtersMem.createChild(TAG_FILTER);
1114:                    child.putString(TAG_ELEMENT, element);
1115:                    child.putString(TAG_IS_ENABLED, String
1116:                            .valueOf(selectedFilters.contains(element)));
1117:                }
1118:
1119:                if (frameList.getCurrentIndex() > 0) {
1120:                    //save frame, it's not the "home"/workspace frame
1121:                    TreeFrame currentFrame = (TreeFrame) frameList
1122:                            .getCurrentFrame();
1123:                    IMemento frameMemento = memento
1124:                            .createChild(TAG_CURRENT_FRAME);
1125:                    currentFrame.saveState(frameMemento);
1126:                } else {
1127:                    //save visible expanded elements
1128:                    Object expandedElements[] = viewer
1129:                            .getVisibleExpandedElements();
1130:                    if (expandedElements.length > 0) {
1131:                        IMemento expandedMem = memento
1132:                                .createChild(TAG_EXPANDED);
1133:                        for (int i = 0; i < expandedElements.length; i++) {
1134:                            if (expandedElements[i] instanceof  IResource) {
1135:                                IMemento elementMem = expandedMem
1136:                                        .createChild(TAG_ELEMENT);
1137:                                elementMem.putString(TAG_PATH,
1138:                                        ((IResource) expandedElements[i])
1139:                                                .getFullPath().toString());
1140:                            }
1141:                        }
1142:                    }
1143:                    //save selection
1144:                    Object elements[] = ((IStructuredSelection) viewer
1145:                            .getSelection()).toArray();
1146:                    if (elements.length > 0) {
1147:                        IMemento selectionMem = memento
1148:                                .createChild(TAG_SELECTION);
1149:                        for (int i = 0; i < elements.length; i++) {
1150:                            if (elements[i] instanceof  IResource) {
1151:                                IMemento elementMem = selectionMem
1152:                                        .createChild(TAG_ELEMENT);
1153:                                elementMem.putString(TAG_PATH,
1154:                                        ((IResource) elements[i]).getFullPath()
1155:                                                .toString());
1156:                            }
1157:                        }
1158:                    }
1159:                }
1160:
1161:                saveLinkingEnabled(memento);
1162:            }
1163:
1164:            /**
1165:             * Saves the linking enabled state.
1166:             */
1167:            private void saveLinkingEnabled(IMemento memento) {
1168:                memento.putInteger(
1169:                        IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR,
1170:                        linkingEnabled ? 1 : 0);
1171:            }
1172:
1173:            /**
1174:             * Selects and reveals the specified elements.
1175:             */
1176:            public void selectReveal(ISelection selection) {
1177:                StructuredSelection ssel = convertSelection(selection);
1178:                if (!ssel.isEmpty()) {
1179:                    getViewer().getControl().setRedraw(false);
1180:                    getViewer().setSelection(ssel, true);
1181:                    getViewer().getControl().setRedraw(true);
1182:                }
1183:            }
1184:
1185:            /**
1186:             * Saves the filters defined as strings in <code>patterns</code> 
1187:             * in the preference store.
1188:             */
1189:            public void setFiltersPreference(String[] patterns) {
1190:
1191:                StringBuffer sb = new StringBuffer();
1192:
1193:                for (int i = 0; i < patterns.length; i++) {
1194:                    if (i != 0) {
1195:                        sb.append(ResourcePatternFilter.COMMA_SEPARATOR);
1196:                    }
1197:                    sb.append(patterns[i]);
1198:                }
1199:
1200:                getPlugin().getPreferenceStore().setValue(
1201:                        ResourcePatternFilter.FILTERS_TAG, sb.toString());
1202:
1203:                // remove value in old workbench preference store location 
1204:                IPreferenceStore preferenceStore = IDEWorkbenchPlugin
1205:                        .getDefault().getPreferenceStore();
1206:                String storedPatterns = preferenceStore
1207:                        .getString(ResourcePatternFilter.FILTERS_TAG);
1208:                if (storedPatterns.length() > 0) {
1209:                    preferenceStore.setValue(ResourcePatternFilter.FILTERS_TAG,
1210:                            ""); //$NON-NLS-1$
1211:                }
1212:            }
1213:
1214:            /**
1215:             * @see IWorkbenchPart#setFocus()
1216:             */
1217:            public void setFocus() {
1218:                getTreeViewer().getTree().setFocus();
1219:            }
1220:
1221:            /**
1222:             * Note: For experimental use only.
1223:             * Sets the decorator for the navigator.
1224:             * <p>
1225:             * As of 2.0, this method no longer has any effect.
1226:             * </p>
1227:             *
1228:             * @param decorator a label decorator or <code>null</code> for no decorations.
1229:             * @deprecated use the decorators extension point instead; see IWorkbench.getDecoratorManager()
1230:             */
1231:            public void setLabelDecorator(ILabelDecorator decorator) {
1232:                // do nothing
1233:            }
1234:
1235:            /**
1236:             * @see IResourceNavigator#setLinkingEnabled(boolean)
1237:             * @since 2.1
1238:             */
1239:            public void setLinkingEnabled(boolean enabled) {
1240:                this .linkingEnabled = enabled;
1241:
1242:                // remember the last setting in the dialog settings		
1243:                settings.put(
1244:                        IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR,
1245:                        enabled);
1246:
1247:                // if turning linking on, update the selection to correspond to the active editor
1248:                if (enabled) {
1249:                    IEditorPart editor = getSite().getPage().getActiveEditor();
1250:                    if (editor != null) {
1251:                        editorActivated(editor);
1252:                    }
1253:                }
1254:            }
1255:
1256:            /**
1257:             * Sets the resource sorter.
1258:             * 
1259:             * @param sorter the resource sorter
1260:             * @since 2.0
1261:             * @deprecated as of 3.3, use {@link ResourceNavigator#setComparator(ResourceComparator)}
1262:             */
1263:            public void setSorter(ResourceSorter sorter) {
1264:                TreeViewer viewer = getTreeViewer();
1265:                ViewerSorter viewerSorter = viewer.getSorter();
1266:
1267:                viewer.getControl().setRedraw(false);
1268:                if (viewerSorter == sorter) {
1269:                    viewer.refresh();
1270:                } else {
1271:                    viewer.setSorter(sorter);
1272:                }
1273:                viewer.getControl().setRedraw(true);
1274:                settings.put(STORE_SORT_TYPE, sorter.getCriteria());
1275:
1276:                // update the sort actions' checked state
1277:                updateActionBars((IStructuredSelection) viewer.getSelection());
1278:            }
1279:
1280:            /**
1281:             * Sets the resource comparator
1282:             * 
1283:             * @param comparator the resource comparator
1284:             * @since 3.3
1285:             */
1286:            public void setComparator(ResourceComparator comparator) {
1287:                TreeViewer viewer = getTreeViewer();
1288:                ViewerComparator viewerComparator = viewer.getComparator();
1289:
1290:                viewer.getControl().setRedraw(false);
1291:                if (viewerComparator == comparator) {
1292:                    viewer.refresh();
1293:                } else {
1294:                    viewer.setComparator(comparator);
1295:                }
1296:                viewer.getControl().setRedraw(true);
1297:                settings.put(STORE_SORT_TYPE, comparator.getCriteria());
1298:
1299:                // update the sort actions' checked state
1300:                updateActionBars((IStructuredSelection) viewer.getSelection());
1301:            }
1302:
1303:            /*
1304:             * @see org.eclipse.ui.views.navigator.IResourceNavigatorPart#setWorkingSet(IWorkingSet)
1305:             * @since 2.0
1306:             */
1307:            public void setWorkingSet(IWorkingSet workingSet) {
1308:                TreeViewer treeViewer = getTreeViewer();
1309:                Object[] expanded = treeViewer.getExpandedElements();
1310:                ISelection selection = treeViewer.getSelection();
1311:
1312:                internalSetWorkingSet(workingSet);
1313:
1314:                workingSetFilter.setWorkingSet(emptyWorkingSet ? null
1315:                        : workingSet);
1316:                if (workingSet != null) {
1317:                    settings.put(STORE_WORKING_SET, workingSet.getName());
1318:                } else {
1319:                    settings.put(STORE_WORKING_SET, ""); //$NON-NLS-1$
1320:                }
1321:                updateTitle();
1322:                treeViewer.refresh();
1323:                treeViewer.setExpandedElements(expanded);
1324:                if (selection.isEmpty() == false
1325:                        && selection instanceof  IStructuredSelection) {
1326:                    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
1327:                    treeViewer.reveal(structuredSelection.getFirstElement());
1328:                }
1329:            }
1330:
1331:            /**
1332:             * Set the internal working set fields specific to the navigator.
1333:             * 
1334:             * @param workingSet
1335:             *            the new working set
1336:             * @since 3.2
1337:             */
1338:            private void internalSetWorkingSet(IWorkingSet workingSet) {
1339:                this .workingSet = workingSet;
1340:                emptyWorkingSet = workingSet != null
1341:                        && workingSet.isAggregateWorkingSet()
1342:                        && workingSet.isEmpty();
1343:            }
1344:
1345:            /**
1346:             * Updates the action bar actions.
1347:             * 
1348:             * @param selection the current selection
1349:             * @since 2.0
1350:             */
1351:            protected void updateActionBars(IStructuredSelection selection) {
1352:                ResourceNavigatorActionGroup group = getActionGroup();
1353:                if (group != null) {
1354:                    group.setContext(new ActionContext(selection));
1355:                    group.updateActionBars();
1356:                }
1357:            }
1358:
1359:            /**
1360:             * Updates the message shown in the status line.
1361:             *
1362:             * @param selection the current selection
1363:             */
1364:            protected void updateStatusLine(IStructuredSelection selection) {
1365:                String msg = getStatusLineMessage(selection);
1366:                getViewSite().getActionBars().getStatusLineManager()
1367:                        .setMessage(msg);
1368:            }
1369:
1370:            /**
1371:             * Updates the title text and title tool tip.
1372:             * Called whenever the input of the viewer changes.
1373:             * Called whenever the input of the viewer changes.
1374:             * 
1375:             * @since 2.0
1376:             */
1377:            public void updateTitle() {
1378:                Object input = getViewer().getInput();
1379:                IWorkspace workspace = ResourcesPlugin.getWorkspace();
1380:                IWorkingSet workingSet = workingSetFilter.getWorkingSet();
1381:
1382:                if (input == null || input.equals(workspace)
1383:                        || input.equals(workspace.getRoot())) {
1384:                    setContentDescription(""); //$NON-NLS-1$
1385:                    if (workingSet != null) {
1386:                        setTitleToolTip(NLS
1387:                                .bind(
1388:                                        ResourceNavigatorMessages.ResourceNavigator_workingSetToolTip,
1389:                                        workingSet.getLabel()));
1390:                    } else {
1391:                        setTitleToolTip(""); //$NON-NLS-1$
1392:                    }
1393:                } else {
1394:                    ILabelProvider labelProvider = (ILabelProvider) getTreeViewer()
1395:                            .getLabelProvider();
1396:                    String inputToolTip = getFrameToolTipText(input);
1397:                    String text = labelProvider.getText(input);
1398:                    if (text != null) {
1399:                        setContentDescription(text);
1400:                    }
1401:                    if (workingSet != null) {
1402:                        setTitleToolTip(NLS
1403:                                .bind(
1404:                                        ResourceNavigatorMessages.ResourceNavigator_workingSetInputToolTip,
1405:                                        inputToolTip, workingSet.getLabel()));
1406:                    } else {
1407:                        setTitleToolTip(inputToolTip);
1408:                    }
1409:                }
1410:            }
1411:
1412:            /**
1413:             * Returns the action group.
1414:             * 
1415:             * @return the action group
1416:             */
1417:            protected ResourceNavigatorActionGroup getActionGroup() {
1418:                return actionGroup;
1419:            }
1420:
1421:            /**
1422:             * Sets the action group.
1423:             * 
1424:             * @param actionGroup the action group
1425:             */
1426:            protected void setActionGroup(
1427:                    ResourceNavigatorActionGroup actionGroup) {
1428:                this .actionGroup = actionGroup;
1429:            }
1430:
1431:            /*
1432:             * @see IWorkbenchPart#getAdapter(Class)
1433:             */
1434:            public Object getAdapter(Class adapter) {
1435:                if (adapter == IShowInSource.class) {
1436:                    return getShowInSource();
1437:                }
1438:                if (adapter == IShowInTarget.class) {
1439:                    return getShowInTarget();
1440:                }
1441:                return null;
1442:            }
1443:
1444:            /**
1445:             * Returns the <code>IShowInSource</code> for this view.
1446:             */
1447:            protected IShowInSource getShowInSource() {
1448:                return new IShowInSource() {
1449:                    public ShowInContext getShowInContext() {
1450:                        return new ShowInContext(getViewer().getInput(),
1451:                                getViewer().getSelection());
1452:                    }
1453:                };
1454:            }
1455:
1456:            /**
1457:             * Returns the <code>IShowInTarget</code> for this view.
1458:             */
1459:            protected IShowInTarget getShowInTarget() {
1460:                return new IShowInTarget() {
1461:                    public boolean show(ShowInContext context) {
1462:                        ArrayList toSelect = new ArrayList();
1463:                        ISelection sel = context.getSelection();
1464:                        if (sel instanceof  IStructuredSelection) {
1465:                            IStructuredSelection ssel = (IStructuredSelection) sel;
1466:                            for (Iterator i = ssel.iterator(); i.hasNext();) {
1467:                                Object o = i.next();
1468:                                if (o instanceof  IResource) {
1469:                                    toSelect.add(o);
1470:                                } else if (o instanceof  IMarker) {
1471:                                    IResource r = ((IMarker) o).getResource();
1472:                                    if (r.getType() != IResource.ROOT) {
1473:                                        toSelect.add(r);
1474:                                    }
1475:                                } else if (o instanceof  IAdaptable) {
1476:                                    IAdaptable adaptable = (IAdaptable) o;
1477:                                    o = adaptable.getAdapter(IResource.class);
1478:                                    if (o instanceof  IResource) {
1479:                                        toSelect.add(o);
1480:                                    } else {
1481:                                        o = adaptable.getAdapter(IMarker.class);
1482:                                        if (o instanceof  IMarker) {
1483:                                            IResource r = ((IMarker) o)
1484:                                                    .getResource();
1485:                                            if (r.getType() != IResource.ROOT) {
1486:                                                toSelect.add(r);
1487:                                            }
1488:                                        }
1489:                                    }
1490:                                }
1491:                            }
1492:                        }
1493:                        if (toSelect.isEmpty()) {
1494:                            Object input = context.getInput();
1495:                            if (input instanceof  IAdaptable) {
1496:                                IAdaptable adaptable = (IAdaptable) input;
1497:                                Object o = adaptable
1498:                                        .getAdapter(IResource.class);
1499:                                if (o instanceof  IResource) {
1500:                                    toSelect.add(o);
1501:                                }
1502:                            }
1503:                        }
1504:                        if (!toSelect.isEmpty()) {
1505:                            selectReveal(new StructuredSelection(toSelect));
1506:                            return true;
1507:                        }
1508:                        return false;
1509:                    }
1510:                };
1511:            }
1512:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.