蓝色天空.知识 · 实践 · 创造
我的收藏
Eclipse 教程目录 35章
Eclipse / LEARN BY DOING

Eclipse 首选项(Preferences)

介绍 Eclipse 首选项对话框的插件可扩展架构、Window > Preferences 打开路径、左侧分类导航、左上角关键字快速检索以及 OK/Cancel 保存回滚机制。

学完这一章,你能够

  • 介绍 Eclipse 首选项对话框的插件可扩展架构、Window > Preferences 打开路径、左侧分类导航、左上角关键字快速检索以及 OK/Cancel 保存回滚机制。

首选项框架与打开路径

Eclipse 的首选项(Preferences)对话框由核心平台框架统一托管,同时对外开放扩展点——每安装一个第三方插件(如 Java 开发工具 JDT、Maven 或 Python 插件),该插件都可以在对话框中注册专属的配置页面。在 Windows/Linux 下,只需点击顶部菜单 `Window > Preferences` 即可打开首选项窗口(macOS 下对应应用程序菜单中的 `Preferences` / `Settings`)。

分类浏览、快速过滤与保存生效

首选项对话框左侧按功能域组织为多级树形分类(如 `General`、`Java` 等),可以逐层展开节点定位具体设置页。由于配置页数量众多,更高效的方式是使用对话框左上角的过滤搜索框:只需输入目标配置项的关键字母(例如输入 `font`),左侧树便会实时筛出字体(`Colors and Fonts`)等相关页面。

完成参数调整后,点击底部 `OK`(新版常标注为 `Apply and Close`)即可持久化保存配置并关闭对话框;若不希望保留本次更改,点击 `Cancel` 按钮即可放弃修改并恢复原值。本站离线实验脚本已对多插件首选项树、关键字过滤及作用域继承与提交/回滚语义完成校验。

运行 Eclipse 首选项多插件分类树、过滤检索与作用域 OK/Cancel 事务离线核验

含完整源码 · 110 行
展开查看收起
python
"""Offline Eclipse Preferences tree, filter search & scope inheritance verifier for Ch22 (preferences)."""
import json

VERSION = "CPython 3.12.14 offline Eclipse Preferences hierarchy & scope verifier (desktop SWT IDE absent)"


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


def main():
    cases = []

    # Case 1: Framework & plugin-contributed preference tree
    pref_pages = [
        {"path": "General > Appearance > Colors and Fonts", "provider": "platform"},
        {"path": "General > Editors > Text Editors", "provider": "platform"},
        {"path": "General > Keys", "provider": "platform"},
        {"path": "General > Workspace", "provider": "platform"},
        {"path": "Java > Compiler", "provider": "jdt-plugin"},
        {"path": "Java > Editor > Content Assist", "provider": "jdt-plugin"},
        {"path": "Maven > User Settings", "provider": "m2e-plugin"},
    ]
    top_categories = sorted({p["path"].split(" > ")[0] for p in pref_pages})
    plugin_pages = [p["path"] for p in pref_pages if p["provider"] != "platform"]

    cases.append(
        check(
            "preference_tree_and_plugin_pages",
            "Verify Window > Preferences combines core platform pages and plugin-contributed pages",
            {
                "top_categories": ["General", "Java", "Maven"],
                "plugin_page_count": 3,
            },
            {
                "top_categories": top_categories,
                "plugin_page_count": len(plugin_pages),
            },
        )
    )

    # Case 2: Top-left quick filter box matches page names case-insensitively (e.g., "font")
    query = "font"
    matched_font = [p["path"] for p in pref_pages if query.lower() in p["path"].lower()]
    query_keys = "keys"
    matched_keys = [p["path"] for p in pref_pages if query_keys.lower() in p["path"].lower()]

    cases.append(
        check(
            "preference_filter_search",
            "Verify top-left filter input narrows preference tree for queries such as 'font' and 'keys'",
            {
                "font_matches": ["General > Appearance > Colors and Fonts"],
                "keys_matches": ["General > Keys"],
            },
            {
                "font_matches": matched_font,
                "keys_matches": matched_keys,
            },
        )
    )

    # Case 3: Preference scope inheritance (default -> instance -> project) and OK vs Cancel transaction
    default_scope = {"encoding": "ISO-8859-1", "tabWidth": 4, "fontSize": 10}
    instance_scope = {"encoding": "UTF-8"}
    project_scope = {"tabWidth": 2}

    def effective(inst):
        merged = dict(default_scope)
        merged.update(inst)
        merged.update(project_scope)
        return merged

    # Edit in dialog then Cancel -> discards draft
    draft_cancel = dict(instance_scope, fontSize=14)
    # Cancel does not write back to instance_scope
    after_cancel = effective(instance_scope)

    # Edit in dialog then OK -> persists draft
    draft_ok = dict(instance_scope, fontSize=12)
    instance_scope = draft_ok
    after_ok = effective(instance_scope)

    cases.append(
        check(
            "preference_scope_and_ok_cancel",
            "Verify preference scope override order and OK commit vs Cancel discard semantics",
            {
                "after_cancel": {"encoding": "UTF-8", "tabWidth": 2, "fontSize": 10},
                "after_ok": {"encoding": "UTF-8", "tabWidth": 2, "fontSize": 12},
            },
            {
                "after_cancel": after_cancel,
                "after_ok": after_ok,
            },
        )
    )

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


if __name__ == "__main__":
    main()
验证平台与插件配置页聚合、输入 `font`/`keys` 精准筛出目标页面,以及 `default -> instance -> project` 作用域覆盖与 `OK`/`Cancel` 提交回滚。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a2259.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2282.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

打开 `Window > Preferences` 后,直接在对话框左上角的过滤输入框中键入英文关键词(如 `font` 或 `keys`),左侧分类树会即时筛选并高亮匹配的配置页面。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习