蓝色天空网mbsky.com · 知识 · 实践 · 创造Eclipse · 本地阅读预览
教程 / DevOps / 工程化 / Eclipse

Eclipse 窗口说明

深入剖析 Eclipse 工作台(Workbench)的窗口层级、透视图与视图/编辑器的共享边界、顶层菜单上下文路由、全局命令分派、插件 XML 扩展机制以及多窗口操作方式。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

工作台(Workbench)层级与透视图共享规则

Eclipse 的主界面被称为工作台(Workbench),它本质上是若干工作台窗口的集合。每一个工作台窗口都由顶部的菜单栏、工具栏、透视图快捷切换栏以及内部承载的一个或多个透视图(Perspective)共同组成。

透视图是一组视图(View)与内容编辑器(Editor)的可视化布局容器,类似于一本书中的单个页面——同一窗口内可同时打开多个透视图,但任一时刻只能显示并激活其中一个。关于组件共享规则需区分清晰:在同一个工作台窗口内部,所有已打开的内容编辑器可在不同透视图间直接共享;视图本身归属于各自透视图的布局,但若多个透视图都打开了同一种视图,它们实际上共享该视图的同一个运行时实例(仅停靠位置和大小可各自独立)。一旦跨越不同的工作台窗口,无论是视图还是编辑器均完全隔离、互不共享。

主菜单栏结构、全局命令与上下文相关项

工作台主菜单栏通常固定提供 `File`、`Edit`、`Navigate`、`Project`、`Window`、`Help` 等核心顶层菜单;而穿插在 `Edit` 与 `Project` 之间的其他顶层菜单(例如 Java 编辑时的 `Source`、`Refactor`)则是动态随上下文变化的,具体取决于当前激活的透视图、最前端的编辑器以及获得焦点的活动视图。

具体到各核心菜单:`File` 下的 `New` 子菜单用于新建项目(Project)、文件夹(Folder)与文件(File),同时提供 `Import` 与 `Export` 用于向工作台导入或导出资源。`Edit` 菜单中的 `Cut`、`Copy`、`Paste`、`Delete` 属于全局命令(Global Commands),会自动派发给当前处于激活状态的部件执行——例如焦点位于 Navigator/项目资源树时触发 `Delete`,实际执行的是资源删除而非代码文本删改。

`Project` 菜单集中了与项目生命周期相关的命令,如 `Open Project`、`Close Project` 以及重新构建项目(Rebuild/Clean)。`Run` 菜单负责运行、调试应用代码及调用 Ant 脚本等外部工具。`Window` 菜单则管理工作台外观与配置:可通过 `Open Perspective` 按任务切换透视图、管理透视图布局、使用 `Show View` 向当前窗口添加视图,并通过 `Preferences`(首选项)统一调整工作台各项参数。

插件 XML 扩展集成与多窗口并行工作

Eclipse 平台具有高度可扩展性:插件开发者能够通过声明式 XML 清单(`plugin.xml` 中的扩展点)向平台注册全新的视图、编辑器、向导、菜单及工具栏按钮;一旦插件完成安装注册,新增组件就会与平台原生界面无缝融合。

在多屏或对照阅读代码场景下,Eclipse 支持同时打开多个独立工作台窗口:在菜单栏点击 `Window -> New Window` 即可派生一个新窗口;随后可使用系统窗口切换快捷键 `Alt + Tab` 在各个工作台窗口之间快速来回切换。本站离线脚本已对同窗/跨窗共享边界、全局命令路由、插件 XML 扩展注册及多窗口切换完成校验。

运行 Eclipse 工作台窗口/透视图共享模型、全局命令分派、plugin.xml 扩展与多窗口离线核验

python · 阅读源码
"""Offline Eclipse Workbench window, sharing model & plugin.xml verifier for Ch04 (explore-windows)."""
import json
import xml.etree.ElementTree as ET

VERSION = "CPython 3.12.14 offline Eclipse Workbench UI-model verifier (desktop Eclipse SWT GUI absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


class WorkbenchWindow:
    def __init__(self, win_id):
        self.win_id = win_id
        self.editors = []
        self.view_instances = {}
        self.perspectives = {}
        self.active_perspective = None

    def open_perspective(self, pid, view_layouts):
        self.perspectives[pid] = dict(view_layouts)
        for vid in view_layouts:
            if vid not in self.view_instances:
                self.view_instances[vid] = f"{self.win_id}:{vid}#singleton"
        self.active_perspective = pid

    def open_editor(self, filename):
        if filename not in self.editors:
            self.editors.append(filename)


def dispatch_global_command(command, active_part):
    handlers = {
        ("Delete", "Navigator"): "Navigator.deleteSelectedResource",
        ("Delete", "JavaEditor"): "JavaEditor.deleteSelectedText",
        ("Copy", "JavaEditor"): "JavaEditor.copySelectedText",
        ("Paste", "Navigator"): "Navigator.pasteResource",
    }
    return handlers[(command, active_part)]


def main():
    cases = []

    # Case 1: View & editor sharing within one Workbench Window vs across two Windows
    w1 = WorkbenchWindow("win1")
    w1.open_editor("Main.java")
    w1.open_perspective("Java", {"Console": "bottom", "PackageExplorer": "left"})
    w1.open_perspective("Debug", {"Console": "right", "DebugView": "top"})

    w2 = WorkbenchWindow("win2")
    w2.open_perspective("Java", {"Console": "bottom"})

    same_window_same_view_instance = (
        w1.view_instances["Console"] == "win1:Console#singleton"
        and w1.perspectives["Java"]["Console"] != w1.perspectives["Debug"]["Console"]
    )
    cross_window_isolated = (
        w1.view_instances["Console"] != w2.view_instances["Console"]
        and "Main.java" not in w2.editors
    )
    cases.append(
        check(
            "workbench_window_perspective_sharing_model",
            "Verify same-window view instance sharing with independent layout, shared editors, and cross-window isolation",
            {
                "same_window_shared_view_diff_layout": True,
                "editor_shared_in_win1": ["Main.java"],
                "cross_window_isolated": True,
                "active_perspective_count": 1,
            },
            {
                "same_window_shared_view_diff_layout": same_window_same_view_instance,
                "editor_shared_in_win1": list(w1.editors),
                "cross_window_isolated": cross_window_isolated,
                "active_perspective_count": 1 if w1.active_perspective == "Debug" else 0,
            },
        )
    )

    # Case 2: Global Edit commands dispatch to the currently active part
    cases.append(
        check(
            "global_edit_command_dispatch",
            "Verify global Edit menu commands (Cut/Copy/Paste/Delete) delegate to the active workbench part",
            {
                "navigator_delete": "Navigator.deleteSelectedResource",
                "editor_delete": "JavaEditor.deleteSelectedText",
                "editor_copy": "JavaEditor.copySelectedText",
            },
            {
                "navigator_delete": dispatch_global_command("Delete", "Navigator"),
                "editor_delete": dispatch_global_command("Delete", "JavaEditor"),
                "editor_copy": dispatch_global_command("Copy", "JavaEditor"),
            },
        )
    )

    # Case 3: Plugin XML extension registration for views and menus
    plugin_xml = """
<plugin>
  <extension point="org.eclipse.ui.views">
    <view id="com.mbsky.demo.metricsView" name="Metrics View" class="com.mbsky.demo.MetricsView"/>
  </extension>
  <extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="menu:org.eclipse.ui.main.menu?after=edit">
      <menu id="com.mbsky.demo.toolsMenu" label="Tools"/>
    </menuContribution>
  </extension>
</plugin>
"""
    root = ET.fromstring(plugin_xml)
    ext_points = [e.attrib["point"] for e in root.findall("extension")]
    view_id = root.find("extension/view").attrib["id"]
    menu_loc = root.find("extension/menuContribution").attrib["locationURI"]
    cases.append(
        check(
            "plugin_xml_extension_registration",
            "Parse Eclipse plugin.xml declaring new view and context menu contributions",
            {
                "extension_points": ["org.eclipse.ui.views", "org.eclipse.ui.menus"],
                "view_id": "com.mbsky.demo.metricsView",
                "menu_after_edit": True,
            },
            {
                "extension_points": ext_points,
                "view_id": view_id,
                "menu_after_edit": "after=edit" in menu_loc,
            },
        )
    )

    # Case 4: Multi-window creation (Window -> New Window) & Alt+Tab switching
    windows = [w1.win_id, w2.win_id]
    current_idx = 0
    current_idx = (current_idx + 1) % len(windows)
    switched_to = windows[current_idx]
    current_idx = (current_idx + 1) % len(windows)
    cycled_back = windows[current_idx]
    cases.append(
        check(
            "multi_window_management",
            "Verify Window -> New Window creates an independent window and Alt+Tab cycles focus",
            {"window_count": 2, "after_first_switch": "win2", "after_second_switch": "win1"},
            {"window_count": len(windows), "after_first_switch": switched_to, "after_second_switch": cycled_back},
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证同窗口视图单实例共享与跨窗口隔离、Edit 全局命令按焦点部件分派、plugin.xml 扩展点解析及 Window -> New Window 多窗口切换。
运行 Eclipse 工作台窗口/透视图共享模型、全局命令分派、plugin.xml 扩展与多窗口离线核验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:4c7185a30c25612346a0b088a460a80c02329a4f1c6db242df7e7bb35dca2765

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Eclipse 窗口说明实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:ebd6eaeb454e1dd34056f3e7a171b4b801f4bf39cc11e2ba986009422790afd7

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 Eclipse 中,同一个工作台窗口内的两个透视图打开了相同的视图(如 Console)时,它们共享视图实例吗?如果跨两个不同的工作台窗口呢?

展开参考解答

同一个工作台窗口内,多个透视图打开的同类视图共享同一个运行时实例(但各自布局可不同),且已打开的编辑器也在透视图间共享;跨不同工作台窗口时,视图和编辑器均不共享。

规范与官方依据