Work with 2.1.1 now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / externaltools / internal / model / VariableContextManager.java
1 package net.sourceforge.phpdt.externaltools.internal.model;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
9
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.runtime.IAdaptable;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.ISelectionListener;
18 import org.eclipse.ui.ISelectionService;
19 import org.eclipse.ui.IWindowListener;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
26
27 /**
28  * Maintains the context used to expand variables. The context is based on
29  * the selected resource, unless a build is in progress - in which case
30  * the context is based on the project being built..
31  */
32 public class VariableContextManager implements IWindowListener, ISelectionListener {
33
34         // singleton
35         private static VariableContextManager fgDefault;
36         
37         private IResource fSelectedResource = null;
38         
39         private boolean fBuilding = false;
40         private IProject fProject = null;
41         private int fKind;
42         
43         private VariableContextManager() {
44                 IWorkbench workbench = PlatformUI.getWorkbench();
45                 if (workbench != null) { //may be running headless
46                         workbench.addWindowListener(this);
47                         IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
48                         if (activeWindow != null) {
49                                 windowActivated(activeWindow);
50                         }
51                 } 
52         }
53         
54         /**
55          * Returns the singleton resource selection manager
56          * 
57          * @return VariableContextManager
58          */
59         public static VariableContextManager getDefault() {
60                 if (fgDefault == null) {
61                         fgDefault = new VariableContextManager(); 
62                 }
63                 return fgDefault;
64         }
65         
66         /**
67          * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
68          */
69         public void windowActivated(IWorkbenchWindow window) {
70                 fSelectedResource = null;
71                 ISelectionService service = window.getSelectionService(); 
72                 service.addSelectionListener(this);
73                 IWorkbenchPage page = window.getActivePage();
74                 if (page != null) {
75                         IWorkbenchPart part = page.getActivePart();
76                         if (part != null) {                             
77                                 ISelection selection = service.getSelection();
78                                 if (selection != null) {
79                                         selectionChanged(part, selection);
80                                 }
81                         }
82                 }
83         }
84
85         /**
86          * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
87          */
88         public void windowClosed(IWorkbenchWindow window) {
89                 window.getSelectionService().removeSelectionListener(this);
90         }
91
92         /**
93          * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
94          */
95         public void windowDeactivated(IWorkbenchWindow window) {
96                 window.getSelectionService().removeSelectionListener(this);
97         }
98
99         /**
100          * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
101          */
102         public void windowOpened(IWorkbenchWindow window) {
103         }
104
105         /**
106          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
107          */
108         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
109                 IResource selectedResource = null;
110                 if (selection instanceof IStructuredSelection) {
111                         Object result = ((IStructuredSelection)selection).getFirstElement();
112                         if (result instanceof IResource) {
113                                 selectedResource = (IResource) result;
114                         } else if (result instanceof IAdaptable) {
115                                 selectedResource = (IResource)((IAdaptable) result).getAdapter(IResource.class);
116                         }
117                 }
118                 
119                 if (selectedResource == null) {
120                         // If the active part is an editor, get the file resource used as input.
121                         if (part instanceof IEditorPart) {
122                                 IEditorPart editorPart = (IEditorPart) part;
123                                 IEditorInput input = editorPart.getEditorInput();
124                                 selectedResource = (IResource) input.getAdapter(IResource.class);
125                         } 
126                 }
127                 
128                 fSelectedResource = selectedResource;
129         }
130         
131         /**
132          * Returns the active variable context. The context is that of the selected
133          * resource, or a project being built.
134          * 
135          * @return variable context
136          */
137         public ExpandVariableContext getVariableContext() {
138                 if (fBuilding) {
139                         return new ExpandVariableContext(fProject, fKind);
140                 } else {
141                         return new ExpandVariableContext(fSelectedResource);
142                 }
143         }
144         
145         /**
146          * Notification that the given project is being built.
147          * 
148          * @param project
149          * @param kind
150          * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
151          */
152         public void buildStarted(IProject project, int kind) {
153                 fBuilding = true;
154                 fProject = project;
155                 fKind = kind;
156         }
157         
158         /**
159          * Notification the building the current project has completed.
160          * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
161          */
162         public void buildEnded() {
163                 fBuilding = false;
164                 fProject= null;
165         }
166 }