From 4368cb9d8c5e57492c9f29c6c7940148bfa2cc33 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 12 Jul 2011 08:03:06 +0000 Subject: [PATCH] Support for sheet in the old model browser git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21471 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/icons/table.png | Bin 0 -> 566 bytes org.simantics.sysdyn.ui/plugin.xml | 12 +++ .../plugin_customization.ini | 1 + .../contributions/AbstractNodeImager.java | 6 ++ .../sysdyn/ui/browser/contributions/Book.java | 29 ++++++ .../ui/browser/contributions/BookLabeler.java | 15 +++ .../browser/contributions/Configuration.java | 16 ++- .../browser/contributions/SheetLabeler.java | 15 +++ .../sysdyn/ui/browser/nodes/BookNode.java | 19 ++++ .../sysdyn/ui/browser/nodes/SheetNode.java | 81 +++++++++++++++ .../ui/handlers/ImportModelHandler.java | 94 ++++++++++++++++++ .../expressionParser/ExpressionParser.jj | 4 +- 12 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 org.simantics.sysdyn.ui/icons/table.png create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Book.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/BookLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SheetLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/BookNode.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SheetNode.java diff --git a/org.simantics.sysdyn.ui/icons/table.png b/org.simantics.sysdyn.ui/icons/table.png new file mode 100644 index 0000000000000000000000000000000000000000..abcd93689a08ec9bdbf0984927e8da06c043c7cd GIT binary patch literal 566 zcmV-60?GY}P)>q?GuNnCdgP^*Bj5V_b?dAq2Ppn9^MBB^YUM zad0N-T{Ujg*A6d~mYV4na=hT4Nz+_}SGTgW|Iir!%$ z;@OGkWI6+j0H}~K4RYR%!7y|zM`O@*K>rL{*&}x3lR**HrMXC1->#slU>X|w!U1xQ zqc + + @@ -1092,6 +1096,14 @@ class="org.simantics.sysdyn.ui.browser.contributions.SharedFunctionsLabeler" preference="2.0"> + + + + diff --git a/org.simantics.sysdyn.ui/plugin_customization.ini b/org.simantics.sysdyn.ui/plugin_customization.ini index ea850333..92e8ed0c 100644 --- a/org.simantics.sysdyn.ui/plugin_customization.ini +++ b/org.simantics.sysdyn.ui/plugin_customization.ini @@ -1 +1,2 @@ org.eclipse.ui/SHOW_PROGRESS_ON_STARTUP = false +org.eclipse.ui/DOCK_PERSPECTIVE_BAR=right \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java index 6b447f4d..e6051f38 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java @@ -18,6 +18,7 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.browser.nodes.BookNode; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder; @@ -30,6 +31,7 @@ import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode; import org.simantics.sysdyn.ui.browser.nodes.ModulesNode; import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode; import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder; +import org.simantics.sysdyn.ui.browser.nodes.SheetNode; import org.simantics.sysdyn.ui.browser.nodes.VariableNode; public class AbstractNodeImager extends ImagerContributor> { @@ -56,6 +58,10 @@ public class AbstractNodeImager extends ImagerContributor image = "icons/chart_organisation.png"; else if (node instanceof VariableNode) image = "icons/brick.png"; + else if (node instanceof BookNode) + image = "icons/table_multiple.png"; + else if (node instanceof SheetNode) + image = "icons/table.png"; if (image != null) return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource(image)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Book.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Book.java new file mode 100644 index 00000000..8bd950fc --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Book.java @@ -0,0 +1,29 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.spreadsheet.resource.SpreadsheetResource; +import org.simantics.sysdyn.ui.browser.nodes.BookNode; +import org.simantics.sysdyn.ui.browser.nodes.SheetNode; + +public class Book extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, BookNode book) throws DatabaseException { + ArrayList> result = new ArrayList>(); + Layer0 l0 = Layer0.getInstance(graph); + for(Resource r : graph.syncRequest(new ObjectsWithType(book.data, l0.ConsistsOf, SpreadsheetResource.getInstance(graph).Spreadsheet))) { + result.add(new SheetNode(r)); + } + return result; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/BookLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/BookLabeler.java new file mode 100644 index 00000000..e0f950d5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/BookLabeler.java @@ -0,0 +1,15 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.BookNode; + +public class BookLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, BookNode input) throws DatabaseException { + return "SpreadSheets"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java index d92b3073..c874e042 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java @@ -14,14 +14,18 @@ package org.simantics.sysdyn.ui.browser.contributions; import java.util.ArrayList; import java.util.Collection; -import org.simantics.browsing.ui.common.node.AbstractNode; import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ui.modelBrowser2.model.SheetsNode; +import org.simantics.spreadsheet.resource.SpreadsheetResource; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.BookNode; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; import org.simantics.sysdyn.ui.browser.nodes.EnumerationNode; import org.simantics.sysdyn.ui.browser.nodes.InputNode; @@ -32,7 +36,7 @@ public class Configuration extends ViewpointContributor getContribution(ReadGraph graph, ConfigurationNode configuration) throws DatabaseException { - ArrayList> result = new ArrayList>(); + ArrayList result = new ArrayList(); Variable variable = configuration.getVariable(); if (variable == null) { @@ -74,6 +78,14 @@ public class Configuration extends ViewpointContributor{ + + @Override + public String getLabel(ReadGraph graph, SheetNode sheet) throws DatabaseException { + return graph.getPossibleRelatedValue(sheet.data, Layer0.getInstance(graph).HasName); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/BookNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/BookNode.java new file mode 100644 index 00000000..325d7774 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/BookNode.java @@ -0,0 +1,19 @@ +package org.simantics.sysdyn.ui.browser.nodes; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.db.Resource; + +public class BookNode extends AbstractNode { + + public BookNode(Resource data) { + super(data); + } + + @SuppressWarnings("rawtypes") + @Override + public Object getAdapter(Class adapter) { + if(clazz == adapter) // There is no resource for this node.. + return null; + return super.getAdapter(adapter); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SheetNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SheetNode.java new file mode 100644 index 00000000..8df2d43d --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SheetNode.java @@ -0,0 +1,81 @@ +package org.simantics.sysdyn.ui.browser.nodes; + +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.common.node.IDoubleClickableNode; +import org.simantics.browsing.ui.common.node.IModifiableNode; +import org.simantics.browsing.ui.content.Labeler.Modifier; +import org.simantics.browsing.ui.graph.impl.LabelModifier; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.workbench.ResourceEditorInput2; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +public class SheetNode extends AbstractNode implements IModifiableNode, IDoubleClickableNode { + + public SheetNode(Resource data) { + super(data); + } + + @Override + public Modifier getModifier(String columnId) { + + Session session = SimanticsUI.getSession(); + LabelModifier modifier = new LabelModifier(session, data, session.getService(Layer0.class).HasName) { + @Override + public String isValid(String label) { + if (label.isEmpty()) + return "Empty label not allowed"; + return null; + } + }; + return modifier; + } + + @Override + public boolean handleDoubleClick() { + + try { + + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + + Variable variable = graph.adapt(data, Variable.class); + Resource model = Variables.getModel(graph, variable); + final String modelURI = graph.getURI(model); + final String RVI = Variables.getRVI(graph, variable); + + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + + private static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2"; + + @Override + public void run() { + try { + System.out.println("Activating sheet: model=" + modelURI + " rvi=" + RVI); + WorkbenchUtils.openEditor(EDITOR_ID, new ResourceEditorInput2(EDITOR_ID, data, modelURI, RVI)); + } catch (PartInitException e) { + e.printStackTrace(); + } + } + }); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + + return true; + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java index ddeaec1b..0faf71bd 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java @@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.handlers; import java.io.File; import java.io.IOException; import java.util.HashSet; +import java.util.UUID; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -29,6 +30,7 @@ import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; @@ -39,6 +41,7 @@ import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.spreadsheet.resource.SpreadsheetResource; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; @@ -134,6 +137,7 @@ public class ImportModelHandler extends AbstractHandler { updateOldConfigurationToBaseRealization(graph, root); addDefaultOntologyLinks(graph, root); addURIsToDiagrams(graph, root); + addSpreadSheetBook(graph, root); } } }); @@ -222,4 +226,94 @@ public class ImportModelHandler extends AbstractHandler { e.printStackTrace(); } } + + + /** + * Add a missing spreadsheet book to the model + * + * @param graph + * @param model + */ + private static void addSpreadSheetBook(WriteGraph graph, Resource model) { + try { + Layer0 l0 = Layer0.getInstance(graph); + SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph); + SimulationResource simu = SimulationResource.getInstance(graph); + Resource conf = graph.getPossibleObject(model, simu.HasConfiguration); + if(conf != null && graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, ssr.Book)).isEmpty()) { + Resource book = graph.newResource(); + graph.claim(book, l0.InstanceOf, null, ssr.Book); + graph.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(conf, l0.ConsistsOf, l0.PartOf, book); + + createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 }); + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + /** + * Create a sheet (Copied from SysdynProject) + * + * @param graph + * @param book + * @param name + * @param colNames + * @param colWidths + * @return + * @throws DatabaseException + */ + private static Resource createSheet(WriteGraph graph, Resource book, String name, String[] colNames, int[] colWidths) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + SpreadsheetResource sr = SpreadsheetResource.getInstance(graph); + + Resource result = graph.newResource(); + graph.claim(result, L0.InstanceOf, null, sr.Spreadsheet); + + if(name == null) { + name = NameUtils.findFreshEscapedName(graph, "Sheet", book, sr.HasSheet); + } + graph.claimLiteral(result, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING); + graph.claim(book, L0.ConsistsOf, L0.PartOf, result); + + { + Resource newCell = graph.newResource(); + graph.claim(newCell, L0.InstanceOf, null, sr.Cell); + graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Dimensions", Bindings.STRING); + graph.addLiteral(newCell, sr.FitColumns, sr.FitColumnsOf, L0.Boolean, false, Bindings.BOOLEAN); + graph.addLiteral(newCell, sr.FitRows, sr.FitRowsOf, L0.Boolean, false, Bindings.BOOLEAN); + graph.addLiteral(newCell, sr.ColumnCount, sr.ColumnCountOf, L0.Integer, 128, Bindings.INTEGER); + graph.addLiteral(newCell, sr.RowCount, sr.RowCountOf, L0.Integer, 256, Bindings.INTEGER); + graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell); + } + + { + Resource newCell = graph.newResource(); + graph.claim(newCell, L0.InstanceOf, null, sr.Cell); + graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Headers", Bindings.STRING); + graph.addLiteral(newCell, sr.ColumnLabels, sr.ColumnLabelsOf, L0.StringArray, colNames, Bindings.STRING_ARRAY); + graph.addLiteral(newCell, sr.ColumnWidths, sr.ColumnWidthsOf, L0.IntegerArray, colWidths, Bindings.INT_ARRAY); + graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell); + } + + { + + double[] doubles = new double[10*2]; + for(int i=0;i<10*2;i++) doubles[i] = i; + + Resource newCell = graph.newResource(); + graph.claim(newCell, L0.InstanceOf, null, sr.DoubleArrayCell); + graph.addLiteral(newCell, sr.DoubleArrayCell_HasWidth, sr.DoubleArrayCell_HasWidth_Inverse, L0.Integer, 10, Bindings.INTEGER); + graph.addLiteral(newCell, sr.HasLocation, sr.HasLocation_Inverse, L0.String, "B2", Bindings.STRING); + graph.addLiteral(newCell, sr.DoubleArrayCell_HasDoubleArray, sr.DoubleArrayCell_HasDoubleArray_Inverse, L0.DoubleArray, doubles, Bindings.DOUBLE_ARRAY); + graph.claim(result, L0.HasChildVariables, L0.HasChildVariables_Inverse, newCell); + + } + + return result; + + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj index f1173c1b..3685cdaa 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -207,14 +207,14 @@ void component_reference(String prevToken) : { //IDENT [ array_subscripts ] [ "." component_reference ] { String name = token.image; - // prevToken != null => this is the second part of an enumeration + // forIndex == true and prevToken != null => this is the second part of an enumeration in for-index if(forIndex == true && prevToken != null) { if(enumerationReferences.get(prevToken) == null) { enumerationReferences.put(prevToken, new ArrayList()); } List list = enumerationReferences.get(prevToken); list.add(token); - // forIndex == true, prevToken == null => this is the enumeration + // forIndex == true and prevToken == null => this is the enumeration in for-index } else if(forIndex == true && prevToken == null) { if(enumerationReferences.get(name) == null) { enumerationReferences.put(name, new ArrayList()); -- 2.47.1