Eclipse 插件开发遇到问题心得总结

Posted on 2011-07-17 00:51 季枫 阅读(3997) 评论(0) 编辑 收藏

1、Eclipse 中插件开发多语言的实现

为了使用 .properties 文件,需要在 META-INF/MANIFEST.MF 文件中定义:
      Bundle-Localization: plugin
这样就会自动加载 plugin.properties 文件(中文找 plugin_zh_CN.properties)
然后在 plugin.xml 文件中,将字符串替换为 %key 就可以了
建议先使用 Eclipse 的外部化字符串目录:

Bundle-Localization: OSGI-INF/l10n/plugin 

2、Eclipse 插件开发初始化隐藏某工具栏按钮

在网上找了好久都找不到解决办法,最后搜索 eclipse 安装目录,从它自己的插件里面找到样例了。样例来自 org.eclipse.jdt.ui/plugin.xml


<extension
<extension point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension  targetID="*">
<!-- 注意这里的 id 是 action 或 command 的 id -->
<hiddenToolBarItem    id="org.eclipse.jdt.ui.actions.OpenProjectWizard">
</hiddenToolBarItem>
</perspectiveExtension>  

3、插件中获取 Eclipse 版本号

 

01.String sEclipseVersion = System.getProperty("osgi.framework.version");  

4、插件中获取路径


// 得到插件所在的路径
Platform.asLocalURL(Platform.getBundle("your plugin ID").getEntry("")).getFile();

// 得到当前工作空间的路径
Platform.getInstanceLocation().getURL().getFile();

// 得到当前工作空间下的所有工程
ResourcesPlugin.getWorkspace().getRoot().getProjects();

// 得到某 PLUGIN 的路径:
Platform.getBundle("mypluginid").getLocation().
// eclipse采用osgi后好像还可以:
Activator.getDefault().getBundle().getLocation(); //前提是这个插件有Activator这个类.这个类继承了ECLIPSE的Plugin类
// eclipse采用osgi前好像好像是:
MyPlugin.getDefault().getBundle().getLocation(); //前提是这个插件有MyPlugin这个类.这个类继承了ECLIPSE的Plugin类

// 得到工作区路径:
Platform.getlocation();
// 或 ResourcesPlugin.getWorkspace(); 好像 Platform.getInstanceLocation() 也可行

// 得到ECLIPSE安装路径
Platform.getInstallLocation();

// 从插件中获得绝对路径:
AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath();

// 通过文件得到 Project:
IProject project = ((IFile)o).getProject();

// 通过文件得到全路径:
String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();

// 得到整个Workspace的根:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

// 从根来查找资源:
IResource resource = root.findMember(new Path(containerName));

// 从Bundle来查找资源:
Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, filePath);

// 得到 Appliaction workspace:
Platform.asLocalURL(PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath();

// 得到 runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();

// 从编辑器来获得编辑文件
IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input = editor.getEditorInput();
if(input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput)input).getFile();
}

// 获取插件的绝对路径:
FileLocator.resolve(BuildUIPlugin.getDefault().getBundle().getEntry("/")).getFile();

5、添加myeclipse JAVAEE Library 与User Library

IClasspathEntry myEclipseJAVAEE5 =JavaCore.newContainerEntry(new Path("melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"));
IClasspathEntry myEclipseUCITPortletDev =JavaCore.newContainerEntry(new Path("org.eclipse.jdt.USER_LIBRARY/UCITPortletDev"));

6、利用Ifile向项目中写文件

 

    /**
     * jar文件输入流
     * @param path
     * @return
     */
    private InputStream fileInput(File path){
        
        try {
            FileInputStream fis=new FileInputStream(path);
            return fis;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Adds a new file to the project.
     * 
     * @param container
     * @param path
     * @param contentStream
     * @param monitor
     * @throws CoreException
     */
    private void addFileToProject(IContainer container, Path path,
            InputStream contentStream, IProgressMonitor monitor)
            throws CoreException {
        final IFile file = container.getFile(path);

if (file.exists()) {
            file.setContents(contentStream, true, true, monitor);
        } else {
            file.create(contentStream, true, monitor);
        }

}

        //写入自动生成portlet环境的jar包
        IContainer container = (IContainer) project;
        IProgressMonitor monitor = new NullProgressMonitor();
        Path autoJar=new Path(WEBROOT+FILESEPARATOR+WEBINF+FILESEPARATOR +LIB+FILESEPARATOR+"UcitPortletDev.jar");    //项目路径
        InputStream jarIS=fileInput(new File("d:/PortletAuto.jar"));    //本地文件路径
        
        //写入自动生成portlet环境的xml配置文件
        Path autoConfigXML=new Path("src"+FILESEPARATOR+"service_portlet.xml");    //项目路径
        InputStream XMLIS=fileInput(new File(selectConfigPath));    //本地文件路径
        
        try {
            addFileToProject(container,autoJar,jarIS,monitor);    //Jar
            monitor = new NullProgressMonitor();
            addFileToProject(container,autoConfigXML,XMLIS,monitor);    //XML
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (jarIS!=null){
                jarIS.close();
            }
            if (XMLIS!=null){
                XMLIS.close(); 
            }
        }

7、获取Eclipse当前项目

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();   
            }   
        }    
        return project;   
    }   

Eclipse 插件开发遇到问题心得总结相关推荐

  1. 【插件开发】—— 1 Eclipse插件开发导盲

    在真正接触eclipse插件开发一个月后,对插件的开发过程以及技术要求,也有了一定的了解.遥想之前像无头苍蝇一样乱撞乱学,真心觉得浪费了不少时间.这里就总结一下学习的主要过程以及需要的资料. Ecli ...

  2. eclipse插件开发_开发Eclipse插件

    eclipse插件开发 最近,我开始与一个团队合作开发Eclipse插件. 团队开发了一个很棒的插件,可以实现预期的目的. 因此,我检查了源并尝试构建它. 项目源包含所有必需的库,并且只能在Eclip ...

  3. Eclipse插件开发中对于外部Jar包和类文件引用的处理(彻底解决插件开发中的NoClassDefFoundError问题)...

    目的:Eclipse插件开发中,经常要引用第三方包或者是引用其他插件中的类,由于插件开发环境引用类路径的设置和运行平台引用类路径的设置不同,经常导致开发过程OK,一旦运行则出现NoClassDefFo ...

  4. eclipse插件开发示例

    eclipse所有的插件均以jar的形式存放在安装目录的plugins目录中,如果我们在eclipse中开发一个插件,最终也是需要打包为jar放入plugins目录中.eclipse插件开发也有一套流 ...

  5. 【eclipse】eclipse插件开发(一)

    eclipse本身是一个开源平台, 给用户提供了很多扩展点.我们完全可以开发属于自己的一套插件,安装在eclipse插件目录下,即可使用我们的插件. 下面说下eclipse插件的快速开发. 1.在ec ...

  6. eclipse插件开发资料 参考代码

    首页 zhangxinzhou 的BLOG 写留言 邀请进圈子 发消息 加友情链接 进家园 加好友 2011年度IT博客大赛 十大杰出IT博客评选中 博客统计信息 用户名:zhangxinzhou 文 ...

  7. [Eclipse插件开发-001] SWT/JFACE布局入门总结

    =============SWT布局管理器入门总结======================= 绝对定位: * setBounds(int x, int y, int width, int heig ...

  8. Eclipse 插件开发 向导

    阅读目录 最近由于特殊需要,开始学习插件开发. 下面就直接弄一个简单的插件吧! 1 新建一个插件工程 2 创建自己的插件名字,这个名字最好特殊一点,一遍融合到eclipse的时候,不会发生冲突. 3 ...

  9. [转]Eclipse插件开发之基础篇(2) 第一个Eclipse插件

    原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/15/eclipse_plugin_1_1_1.html 在Eclipse中使用PDE(Plug ...

最新文章

  1. 如何用C语言改变宏定义的大小,C语言中宏定义使用的小细节
  2. Jenkins搭建的几个坑记下
  3. 机器学习知识点(八)感知机模型Java实现
  4. infinity mysql_MySql锁机制
  5. 沟通篇:产品经理如何与UI进行沟通
  6. esp32树莓派_用 ESP32 制造炫彩 LED 球
  7. gitlab 分支操作笔记\新建远程分支\抓取远程分支\复制远程\删除分支
  8. 奇迹觉醒qq服务器比微信少,十年内最大的奇迹!功能比QQ还少的微信为什么能成功?...
  9. 前端学习(1889)vue之电商管理系统电商系统之绘制用户列表组件的基本布局
  10. 大数据-----软件开发模型(详细讲解)
  11. 使用数组初始化vector对象
  12. harmonyos2.0系统,Harmonyos2.0刷机包
  13. 大数据实效_新都区:运用“大数据”提升专项巡察实效
  14. Pycharm中文字体变成繁体解决方法
  15. linux之U盘读写速度测试
  16. 过犹不及,别再在编程中高射炮打蚊子
  17. 《数学之美》阅读笔记1
  18. Proe转Solidworks
  19. 【洛谷】入门2 分支结构
  20. Kubernetes K8S之Taints污点与Tolerations容忍详解

热门文章

  1. Windows主机安全加固
  2. linux系统调用open、write、close、read以及stat函数详解
  3. 160 - 52 egis.1
  4. 160 - 31 cracking4all.2
  5. 【数据结构基础应用】【查找和排序算法】
  6. 数字签名 那些密码技术_密码学中的数字签名
  7. Java GregorianCalendar getActualMaximum()方法与示例
  8. 制作两个字符串字谜的最小步骤数
  9. C——用冒泡排序法、选择排序法对随机输入的10个整数从小到大排序
  10. uva 12627——Erratic Expansion