Skip to content
Snippets Groups Projects
Commit 54852412 authored by Markus Alexander Kuppe's avatar Markus Alexander Kuppe
Browse files

Rewrite automatic PlusCal Translation handler to use dependency

injection to reduce dependencies and make startup initialization more
explicit.

[Refactor][Toolbox]
parent 99294b04
Branches
No related tags found
No related merge requests found
......@@ -18,9 +18,11 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.filesystem
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.e4.core.services.events,
Import-Package: org.eclipse.e4.core.di.annotations;version="1.6.0",
org.eclipse.e4.core.services.events,
org.eclipse.ui.forms,
org.osgi.service.event;version="1.3.1"
Export-Package: org.lamport.tla.toolbox.editor.basic,
org.lamport.tla.toolbox.editor.basic.handlers;x-friends:="org.lamport.tla.toolbox",
org.lamport.tla.toolbox.editor.basic.tla,
org.lamport.tla.toolbox.editor.basic.util
......@@ -309,7 +309,7 @@
</command>
<command
categoryId="org.eclipse.ui.category.textEditor"
defaultHandler="org.lamport.tla.toolbox.editor.basic.handlers.PCalTranslateAutomaticallyHandler"
defaultHandler="org.lamport.tla.toolbox.util.e4.E4HandlerWrapper:org.lamport.tla.toolbox.editor.basic.handlers.PCalTranslateAutomaticallyHandler"
description="Automatically translate PlusCal to TLA+ on TLA+ editor save."
id="toolbox.command.module.translate.automatially"
name="Translate PlusCal automatically">
......
......@@ -27,12 +27,16 @@ package org.lamport.tla.toolbox.editor.basic.handlers;
import java.lang.reflect.InvocationTargetException;
import javax.inject.Inject;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.State;
import org.eclipse.core.runtime.Assert;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.RegistryToggleState;
......@@ -43,28 +47,40 @@ import org.osgi.service.event.EventHandler;
public class PCalTranslateAutomaticallyHandler extends PCalTranslator implements EventHandler {
public PCalTranslateAutomaticallyHandler() {
@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 requests automatic PlusCal translation, it will enabled even after a
// Toolbox restart. This means we have to register this at the IEventBroker
// during Toolbox startup.
// 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 but the IHandler's state
// broker would be disposed when a new spec gets opened while the IHandler's state
// remains enabled.
final IEventBroker service = PlatformUI.getWorkbench().getService(IEventBroker.class);
service.subscribe(TLAEditor.PRE_SAVE_EVENT, this);
workbench.getService(IEventBroker.class).unsubscribe(this);
Assert.isTrue(workbench.getService(IEventBroker.class).subscribe(TLAEditor.PRE_SAVE_EVENT, this));
}
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(final ExecutionEvent event) throws ExecutionException {
// Toggle the IHandler's state at the UI level (check mark on/off). The
// IEventBroker handler remains subscribed to the event bus but will simply
// ignore the events.
HandlerUtil.toggleCommandState(event.getCommand());
return null;
@Execute
public void execute(final ExecutionEvent event, final IWorkbench workbench) throws ExecutionException {
// Toggle the IHandler's state at the UI level (check mark on/off) and
// unsubscribe/subscribe the EventHandler.
final IEventBroker eventBroker = workbench.getService(IEventBroker.class);
if (HandlerUtil.toggleCommandState(event.getCommand())) {
eventBroker.unsubscribe(this);
} else {
eventBroker.unsubscribe(this);
Assert.isTrue(eventBroker.subscribe(TLAEditor.PRE_SAVE_EVENT, this));
}
}
/* (non-Javadoc)
......@@ -72,10 +88,6 @@ public class PCalTranslateAutomaticallyHandler extends PCalTranslator implements
*/
@Override
public void handleEvent(final Event event) {
if (!isAutomaticEnabled()) {
// If the UI state is false, the user doesn't want automatic PlusCal translation.
return;
}
try {
final TLAEditor editor = (TLAEditor) event.getProperty(IEventBroker.DATA);
translate(editor, editor.getSite().getShell(), false);
......@@ -83,15 +95,4 @@ public class PCalTranslateAutomaticallyHandler extends PCalTranslator implements
throw new RuntimeException(e);
}
}
/*
* Check the UI state of the IHandler/Command which indicates if the user
* enabled automatic PlusCal conversion.
*/
private static boolean isAutomaticEnabled() {
final ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
final Command command = service.getCommand("toolbox.command.module.translate.automatially");
final State state = command.getState(RegistryToggleState.STATE_ID);
return ((Boolean) state.getValue()).booleanValue();
}
}
......@@ -35,13 +35,14 @@ import org.eclipse.ui.handlers.HandlerUtil;
import org.lamport.tla.toolbox.editor.basic.TLAEditor;
import org.lamport.tla.toolbox.editor.basic.TLAEditorAndPDFViewer;
import org.lamport.tla.toolbox.editor.basic.pcal.PCalTranslator;
import org.lamport.tla.toolbox.ui.handler.SaveDirtyEditorAbstractHandler;
/**
* Triggers the PCal translation of the module
*
* @author Simon Zambrovski
*/
public class PCalTranslateModuleHandler extends PCalTranslator {
public class PCalTranslateModuleHandler extends SaveDirtyEditorAbstractHandler {
public Object execute(final ExecutionEvent event) throws ExecutionException {
if (!saveDirtyEditor(event)) {
......@@ -55,7 +56,7 @@ public class PCalTranslateModuleHandler extends PCalTranslator {
}
try {
translate(tlaEditor, HandlerUtil.getActiveShell(event));
new PCalTranslator().translate(tlaEditor, HandlerUtil.getActiveShell(event));
} catch (InvocationTargetException | InterruptedException e) {
throw new ExecutionException(e.getMessage(), e);
}
......
......@@ -44,7 +44,6 @@ import org.eclipse.ui.part.FileEditorInput;
import org.lamport.tla.toolbox.editor.basic.TLAEditor;
import org.lamport.tla.toolbox.spec.Spec;
import org.lamport.tla.toolbox.tool.ToolboxHandle;
import org.lamport.tla.toolbox.ui.handler.SaveDirtyEditorAbstractHandler;
import org.lamport.tla.toolbox.util.TLAMarkerHelper;
import org.lamport.tla.toolbox.util.UIHelper;
import org.lamport.tla.toolbox.util.pref.IPreferenceConstants;
......@@ -52,13 +51,13 @@ import org.lamport.tla.toolbox.util.pref.PreferenceStoreHelper;
import pcal.Translator;
public abstract class PCalTranslator extends SaveDirtyEditorAbstractHandler {
public class PCalTranslator {
protected void translate(final TLAEditor tlaEditor, final Shell shell) throws InvocationTargetException, InterruptedException {
public void translate(final TLAEditor tlaEditor, final Shell shell) throws InvocationTargetException, InterruptedException {
translate(tlaEditor, shell, true);
}
protected void translate(final TLAEditor tlaEditor, final Shell shell, final boolean saveEditor) throws InvocationTargetException, InterruptedException {
public void translate(final TLAEditor tlaEditor, final Shell shell, final boolean saveEditor) throws InvocationTargetException, InterruptedException {
// Running the PlusCal translator takes too long for the UI thread. Thus, the
// call to the PlusCal translator call is forked off into a non-UI thread.
// However, we use a ProgressMonitorDialog to lock the UI from further
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment