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 **********************************************************************/
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
34 * the selected resource, unless a build is in progress - in which case
35 * the context is based on the project being built..
37 public class VariableContextManager implements IWindowListener, ISelectionListener {
40 private static VariableContextManager fgDefault;
42 private IResource fSelectedResource = null;
44 private boolean fBuilding = false;
45 private IProject fProject = null;
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);
60 * Returns the singleton resource selection manager
62 * @return VariableContextManager
64 public static VariableContextManager getDefault() {
65 if (fgDefault == null) {
66 fgDefault = new VariableContextManager();
72 * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
74 public void windowActivated(IWorkbenchWindow window) {
75 fSelectedResource = null;
76 ISelectionService service = window.getSelectionService();
77 service.addSelectionListener(this);
78 IWorkbenchPage page = window.getActivePage();
80 IWorkbenchPart part = page.getActivePart();
82 ISelection selection = service.getSelection();
83 if (selection != null) {
84 selectionChanged(part, selection);
91 * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
93 public void windowClosed(IWorkbenchWindow window) {
94 window.getSelectionService().removeSelectionListener(this);
98 * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
100 public void windowDeactivated(IWorkbenchWindow window) {
101 window.getSelectionService().removeSelectionListener(this);
105 * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
107 public void windowOpened(IWorkbenchWindow window) {
111 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
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);
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);
133 fSelectedResource = selectedResource;
137 * Returns the active variable context. The context is that of the selected
138 * resource, or a project being built.
140 * @return variable context
142 public ExpandVariableContext getVariableContext() {
144 return new ExpandVariableContext(fProject, fKind);
146 return new ExpandVariableContext(fSelectedResource);
151 * Notification that the given project is being built.
155 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
157 public void buildStarted(IProject project, int kind) {
164 * Notification the building the current project has completed.
165 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
167 public void buildEnded() {