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. 效果图

相关推荐
Electrolux5 分钟前
基于WASM的纯前端Office解决方案:在线编辑/导入导出/权限切换(已开源)
前端
合作小小程序员小小店24 分钟前
web网页开发,在线%医院诊断管理%系统,基于Idea,html,css,jQuery,java,jsp,ssh,mysql。
java·前端·css·数据库·jdk·html·intellij-idea
爱学习的程序媛34 分钟前
【Web前端】Vue2与Vue3核心概览与优化对比
前端·javascript·vue.js·typescript
li@h1 小时前
如何在电脑端访问小程序时在胶囊添加一个全屏和缩放功能
前端
LFly_ice2 小时前
学习React-23-React-router
前端·学习·react.js
我叫张小白。2 小时前
TypeScript对象类型与接口:构建复杂数据结构
前端·javascript·typescript
墨客希2 小时前
如何快速掌握大型Vue项目
前端·javascript·vue.js
大福ya2 小时前
AI开源项目改造NextChat(ChatGPT-Next-Web)实现前端SSR改造打造一个初始框架
前端·chatgpt·前端框架·开源·aigc·reactjs·ai编程
n***33352 小时前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
纯粹的热爱3 小时前
🌐 阿里云 Linux 服务器 Let's Encrypt 免费 SSL 证书完整部署指南
前端