1 package org.simantics.scl.ui.console;
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;
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.IAction;
14 import org.eclipse.jface.action.IMenuCreator;
15 import org.eclipse.jface.action.IToolBarManager;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.preference.IPersistentPreferenceStore;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.dnd.DND;
20 import org.eclipse.swt.dnd.DropTarget;
21 import org.eclipse.swt.dnd.DropTargetAdapter;
22 import org.eclipse.swt.dnd.DropTargetEvent;
23 import org.eclipse.swt.dnd.FileTransfer;
24 import org.eclipse.swt.dnd.Transfer;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31 import org.eclipse.ui.part.ViewPart;
32 import org.eclipse.ui.preferences.ScopedPreferenceStore;
33 import org.simantics.scl.compiler.commands.CommandSession;
34 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
35 import org.simantics.scl.compiler.commands.SCLConsoleListener;
36 import org.simantics.scl.compiler.module.repository.UpdateListener;
37 import org.simantics.scl.compiler.testing.TestRunnable;
38 import org.simantics.scl.osgi.internal.TestUtils;
39 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
40 import org.simantics.scl.ui.Activator;
41 import org.simantics.scl.ui.imports.internal.ManageImportsDialog;
42 import org.simantics.scl.ui.tests.SCLTestsDialog;
44 public class SCLConsoleView extends ViewPart {
46 public static final String PLUGIN_ID = "org.simantics.scl.ui";
47 public static final String IMPORTS = "imports";
48 public static final String REFRESH_AUTOMATICALLY = "refresh-automatically";
49 public static final String SEPARATOR = ";";
50 public static final String DISABLED_TAG = "[DISABLED]";
52 IPersistentPreferenceStore store;
54 boolean refreshAutomatically = false;
55 MenuItem refreshAutomaticallyItem;
57 private ArrayList<CommandSessionImportEntry> readImportPreferences() {
58 String importsString = store.getString(IMPORTS);
60 String[] splitted = importsString.split(SEPARATOR);
61 ArrayList<CommandSessionImportEntry> result = new ArrayList<CommandSessionImportEntry>(splitted.length);
62 for(String entryString : splitted) {
63 if(entryString.isEmpty())
65 boolean disabled = false;
66 if(entryString.startsWith(DISABLED_TAG)) {
68 entryString = entryString.substring(DISABLED_TAG.length());
70 String[] parts = entryString.split("=");
71 CommandSessionImportEntry entry;
73 entry = new CommandSessionImportEntry(parts[0], "", true);
75 entry = new CommandSessionImportEntry(parts[1], parts[0], true);
76 entry.disabled = disabled;
82 private void writeImportPreferences(ArrayList<CommandSessionImportEntry> entries) {
83 StringBuilder b = new StringBuilder();
86 for(CommandSessionImportEntry entry : entries)
87 if(entry.persistent) {
93 b.append(DISABLED_TAG);
94 if(!entry.localName.isEmpty()) {
95 b.append(entry.localName);
98 b.append(entry.moduleName);
101 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
102 store.setValue(IMPORTS, b.toString());
105 private ArrayList<CommandSessionImportEntry> getCurrentImports() {
106 return console.getSession().getImportEntries();
109 private void setCurrentImports(ArrayList<CommandSessionImportEntry> entries) {
110 console.getSession().setImportEntries(entries);
113 private void manageImports() {
114 ManageImportsDialog dialog = new ManageImportsDialog(
115 getSite().getShell(),
116 getCurrentImports());
117 if(dialog.open() == Dialog.OK) {
118 writeImportPreferences(dialog.getImports());
119 setCurrentImports(dialog.getImports());
123 private void sclTestDialog() {
124 List<TestRunnable> tests = TestUtils.getTests();
125 SCLTestsDialog dialog = new SCLTestsDialog(
126 getSite().getShell(),
128 if(dialog.open() == Dialog.OK) {
129 for(Object result : dialog.getResult()) {
130 TestRunnable test = (TestRunnable) result;
132 // Bit of a haxx solution to get around a deadlock caused by simply
133 // running the test with test.run()
134 console.execute("import \"Commands/Tests\"");
135 console.execute("runByName \"" + test.getName() + "\"");
137 } catch (Exception e) {
144 private UpdateListener dependencyListener = new UpdateListener() {
146 public void notifyAboutUpdate() {
147 if(refreshAutomatically)
148 console.getSession().updateRuntimeEnvironment(true);
152 private void setRefreshAutomatically(boolean refreshAutomatically, boolean refreshAlso) {
153 this.refreshAutomatically = refreshAutomatically;
154 if(refreshAutomaticallyItem != null)
155 refreshAutomaticallyItem.setSelection(refreshAutomatically);
157 store.setValue(REFRESH_AUTOMATICALLY, refreshAutomatically);
159 if(refreshAutomatically) {
160 console.getSession().setDependenciesListener(dependencyListener);
162 console.getSession().updateRuntimeEnvironment(true);
165 console.getSession().setDependenciesListener(null);
166 dependencyListener.stopListening();
171 public void createPartControl(Composite parent) {
172 store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
173 this.console = new SCLConsole(parent, SWT.NONE);
175 setRefreshAutomatically(store.getBoolean(REFRESH_AUTOMATICALLY), false);
176 setCurrentImports(readImportPreferences());
178 addScriptDropSupport(console);
180 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
183 final Action interruptAction = new Action("Interrupt current command",
184 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop.png")) {
187 console.interruptCurrentCommands();
190 interruptAction.setDisabledImageDescriptor(
191 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop_disabled.png"));
192 interruptAction.setEnabled(false);
193 toolBarManager.add(interruptAction);
195 // Clear console action
196 final Action clearAction = new Action("Clear console",
197 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console.png")) {
204 clearAction.setDisabledImageDescriptor(
205 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console_disabled.png"));
206 clearAction.setEnabled(false);
207 toolBarManager.add(clearAction);
208 console.addListener(new SCLConsoleListener() {
210 public void startedExecution() {
211 interruptAction.setEnabled(true);
214 public void finishedExecution() {
215 interruptAction.setEnabled(false);
218 public void consoleIsNotEmptyAnymore() {
219 clearAction.setEnabled(true);
224 toolBarManager.add(new Action("Refresh modules", IAction.AS_DROP_DOWN_MENU) {
226 setImageDescriptor(Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png"));
227 setMenuCreator(new IMenuCreator() {
230 public Menu getMenu(Menu parent) {
231 throw new UnsupportedOperationException();
235 public Menu getMenu(Control parent) {
237 menu = new Menu(parent);
238 refreshAutomaticallyItem = new MenuItem(menu, SWT.CHECK);
239 refreshAutomaticallyItem.setText("Refresh automatically");
240 refreshAutomaticallyItem.setSelection(refreshAutomatically);
241 refreshAutomaticallyItem.addSelectionListener(new SelectionAdapter() {
243 public void widgetSelected(SelectionEvent e) {
244 setRefreshAutomatically(!refreshAutomatically, true);
252 public void dispose() {
260 console.getSession().getModuleRepository().getSourceRepository().checkUpdates();
261 console.getSession().updateRuntimeEnvironment(true);
262 console.appendOutput("refresh completed\n", console.greenColor, null);
265 toolBarManager.add(new Action("Manage imports",
266 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/configure_imports.png")) {
273 // Show action for running SCL tests if in development mode
274 if (Platform.inDevelopmentMode()) {
275 toolBarManager.add(new Action("Run tests",
276 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/run_tests.png")) {
284 toolBarManager.update(true);
287 private class ScriptRunningDropTarget extends DropTargetAdapter {
289 public void dragEnter(DropTargetEvent event) {
290 if (event.detail == DND.DROP_DEFAULT) {
291 event.detail = DND.DROP_LINK;
296 public void dragOperationChanged(DropTargetEvent event) {
297 if (event.detail == DND.DROP_DEFAULT) {
298 event.detail = DND.DROP_LINK;
302 public void drop(DropTargetEvent event) {
303 if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
304 String[] files = ((String[]) event.data).clone();
305 // Sort files by name to allow deterministic multi-file drop
307 for (String file : files) {
308 Path p = Paths.get(file).toAbsolutePath();
309 if (isScriptFile(p)) {
310 console.execute("runFromFile \"" + p.toString().replace('\\', '/') + "\"");
316 private boolean isScriptFile(Path p) {
317 return Files.isRegularFile(p)
318 //&& p.toString().toLowerCase().endsWith(".scl")
323 private void addScriptDropSupport(SCLConsole console) {
324 DropTarget target = new DropTarget(console.getOutputWidget(), DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_DEFAULT);
325 target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
326 target.addDropListener(new ScriptRunningDropTarget());
330 public void setFocus() {
335 public void dispose() {
340 @SuppressWarnings("unchecked")
342 public <T> T getAdapter(Class<T> adapter) {
343 if (adapter == CommandSession.class)
344 return (T) console.getSession();
345 if (adapter == SCLReportingHandler.class)
346 return (T) console.getHandler();
347 return super.getAdapter(adapter);