Skip to content

Commit

Permalink
Merge pull request #133 from integratedmodelling/IM-247-UI-modificati…
Browse files Browse the repository at this point in the history
…ons-for-PeopleEA

Im 247 UI modifications for people ea
  • Loading branch information
euskalhenriko authored Feb 20, 2024
2 parents 8985387 + bf786e6 commit 701fc29
Show file tree
Hide file tree
Showing 17 changed files with 1,253 additions and 1,246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import org.integratedmodelling.klab.components.runtime.actors.extensions.Grid;
import org.integratedmodelling.klab.components.time.extents.Time;
import org.integratedmodelling.klab.components.time.extents.TimeInstant;
import org.integratedmodelling.klab.documentation.extensions.table.TableArtifact;
import org.integratedmodelling.klab.documentation.extensions.table.AbstractTableArtifact;
import org.integratedmodelling.klab.engine.resources.CoreOntology.NS;
import org.integratedmodelling.klab.engine.resources.Worldview;
import org.integratedmodelling.klab.engine.runtime.Session;
Expand Down Expand Up @@ -580,7 +580,7 @@ public void run() {
try {
File file = null;
if (dtabs) {
file = TableArtifact.exportMultiple(identity.getParentIdentity(Session.class).getState().getTables(),
file = AbstractTableArtifact.exportMultiple(identity.getParentIdentity(Session.class).getState().getTables(),
file);
} else {
file = Observations.INSTANCE.packObservations(args, identity.getMonitor());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.integratedmodelling.klab.documentation.extensions.table;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.integratedmodelling.klab.api.documentation.views.IDocumentationView;
import org.integratedmodelling.klab.api.documentation.views.ITableView;
import org.integratedmodelling.klab.api.observations.IKnowledgeView;
import org.integratedmodelling.klab.exceptions.KlabIOException;
import org.integratedmodelling.klab.exceptions.KlabValidationException;
import org.integratedmodelling.klab.provenance.Artifact;

public abstract class AbstractTableArtifact extends Artifact implements IKnowledgeView {

private Map<String, ITableView> compiledViews = new HashMap<>();

public static final String EXCEL_MEDIA_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public static final String HTML_MEDIA_TYPE = "text/html";
public static final String TEXT_PLAIN_MEDIA_TYPE = "text/plain";
public static final String MARKDOWN_MEDIA_TYPE = "text/markdown";

protected abstract IDocumentationView getCompiledView(ITableView view, int sheetId);

/**
* TODO turn into a private function that takes a table view and a sheet handle,
* so it can be generalized to >1 sheets.
*/
@Override
public IDocumentationView getCompiledView(String mediaType) {

ITableView ret = this.compiledViews.get(mediaType);
if (ret == null) {

if (HTML_MEDIA_TYPE.equals(mediaType)) {
ret = new TableView();
} else if (EXCEL_MEDIA_TYPE.equals(mediaType)) {
ret = new ExcelView();
} else if (MARKDOWN_MEDIA_TYPE.equals(mediaType)) {
ret = new MarkdownView(false);
} else if (TEXT_PLAIN_MEDIA_TYPE.equals(mediaType)) {
ret = new MarkdownView(true);
}

if (ret == null) {
throw new KlabValidationException("table view: media type " + mediaType + " is not supported");
}

getCompiledView(ret, ret.sheet(getLabel()));
this.compiledViews.put(mediaType, ret);
}
return ret;
}
/**
* Export multiple tables as one XLS file with multiple sheets.
*
* @param tables
* @param file if null, a temp file is created (erased after shutdown)
* @return
*/
public static File exportMultiple(Collection<AbstractTableArtifact> tables, File file) {

if (file == null) {
try {
file = File.createTempFile("tables", ".xlsx");
file.deleteOnExit();
} catch (IOException e) {
throw new KlabIOException(e);
}
}

ITableView ret = new ExcelView();
Map<String, Integer> sheetNames = new HashMap<String, Integer>();
for (AbstractTableArtifact table : tables) {
StringBuffer sheetName = new StringBuffer(table.getLabel());
if (sheetNames.containsKey(table.getLabel())) {
int index = sheetNames.get(table.getLabel()) + 1;
sheetName.append(" (").append(index).append(")");
sheetNames.put(table.getLabel(), index);
} else {
sheetNames.put(table.getLabel(), 0);
}
table.getCompiledView(ret, ret.sheet(sheetName.toString()));
}

try (OutputStream out = new FileOutputStream(file)) {
ret.write(out);
} catch (Throwable e) {
throw new KlabIOException(e);
}

return file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@
import org.integratedmodelling.klab.common.Geometry;
import org.integratedmodelling.klab.engine.runtime.api.IRuntimeScope;
import org.integratedmodelling.klab.exceptions.KlabIOException;
import org.integratedmodelling.klab.exceptions.KlabValidationException;
import org.integratedmodelling.klab.provenance.Artifact;
import org.integratedmodelling.klab.rest.DocumentationNode.Table;
import org.integratedmodelling.klab.rest.DocumentationNode.Table.Column;
import org.integratedmodelling.klab.rest.ObservationReference.ExportFormat;
import org.integratedmodelling.klab.utils.JsonUtils;
import org.integratedmodelling.klab.utils.NameGenerator;
import org.integratedmodelling.klab.utils.Pair;

public class SimpleTableArtifact extends Artifact implements IKnowledgeView {
public class SimpleTableArtifact extends AbstractTableArtifact {

String emptyValue = "0";
String noDataValue = "0";
Expand Down Expand Up @@ -296,34 +294,24 @@ public long getLastUpdate() {
}

@Override
public IDocumentationView getCompiledView(String mediaType) {

ITableView ret = null;

if (TableArtifact.HTML_MEDIA_TYPE.equals(mediaType)) {
ret = new TableView();
} else if (TableArtifact.EXCEL_MEDIA_TYPE.equals(mediaType)) {
ret = new ExcelView();
}

if (ret == null) {
throw new KlabValidationException("table view: media type " + mediaType + " is not supported");
}

return getCompiledView(ret, ret.sheet(tableCompiler.getLabel()));
}

private IDocumentationView getCompiledView(ITableView view, int sheetId) {
protected IDocumentationView getCompiledView(ITableView view, int sheetId) {

int hTable = view.table(this.tableCompiler.getTitle(), sheetId);
/*
* data and row titles. Row groups can go to hell for now.
* data and row titles. Single level is now managed
*/
int hBody = view.body(hTable);
int headerRow = view.newRow(hBody);
if (rowHeaders) {
view.write(view.newHeaderCell(headerRow, true), "", Double.NaN, rows.size() > 0 ? rows.get(0).style : null);
}
for (Dimension column : columns) {
view.write(view.newHeaderCell(headerRow, true), column.label != null ? column.label : "", Double.NaN, column.style);
}
for (Dimension rDesc : rows) {
int hRow = view.newRow(hBody);
for (Dimension column : columns) {
view.write(view.newHeaderCell(hRow, true), column.label, Double.NaN, rDesc.style);
if (rowHeaders) {
view.write(view.newHeaderCell(hRow, true), rDesc.label != null ? rDesc.label : "", Double.NaN, rDesc.style);
}
for (Dimension cDesc : columns) {
Cell cell = cells.get(new Pair<>(rDesc.id, cDesc.id));
Expand Down
Loading

0 comments on commit 701fc29

Please sign in to comment.