博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
eclipse插件开发--获取当前项目路径
阅读量:6279 次
发布时间:2019-06-22

本文共 6287 字,大约阅读时间需要 20 分钟。

1 import java.io.BufferedReader;  2 import java.io.IOException;  3 import java.io.InputStreamReader;  4 import org.eclipse.jface.action.IAction;  5 import org.eclipse.jface.viewers.ISelection;  6 import org.eclipse.jface.viewers.IStructuredSelection;  7 import org.eclipse.ui.ISelectionService;  8 import org.eclipse.ui.IWorkbenchWindow;  9 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 10 import org.eclipse.ui.internal.Workbench; 11 import org.eclipse.core.resources.IProject; 12 import org.eclipse.core.resources.IResource; 13 import org.eclipse.core.runtime.Platform; 14 import org.eclipse.jdt.core.IJavaElement; 15 import org.eclipse.jdt.core.IJavaProject; 16 import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; 17 import org.eclipse.cdt.core.model.ICElement;  18 import org.eclipse.cdt.core.model.ICProject; 19  20 /** 21  * Our sample action implements workbench action delegate. 22  * The action proxy will be created by the workbench and 23  * shown in the UI. When the user tries to use the action, 24  * this delegate will be created and execution will be  25  * delegated to it. 26  * @see IWorkbenchWindowActionDelegate 27  */ 28 @SuppressWarnings({ "restriction" }) 29 public class RuntimePower implements IWorkbenchWindowActionDelegate { 30 //    private IWorkbenchWindow window; 31     /** 32      * The constructor. 33      */ 34     public RuntimePower() { 35     } 36  37     /** 38      * The action has been activated. The argument of the 39      * method represents the 'real' action sitting 40      * in the workbench UI. 41      * @see IWorkbenchWindowActionDelegate#run 42      */ 43     public void run(IAction action) { 44         try { 45             String path = getProjectPath(); 46             IProject myProject = getCurrentProject(); 47             path = path + myProject.getFullPath(); 48             runProc("/chai/run_java.sh"); 49             String command = path + "/Debug" + myProject.getFullPath(); 50             runProc("/chai/run.sh" + " " + command);             51              52         } catch (IOException e) { 53             // TODO Auto-generated catch block 54             e.printStackTrace(); 55         } catch (SecurityException e) { 56             // TODO Auto-generated catch block 57             e.printStackTrace(); 58         } catch (IllegalArgumentException e) { 59             // TODO Auto-generated catch block 60             e.printStackTrace(); 61         }  62     } 63  64     public static String runProc(String para) throws IOException 65     { 66         Process p = Runtime.getRuntime().exec(para); 67         String pid = getPID(p); 68         return pid; 69     } 70     public static String getPID(Process p) throws IOException 71     { 72         BufferedReader br = new  73                 BufferedReader(new InputStreamReader(p.getInputStream())); 74         String line = null; 75         line = br.readLine(); 76         return line; 77     } 78     public static String getProjectPath() 79     { 80         String path = null; 81         path = Platform.getLocation().toString(); 82         return path; 83     } 84      85     public static IProject getCurrentProject(){   86         ISelectionService selectionService =    87             Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();   88    89         ISelection selection = selectionService.getSelection();   90    91         IProject project = null;   92         if(selection instanceof IStructuredSelection) {   93             Object element = ((IStructuredSelection)selection).getFirstElement();   94              95             if (element instanceof IResource) {   96                 project= ((IResource)element).getProject();   97             }else if (element instanceof PackageFragmentRootContainer) 98             { 99                 IJavaProject jProject =   100                         ((PackageFragmentRootContainer)element).getJavaProject();  101                     project = jProject.getProject();102             }else if (element instanceof IJavaElement) {  103                 IJavaProject jProject= ((IJavaElement)element).getJavaProject();  104                 project = jProject.getProject();  105             }else if (element instanceof ICElement){106                 ICProject cProject = ((ICElement)element).getCProject();107                 project = cProject.getProject();108             }109 110         }111         return project;  112     }  113     /**114      * Selection in the workbench has been changed. We 115      * can change the state of the 'real' action here116      * if we want, but this can only happen after 117      * the delegate has been created.118      * @see IWorkbenchWindowActionDelegate#selectionChanged119      */120     public void selectionChanged(IAction action, ISelection selection) {121     }122 123     /**124      * We can use this method to dispose of any system125      * resources we previously allocated.126      * @see IWorkbenchWindowActionDelegate#dispose127      */128     public void dispose() {129     }130 131     /**132      * We will cache window object in order to133      * be able to provide parent shell for the message dialog.134      * @see IWorkbenchWindowActionDelegate#init135      */136     public void init(IWorkbenchWindow window) {137     //    this.window = window;138     }139 }

 

其中,run_java.sh如下:

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

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

run.sh如下:

1 #!/bin/bash2 $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.

我下载的是最新版本

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();

 

你可能感兴趣的文章
《算法基础:打开算法之门》一3.4 归并排序
查看>>
高德开放平台开放源代码 鼓励开发者创新
查看>>
《高并发Oracle数据库系统的架构与设计》一2.5 索引维护
查看>>
《Exchange Server 2010 SP1/SP2管理实践》——2.4 部署外部网络环境
查看>>
Firefox 是 Pwn2own 2014 上攻陷次数最多的浏览器
查看>>
阿里感悟(十八)- 应届生Review
查看>>
《计算广告:互联网商业变现的市场与技术》一第一部分 在线广告市场与背景...
查看>>
话说模式匹配(5) for表达式中的模式匹配
查看>>
《锋利的SQL(第2版)》——1.7 常用函数
查看>>
《Arduino家居安全系统构建实战》——1.5 介绍用于机器学习的F
查看>>
jquery中hover()的用法。简单粗暴
查看>>
线程管理(六)等待线程的终结
查看>>
spring boot集成mongodb最简单版
查看>>
DELL EqualLogic PS存储数据恢复全过程整理
查看>>
《Node.js入门经典》一2.3 安装模块
查看>>
《Java 开发从入门到精通》—— 2.5 技术解惑
查看>>
Linux 性能诊断 perf使用指南
查看>>
实操分享:看看小白我如何第一次搭建阿里云windows服务器(Tomcat+Mysql)
查看>>
Sphinx 配置文件说明
查看>>
数据结构实践——顺序表应用
查看>>