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

相关推荐
爱分享的程序猿-Clark23 分钟前
【前端分享】CSS实现3种翻页效果类型,附源码!
前端·css
Code哈哈笑37 分钟前
【图书管理系统】深度讲解:图书列表展示的后端实现、高内聚低耦合的应用、前端代码讲解
java·前端·数据库·spring boot·后端
无名之逆1 小时前
Hyperlane: Unleash the Power of Rust for High-Performance Web Services
java·开发语言·前端·后端·http·rust·web
数据潜水员1 小时前
`待办事项css样式
前端·css·css3
_处女座程序员的日常1 小时前
css媒体查询及css变量
前端·css·媒体
GanGuaGua3 小时前
CSS:盒子模型
开发语言·前端·css·html
GalenWu9 小时前
对象转换为 JSON 字符串(或反向解析)
前端·javascript·微信小程序·json
GUIQU.9 小时前
【Vue】微前端架构与Vue(qiankun、Micro-App)
前端·vue.js·架构
数据潜水员10 小时前
插槽、生命周期
前端·javascript·vue.js
2401_8370885010 小时前
CSS vertical-align
前端·html