eclipse插件开发_遇到问题总结

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;   
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一章 Eclipse插件概述 5 一、Eclipse概述 5 二、Eclipse插件(plug-in)概述 6 三、Eclipse插件开发环境与开发入门 8 1.3.1 创建工程及设置环境 8 1.3.2 Activator类 13 1.3.3 plugin.xml与org.eclipse.ui.actionSets扩展点 15 1.3.4 menubarPath属性 23 1.3.5 groupMarker标记 26 1.3.6 separator标记 30 四、插件开发调试 34 五、小结 35 第二章 SWT/jFace简介 36 一、SWT/jFace概述 36 二、SWT使用入门 38 三、插件环境下的Dialog 41 2.3.1 Dialog概述与典型结构 41 2.3.1 与主调程序交互 51 四、小结 53 第三章 TitleAreaDialog与布局 54 一、TitleAreaDialog 54 3.1.1 概述 54 3.1.2 标题区域(Title Area) 56 3.1.3 DialogTray 58 二、布局概述 61 三、FillLayout 62 四、RowLayout 65 五、GridLayout 68 六、FormLayout 79 3.6.1 FormLayout基本概念 79 3.6.2 参照物 83 七、小结 86 第四章 WizardDialog与标准对话框 87 一、向导对话框 87 4.1.1 WizardDialog概述 87 4.1.2 WizardPage 90 4.1.3 Wizard 95 4.1.4 org.eclipse.ui.newWizards扩展点 97 4.1.5 org.eclipse.ui.importWizards扩展点 101 4.1.6 org.eclipse.ui.exportWizards扩展点 104 4.1.7 用户档案输入向导 105 二、内置对话框 105 4.3.1 消息框 105 4.3.2 询问框 106 4.3.3 错误消息框 108 4.3.4 输入框 108 4.3.5 进度条监视对话框 109 三、文件与目录选择对话框 111 4.3.1 文件选择对话框FileDialog 111 4.3.2 目录选择对话框 112 四、小结 117 第五章 Label、Text、Button、List与Combo 119 一、Label 119 二、Text 122 三、Button 126 四、List 134 五、Combo 136

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值