01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.ide.commands;
11:
12: import org.eclipse.core.commands.AbstractParameterValueConverter;
13: import org.eclipse.core.commands.ParameterValueConversionException;
14: import org.eclipse.core.resources.IResource;
15: import org.eclipse.core.resources.IWorkspaceRoot;
16: import org.eclipse.core.resources.ResourcesPlugin;
17: import org.eclipse.core.runtime.Path;
18:
19: /**
20: * A command parameter value converter to convert between IResources and strings
21: * encoding the path of a resource.
22: *
23: * @since 3.2
24: */
25: public final class ResourcePathConverter extends
26: AbstractParameterValueConverter {
27:
28: public final Object convertToObject(final String parameterValue)
29: throws ParameterValueConversionException {
30: final Path path = new Path(parameterValue);
31: final IWorkspaceRoot workspaceRoot = ResourcesPlugin
32: .getWorkspace().getRoot();
33: final IResource resource = workspaceRoot.findMember(path);
34:
35: if ((resource == null) || (!resource.exists())) {
36: throw new ParameterValueConversionException(
37: "parameterValue must be the path of an existing resource"); //$NON-NLS-1$
38: }
39:
40: return resource;
41: }
42:
43: public final String convertToString(final Object parameterValue)
44: throws ParameterValueConversionException {
45: if (!(parameterValue instanceof IResource)) {
46: throw new ParameterValueConversionException(
47: "parameterValue must be an IResource"); //$NON-NLS-1$
48: }
49: final IResource resource = (IResource) parameterValue;
50: return resource.getFullPath().toString();
51: }
52: }
|