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 net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.ISelectionListener;
20 import org.eclipse.ui.ISelectionService;
21 import org.eclipse.ui.IWindowListener;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
29 * Maintains the context used to expand variables. The context is based on
30 * the selected resource, unless a build is in progress - in which case
31 * the context is based on the project being built..
33 public class VariableContextManager implements IWindowListener, ISelectionListener {
36 private static VariableContextManager fgDefault;
38 private IResource fSelectedResource = null;
40 private boolean fBuilding = false;
41 private IProject fProject = null;
44 private VariableContextManager() {
45 IWorkbench workbench = PlatformUI.getWorkbench();
46 if (workbench != null) { //may be running headless
47 workbench.addWindowListener(this);
48 IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
49 if (activeWindow != null) {
50 windowActivated(activeWindow);
56 * Returns the singleton resource selection manager
58 * @return VariableContextManager
60 public static VariableContextManager getDefault() {
61 if (fgDefault == null) {
62 fgDefault = new VariableContextManager();
68 * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
70 public void windowActivated(IWorkbenchWindow window) {
71 fSelectedResource = null;
72 ISelectionService service = window.getSelectionService();
73 service.addSelectionListener(this);
74 IWorkbenchPage page = window.getActivePage();
76 IWorkbenchPart part = page.getActivePart();
78 ISelection selection = service.getSelection();
79 if (selection != null) {
80 selectionChanged(part, selection);
87 * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
89 public void windowClosed(IWorkbenchWindow window) {
90 window.getSelectionService().removeSelectionListener(this);
94 * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
96 public void windowDeactivated(IWorkbenchWindow window) {
97 window.getSelectionService().removeSelectionListener(this);
101 * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
103 public void windowOpened(IWorkbenchWindow window) {
107 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
109 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
110 IResource selectedResource = null;
111 if (selection instanceof IStructuredSelection) {
112 Object result = ((IStructuredSelection)selection).getFirstElement();
113 if (result instanceof IResource) {
114 selectedResource = (IResource) result;
115 } else if (result instanceof IAdaptable) {
116 selectedResource = (IResource)((IAdaptable) result).getAdapter(IResource.class);
120 if (selectedResource == null) {
121 // If the active part is an editor, get the file resource used as input.
122 if (part instanceof IEditorPart) {
123 IEditorPart editorPart = (IEditorPart) part;
124 IEditorInput input = editorPart.getEditorInput();
125 selectedResource = (IResource) input.getAdapter(IResource.class);
129 fSelectedResource = selectedResource;
133 * Returns the active variable context. The context is that of the selected
134 * resource, or a project being built.
136 * @return variable context
138 public ExpandVariableContext getVariableContext() {
140 return new ExpandVariableContext(fProject, fKind);
142 return new ExpandVariableContext(fSelectedResource);
147 * Notification that the given project is being built.
151 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
153 public void buildStarted(IProject project, int kind) {
160 * Notification the building the current project has completed.
161 * @see ExternalToolBuilder#build(int, Map, IProgressMonitor)
163 public void buildEnded() {