Eclipse-Rcp创建首选项

Eclipse-Rcp创建首选项

1. 通过e4xmi创建首选项界面

创建顶级窗口

创建窗口中视图

java 复制代码
package com.flx.studnetmanager;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.osgi.service.prefs.Preferences;

import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.inject.Named;

public class SettingPage {
    private Text textField;
    private Button checkBox;

    @Inject
    @Optional
    @Named("preferences.node") // 上下文中的首选项节点标识
    private Preferences preferences;

    @PostConstruct
    public void createUI(Composite parent) {
        parent.setLayout(new GridLayout(2, false));

        // 初始化默认值(防止 NPE)
        if (preferences == null) {
            preferences = InstanceScope.INSTANCE.getNode("com.flx.studnetmanager");
        }

        // 文本框
        new Label(parent, SWT.NONE).setText("Username:");
        textField = new Text(parent, SWT.BORDER);
        textField.setText(preferences.get("username", "admin"));

        // 复选框
        new Label(parent, SWT.NONE).setText("Enable Feature:");
        checkBox = new Button(parent, SWT.CHECK);
        checkBox.setSelection(preferences.getBoolean("feature_enabled", false));

        // 保存按钮
        Button saveButton = new Button(parent, SWT.PUSH);
        saveButton.setText("Save");
        saveButton.addListener(SWT.Selection, e -> savePreferences());
    }

    private void savePreferences() {
        preferences.put("username", textField.getText());
        preferences.putBoolean("feature_enabled", checkBox.getSelection());
        try {
            preferences.flush(); // 持久化保存
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. 绑定菜单打开首选项界面

java 复制代码
package com.flx.studnetmanager;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
import org.osgi.service.prefs.Preferences;

public class Setting {

    @Execute
    public void execute(EModelService modelService, MApplication application, EPartService partService , IEclipseContext context) {
        // 获取用户级首选项节点
        Preferences prefsNode = InstanceScope.INSTANCE.getNode("com.flx.studnetmanager");

        // 将节点绑定到上下文
        context.set("preferences.node", prefsNode);
        // 查找 TrimmedWindow
        MTrimmedWindow window = (MTrimmedWindow) modelService.find("copy.trimmedwindow.011", application);
        if (window != null) {
            // 激活窗口
            window.setToBeRendered(true);
            window.setVisible(true);
            partService.showPart("copy.part.022", PartState.ACTIVATE);
        }
    }

}

3. 在打开时候设置默认值

java 复制代码
@PostContextCreate
void postContextCreate(IEclipseContext workbenchContext) {
    Preferences prefs = InstanceScope.INSTANCE.getNode("com.flx.studnetmanager");
    prefs.put("username", "admin");
    prefs.putBoolean("feature_enabled", false);
}

4. 效果图

相关推荐
李伟_Li慢慢4 分钟前
01-threejs架构原理-课程简介
前端·three.js
_瑞1 小时前
深入理解 iOS 渲染原理
前端·ios
IT_陈寒1 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
iCOD3R2 小时前
Skill - kill-ai-slop 解决“AI 味”样式
前端·css·ai编程
皮音2 小时前
Skill 在项目中的实践 —— 从 Agent 困境到 Skill 工程化
前端
大龄秃头程序员3 小时前
RN 0.86 新架构实战:TurboModule + Fabric 组件 + iOS 原生容器,完整代码开源
前端·react native
这是个栗子3 小时前
前端开发中的常用工具函数(八)
开发语言·前端·javascript
Hilaku3 小时前
Vue 和 React 真正的差距,不在语法,而在团队犯错成本
前端·javascript·程序员
研☆香3 小时前
为什么变量声明在赋值前也能使用?—— 深入理解 JavaScript 变量提升与作用域
开发语言·前端·javascript
Liora_Yvonne3 小时前
目录结构到底怎么设计?别再把 components 和 utils 当垃圾桶了
前端