1 package net.sourceforge.phpeclipse.resourcesview;
3 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
5 import org.eclipse.core.resources.IContainer;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.runtime.IAdaptable;
8 import org.eclipse.jface.action.IMenuListener;
9 import org.eclipse.jface.action.IMenuManager;
10 import org.eclipse.jface.action.MenuManager;
11 import org.eclipse.jface.viewers.IOpenListener;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.viewers.OpenEvent;
15 import org.eclipse.jface.viewers.TreeViewer;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.ui.IWorkbenchPartSite;
19 import org.eclipse.ui.actions.ActionContext;
20 import org.eclipse.ui.model.WorkbenchContentProvider;
21 import org.eclipse.ui.model.WorkbenchLabelProvider;
22 import org.eclipse.ui.part.ISetSelectionTarget;
23 import org.eclipse.ui.part.ViewPart;
25 public class PHPResourcesView extends ViewPart implements ISetSelectionTarget, IMenuListener {
26 protected TreeViewer viewer;
27 protected MainActionGroup mainActionGroup;
29 public PHPResourcesView() {
33 public void createPartControl(Composite parent) {
34 this.setViewer(new TreeViewer(parent));
36 this.getViewer().addFilter(new PHPViewerFilter());
37 this.getViewer().setContentProvider(new WorkbenchContentProvider());
38 this.getViewer().setLabelProvider(new WorkbenchLabelProvider());
39 this.getViewer().setInput(getInitialInput());
41 this.getViewer().addOpenListener(new IOpenListener() {
42 public void open(OpenEvent event) {
43 getMainActionGroup().runDefaultAction((IStructuredSelection) event.getSelection());
47 MenuManager menuMgr = new MenuManager("net.sourceforge.phpeclipse.PHPPopupMenu"); //$NON-NLS-1$
48 menuMgr.setRemoveAllWhenShown(true);
49 menuMgr.addMenuListener(this);
51 Menu fContextMenu = menuMgr.createContextMenu(this.getViewer().getTree());
52 this.getViewer().getTree().setMenu(fContextMenu);
54 // Register viewer with site. This must be done before making the actions.
55 IWorkbenchPartSite site = getSite();
56 site.registerContextMenu(menuMgr, viewer);
59 public void selectReveal(ISelection selection) {
60 viewer.setSelection(selection, true);
63 protected IContainer getInitialInput() {
64 IAdaptable input = getSite().getPage().getInput();
65 IResource resource = null;
66 if (input instanceof IResource) {
67 resource = (IResource) input;
69 resource = (IResource) input.getAdapter(IResource.class);
71 if (resource != null) {
72 switch (resource.getType()) {
74 return resource.getParent();
75 case IResource.FOLDER :
76 case IResource.PROJECT :
78 return (IContainer) resource;
83 return PHPeclipsePlugin.getWorkspace().getRoot();
86 public void setFocus() {
87 this.getViewer().getControl().setFocus();
90 public void menuAboutToShow(IMenuManager manager) {
91 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
92 this.getMainActionGroup().setContext(new ActionContext(selection));
93 this.getMainActionGroup().fillContextMenu(manager);
94 this.getMainActionGroup().setContext(null);
97 public MainActionGroup getMainActionGroup() {
98 if (mainActionGroup == null) {
99 // lazy initialization, because this.getSite() delivers null in constructor
100 mainActionGroup = new MainActionGroup(this);
102 return mainActionGroup;
105 public void setMainActionGroup(MainActionGroup mainActionGroup) {
106 this.mainActionGroup = mainActionGroup;
109 public TreeViewer getViewer() {
113 protected void setViewer(TreeViewer viewer) {
114 this.viewer = viewer;