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

1.插件中获取路径

// 得到插件所在的路径
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();

2.添加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"));

3. 利用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(); 
            }
        }

4. 获取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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值