下面列出了怎么用org.eclipse.ui.handlers.RegistryToggleState的API类实例代码及写法,或者点击链接到github查看源代码。
@Inject
public void initializeAtStartup(final IWorkbench workbench, final ICommandService commandService) {
/*
* Check the UI state of the IHandler/Command which indicates if the user
* enabled automatic PlusCal conversion.
*/
final Command command = commandService.getCommand("toolbox.command.module.translate.automatially");
final State state = command.getState(RegistryToggleState.STATE_ID);
if (!((Boolean) state.getValue()).booleanValue()) {
return;
}
// This IHander is stateful even across Toolbox restarts. In other words, if the
// user enables automatic PlusCal translation, it will remain enabled even after
// a Toolbox restart. This means we have to register this EventHandler at the
// IEventBroker during Toolbox startup.
// It is vital that we use the Workbench's IEventBroker instance. If e.g. we
// would use an IEventBroker from higher up the IEclipseContext hierarchy, the
// broker would be disposed when a new spec gets opened while the IHandler's state
// remains enabled.
workbench.getService(IEventBroker.class).unsubscribe(this);
Assert.isTrue(workbench.getService(IEventBroker.class).subscribe(TLAEditor.PRE_SAVE_EVENT, this));
}
public boolean test(final Object receiver, final String property,
final Object[] args, final Object expectedValue) {
if (receiver instanceof IServiceLocator && args.length == 1
&& args[0] instanceof String) {
final IServiceLocator locator = (IServiceLocator) receiver;
if (TOGGLE_PROPERTY_NAME.equals(property)) {
final String commandId = args[0].toString();
final ICommandService commandService = (ICommandService) locator
.getService(ICommandService.class);
final Command command = commandService.getCommand(commandId);
final State state = command
.getState(RegistryToggleState.STATE_ID);
if (state != null) {
return state.getValue().equals(expectedValue);
}
}
}
return false;
}
public boolean test(final Object receiver, final String property,
final Object[] args, final Object expectedValue) {
if (receiver instanceof IServiceLocator && args.length == 1
&& args[0] instanceof String) {
final IServiceLocator locator = (IServiceLocator) receiver;
if (TOGGLE_PROPERTY_NAME.equals(property)) {
final String commandId = args[0].toString();
final ICommandService commandService = (ICommandService) locator
.getService(ICommandService.class);
final Command command = commandService.getCommand(commandId);
final State state = command
.getState(RegistryToggleState.STATE_ID);
if (state != null) {
return state.getValue().equals(expectedValue);
}
}
}
return false;
}
@Override
public void setEnabled(Object evaluationContext)
{
Object activeSite = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_SITE_NAME);
Object activeEditor = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_EDITOR_NAME);
if (activeSite instanceof IWorkbenchSite && activeEditor instanceof AbstractThemeableEditor)
{
ICommandService commandService = (ICommandService) ((IWorkbenchSite) activeSite)
.getService(ICommandService.class);
Command command = commandService.getCommand(COMMAND_ID);
State state = command.getState(RegistryToggleState.STATE_ID);
state.setValue(((AbstractThemeableEditor) activeEditor).getWordWrapEnabled());
}
}