问题是我的代码中的命令对象为null,我不知道为什么。
我将Lars Vogel的e4-perspective-switcher与这里的问题How to add Perspective Bar Switcher to pure eclipse 4 rcp application结合使用。
public class PerspectiveSwitcher {
@Inject
private ECommandService commandService;
@Inject
private EHandlerService handlerService;
@PostConstruct
public void createControls(Composite parent, EModelService modelService) {
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout());
body.setBackground(parent.getBackground());
Button button = new Button(body, SWT.PUSH);
button.setText("switch");
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
data.minimumHeight = 20;
button.setLayoutData(data);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
List<MPerspective> perspectives = modelService.findElements(app,
PTApplicationConstants.PERSPECTIVE_ID_MANAGER, MPerspective.class, null);
MPerspective perspective = perspectives.get(0);
HashMap<String, Object> parameters = new HashMap<>(2);
parameters.put(E4WorkbenchParameterConstants.COMMAND_PERSPECTIVE_ID, perspective.getElementId());
parameters.put(E4WorkbenchParameterConstants.COMMAND_PERSPECTIVE_NEW_WINDOW, "false");
ParameterizedCommand command = commandService
.createCommand(E4WorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE, parameters);
handlerService.executeHandler(command);
partService.switchPerspective(perspective);
super.widgetSelected(e);
}
});
}
}