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