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