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 org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.runtime.IAdaptable;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.ISelectionListener;
18 import org.eclipse.ui.ISelectionService;
19 import org.eclipse.ui.IWindowListener;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
28 * Maintains the context used to expand variables. The context is based on
29 * the selected resource, unless a build is in progress - in which case
30 * the context is based on the project being built..
32 public class VariableContextManager implements IWindowListener, ISelectionListener {
35 private static VariableContextManager fgDefault;
37 private IResource fSelectedResource = null;
39 private boolean fBuilding = false;
40 private IProject fProject = null;
43 private VariableContextManager() {
44 IWorkbench workbench = PlatformUI.getWorkbench();
45 if (workbench != null) { //may be running headless
46 workbench.addWindowListener(this);
47 IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
48 if (activeWindow != null) {
49 windowActivated(activeWindow);
55 * Returns the singleton resource selection manager
57 * @return VariableContextManager
59 public static VariableContextManager getDefault() {
60 if (fgDefault == null) {
61 fgDefault = new VariableContextManager();
67 * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
69 public void windowActivated(IWorkbenchWindow window) {
70 fSelectedResource = null;
71 ISelectionService service = window.getSelectionService();
72 service.addSelectionListener(this);
73 IWorkbenchPage page = window.getActivePage();
75 IWorkbenchPart part = page.getActivePart();
77 ISelection selection = service.getSelection();
78 if (selection != null) {
79 selectionChanged(part, selection);
86 * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
88 public void windowClosed(IWorkbenchWindow window) {
89 window.getSelectionService().removeSelectionListener(this);
93 * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
95 public void windowDeactivated(IWorkbenchWindow window) {
96 window.getSelectionService().removeSelectionListener(this);
100 * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
102 public void windowOpened(IWorkbenchWindow window) {
106 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
108 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
109 IResource selectedResource = null;
110 if (selection instanceof IStructuredSelection) {
111 Object result = ((IStructuredSelection)selection).getFirstElement();
112 if (result instanceof IResource) {
113 selectedResource = (IResource) result;
114 } else if (result instanceof IAdaptable) {
115 selectedResource = (IResource)((IAdaptable) result).getAdapter(IResource.class);
119 if (selectedResource == null) {
120 // If the active part is an editor, get the file resource used as input.
121 if (part instanceof IEditorPart) {
122 IEditorPart editorPart = (IEditorPart) part;
123 IEditorInput input = editorPart.getEditorInput();
124 selectedResource = (IResource) input.getAdapter(IResource.class);
128 fSelectedResource = selectedResource;
132 * Returns the active variable context. The context is that of the selected
133 * resource, or a project being built.
135 * @return variable context
137 public ExpandVariableContext getVariableContext() {
139 return new ExpandVariableContext(fProject, fKind);
141 return new ExpandVariableContext(fSelectedResource);
146 * Notification that the given project is being built.
150 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
152 public void buildStarted(IProject project, int kind) {
159 * Notification the building the current project has completed.
160 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
162 public void buildEnded() {