diff --git a/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/console/ConsoleView.java b/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/console/ConsoleView.java index d70da6b1a0..5aae85dd2f 100644 --- a/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/console/ConsoleView.java +++ b/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/console/ConsoleView.java @@ -43,8 +43,8 @@ import ummisco.gama.ui.views.toolbar.GamaToolbarFactory; import ummisco.gama.ui.views.toolbar.IToolbarDecoratedView; -public class ConsoleView extends GamaViewPart - implements IToolbarDecoratedView.Sizable, IToolbarDecoratedView.Pausable, IGamaView.Console { +public class ConsoleView extends GamaViewPart implements IToolbarDecoratedView.Sizable, IToolbarDecoratedView.Pausable, + IToolbarDecoratedView.LogExportable, IGamaView.Console { private IOConsole msgConsole; IOConsoleViewer viewer; @@ -131,9 +131,7 @@ public void append(final String text, final ITopLevelAgent root, final GamaUICol pauseBuffer.delete(0, pauseBuffer.length() - maxMemorized - 1); pauseBuffer.insert(0, "(...)\n"); } - } else if (maxMemorized == -1) { - pauseBuffer.append(text); - } + } else if (maxMemorized == -1) { pauseBuffer.append(text); } if (!indicated) { WorkbenchHelper.run(() -> { if (toolbar != null) { @@ -167,7 +165,7 @@ public void reset() { @Override public Control getSizableFontControl() { - if (viewer == null) { return null; } + if (viewer == null) return null; return viewer.getTextWidget(); } @@ -230,4 +228,9 @@ protected boolean needsOutput() { @Override public void synchronizeChanged() {} + @Override + public String getContents() { + return viewer.getDocument().get(); + } + } diff --git a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/InteractiveConsoleView.java b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/InteractiveConsoleView.java index ef0d2c5b2c..5e5f837677 100644 --- a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/InteractiveConsoleView.java +++ b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/InteractiveConsoleView.java @@ -15,11 +15,15 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocumentListener; import org.eclipse.swt.SWT; @@ -29,6 +33,7 @@ import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.IViewSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.console.IOConsole; @@ -61,8 +66,8 @@ import ummisco.gama.ui.views.toolbar.GamaToolbarFactory; import ummisco.gama.ui.views.toolbar.IToolbarDecoratedView; -public class InteractiveConsoleView extends GamaViewPart - implements IToolbarDecoratedView.Sizable, IGamaView.Console, IExecutionContext, IVarDescriptionProvider { +public class InteractiveConsoleView extends GamaViewPart implements IToolbarDecoratedView.Sizable, + IToolbarDecoratedView.LogExportable, IGamaView.Console, IExecutionContext, IVarDescriptionProvider { private IOConsole msgConsole; IOConsoleViewer viewer; @@ -188,14 +193,10 @@ private void insertHistory(final boolean back) { return; } if (indexInHistory <= 0) { - if (back) { - ViewsHelper.requestUserAttention(this, "No more history"); - } + if (back) { ViewsHelper.requestUserAttention(this, "No more history"); } indexInHistory = 0; } else if (indexInHistory >= history.size() - 1) { - if (!back) { - ViewsHelper.requestUserAttention(this, "No more history"); - } + if (!back) { ViewsHelper.requestUserAttention(this, "No more history"); } indexInHistory = history.size() - 1; } try { @@ -227,9 +228,7 @@ public void append(final String text, final boolean error, final boolean showPro try { writer.append(text); writer.flush(); - if (showPrompt) { - showPrompt(); - } + if (showPrompt) { showPrompt(); } } catch (final IOException e) {} }); @@ -257,7 +256,7 @@ public void reset() { @Override public Control getSizableFontControl() { - if (viewer == null) { return null; } + if (viewer == null) return null; return viewer.getTextWidget(); } @@ -297,9 +296,7 @@ protected boolean needsOutput() { @Override public void append(final String text, final ITopLevelAgent agent, final GamaColor color) { setExecutorAgent(agent); - if (text != null) { - append(text, false, true); - } + if (text != null) { append(text, false, true); } } private void setExecutorAgent(final ITopLevelAgent agent) { @@ -310,9 +307,7 @@ private void setExecutorAgent(final ITopLevelAgent agent) { if (agent == null) { WorkbenchHelper.asyncRun(() -> { - if (toolbar != null && !toolbar.isDisposed()) { - toolbar.wipe(SWT.LEFT, true); - } + if (toolbar != null && !toolbar.isDisposed()) { toolbar.wipe(SWT.LEFT, true); } }); } else { scope = new ExecutionScope(agent, " in console", this); @@ -338,9 +333,7 @@ protected void processInput(final String s) { } else { try { final var expr = GAML.compileExpression(s, agent, this, false); - if (expr != null) { - result = StringUtils.toGaml(scope.evaluate(expr, agent).getValue(), true); - } + if (expr != null) { result = StringUtils.toGaml(scope.evaluate(expr, agent).getValue(), true); } } catch (final Exception e) { error = true; result = "> Error: " + e.getMessage(); @@ -348,13 +341,9 @@ protected void processInput(final String s) { agent.getSpecies().removeTemporaryAction(); } } - if (result == null) { - result = "nil"; - } + if (result == null) { result = "nil"; } append(result, error, true); - if (!error && GAMA.getExperiment() != null) { - GAMA.getExperiment().refreshAllOutputs(); - } + if (!error && GAMA.getExperiment() != null) { GAMA.getExperiment().refreshAllOutputs(); } } } @@ -368,9 +357,7 @@ public void setParentOfControlToDisplayFullScreen(final Composite parentOfContro } private IAgent getListeningAgent() { - if (scope == null) { - setExecutorAgent(GAMA.getPlatformAgent()); - } + if (scope == null) { setExecutorAgent(GAMA.getPlatformAgent()); } return scope.getRoot(); } @@ -447,4 +434,26 @@ public boolean hasAttribute(final String name) { return temps.containsKey(name); } + public String getContents() { + return viewer.getDocument().get(); + } + + @Override + public void saveAsLog() { + String text = getContents(); + FileDialog fd = new FileDialog(WorkbenchHelper.getShell(), SWT.SAVE); + fd.setText("Choose a destination file"); + fd.setFilterExtensions(new String[] { "*.log" }); + if (GAMA.getExperiment() != null && GAMA.getExperiment().getAgent() != null) { + fd.setFilterPath(GAMA.getExperiment().getAgent().getProjectPath()); + } else { + fd.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); + } + String f = fd.open(); + if (f == null) return; + try { + Files.writeString(Path.of(f), text, StandardCharsets.UTF_8); + } catch (IOException e) {} + } + } diff --git a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/GamaToolbarFactory.java b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/GamaToolbarFactory.java index 588d19577c..37afb1c34a 100644 --- a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/GamaToolbarFactory.java +++ b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/GamaToolbarFactory.java @@ -292,6 +292,11 @@ public static void buildToolbar(final IToolbarDecoratedView view, final GamaTool new CSVExportationController((IToolbarDecoratedView.CSVExportable) view); csv.install(tb); } + if (view instanceof IToolbarDecoratedView.LogExportable) { + final LogExportationController log = + new LogExportationController((IToolbarDecoratedView.LogExportable) view); + log.install(tb); + } view.createToolItems(tb); tb.refresh(true); diff --git a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/IToolbarDecoratedView.java b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/IToolbarDecoratedView.java index 54f95ca65e..f0ed3354ba 100644 --- a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/IToolbarDecoratedView.java +++ b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/IToolbarDecoratedView.java @@ -11,11 +11,21 @@ **********************************************************************************************/ package ummisco.gama.ui.views.toolbar; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.IWorkbenchSite; import msi.gama.outputs.IDisplayOutput; +import msi.gama.runtime.GAMA; import ummisco.gama.ui.resources.GamaColors.GamaUIColor; +import ummisco.gama.ui.utils.WorkbenchHelper; /** * Class IToolbarDecoratedView. @@ -58,7 +68,6 @@ public interface Sizable extends IToolbarDecoratedView { } public interface Colorizable extends IToolbarDecoratedView { - String[] getColorLabels(); GamaUIColor getColor(int index); @@ -67,13 +76,32 @@ public interface Colorizable extends IToolbarDecoratedView { } public interface CSVExportable extends IToolbarDecoratedView { - void saveAsCSV(); + } + public interface LogExportable extends IToolbarDecoratedView { + default void saveAsLog() { + String text = getContents(); + FileDialog fd = new FileDialog(WorkbenchHelper.getShell(), SWT.SAVE); + fd.setText("Choose a destination file"); + fd.setFilterExtensions(new String[] { "*.log" }); + if (GAMA.getExperiment() != null && GAMA.getExperiment().getAgent() != null) { + fd.setFilterPath(GAMA.getExperiment().getAgent().getProjectPath()); + } else { + fd.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); + } + String f = fd.open(); + if (f == null) return; + try { + Files.writeString(Path.of(f), text, StandardCharsets.UTF_8); + } catch (IOException e) {} + + } + + String getContents(); } public interface Zoomable extends IToolbarDecoratedView { - void zoomIn(); void zoomOut(); diff --git a/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/LogExportationController.java b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/LogExportationController.java new file mode 100644 index 0000000000..2119e6854f --- /dev/null +++ b/ummisco.gama.ui.shared/src/ummisco/gama/ui/views/toolbar/LogExportationController.java @@ -0,0 +1,47 @@ +/********************************************************************************************* + * + * 'CSVExportationController.java, in plugin ummisco.gama.ui.shared, is part of the source code of the GAMA modeling and + * simulation platform. (v. 1.8.1) + * + * (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners + * + * Visit https://github.com/gama-platform/gama for license information and developers contact. + * + * + **********************************************************************************************/ +package ummisco.gama.ui.views.toolbar; + +import org.eclipse.swt.SWT; + +import ummisco.gama.ui.resources.GamaIcons; +import ummisco.gama.ui.resources.IGamaIcons; +import ummisco.gama.ui.views.toolbar.IToolbarDecoratedView.LogExportable; + +/** + * Class ZoomController. + * + * @author drogoul + * @since 9 févr. 2015 + * + */ +public class LogExportationController { + + private final IToolbarDecoratedView.LogExportable view; + + /** + * @param view2 + */ + public LogExportationController(final LogExportable view2) { + this.view = view2; + } + + /** + * @param tb + */ + public void install(final GamaToolbar2 tb) { + tb.button(GamaIcons.create(IGamaIcons.DISPLAY_TOOLBAR_CSVEXPORT).getCode(), "Export to log file", + "Export to log file", e -> view.saveAsLog(), SWT.RIGHT); + + } + +}