`
qq123zhz
  • 浏览: 534621 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

eclipse 获得透视图切换事件

 
阅读更多
/*******************************************************************************
 * Copyright (c) 2008, Ralf Ebert
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Ralf Ebert nor the names of its contributors may
 *       be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Ralf Ebert ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Ralf Ebert BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/
package de.ralfebert.rcp.tools.preferredperspective;

/**
 * Let Workbench parts implement this interface if you want
 * {@link PreferredPerspectivePartListener} to automatically activate the
 * preferred perspective on part activation.
 */
public interface IPrefersPerspective {

    /**
     * @return the preferred perspective of this part or null if no perspective
     *         is preferred.
     */
    String getPreferredPerspectiveId();

}Add PreferredPerspectivePartListener to your application. This class is responsible for switching the perspective upon activation of a part implementing IPrefersPerspective.

/*******************************************************************************
 * Copyright (c) 2008, Ralf Ebert
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Ralf Ebert nor the names of its contributors may
 *       be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Ralf Ebert ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Ralf Ebert BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/
package de.ralfebert.rcp.tools.preferredperspective;

import java.util.logging.Logger;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

/**
 * PreferredPerspectivePartListener is to be registered using the extension
 * point "org.eclipse.ui.startup". It will register itself as listener for the
 * activation of parts. When a part which implements IPrefersPerspective is
 * activated it will activate the preferred perspective of this part.
 */
public class PreferredPerspectivePartListener implements IPartListener, IStartup {

    private static final Logger log = Logger.getLogger(PreferredPerspectivePartListener.class);

    public void partActivated(IWorkbenchPart part) {
        refresh(part);
    }

    public static void refresh(final IWorkbenchPart part) {
        if (!(part instanceof IPrefersPerspective)) {
            return;
        }

        final IWorkbenchWindow workbenchWindow = part.getSite().getPage().getWorkbenchWindow();

        IPerspectiveDescriptor activePerspective = workbenchWindow.getActivePage().getPerspective();
        final String preferredPerspectiveId = ((IPrefersPerspective) part)
                .getPreferredPerspectiveId();

        if (preferredPerspectiveId == null) {
            return;
        }

        if (activePerspective == null || !activePerspective.getId().equals(preferredPerspectiveId)) {
            // Switching of the perspective is delayed using Display.asyncExec
            // because switching the perspective while the workbench is
            // activating parts might cause conflicts.
            Display.getCurrent().asyncExec(new Runnable() {

                public void run() {
                    log.debug("Switching to preferred perspective " + preferredPerspectiveId
                            + " for " + part.getClass());
                    try {
                        workbenchWindow.getWorkbench().showPerspective(preferredPerspectiveId,
                                workbenchWindow);
                    } catch (WorkbenchException e) {
                        log.warn("Could not switch to preferred perspective "
                                + preferredPerspectiveId + " for " + part.getClass(), e);
                    }
                }

            });
        }

    }

    public void earlyStartup() {
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                try {
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                            .addPartListener(new PreferredPerspectivePartListener());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }

        });
    }

    public void partBroughtToTop(IWorkbenchPart part) {
        // nothing to do
    }

    public void partClosed(IWorkbenchPart part) {
        // nothing to do
    }

    public void partDeactivated(IWorkbenchPart part) {
        // nothing to do
    }

    public void partOpened(IWorkbenchPart part) {
        // nothing to do
    }

}

 

<extension point="org.eclipse.ui.startup">
   <startup class="de.ralfebert.rcp.tools.preferredperspective.PreferredPerspectivePartListener"/>
</extension>

 

 

引用地址:http://www.ralfebert.de/blog/eclipsercp/auto_perspective_switch/

http://eclipse.dzone.com/articles/disable-or-enable-actions-sets

 

分享到:
评论

相关推荐

    RCP自定义透视图切换按钮的右键菜单

    默认情况下,Eclipse RCP提供了标准的透视图切换方式,但开发者可以根据需求进行自定义。下面我们将逐步解析这个过程: 1. **理解org.eclipse.ui.presentationFactories扩展点**: `org.eclipse.ui....

    Eclipse集成开发环境介绍

    3. **透视图切换器** - 位于工具栏最右侧。 - 可以通过点击透视图切换器按钮来展示不同的透视图供用户选择。 - **透视图**是Eclipse中的一种布局模式,用于组织不同的开发任务界面。 - 用户可以自定义不同的透视...

    免安装中文版Eclipse

    6. **透视图(Perspective)**:Eclipse的透视图功能允许用户根据当前的工作需求定制工作区布局,例如,可以切换到Java透视图进行编程,或者切换到调试透视图进行程序调试。 7. **市场(Eclipse Marketplace)**:...

    eclipse(中文版)

    同时,Eclipse的透视图(Perspective)概念使得开发者可以根据当前的工作需求切换不同的视图布局,如Java开发透视图、调试透视图等。 对于初学者,Eclipse的中文版提供了一份详尽的中文帮助文档,覆盖了从安装到...

    Eclipse开发技术详解 源码

    源码分析能让我们明白如何定义新的透视图,以及如何在不同透视图之间切换。 4. **视图(View)和编辑器(Editor)**:Eclipse的视图和编辑器提供了展示和编辑项目信息的界面。源码中,你可以学习如何创建自定义视图...

    Eclipse基础整合工具

    - Eclipse的核心是其工作台,它由多个视图(View)、编辑器(Editor)和透视图(Perspective)组成,可根据不同开发任务灵活切换。 3. **透视图(Perspective)**: - 透视图是一种组织工作空间的方式,如Java...

    Eclipse中文教程.rar

    9. **透视图**:Eclipse的透视图功能允许用户根据不同的工作需求切换不同的视图布局,例如Java透视图、调试透视图和资源透视图等。 10. **市场与更新**:Eclipse Marketplace是获取和安装插件的平台。用户可以在...

    rcp perspective

    5. **替换透视图切换器**:EclipseSource Blog中提到的话题可能涉及如何自定义RCP应用的透视图切换逻辑,这可能涉及到重写默认的透视图切换行为,提供更加个性化的用户体验。 6. **视图管理**:RCP小技巧可能包括...

    eclipse.tar.gz

    8. **透视图(Perspective)**:透视图允许用户根据当前任务切换不同的视图布局,比如Java开发者常用的Java透视图,以及用于测试的JUnit透视图。 9. **任务(Task)**:Eclipse支持任务管理,用户可以创建、跟踪和...

    Eclipse教程,Eclipse电子书

    通过切换透视图,可以快速改变工作台的布局以适应不同的任务。 - **重新排列视图和编辑器**:用户可以根据自己的喜好重新排列视图和编辑器的位置,以提高工作效率。例如,可以并排放置编辑器,或将特定的视图固定在...

    eclipse中文帮助文档

    4. **透视图(Perspective)**:透视图定义了Eclipse窗口中显示的视图和编辑器布局,例如Java透视图、调试透视图和XML透视图,可以根据不同的开发任务选择适合的视角。 5. **编辑器(Editor)**:编辑器用于编写...

    Eclipse教程.pdf

    你可以通过“窗口”菜单中的“透视图”选项来切换或创建新的透视图。 3. **创建一个项目** - 在Eclipse中,项目是代码组织的基本单元。要创建一个新的Java项目,你需要选择“文件”-&gt;“新建”-&gt;“Java项目”。输入...

    eclipse插件开发官方示例

    `org.eclipse.ui.examples`可能包括不同类型的透视图示例,展示如何创建和切换不同的工作空间配置。 至于导入和运行示例,按照描述,你需要将下载的I20150728-0800压缩包解压后导入到Eclipse环境中。然后,选择项目...

    Eclipse 安装包.zip

    同时,Eclipse的透视图(Perspective)功能允许你根据不同的工作场景切换界面布局,例如Java开发透视图、调试透视图等。 总之,Eclipse作为一款功能强大的Java开发工具,其易用性和灵活性使其成为开发者首选的IDE之...

    Eclipse_开发学习笔记

    - **透视图切换器**:用于在不同透视图间切换,以适应不同的开发任务。 - **工作台工具栏**:根据透视图变化,提供特定的工具。 - **视图工具栏**:位于各个视图的顶部,用于控制视图内的内容。 - **快速视图栏*...

    eclipse工具的学习

    通过"Window" -&gt; "Perspective" -&gt; "Open Perspective"切换或创建新的透视图。 九、资源管理 Eclipse的资源管理功能强大,支持文件和项目的导入导出、搜索、重命名、删除等操作。"Package Explorer"和"Project ...

    eclipse+jdk1.8资源下载

    其特有的“透视图”(Perspective)功能,可以根据开发者的不同需求,灵活切换不同的工作视图,如Java开发透视图、Web开发透视图等。 接下来,我们关注JDK1.8,即Java Development Kit的第8个主要版本。JDK是Oracle...

    eclipse官方使用教程翻译.doc

    - **透视图(Perspective)**:透视图是Eclipse中视图和编辑器的组合,提供不同类型的开发环境。你可以通过Window菜单切换透视图(Window → Open Perspective → Other)。例如,Java开发通常使用Java透视图,它...

    eclipse使用入门初级知识

    可以通过`Window &gt; Open Perspective &gt; Java`来切换到Java透视图。 3. **创建项目** 要创建一个新的Java项目,选择`File &gt; New &gt; Project`,然后在弹出的向导中选择`Java Project`。在项目名称中输入,如...

Global site tag (gtag.js) - Google Analytics