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;
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..
36 public class VariableContextManager implements IWindowListener,
40 private static VariableContextManager fgDefault;
42 private IResource fSelectedResource = null;
44 private boolean fBuilding = false;
46 private IProject fProject = null;
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);
63 * Returns the singleton resource selection manager
65 * @return VariableContextManager
67 public static VariableContextManager getDefault() {
68 if (fgDefault == null) {
69 fgDefault = new VariableContextManager();
75 * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
77 public void windowActivated(IWorkbenchWindow window) {
78 fSelectedResource = null;
79 ISelectionService service = window.getSelectionService();
80 service.addSelectionListener(this);
81 IWorkbenchPage page = window.getActivePage();
83 IWorkbenchPart part = page.getActivePart();
85 ISelection selection = service.getSelection();
86 if (selection != null) {
87 selectionChanged(part, selection);
94 * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
96 public void windowClosed(IWorkbenchWindow window) {
97 window.getSelectionService().removeSelectionListener(this);
101 * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
103 public void windowDeactivated(IWorkbenchWindow window) {
104 window.getSelectionService().removeSelectionListener(this);
108 * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
110 public void windowOpened(IWorkbenchWindow window) {
114 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
115 * org.eclipse.jface.viewers.ISelection)
117 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
118 IResource selectedResource = null;
119 if (selection instanceof IStructuredSelection) {
120 Object result = ((IStructuredSelection) selection)
122 if (result instanceof IResource) {
123 selectedResource = (IResource) result;
124 } else if (result instanceof IAdaptable) {
125 selectedResource = (IResource) ((IAdaptable) result)
126 .getAdapter(IResource.class);
130 if (selectedResource == null) {
131 // If the active part is an editor, get the file resource used as
133 if (part instanceof IEditorPart) {
134 IEditorPart editorPart = (IEditorPart) part;
135 IEditorInput input = editorPart.getEditorInput();
136 selectedResource = (IResource) input
137 .getAdapter(IResource.class);
141 fSelectedResource = selectedResource;
145 * Returns the active variable context. The context is that of the selected
146 * resource, or a project being built.
148 * @return variable context
150 public ExpandVariableContext getVariableContext() {
152 return new ExpandVariableContext(fProject, fKind);
154 return new ExpandVariableContext(fSelectedResource);
159 * Notification that the given project is being built.
163 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
165 public void buildStarted(IProject project, int kind) {
172 * Notification the building the current project has completed.
174 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
176 public void buildEnded() {