如何在 Eclipse 中订阅 OpenProject 事件?

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

我正在开发一个 Eclipse 插件。

我一直在阅读如何使用接口 IResourceChangeListener,并使用 PRE_CLOSE事件类型。以下文本摘自 Eclipse 帮助

通知听众项目即将关闭。本次活动 可用于从数据中提取并保存必要的信息 项目的内存表示(例如会话属性) 在关闭之前。 (当项目关闭时,内存中 代表已处置)。工作区被锁定(没有资源可以 在此活动期间进行更新)。该事件包含的项目是 正在关闭。

我没有找到如何在项目即将打开时收到通知。

I'm developing an Eclipse plugin.

I have been reading how to subscribe get notification when a project is about to be closed, using the interface IResourceChangeListener, and using the PRE_CLOSE event type. The following text has been taken from the Eclipse help:

Notifies listeners that a project is about to be closed. This event
can be used to extract and save necessary information from the
in-memory representation (e.g., session properties) of a project
before it is closed. (When a project is closed, the in-memory
representation is disposed). The workspace is locked (no resources can
be updated) during this event. The event contains the project that is
being closed.

I didn't found how to be notified when a project is about to be opened.

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

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

发布评论

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

评论(3

甜扑 2024-12-15 08:54:27

您可以创建自己的 IResourceChangeListener 并通过 IResourceDelta.OPEN 过滤增量类型,这仅影响 IProjects,并且在打开和关闭项目时都会触发:

public void resourceChanged(IResourceChangeEvent event) {
    if (event == null || event.getDelta() == null)
        return;
    event.getDelta().accept(new IResourceDeltaVisitor() {
        public boolean visit(IResourceDelta delta) throws CoreException {
            if (delta.getKind() == IResourceDelta.OPEN)
                final IResource resource = delta.getResource();
                if (!(resource instanceof IProject))
                    return;
               //do your stuff and check the project is opened or closed
        }
}

有用的链接:
http://www.eclipse.org/articles/Article-Resource -deltas/resource-deltas.html

You can create your own IResourceChangeListener and filter the kind of delta by IResourceDelta.OPEN, which only affects to IProjects, and it's fired both when opening and closing a project:

public void resourceChanged(IResourceChangeEvent event) {
    if (event == null || event.getDelta() == null)
        return;
    event.getDelta().accept(new IResourceDeltaVisitor() {
        public boolean visit(IResourceDelta delta) throws CoreException {
            if (delta.getKind() == IResourceDelta.OPEN)
                final IResource resource = delta.getResource();
                if (!(resource instanceof IProject))
                    return;
               //do your stuff and check the project is opened or closed
        }
}

Useful link:
http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html

允世 2024-12-15 08:54:27

我知道这个问题早已得到解答,但我想用一个工作代码片段来更新它,以防万一有人需要它。我在 Eclipse Luna、Indigo 和 Kepler 上测试了它。

public void resourceChanged(final IResourceChangeEvent event) {
    if (event == null || event.getDelta() == null) {
        return;
    }

    try {
        event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(final IResourceDelta delta) throws CoreException {
                IResource resource = delta.getResource();
                if (((resource.getType() & IResource.PROJECT) != 0)
                        && resource.getProject().isOpen()
                        && delta.getKind() == IResourceDelta.CHANGED
                        && ((delta.getFlags() & IResourceDelta.OPEN) != 0)) {

                    IProject project = (IProject)resource;
                    projectOpened(project);
                }
                return true;
            }
        });
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

I know this question has been long answered, but I want to update it with a working code snippet, just in case anybody will need it. I tested it on Eclipse Luna, Indigo, and Kepler.

public void resourceChanged(final IResourceChangeEvent event) {
    if (event == null || event.getDelta() == null) {
        return;
    }

    try {
        event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(final IResourceDelta delta) throws CoreException {
                IResource resource = delta.getResource();
                if (((resource.getType() & IResource.PROJECT) != 0)
                        && resource.getProject().isOpen()
                        && delta.getKind() == IResourceDelta.CHANGED
                        && ((delta.getFlags() & IResourceDelta.OPEN) != 0)) {

                    IProject project = (IProject)resource;
                    projectOpened(project);
                }
                return true;
            }
        });
    } catch (CoreException e) {
        e.printStackTrace();
    }
}
御弟哥哥 2024-12-15 08:54:27

IResourceChangeEventPOST_CHANGE 和相关的 IResourceDelta 类型是IResourceDelta.CHANGED 和标志包括 IResourceDelta.OPEN

IResourceChangeEvent is POST_CHANGE and related IResourceDelta kind is IResourceDelta.CHANGED and flags include IResourceDelta.OPEN.

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