]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/DrawingTemplateInfo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / DrawingTemplateInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.function;
13
14 import java.awt.geom.AffineTransform;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.TreeMap;
18
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;
31
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 = "";
37 //
38 //Column
39 //      public String getDigest() {
40 //              return digest;
41 //      }
42
43         public DrawingTemplateInfo(Resource template) {
44         super(template);
45     }
46
47         /**
48          * The byte[] returned by MessageDigest does not have a nice
49          * textual representation, so some form of encoding is usually performed.
50          *
51          * This implementation follows the example of David Flanagan's book
52          * "Java In A Nutshell", and converts a byte array into a String
53          * of hex characters.
54          *
55          * Another popular alternative is to use a "Base64" encoding.
56          */
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] );
64 //              }
65 //              return result.toString();
66 //      }
67
68         @Override
69         public TreeMap<String, FlagTableInfo> perform(ReadGraph g) throws DatabaseException {
70         TreeMap<String,FlagTableInfo> entries = new TreeMap<String,FlagTableInfo>();
71         try {
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);
77
78 //        MessageDigest sha;
79 //        sha = MessageDigest.getInstance("SHA-1");
80 //        byte[] result =  sha.digest(info.serialize());
81 //        System.err.println(hexEncode(result));
82
83
84         Collection<Resource> slotTables = g.getObjects(resource, L0.ConsistsOf);
85         for (Resource slotTable:slotTables){
86                 if (!g.isInstanceOf(slotTable, TEMPLATE2D.FlagTable))
87                         continue;
88                 
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)
96                         continue;
97
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();
104                 if (align != null) {
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);
109                 }
110                 entries.put(name, table);
111
112                 // Columns information
113                         Resource res = g.getPossibleObject(slotTable, SG.Node_children);
114                         if(res == null || !g.isInstanceOf(res, L0.List))
115                                 continue;
116                         List<Resource> columns = ListUtils.toList(g, res);
117
118                         Float weightTotal = 0.0F;
119                 for (Resource column:columns){
120                         if (!(g.isInstanceOf(column, TEMPLATE2D.FlagTable_Column) || g.isInstanceOf(column, TEMPLATE2D.FlagTable_RowNumberColumn)))
121                                 continue;
122
123                         Float weight = g.getPossibleRelatedValue(column, TEMPLATE2D.FlagTable_Column_HasWeight, Bindings.FLOAT);
124                         if (weight == null)
125                                 continue;
126                         
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);
131
132                                 Integer start = g.getPossibleRelatedValue(column, TEMPLATE2D.FlagTable_RowNumberColumn_start, Bindings.INTEGER);
133                                 col.setStartOffset(start);
134                         }
135                         
136                         weightTotal = weightTotal + weight; 
137                                 //sha.update(weight.toString().getBytes());
138                         
139                         // Header and Data information
140                                 Resource res2 = g.getPossibleObject(column, SG.Node_children);
141                                 if(res2 == null || !g.isInstanceOf(res2, L0.List))
142                                         continue;
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));
148                                         if (font != null){
149                                                 monitorInfo.setFont(font);
150                                                 //sha.update(font.serialize());
151                                         }
152
153                                         RGB.Integer color = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
154                                         if (color != null){
155                                                 monitorInfo.setColor(color);
156                                                 //sha.update(color.serialize());
157                                         }
158
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());
163                                 }
164                                 
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());
169                                 }
170
171                                 monitorInfo.setResource(columnData);
172                                 //col.setColumnData(monitorInfo);
173                                 col.addColumnData(monitorInfo);
174                                 }
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));
178                                         if (font != null){
179                                                 monitorInfo.setFont(font);
180                                                 //sha.update(font.serialize());
181                                         }
182
183                                         RGB.Integer color = g.getPossibleRelatedValue2(columnData, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
184                                         if (color != null){
185                                                 monitorInfo.setColor(color);
186                                                 //sha.update(color.serialize());
187                                         }
188
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());
193                                 }
194                                 
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());
199                                 }
200
201                                 monitorInfo.setResource(columnData);
202                                 //col.setColumnHeader(monitorInfo);
203                                 col.addColumnHeader(monitorInfo);
204                                 }
205                         }
206                 }
207                 table.setWeightTotal(weightTotal);
208         }
209         //digest = hexEncode(sha.digest());
210         } catch (Throwable e){
211                 e.printStackTrace(System.err);
212         }
213         return entries;
214     }
215 }