"""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()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/eclipse-explore-windows-lab/explore-explore-windows.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-explore-windows.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2275.zip