Skip to content

Commit

Permalink
Fixes #3047 by supporting saving consoles to log files
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Aug 17, 2021
1 parent b8cfde4 commit 459548b
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 39 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -167,7 +165,7 @@ public void reset() {

@Override
public Control getSizableFontControl() {
if (viewer == null) { return null; }
if (viewer == null) return null;
return viewer.getTextWidget();
}

Expand Down Expand Up @@ -230,4 +228,9 @@ protected boolean needsOutput() {
@Override
public void synchronizeChanged() {}

@Override
public String getContents() {
return viewer.getDocument().get();
}

}
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {}

});
Expand Down Expand Up @@ -257,7 +256,7 @@ public void reset() {

@Override
public Control getSizableFontControl() {
if (viewer == null) { return null; }
if (viewer == null) return null;
return viewer.getTextWidget();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -338,23 +333,17 @@ 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();
} finally {
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(); }
}

}
Expand All @@ -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();
}

Expand Down Expand Up @@ -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) {}
}

}
Expand Up @@ -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);
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -58,7 +68,6 @@ public interface Sizable extends IToolbarDecoratedView {
}

public interface Colorizable extends IToolbarDecoratedView {

String[] getColorLabels();

GamaUIColor getColor(int index);
Expand All @@ -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();
Expand Down
@@ -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);

}

}

0 comments on commit 459548b

Please sign in to comment.