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