KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > variables > BuildProjectResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.variables;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.variables.IDynamicVariable;
20 import org.eclipse.core.variables.IDynamicVariableResolver;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.ui.externaltools.internal.model.ExternalToolBuilder;
23 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
24
25
26 public class BuildProjectResolver implements IDynamicVariableResolver {
27
28     /* (non-Javadoc)
29      * @see org.eclipse.core.variables.IDynamicVariableResolver#resolveValue(org.eclipse.core.variables.IDynamicVariable, java.lang.String)
30      */

31     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
32         IResource resource= ExternalToolBuilder.getBuildProject();
33         if (argument != null && resource != null) {
34             resource = ((IProject)resource).findMember(new Path(argument));
35         }
36         if (resource != null && resource.exists()) {
37             return resource.getLocation().toOSString();
38         }
39         abort(NLS.bind(VariableMessages.BuildProjectResolver_3, new String JavaDoc[]{getReferenceExpression(variable, argument)}), null);
40         return null;
41     }
42     
43     /**
44      * Throws an exception with the given message and underlying exception.
45      *
46      * @param message exception message
47      * @param exception underlying exception or <code>null</code>
48      * @throws CoreException
49      */

50     protected void abort(String JavaDoc message, Throwable JavaDoc exception) throws CoreException {
51         throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.ERR_INTERNAL_ERROR, message, exception));
52     }
53     
54     /**
55      * Returns an expression used to reference the given variable and optional argument.
56      * For example, <code>${var_name:arg}</code>.
57      *
58      * @param variable referenced variable
59      * @param argument referenced argument or <code>null</code>
60      * @return vraiable reference expression
61      */

62     protected String JavaDoc getReferenceExpression(IDynamicVariable variable, String JavaDoc argument) {
63         StringBuffer JavaDoc reference = new StringBuffer JavaDoc();
64         reference.append("${"); //$NON-NLS-1$
65
reference.append(variable.getName());
66         if (argument != null) {
67             reference.append(":"); //$NON-NLS-1$
68
reference.append(argument);
69         }
70         reference.append("}"); //$NON-NLS-1$
71
return reference.toString();
72     }
73 }
74
Popular Tags