X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Fconsole%2FSCLConsole.java;h=7221158c1bacfbfa8d91e27791ab84a49c8ee388;hb=f62bab9b78e60b94e055d51db98a03141415323e;hp=59908c870a1e51c60474ab83cc18ea20f54bbfed;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java index 59908c870..7221158c1 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java @@ -1,7 +1,5 @@ package org.simantics.scl.ui.console; -import gnu.trove.set.hash.THashSet; - import java.util.ArrayList; import org.eclipse.core.runtime.IProgressMonitor; @@ -26,6 +24,10 @@ import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.scl.ui.Activator; import org.simantics.scl.ui.assist.SCLContentProposalProvider; import org.simantics.scl.ui.assist.StyledTextContentAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import gnu.trove.set.hash.THashSet; /** * An SCL console with input and output area that can be embedded @@ -33,10 +35,17 @@ import org.simantics.scl.ui.assist.StyledTextContentAdapter; * @author Hannu Niemistö */ public class SCLConsole extends AbstractCommandConsole { + + /** + * Use this option mask to exclude {@link SCLConsoleListener}s contributed as + * OSGi services from listening to this console. + */ + public static final int EXCLUDE_CONTRIBUTED_LISTENERS = 1 << 10; + public static final String JOB_NAME = "org.simantics.scl.console.job"; + public static final long TERMINATE_GRACE_PERIOD = 1000L; private THashSet currentJobs = new THashSet(); - private Thread currentThread; private final IdentitySchedulingRule schedulingRule = new IdentitySchedulingRule(); private ArrayList listeners = new ArrayList(2); private boolean consoleIsEmpty = true; @@ -58,13 +67,24 @@ public class SCLConsole extends AbstractCommandConsole { CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler); ContentProposalAdapter contentProposalAdapter; - + public SCLConsole(Composite parent, int style) { - super(parent, style); - + this(parent, style, 0); + } + + public SCLConsole(Composite parent, int style, int options) { + super(parent, style, options); + createContentProposalAdapter(); + if (!hasOption(EXCLUDE_CONTRIBUTED_LISTENERS)) + addContributedListeners(); + } + + protected void createContentProposalAdapter() { + if (input == null) + return; + StyledTextContentAdapter styledTextContentAdapter = new StyledTextContentAdapter(); SCLContentProposalProvider contentProvider = new SCLContentProposalProvider(session); - try { contentProposalAdapter = new ContentProposalAdapter( input, @@ -76,15 +96,13 @@ public class SCLConsole extends AbstractCommandConsole { } catch (ParseException e) { // No content assist then. } - - addContributedListeners(); } @Override protected boolean canExecuteCommand() { - return !contentProposalAdapter.isProposalPopupOpen(); + return contentProposalAdapter == null || !contentProposalAdapter.isProposalPopupOpen(); } - + @Override public ErrorAnnotation[] validate(String command) { if(command.isEmpty()) @@ -126,14 +144,10 @@ public class SCLConsole extends AbstractCommandConsole { @Override protected IStatus run(IProgressMonitor monitor) { try { - synchronized(currentJobs) { - currentJobs.remove(this); - currentThread = Thread.currentThread(); - } session.execute(command, handler); } finally { synchronized(currentJobs) { - currentThread = null; + currentJobs.remove(this); if(currentJobs.isEmpty()) for(SCLConsoleListener listener : listeners) listener.finishedExecution(); @@ -141,6 +155,24 @@ public class SCLConsole extends AbstractCommandConsole { } return Status.OK_STATUS; } + + @Override + protected void canceling() { + Thread thread = getThread(); + if(thread != null) + thread.interrupt(); + + try { + Thread.sleep(TERMINATE_GRACE_PERIOD); + } catch (InterruptedException e) { + e.printStackTrace(); + return; + } + + thread = getThread(); + if(thread != null) + thread.stop(); + } }; job.setRule(schedulingRule); synchronized(currentJobs) { @@ -159,13 +191,16 @@ public class SCLConsole extends AbstractCommandConsole { public CommandSession getSession() { return session; } + + public SCLReportingHandler getHandler() { + return handler; + } + public void interruptCurrentCommands() { synchronized(currentJobs) { for(Job job : currentJobs) job.cancel(); currentJobs.clear(); - if(currentThread != null) - currentThread.interrupt(); } } @@ -199,7 +234,7 @@ public class SCLConsole extends AbstractCommandConsole { consoleIsEmpty = true; } - private void addContributedListeners() { + protected void addContributedListeners() { final BundleContext context = Activator.getInstance().getBundle().getBundleContext(); new ServiceTracker(context, SCLConsoleListener.class, null) { @@ -225,4 +260,10 @@ public class SCLConsole extends AbstractCommandConsole { } }.open(); } + + @Override + public Logger getLogger() { + return LoggerFactory.getLogger(getClass()); + } + }