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