已加载项目的 Eclipse 插件

发布于 2024-12-08 12:27:05 字数 154 浏览 0 评论 0原文

我有一个插件,想要检测项目何时添加到工作区,以便从我的插件代码 Any Ideas 设置一些项目设置。

特别是我想在一些派生文件的资源中调用 setHidden ,因为此设置似乎不是项目的一部分,我的意思是,如果我将项目导入到新的工作区中,则只要资源被隐藏,似乎就不会持续存在。

I have a plug-in and want to detect when projects are added to workspace, to set some project settings from my plug-in code, Any Ideas.

Specially i want to call setHidden in some resources that are derived files, as this settings seems to not be part of the project, i mean whenever a resources is hidden seems to not persist if i import the project in a new workspace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2024-12-15 12:27:05

讽刺的是,我昨天才写了这样的东西。它比你想要的要复杂一些。以下是供您使用的代码片段:

public class ProjectListener implements IResourceChangeListener {

public void resourceChanged(IResourceChangeEvent event) {
    if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
        List<IProject> projects = getProjects(event.getDelta());
        // do something with new projects
    }
}

private List<IProject> getProjects(IResourceDelta delta) {
    final List<IProject> projects = new ArrayList<IProject>();
    try {
        delta.accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {
                if (delta.getKind() == IResourceDelta.ADDED && 
                  delta.getResource().getType() == IResource.PROJECT) {
                    IProject project = (IProject) delta.getResource();
                    if (project.isAccessible()) {
                        projects.add(project);
                    }
                }
                // only continue for the workspace root
                return delta.getResource().getType() == IResource.ROOT;
            }
        });
    } catch (CoreException e) {
        // handle error
    }
    return projects;
}

然后,您需要将此 ProjectListener 添加到工作区,最好是在插件激活器的 start 方法中:

ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectListener.LISTENER, IResourceChangeEvent.POST_CHANGE);

然后您想在 <代码>停止方法。我昨天刚刚写了这段代码。我希望它有帮助。

Ironically, I just wrote something like this yesterday. It is a bit more complicated than you would like. Here is a code snippet for you to play with:

public class ProjectListener implements IResourceChangeListener {

public void resourceChanged(IResourceChangeEvent event) {
    if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
        List<IProject> projects = getProjects(event.getDelta());
        // do something with new projects
    }
}

private List<IProject> getProjects(IResourceDelta delta) {
    final List<IProject> projects = new ArrayList<IProject>();
    try {
        delta.accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {
                if (delta.getKind() == IResourceDelta.ADDED && 
                  delta.getResource().getType() == IResource.PROJECT) {
                    IProject project = (IProject) delta.getResource();
                    if (project.isAccessible()) {
                        projects.add(project);
                    }
                }
                // only continue for the workspace root
                return delta.getResource().getType() == IResource.ROOT;
            }
        });
    } catch (CoreException e) {
        // handle error
    }
    return projects;
}

Then, you need to add this ProjectListener to the Workspace, preferably in the start method of your plugin activator:

ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectListener.LISTENER, IResourceChangeEvent.POST_CHANGE);

And then you want to remove it in the stop method. I literally just wrote this code yesterday. I hope it helps.

惜醉颜 2024-12-15 12:27:05

您可以为工作区定义资源侦听器,并查找资源根中的更改。有关详细信息,请参阅以下文章:http://www.eclipse。 org/articles/Article-Resource-deltas/resource-deltas.html

You can define a resourcelistener to the workspace, and look for changes in the resource root. See the following article for details: http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文