(refs #7298) Automatic refresh to SCL Console 22/622/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 14 Jun 2017 07:43:10 +0000 (10:43 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 14 Jun 2017 20:11:31 +0000 (23:11 +0300)
Change-Id: I2799a6d1afa3d3e4724c55dda81ef88cc06b975e

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java

index a8f3d2045efb105928e18c951d1322410bd13a0f..d7f1aa11e568c2d5e1351c973e5022cb9a49eb5d 100644 (file)
@@ -11,7 +11,6 @@ import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -43,6 +42,7 @@ import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.repository.ImportFailure;
 import org.simantics.scl.compiler.module.repository.ImportFailureException;
 import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
 import org.simantics.scl.compiler.top.ExpressionEvaluator;
 import org.simantics.scl.compiler.top.LocalStorage;
@@ -76,6 +76,7 @@ public class CommandSession {
     THashMap<String,Type> variableTypes = new THashMap<String,Type>();
     
     PrintStream fileOutput;
+    private UpdateListener dependenciesListener;
     
     /**
      * Only checks the commands for compilation errors but does not run them.
@@ -108,10 +109,13 @@ public class CommandSession {
         
         runtimeEnvironment = null;
         try {
+            if(dependenciesListener != null)
+                dependenciesListener.stopListening();
             try {
                 runtimeEnvironment = moduleRepository.createRuntimeEnvironment(
                         environmentSpecification,
-                        getClass().getClassLoader());
+                        getClass().getClassLoader(),
+                        dependenciesListener);
             } catch(ImportFailureException e) {
                 THashSet<String> failedModules = new THashSet<String>();
                 for(ImportFailure failure : e.failures) {
@@ -129,7 +133,7 @@ public class CommandSession {
                 try {
                     runtimeEnvironment = moduleRepository.createRuntimeEnvironment(
                             environmentSpecification,
-                            getClass().getClassLoader());
+                            getClass().getClassLoader()); // no listener here, because should listen also failed modules
                 } catch (ImportFailureException e1) {
                     for(ImportFailure failure : e1.failures)
                         defaultHandler.printError(failure.toString());
@@ -665,4 +669,7 @@ public class CommandSession {
         return validate(new StringReader(command));
     }
 
+    public void setDependenciesListener(UpdateListener dependenciesListener) {
+        this.dependenciesListener = dependenciesListener;
+    }
 }
index cb2181912e1464b5978298c6b7314e81bdff6e48..a7eb4332333cc0cba1a4325c36e69b829c7a7a75 100644 (file)
@@ -10,6 +10,8 @@ import java.util.List;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuCreator;
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.preference.IPersistentPreferenceStore;
@@ -20,11 +22,17 @@ import org.eclipse.swt.dnd.DropTargetAdapter;
 import org.eclipse.swt.dnd.DropTargetEvent;
 import org.eclipse.swt.dnd.FileTransfer;
 import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.ui.part.ViewPart;
 import org.eclipse.ui.preferences.ScopedPreferenceStore;
 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
 import org.simantics.scl.compiler.commands.SCLConsoleListener;
+import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.testing.TestRunnable;
 import org.simantics.scl.osgi.internal.TestUtils;
 import org.simantics.scl.ui.Activator;
@@ -35,13 +43,16 @@ public class SCLConsoleView extends ViewPart {
 
     public static final String PLUGIN_ID = "org.simantics.scl.ui";
     public static final String IMPORTS = "imports";
+    public static final String REFRESH_AUTOMATICALLY = "refresh-automatically";
     public static final String SEPARATOR = ";";
     public static final String DISABLED_TAG = "[DISABLED]";
     
+    IPersistentPreferenceStore store;
     SCLConsole console;
+    boolean refreshAutomatically = false;
+    MenuItem refreshAutomaticallyItem;
     
     private ArrayList<CommandSessionImportEntry> readImportPreferences() {
-        IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
         String importsString = store.getString(IMPORTS);
         
         String[] splitted = importsString.split(SEPARATOR);
@@ -86,7 +97,7 @@ public class SCLConsoleView extends ViewPart {
             }
         
         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
-        store.putValue(IMPORTS, b.toString());
+        store.setValue(IMPORTS, b.toString());
     }
     
     private ArrayList<CommandSessionImportEntry> getCurrentImports() {
@@ -127,10 +138,39 @@ public class SCLConsoleView extends ViewPart {
             }
         }
     }
+    
+    private UpdateListener dependencyListener = new UpdateListener() {
+        @Override
+        public void notifyAboutUpdate() {
+            if(refreshAutomatically)
+                console.getSession().updateRuntimeEnvironment(true);
+        }
+    };
 
+    private void setRefreshAutomatically(boolean refreshAutomatically, boolean refreshAlso) {
+        this.refreshAutomatically = refreshAutomatically;
+        if(refreshAutomaticallyItem != null)
+            refreshAutomaticallyItem.setSelection(refreshAutomatically);
+        
+        store.setValue(REFRESH_AUTOMATICALLY, refreshAutomatically);
+        
+        if(refreshAutomatically) {
+            console.getSession().setDependenciesListener(dependencyListener);
+            if(refreshAlso)
+                console.getSession().updateRuntimeEnvironment(true);
+        }
+        else {
+            console.getSession().setDependenciesListener(null);
+            dependencyListener.stopListening();
+        }
+    }
+    
     @Override
     public void createPartControl(Composite parent) {
+        store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
         this.console = new SCLConsole(parent, SWT.NONE);
+        
+        setRefreshAutomatically(store.getBoolean(REFRESH_AUTOMATICALLY), false);
         setCurrentImports(readImportPreferences());
 
         addScriptDropSupport(console);
@@ -179,8 +219,40 @@ public class SCLConsoleView extends ViewPart {
         });
         
         // Refresh action
-        toolBarManager.add(new Action("Refresh modules",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png")) {
+        toolBarManager.add(new Action("Refresh modules", IAction.AS_DROP_DOWN_MENU) {
+            {
+                setImageDescriptor(Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png"));
+                setMenuCreator(new IMenuCreator() {
+                    Menu menu;
+                    @Override
+                    public Menu getMenu(Menu parent) {
+                        throw new UnsupportedOperationException();
+                    }
+                    
+                    @Override
+                    public Menu getMenu(Control parent) {
+                        if(menu == null) {
+                            menu = new Menu(parent);
+                            refreshAutomaticallyItem = new MenuItem(menu, SWT.CHECK);
+                            refreshAutomaticallyItem.setText("Refresh automatically");
+                            refreshAutomaticallyItem.setSelection(refreshAutomatically);
+                            refreshAutomaticallyItem.addSelectionListener(new SelectionAdapter() {
+                                @Override
+                                public void widgetSelected(SelectionEvent e) {
+                                    setRefreshAutomatically(!refreshAutomatically, true);
+                                }
+                            });
+                        }
+                        return menu;
+                    }
+                    
+                    @Override
+                    public void dispose() {
+                        if(menu != null)
+                            menu.dispose();
+                    }
+                });
+            }
             @Override
             public void run() {
                 console.getSession().getModuleRepository().getSourceRepository().checkUpdates();