]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java
Testing small changes to SCL console view drop target handling.
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / SCLConsoleView.java
1 package org.simantics.scl.ui.console;
2
3 import java.nio.file.Files;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.List;
9
10 import org.eclipse.core.runtime.Platform;
11 import org.eclipse.core.runtime.preferences.InstanceScope;
12 import org.eclipse.jface.action.Action;
13 import org.eclipse.jface.action.IToolBarManager;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.preference.IPersistentPreferenceStore;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.dnd.DND;
18 import org.eclipse.swt.dnd.DropTarget;
19 import org.eclipse.swt.dnd.DropTargetAdapter;
20 import org.eclipse.swt.dnd.DropTargetEvent;
21 import org.eclipse.swt.dnd.FileTransfer;
22 import org.eclipse.swt.dnd.Transfer;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.part.ViewPart;
25 import org.eclipse.ui.preferences.ScopedPreferenceStore;
26 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
27 import org.simantics.scl.compiler.commands.SCLConsoleListener;
28 import org.simantics.scl.compiler.testing.TestRunnable;
29 import org.simantics.scl.osgi.internal.TestUtils;
30 import org.simantics.scl.ui.Activator;
31 import org.simantics.scl.ui.imports.internal.ManageImportsDialog;
32 import org.simantics.scl.ui.tests.SCLTestsDialog;
33
34 public class SCLConsoleView extends ViewPart {
35
36     public static final String PLUGIN_ID = "org.simantics.scl.ui";
37     public static final String IMPORTS = "imports";
38     public static final String SEPARATOR = ";";
39     public static final String DISABLED_TAG = "[DISABLED]";
40     
41     SCLConsole console;
42     
43     private ArrayList<CommandSessionImportEntry> readImportPreferences() {
44         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
45         String importsString = store.getString(IMPORTS);
46         
47         String[] splitted = importsString.split(SEPARATOR);
48         ArrayList<CommandSessionImportEntry> result = new ArrayList<CommandSessionImportEntry>(splitted.length);
49         for(String entryString : splitted) {
50             if(entryString.isEmpty())
51                 continue;
52             boolean disabled = false;
53             if(entryString.startsWith(DISABLED_TAG)) {
54                 disabled = true;
55                 entryString = entryString.substring(DISABLED_TAG.length());
56             }
57             String[] parts = entryString.split("=");
58             CommandSessionImportEntry entry;
59             if(parts.length == 1)
60                 entry = new CommandSessionImportEntry(parts[0], "", true);
61             else
62                 entry = new CommandSessionImportEntry(parts[1], parts[0], true);
63             entry.disabled = disabled;
64             result.add(entry);
65         }
66         return result;
67     }
68     
69     private void writeImportPreferences(ArrayList<CommandSessionImportEntry> entries) {
70         StringBuilder b = new StringBuilder();
71         
72         boolean first = true;
73         for(CommandSessionImportEntry entry : entries)
74             if(entry.persistent) {
75                 if(first)
76                     first = false;
77                 else
78                     b.append(SEPARATOR);
79                 if(entry.disabled)
80                     b.append(DISABLED_TAG);
81                 if(!entry.localName.isEmpty()) {
82                     b.append(entry.localName);
83                     b.append("=");
84                 }
85                 b.append(entry.moduleName);
86             }
87         
88         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
89         store.putValue(IMPORTS, b.toString());
90     }
91     
92     private ArrayList<CommandSessionImportEntry> getCurrentImports() {
93         return console.getSession().getImportEntries();
94     }
95     
96     private void setCurrentImports(ArrayList<CommandSessionImportEntry> entries) {
97         console.getSession().setImportEntries(entries);
98     }
99     
100     private void manageImports() {
101         ManageImportsDialog dialog = new ManageImportsDialog(
102                 getSite().getShell(),
103                 getCurrentImports());
104         if(dialog.open() == Dialog.OK) {
105             writeImportPreferences(dialog.getImports());
106             setCurrentImports(dialog.getImports());
107         }
108     }
109     
110     private void sclTestDialog() {
111         List<TestRunnable> tests = TestUtils.getTests();
112         SCLTestsDialog dialog = new SCLTestsDialog(
113                 getSite().getShell(),
114                 tests, true);
115         if(dialog.open() == Dialog.OK) {
116             for(Object result : dialog.getResult()) {
117                 TestRunnable test = (TestRunnable) result;
118                 try {
119                     // Bit of a haxx solution to get around a deadlock caused by simply
120                     // running the test with test.run()
121                     console.execute("import \"Commands/Tests\"");
122                     console.execute("runByName \"" + test.getName() + "\"");
123 //                    test.run();
124                 } catch (Exception e) {
125                     e.printStackTrace();
126                 }
127             }
128         }
129     }
130
131     @Override
132     public void createPartControl(Composite parent) {
133         this.console = new SCLConsole(parent, SWT.NONE);
134         setCurrentImports(readImportPreferences());
135
136         addScriptDropSupport(console);
137
138         IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
139         
140         // Interrupt action
141         final Action interruptAction = new Action("Interrupt current command",
142                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop.png")) {
143             @Override
144             public void run() {
145                 console.interruptCurrentCommands();
146             }
147         };
148         interruptAction.setDisabledImageDescriptor(
149                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop_disabled.png"));
150         interruptAction.setEnabled(false);
151         toolBarManager.add(interruptAction);
152         
153         // Clear console action
154         final Action clearAction = new Action("Clear console",
155                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console.png")) {
156             @Override
157             public void run() {
158                 setEnabled(false);
159                 console.clear();
160             }
161         };
162         clearAction.setDisabledImageDescriptor(
163                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console_disabled.png"));
164         clearAction.setEnabled(false);
165         toolBarManager.add(clearAction);
166         console.addListener(new SCLConsoleListener() {
167             @Override
168             public void startedExecution() {
169                 interruptAction.setEnabled(true);
170             }
171             @Override
172             public void finishedExecution() {
173                 interruptAction.setEnabled(false);
174             }
175             @Override
176             public void consoleIsNotEmptyAnymore() {
177                 clearAction.setEnabled(true);
178             }
179         });
180         
181         // Refresh action
182         toolBarManager.add(new Action("Refresh modules",
183                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png")) {
184             @Override
185             public void run() {
186                 console.getSession().getModuleRepository().getSourceRepository().checkUpdates();
187                 console.getSession().updateRuntimeEnvironment(true);
188                 console.appendOutput("refresh completed\n", console.greenColor, null);
189             }
190         });
191         toolBarManager.add(new Action("Manage imports",
192                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/configure_imports.png")) {
193             @Override
194             public void run() {
195                 manageImports();
196             }
197         });
198         
199         // Show action for running SCL tests if in development mode
200         if (Platform.inDevelopmentMode()) {
201             toolBarManager.add(new Action("Run tests",
202                     Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/run_tests.png")) {
203                 @Override
204                 public void run() {
205                     sclTestDialog();
206                 }
207             });
208         }
209         
210         toolBarManager.update(true);
211     }
212
213     private class ScriptRunningDropTarget extends DropTargetAdapter {
214         @Override
215         public void dragEnter(DropTargetEvent event) {
216             if (event.detail == DND.DROP_DEFAULT) {
217                 event.detail = DND.DROP_LINK;
218             }
219         }
220
221         @Override
222         public void dragOperationChanged(DropTargetEvent event) {
223             if (event.detail == DND.DROP_DEFAULT) {
224                 event.detail = DND.DROP_LINK;
225             }
226         }
227
228         public void drop(DropTargetEvent event) {
229             if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
230                 String[] files = ((String[]) event.data).clone();
231                 // Sort files by name to allow deterministic multi-file drop
232                 Arrays.sort(files);
233                 for (String file : files) {
234                     Path p = Paths.get(file).toAbsolutePath();
235                     if (isScriptFile(p)) {
236                         console.execute("runFromFile \"" + p.toString().replace('\\', '/') + "\"");
237                     }
238                 }
239             }
240         }
241
242         private boolean isScriptFile(Path p) {
243             return Files.isRegularFile(p)
244                     //&& p.toString().toLowerCase().endsWith(".scl")
245                     ;
246         }
247     }
248
249     private void addScriptDropSupport(SCLConsole console) {
250         DropTarget target = new DropTarget(console.getOutputWidget(), DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_DEFAULT);
251         target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
252         target.addDropListener(new ScriptRunningDropTarget());
253     }
254
255     @Override
256     public void setFocus() {
257         console.setFocus();
258     }
259     
260     @Override
261     public void dispose() {
262         super.dispose();
263         console.dispose();
264     }
265
266 }