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);
}