⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pathutil.java

📁 对eclipse gef进行封装,可以生成图形化编辑器
💻 JAVA
字号:
/*******************************************************************************
 * $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/util/PathUtil.java,v 1.1 2006/11/17 03:15:13 niegy Exp $
 * $Revision: 1.1 $
 * $Date: 2006/11/17 03:15:13 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved. 
 * 
 * Created on 2006-10-30
 *******************************************************************************/


package com.primeton.studio.gef.ui.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.Bundle;

/**
 * TODO此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: PathUtil.java,v $
 * Revision 1.1  2006/11/17 03:15:13  niegy
 * create
 * 
 */
public class PathUtil {

    /**
     * 返回当前eclipse的安装目录
     * @return eclipse安装目录
     * @throws IOException
     */
    public static String getEclipseHome() throws IOException {
        String path = ""; //$NON-NLS-1$
        path = Platform.asLocalURL(Platform.getInstallLocation().getURL())
                .getFile();
        return path;
    }

    /**
     * 根据 plugin ID, 返回 plugin 的安装目录
     * @param pluginID 插件的pluginID
     * @return plugin的安装目录
     */
    public static String pluginInstallPath(String pluginID) {
        Plugin plugin = Platform.getPlugin(pluginID);
        if (plugin == null) {
            return null;
        } else {
            return pluginInstallPath(plugin);
        }
    }

    /**
     * 根据 plugin ID, 得到plugin安装目录下的子目录
     * @param pluginID
     * @param subPath
     * @return
     */
    public static String pluginInstallPath(String pluginID, String subPath) {
        Plugin plugin = Platform.getPlugin(pluginID);
        if (plugin == null) {
            return null;
        } else {
            return pluginInstallPath(plugin, subPath);
        }
    }

    /**
     * 查找一个plugin的安装路径(绝对路径)
     * @param plugin
     * @return
     */
    public static String pluginInstallPath(Plugin plugin) {
        try {
            Bundle bundle = plugin.getBundle();
            URL starterURL = bundle.getEntry("/"); //$NON-NLS-1$
            String path = Platform.asLocalURL(starterURL).getFile();
            return path;
        } catch (Exception e) {
//            Logger.error(DSBCore.getDefault(), e);
            return ""; //$NON-NLS-1$
        }
    }

    /**
     * 查找一个plugin下面的一个指定目录的绝对路径
     * @param plugin
     * @param subPath
     * @return
     */
    public static String pluginInstallPath(Plugin plugin, String subPath) {
        try {
            if (subPath == null || ".".equals(subPath) || "/".equals(subPath) || //$NON-NLS-1$ //$NON-NLS-2$
                    File.separator.equals(subPath)) {
                return pluginInstallPath(plugin);
            }
            URL starterURL = plugin.getBundle().getEntry(
                    "/" + File.separator + subPath); //$NON-NLS-1$
            if (starterURL == null) {
                return ""; //$NON-NLS-1$
            }
            String path = Platform.asLocalURL(starterURL).getFile();
            path = path.replace('/', File.separatorChar);
            return path;
        } catch (Exception e) {
//            Logger.error(DSBCore.getDefault(), e);
            return ""; //$NON-NLS-1$
        }
    }

    /**
     * 获取 plugin .metadata 目录下指定的子目录或文件的绝对路径
     * 如果获得 .metadata 目录, subPath = "/"
     * @param plugin
     * @param subPath
     * @return
     */
    public static String pluginMetadataPath(Plugin plugin, String subPath) {
        try {
            if (plugin == null) {
                return null;
            }
            String path = plugin.getStateLocation().toString();

            if (subPath == null || ".".equals(subPath) || "/".equals(subPath)) { //$NON-NLS-1$ //$NON-NLS-2$
                return path;
            }
            subPath = subPath.replace('/', File.separatorChar);
            return path + File.separator + subPath;
        } catch (Exception e) {
//            Logger.error(DSBCore.getDefault(), e);
            return ""; //$NON-NLS-1$
        }
    }

    public static String getAbsolutePath(IFile file) {
        IProject project = file.getProject();
        String path = file.getFullPath().toOSString();
        String prjName = project.getName();
        if (path.startsWith(File.separator + prjName + File.separator)) {
            path = path.substring(("/" + prjName).length() + 1); //$NON-NLS-1$
        }
        path = path.replace('/', File.separatorChar);

        String prjPath = project.getProject().getLocation().toOSString();
        path = prjPath + File.separator + path;
        return path;
    }

    /**
     * 获取子目录的路径
     * @param parent
     * @param subPath
     * @return
     */
    public static String subPath(String parent, String subPath) {
        if (parent == null || subPath == null) {
            return null;
        }
        if (parent.endsWith(File.separator)) {
            return parent.concat(subPath);
        } else {
            return parent.concat(File.separator).concat(subPath);
        }
    }

    /**
     * 删除一个指定目录或文件
     * @param outputFolder
     */
    public static void deleteFolder(File outputFolder) {
        if ((null == outputFolder) || (!outputFolder.exists()))
            return;
        if (outputFolder.isFile()) {
            if (!outputFolder.delete()) {
//                Logger.debug(DSBCore.getDefault(), Messages
//                        .getString("PathUtil.delete_fail")
//                        + outputFolder);
            }
        } else {
            File[] files = outputFolder.listFiles();
            for (int i = 0; i < files.length; i++) {
                deleteFolder(files[i]);
            }
            if (!outputFolder.delete()) {
//                Logger.debug(DSBCore.getDefault(), Messages
//                        .getString("PathUtil.delete_fail")
//                        + outputFolder);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -