]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java
Externalize strings in org.simantics.scl.ui
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / SCLConsole.java
index 59908c870a1e51c60474ab83cc18ea20f54bbfed..9df117f7e1288ef63869002bfde0952f8a42249e 100644 (file)
@@ -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 {
-       public static final String JOB_NAME = "org.simantics.scl.console.job";
+
+    /**
+     * 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"; //$NON-NLS-1$
+       public static final long TERMINATE_GRACE_PERIOD = 1000L;
        
        private THashSet<Job> currentJobs = new THashSet<Job>();
-       private Thread currentThread;
        private final IdentitySchedulingRule schedulingRule = new IdentitySchedulingRule();
        private ArrayList<SCLConsoleListener> listeners = new ArrayList<SCLConsoleListener>(2);
        private boolean consoleIsEmpty = true;
@@ -44,47 +53,56 @@ public class SCLConsole extends AbstractCommandConsole {
        SCLReportingHandler handler = new AbstractSCLReportingHandler() {
         @Override
         public void print(String text) {
-            appendOutput(text + "\n", null, null);
+            appendOutput(text + "\n", null, null); //$NON-NLS-1$
         }
         @Override
         public void printError(String error) {
-            appendOutput(error + "\n", redColor, null);
+            appendOutput(error + "\n", redColor, null); //$NON-NLS-1$
         }
         @Override
         public void printCommand(String command) {
-            appendOutput("> " + command.replace("\n", "\n  ") + "\n", greenColor, null);
+            appendOutput("> " + command.replace("\n", "\n  ") + "\n", greenColor, null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
         }
     };
 
     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, 
                     styledTextContentAdapter, 
                     contentProvider, 
-                    KeyStroke.getInstance("Ctrl+Space"), 
+                    KeyStroke.getInstance("Ctrl+Space"),  //$NON-NLS-1$
                     null);
             contentProposalAdapter.setAutoActivationDelay(200);
         } 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())
@@ -117,7 +135,7 @@ public class SCLConsole extends AbstractCommandConsole {
     }
     
     private String jobNameFromCommand(String command) {
-        return command.split("\n")[0];
+        return command.split("\n")[0]; //$NON-NLS-1$
     }
 
     @Override
@@ -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<SCLConsoleListener, SCLConsoleListener>(context,
                 SCLConsoleListener.class, null) {
@@ -225,4 +260,10 @@ public class SCLConsole extends AbstractCommandConsole {
                     }
                 }.open();
     }
+
+    @Override
+    public Logger getLogger() {
+        return LoggerFactory.getLogger(getClass());
+    }
+
 }