1 package net.sourceforge.phpdt.externaltools.internal.model;
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 **********************************************************************/
10 //import java.util.Map;
12 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
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;
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..
37 public class VariableContextManager implements IWindowListener,
41 private static VariableContextManager fgDefault;
43 private IResource fSelectedResource = null;
45 private boolean fBuilding = false;
47 private IProject fProject = null;
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);
64 * Returns the singleton resource selection manager
66 * @return VariableContextManager
68 public static VariableContextManager getDefault() {
69 if (fgDefault == null) {
70 fgDefault = new VariableContextManager();
76 * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
78 public void windowActivated(IWorkbenchWindow window) {
79 fSelectedResource = null;
80 ISelectionService service = window.getSelectionService();
81 service.addSelectionListener(this);
82 IWorkbenchPage page = window.getActivePage();
84 IWorkbenchPart part = page.getActivePart();
86 ISelection selection = service.getSelection();
87 if (selection != null) {
88 selectionChanged(part, selection);
95 * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
97 public void windowClosed(IWorkbenchWindow window) {
98 window.getSelectionService().removeSelectionListener(this);
102 * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
104 public void windowDeactivated(IWorkbenchWindow window) {
105 window.getSelectionService().removeSelectionListener(this);
109 * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
111 public void windowOpened(IWorkbenchWindow window) {
115 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
116 * org.eclipse.jface.viewers.ISelection)
118 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
119 IResource selectedResource = null;
120 if (selection instanceof IStructuredSelection) {
121 Object result = ((IStructuredSelection) selection)
123 if (result instanceof IResource) {
124 selectedResource = (IResource) result;
125 } else if (result instanceof IAdaptable) {
126 selectedResource = (IResource) ((IAdaptable) result)
127 .getAdapter(IResource.class);
131 if (selectedResource == null) {
132 // If the active part is an editor, get the file resource used as
134 if (part instanceof IEditorPart) {
135 IEditorPart editorPart = (IEditorPart) part;
136 IEditorInput input = editorPart.getEditorInput();
137 selectedResource = (IResource) input
138 .getAdapter(IResource.class);
142 fSelectedResource = selectedResource;
146 * Returns the active variable context. The context is that of the selected
147 * resource, or a project being built.
149 * @return variable context
151 public ExpandVariableContext getVariableContext() {
153 return new ExpandVariableContext(fProject, fKind);
155 return new ExpandVariableContext(fSelectedResource);
160 * Notification that the given project is being built.
164 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
166 // public void buildStarted(IProject project, int kind) {
168 // fProject = project;
173 * Notification the building the current project has completed.
175 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
177 // public void buildEnded() {
178 // fBuilding = false;