/*
 * Created on 2005-04-26
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
package pl.zalewski.mikolaj.symbdev.utils;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;

/**
 * @author mikolaj
 */
public class PathUtils {

    public static String makeRelativePath(IPath path, IPath currentDir) {
        String[] segments=path.segments();
        int ok=path.matchingFirstSegments(currentDir);
        StringBuffer b=new StringBuffer();
        for (int i=ok; i<currentDir.segments().length; i++)
            b.append("..\\");
        for (int i=ok; i<segments.length; i++)
        {
            b.append(segments[i]);
            b.append('\\');
        }
        if (b.length()==0)
            return ".";
        b.setLength(b.length()-1);     // remove the last '\\'
        return b.toString();   
    }

    public static IPath getProjectPathForLocation(IProject project, IPath location) {
        if (location==null)
            return null;
        IFile file=ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(location);
        if (file.getProject()!=project)
            return null;
        return file.getProjectRelativePath();
    }
        
    public static IPath getLocationForProjectMember(IProject project, IPath path) {
        IResource res=project.findMember(path);
        if (res!=null)
            return res.getLocation();
        	else return project.getLocation().append(path);        
    }
}
