eclipse插件开发--获取当前项目路径

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
import org.eclipse.cdt.core.model.ICElement; 
import org.eclipse.cdt.core.model.ICProject;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
@SuppressWarnings({ "restriction" })
public class RuntimePower implements IWorkbenchWindowActionDelegate {
//    private IWorkbenchWindow window;
    /**
     * The constructor.
     */
    public RuntimePower() {
    }

    /**
     * The action has been activated. The argument of the
     * method represents the 'real' action sitting
     * in the workbench UI.
     * @see IWorkbenchWindowActionDelegate#run
     */
    public void run(IAction action) {
        try {
            String path = getProjectPath();
            IProject myProject = getCurrentProject();
            path = path + myProject.getFullPath();
            runProc("/chai/run_java.sh");
            String command = path + "/Debug" + myProject.getFullPath();
            runProc("/chai/run.sh" + " " + command);            
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    public static String runProc(String para) throws IOException
    {
        Process p = Runtime.getRuntime().exec(para);
        String pid = getPID(p);
        return pid;
    }
    public static String getPID(Process p) throws IOException
    {
        BufferedReader br = new 
                BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        line = br.readLine();
        return line;
    }
    public static String getProjectPath()
    {
        String path = null;
        path = Platform.getLocation().toString();
        return path;
    }
    
    public static IProject getCurrentProject(){  
        ISelectionService selectionService =   
            Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();  
  
        ISelection selection = selectionService.getSelection();  
  
        IProject project = null;  
        if(selection instanceof IStructuredSelection) {  
            Object element = ((IStructuredSelection)selection).getFirstElement();  
            
            if (element instanceof IResource) {  
                project= ((IResource)element).getProject();  
            }else if (element instanceof PackageFragmentRootContainer)
            {
                IJavaProject jProject =   
                        ((PackageFragmentRootContainer)element).getJavaProject();  
                    project = jProject.getProject();
            }else if (element instanceof IJavaElement) {  
                IJavaProject jProject= ((IJavaElement)element).getJavaProject();  
                project = jProject.getProject();  
            }else if (element instanceof ICElement){
                ICProject cProject = ((ICElement)element).getCProject();
                project = cProject.getProject();
            }

        }
        return project;  
    }  
    /**
     * Selection in the workbench has been changed. We 
     * can change the state of the 'real' action here
     * if we want, but this can only happen after 
     * the delegate has been created.
     * @see IWorkbenchWindowActionDelegate#selectionChanged
     */
    public void selectionChanged(IAction action, ISelection selection) {
    }

    /**
     * We can use this method to dispose of any system
     * resources we previously allocated.
     * @see IWorkbenchWindowActionDelegate#dispose
     */
    public void dispose() {
    }

    /**
     * We will cache window object in order to
     * be able to provide parent shell for the message dialog.
     * @see IWorkbenchWindowActionDelegate#init
     */
    public void init(IWorkbenchWindow window) {
    //    this.window = window;
    }
}


其中,run_java.sh如下:

1 #!/bin/bash
2 java -jar /chai/Monitor.jar &
3 echo $! 

Monitor.jar 为用jfreechart写的能耗时序图代码,打包而成

run.sh如下:

1 #!/bin/bash
2 $1 &
3 echo $!

$1表示传入给run.sh的第一个参数,如 run.sh "echo helloworld"

查看interface 可以查找:http://help.eclipse.org

以下内容转自:http://blog.csdn.net/u010436741/article/details/9132925

对于CDT的安装:

解决方法:

1. 下载地址:http://www.eclipse.org/cdt/downloads.php

我下载的是最新版本cdt-master-8.1.2.zip

2. 安装到eclipse上

步骤:Eclipse ->Help -> Install New Software -> 点击ADD添加 -> 名称填写CDT ,地址那里选择Archive,然后选择已下好的cdt-master-8.1.2.zip   -> OK下一步下一步…最后重启Eclipse

 

当要在其他按钮里触发获取项目,或者获取Package Explorer里当前选项时,可以如下:

IStructuredSelection structured =

(IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow()

.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");

//或者 "org.eclipse.ui.navigator.ProjectExplorer"

Object selected = structured.getFirstElement();

if(selected instanceof IResource){

    IProject project = ((IResource)selected).getFirstElement();


转自:http://www.cnblogs.com/dorothychai/p/3430451.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值