1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.function;
14 import java.awt.geom.AffineTransform;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.TreeMap;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.datatypes.literal.Font;
21 import org.simantics.datatypes.literal.RGB;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.ResourceRead;
25 import org.simantics.db.common.utils.ListUtils;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.template2d.ontology.Template2dResource;
30 import org.simantics.scenegraph.ontology.ScenegraphResources;
32 public class DrawingTemplateInfo extends ResourceRead<TreeMap<String, FlagTableInfo>> {
33 public static final Integer ALIGN_LEFT = 0;
34 public static final Integer ALIGN_RIGHT = 1;
35 public static final float BASELINE_VERTICAL_OFFSET = 0.8F; // percent of line height
36 // private String digest = "";
39 // public String getDigest() {
43 public DrawingTemplateInfo(Resource template) {
48 * The byte[] returned by MessageDigest does not have a nice
49 * textual representation, so some form of encoding is usually performed.
51 * This implementation follows the example of David Flanagan's book
52 * "Java In A Nutshell", and converts a byte array into a String
55 * Another popular alternative is to use a "Base64" encoding.
57 // static private String hexEncode( byte[] aInput){
58 // StringBuilder result = new StringBuilder();
59 // char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
60 // for ( int idx = 0; idx < aInput.length; ++idx) {
61 // byte b = aInput[idx];
62 // result.append( digits[ (b&0xf0) >> 4 ] );
63 // result.append( digits[ b&0x0f] );
65 // return result.toString();
69 public TreeMap<String, FlagTableInfo> perform(ReadGraph g) throws DatabaseException {
70 TreeMap<String,FlagTableInfo> entries = new TreeMap<String,FlagTableInfo>();
72 Layer0 L0 = Layer0.getInstance(g);
73 Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
74 DiagramResource DIA = DiagramResource.getInstance(g);
75 ScenegraphResources SG = ScenegraphResources.getInstance(g);
76 //DatatypeResource DTR = DatatypeResource.getInstance(g);
79 // sha = MessageDigest.getInstance("SHA-1");
80 // byte[] result = sha.digest(info.serialize());
81 // System.err.println(hexEncode(result));
84 Collection<Resource> slotTables = g.getObjects(resource, L0.ConsistsOf);
85 for (Resource slotTable:slotTables){
86 if (!g.isInstanceOf(slotTable, TEMPLATE2D.FlagTable))
89 String name = g.getPossibleRelatedValue(slotTable, L0.HasName, Bindings.STRING);
90 double[] transform = g.getPossibleRelatedValue2(slotTable, DIA.Scenegraph_Composite_transform, Bindings.getBindingUnchecked(double[].class));
91 Resource align = g.getPossibleObject(slotTable, TEMPLATE2D.FlagTable_HasAlignment);
92 Float width = g.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasWidth, Bindings.FLOAT);
93 Float rowHeight = g.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowHeigth, Bindings.FLOAT);
94 Integer rowCount = g.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowCount, Bindings.INTEGER);
95 if (name == null || width == null || rowCount == null)
98 FlagTableInfo table = new FlagTableInfo();
99 table.setWidth(width);
100 table.setRowCount(rowCount);
101 table.setRowHeight(rowHeight);
102 table.setTransform(transform);
103 table.affineTransform = (transform != null) ? new AffineTransform(transform) : new AffineTransform();
105 if (align.equals(TEMPLATE2D.FlagTable_Alignment_Left))
106 table.setAlignment(ALIGN_LEFT);
107 if (align.equals(TEMPLATE2D.FlagTable_Alignment_Right))
108 table.setAlignment(ALIGN_RIGHT);
110 entries.put(name, table);
112 // Columns information
113 Resource res = g.getPossibleObject(slotTable, SG.Node_children);
114 if(res == null || !g.isInstanceOf(res, L0.List))
116 List<Resource> columns = ListUtils.toList(g, res);
118 Float weightTotal = 0.0F;
119 for (Resource column:columns){
120 if (!(g.isInstanceOf(column, TEMPLATE2D.FlagTable_Column) || g.isInstanceOf(column, TEMPLATE2D.FlagTable_RowNumberColumn)))
123 Float weight = g.getPossibleRelatedValue(column, TEMPLATE2D.FlagTable_Column_HasWeight, Bindings.FLOAT);
127 FlagTableColumnInfo col = new FlagTableColumnInfo(weight);
128 table.addColumn(col);
129 if (g.isInstanceOf(column, TEMPLATE2D.FlagTable_RowNumberColumn)) {
130 col.setType(FlagTableColumnInfo.TYPE_ROW_NUMBERING);
132 Integer start = g.getPossibleRelatedValue(column, TEMPLATE2D.FlagTable_RowNumberColumn_start, Bindings.INTEGER);
133 col.setStartOffset(start);
136 weightTotal = weightTotal + weight;
137 //sha.update(weight.toString().getBytes());
139 // Header and Data information
140 Resource res2 = g.getPossibleObject(column, SG.Node_children);
141 if(res2 == null || !g.isInstanceOf(res2, L0.List))
143 List<Resource> columnDatas = ListUtils.toList(g, res2);
144 for (Resource columnData:columnDatas){
145 if (g.isInstanceOf(columnData, TEMPLATE2D.FlagTable_Column_Data)){
146 MonitorInfo monitorInfo = new MonitorInfo();
147 Font font = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_font, Bindings.getBindingUnchecked(Font.class));
149 monitorInfo.setFont(font);
150 //sha.update(font.serialize());
153 RGB.Integer color = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
155 monitorInfo.setColor(color);
156 //sha.update(color.serialize());
159 String path = g.getRelatedValue(columnData, DIA.Scenegraph_Monitor_reference, Bindings.STRING);
160 if (path != null && path.length() > 0){
161 monitorInfo.setText(path);
162 //sha.update(path.getBytes());
165 double[] dataTransform = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_transform, Bindings.getBindingUnchecked(double[].class));
166 if (dataTransform != null){
167 monitorInfo.setTransform(dataTransform);
168 //sha.update(path.getBytes());
171 monitorInfo.setResource(columnData);
172 //col.setColumnData(monitorInfo);
173 col.addColumnData(monitorInfo);
175 if (g.isInstanceOf(columnData, TEMPLATE2D.FlagTable_Column_Header)){
176 MonitorInfo monitorInfo = new MonitorInfo();
177 Font font = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_font, Bindings.getBindingUnchecked(Font.class));
179 monitorInfo.setFont(font);
180 //sha.update(font.serialize());
183 RGB.Integer color = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
185 monitorInfo.setColor(color);
186 //sha.update(color.serialize());
189 String path = g.getRelatedValue(columnData, DIA.Scenegraph_Monitor_reference, Bindings.STRING);
190 if (path != null && path.length() > 0){
191 monitorInfo.setText(path);
192 //sha.update(path.getBytes());
195 double[] dataTransform = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_transform, Bindings.getBindingUnchecked(double[].class));
196 if (dataTransform != null){
197 monitorInfo.setTransform(dataTransform);
198 //sha.update(path.getBytes());
201 monitorInfo.setResource(columnData);
202 //col.setColumnHeader(monitorInfo);
203 col.addColumnHeader(monitorInfo);
207 table.setWeightTotal(weightTotal);
209 //digest = hexEncode(sha.digest());
210 } catch (Throwable e){
211 e.printStackTrace(System.err);