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



   
      

      
     

3、插件中获取 Eclipse 版本号

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

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;   
    }  

转载自https://www.cnblogs.com/jifeng/archive/2011/07/17/2108505.html

你可能感兴趣的:(Eclipse 插件开发中的代码总结)