1 /******************************************************************************* 2 * Copyright (c) 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 12 package org.eclipse.ui.internal.ide.commands; 13 14 import org.eclipse.core.commands.AbstractParameterValueConverter; 15 import org.eclipse.core.commands.ParameterValueConversionException; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.Path; 20 21 /** 22 * A command parameter value converter to convert between IResources and strings 23 * encoding the path of a resource. 24 * 25 * @since 3.2 26 */ 27 public final class ResourcePathConverter extends 28 AbstractParameterValueConverter { 29 30 public final Object convertToObject(final String parameterValue) 31 throws ParameterValueConversionException { 32 final Path path = new Path(parameterValue); 33 final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace() 34 .getRoot(); 35 final IResource resource = workspaceRoot.findMember(path); 36 37 if ((resource == null) || (!resource.exists())) { 38 throw new ParameterValueConversionException( 39 "parameterValue must be the path of an existing resource"); //$NON-NLS-1$ 40 } 41 42 return resource; 43 } 44 45 public final String convertToString(final Object parameterValue) 46 throws ParameterValueConversionException { 47 if (!(parameterValue instanceof IResource)) { 48 throw new ParameterValueConversionException( 49 "parameterValue must be an IResource"); //$NON-NLS-1$ 50 } 51 final IResource resource = (IResource) parameterValue; 52 return resource.getFullPath().toString(); 53 } 54 } 55