]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java
Fixed SCL Console Clear Console active-state when imports are erroneous
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / SCLConsoleView.java
index 5a0d75fbbd25234b9f2e5c2a993abe89e4d34abb..67de0b2ffe9e02571d75ec114c22416498e51274 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,13 +22,21 @@ 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.CommandSession;
 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.runtime.reporting.SCLReportingHandler;
 import org.simantics.scl.ui.Activator;
 import org.simantics.scl.ui.imports.internal.ManageImportsDialog;
 import org.simantics.scl.ui.tests.SCLTestsDialog;
@@ -35,13 +45,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 +99,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,42 +140,50 @@ 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);
+        store.setDefault(REFRESH_AUTOMATICALLY, true);
+        
         this.console = new SCLConsole(parent, SWT.NONE);
-        setCurrentImports(readImportPreferences());
-
-        addScriptDropSupport(console);
 
         IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
         
         // Interrupt action
-        final Action interruptAction = new Action("Interrupt current command",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop.png")) {
-            @Override
-            public void run() {
-                console.interruptCurrentCommands();
-            }
-        };
-        interruptAction.setDisabledImageDescriptor(
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop_disabled.png"));
-        interruptAction.setEnabled(false);
+        Action interruptAction = ConsoleActions.createInterruptAction(console);
         toolBarManager.add(interruptAction);
         
         // Clear console action
-        final Action clearAction = new Action("Clear console",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console.png")) {
-            @Override
-            public void run() {
-                setEnabled(false);
-                console.clear();
-            }
-        };
-        clearAction.setDisabledImageDescriptor(
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console_disabled.png"));
-        clearAction.setEnabled(false);
+        Action clearAction = ConsoleActions.createClearAction(console);
         toolBarManager.add(clearAction);
+        
         console.addListener(new SCLConsoleListener() {
             @Override
             public void startedExecution() {
@@ -179,8 +200,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();
@@ -195,7 +248,7 @@ public class SCLConsoleView extends ViewPart {
                 manageImports();
             }
         });
-        
+
         // Show action for running SCL tests if in development mode
         if (Platform.inDevelopmentMode()) {
             toolBarManager.add(new Action("Run tests",
@@ -208,9 +261,29 @@ public class SCLConsoleView extends ViewPart {
         }
         
         toolBarManager.update(true);
+
+        setRefreshAutomatically(store.getBoolean(REFRESH_AUTOMATICALLY), false);
+        // Do this after the actions and SCLConsoleListener are
+        // registered because it can cause output to the console.
+        setCurrentImports(readImportPreferences());
+        addScriptDropSupport(console);
     }
 
     private class ScriptRunningDropTarget extends DropTargetAdapter {
+        @Override
+        public void dragEnter(DropTargetEvent event) {
+            if (event.detail == DND.DROP_DEFAULT) {
+                event.detail = DND.DROP_LINK;
+            }
+        }
+
+        @Override
+        public void dragOperationChanged(DropTargetEvent event) {
+            if (event.detail == DND.DROP_DEFAULT) {
+                event.detail = DND.DROP_LINK;
+            }
+        }
+
         public void drop(DropTargetEvent event) {
             if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
                 String[] files = ((String[]) event.data).clone();
@@ -233,7 +306,7 @@ public class SCLConsoleView extends ViewPart {
     }
 
     private void addScriptDropSupport(SCLConsole console) {
-        DropTarget target = new DropTarget(console.getOutputWidget(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
+        DropTarget target = new DropTarget(console.getOutputWidget(), DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_DEFAULT);
         target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
         target.addDropListener(new ScriptRunningDropTarget());
     }
@@ -249,4 +322,14 @@ public class SCLConsoleView extends ViewPart {
         console.dispose();
     }
 
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getAdapter(Class<T> adapter) {
+        if (adapter == CommandSession.class)
+            return (T) console.getSession();
+        if (adapter == SCLReportingHandler.class)
+            return (T) console.getHandler();
+        return super.getAdapter(adapter);
+    }
+
 }