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