eclipse 插件开发经验积累

本文分享了Eclipse插件开发中的实用技巧,包括如何基于TextEditor创建代码编辑器、获取当前活动页面上的编辑器实例、遍历工作空间中的所有项目、检查项目是否包含特定文件及如何给项目添加Nature等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1、从TextEditor继承,调用setSourceViewerConfiguration,并传进去一个从SourceViewerConfiguration 继承的配置类,就可以实现各种代码editor。
2、swt尽量使用GridLayout布局(不是java.awt中的GridLayout,而是swt中的)和GridData域。文章:http://coolbear.yculblog.com/post.89429.html

3、得到文件的编辑器的方法:
    public static IEditorPart findEditor(IFile file){
        IEditorReference[] editors = getActivePage().getEditorReferences();;
        for (int i = 0; i < editors.length; i++) {
            IEditorPart part = (IEditorPart)editors[i].getPart(false);
            if (part != null ){
                IEditorInput input = part.getEditorInput();
                if(input instanceof FileEditorInput && ((FileEditorInput)input).getFile().equals(file))
                    return part;
            }               
        }
        return null;
    }

4、得到工作区中所有工程的方法:
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
                .getProjects();
这在开发自己的工程向导的时候很有用处。
5、工程特有文件判断方法
project.getFile("cownew.prj").exists();
project.getDescription().hasNature();
给工程增加Nature的方法:
 IProjectDescription desc = project.getDescription();
String[] oldNatureIds = desc.getNatureIds();
                String[] newNatureIds  = new String[oldNatureIds.length +1];
                System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length);
                newNatureIds[oldNatureIds.length] = "CowNewNature";
                desc.setNatureIds(newNatureIds);
                project.setDescription(desc, monitor);
6、创建文件夹的方法:
IFolder folder = project.getFolder("myfold");
if (folder!=null && !folder.exists())
  folder.create(false, true, null);

7、弹出包选择对话框的方法:
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
                    getShell(), new LabelProvider());
            dialog.setIgnoreCase(false);
dialog.setElements(getAllPackages().toArray());
            String path = currentPackage();
 dialog.setInitialSelections(new Object[] { path });
dialog.open();
fPKName.setText((String) dialog.getFirstResult());

public List getAllPackages() {
        List list = new ArrayList();
        IResource res = getFirstSelection();
        IProject project = res.getProject();
        File file = project.getFolder("src").getLocation().toFile();
        File[] fs = file.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].isDirectory())
                iterator("", fs[i], list);
        }
        Collections.sort(list);
        return list;
    }
8 objectClass="org.eclipse.core.resources.IFile"代表菜单应用到文件
9 透视图的的实现很简单,就是在构造函数里边打开一些视图,使一些action(这样菜单和按钮也就都可用)可以用,比如:
String editorArea = layout.getEditorArea();
        IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
                0.30f, editorArea);
        left.addView(PACKAGE_VIEW_ID);
 layout.setEditorAreaVisible(true);
 layout.addShowViewShortcut(IDESystem.BUSINESSVIEW_ID);

原文:http://www.blogjava.net/huanzhugege/archive/2006/07/21/59303.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值