]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/TranslateFlag.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / TranslateFlag.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.template2d.ui.function;\r
13 \r
14 import java.awt.geom.AffineTransform;\r
15 import java.awt.geom.Point2D;\r
16 import java.util.Arrays;\r
17 import java.util.Map;\r
18 \r
19 public class TranslateFlag {\r
20         public static final float lineWidth = 0.3F;\r
21         private FlagInfo flagInfo = null;\r
22         private Map<String, FlagTableInfo> tables = null;\r
23         private double[] transform = null;\r
24         private double gridSize = 0.0;\r
25 \r
26         public TranslateFlag(FlagInfo flagInfo, Map<String, FlagTableInfo> tables, double[] transform, double gridSize){\r
27                 this.flagInfo = flagInfo;\r
28                 this.tables = tables;\r
29                 this.transform = transform;\r
30                 this.gridSize = gridSize;\r
31         }\r
32 \r
33         public double[] perform(){\r
34                 // flag transforming\r
35                 FlagTableInfo table = tables.get(flagInfo.flagTableName);\r
36                 if (table == null)\r
37                         return this.transform;\r
38 \r
39 //              Double x = table.getPosition().getX()+((table.getAlignment()==TemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0);\r
40 //              Double y = table.getPosition().getY()+table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();\r
41 //        double[] copy = Arrays.copyOf(transform, transform.length);\r
42 //        copy[4] = x;\r
43 //        copy[5] = y;\r
44 //        return copy;\r
45 \r
46 \r
47                 //double yy = table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();\r
48                 double[] tableTrans = table.getTransform();\r
49                 double scale = tableTrans[3];\r
50                 double scaledRowHeigth = scale*table.getRowHeight();\r
51 \r
52                 // move to coordinate system of parent of iotable\r
53                 double rowMiddleY = tableTrans[5]+(flagInfo.flagTableRowIndex+0.5F)*scaledRowHeigth;\r
54                 if (gridSize != 0.0) {\r
55                         double modulo = rowMiddleY%gridSize;\r
56                         int count =(int) (rowMiddleY/gridSize);\r
57                         double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;\r
58                         double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;\r
59                         if (diff < (scaledRowHeigth/2))\r
60                                 rowMiddleY = gridOnRow; \r
61                 }\r
62                 // move back to iotable coordinate system\r
63                 rowMiddleY = (rowMiddleY-tableTrans[5])/scale;\r
64 \r
65 //              double yy = 0.0;\r
66 //              yy = (flagInfo.getSlotTableRowIndex()+0.5F)*table.getRowHeigth()-lineWidth/2;\r
67 //              double yy2 = 0.0;\r
68 //\r
69 //              // calculate flag position if it is snap to grid\r
70 //              if (gridSize != 0.0) {\r
71 //                      double modulo = yy%gridSize;\r
72 //                      int count =(int) (yy/gridSize);\r
73 //                      yy2 = ((yy%gridSize) > (gridSize/2))?gridSize*(1+ (int)(yy/gridSize))-lineWidth/2:gridSize*(int)(yy/gridSize)-lineWidth/2;\r
74 //              }\r
75 //\r
76 //              // check that the nearist grid layes on current row\r
77 //              if (yy2-yy < table.getRowHeigth()/2)\r
78 //                      yy = yy2;\r
79 \r
80                 double xx = ((table.getAlignment()==DrawingTemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0); \r
81 \r
82                 AffineTransform trans = new AffineTransform(new double[]{tableTrans[0],tableTrans[1],tableTrans[2],tableTrans[3],0.0,0.0});\r
83                 Point2D point = new Point2D.Double(xx, rowMiddleY);\r
84                 trans.transform(point, point);\r
85 \r
86                 double[] copy = Arrays.copyOf(transform, transform.length);\r
87                 copy[4] = tableTrans[4] + point.getX();\r
88                 copy[5] = tableTrans[5] + point.getY();\r
89                 return copy;\r
90 //              Double x = table.getTransform()[4]+;\r
91 //              Double y = table.getTransform()[5]+;\r
92 //              \r
93 //        double[] copy = Arrays.copyOf(transform, transform.length);\r
94 //        copy[4] = x;//transform[4];\r
95 //        copy[5] = y;\r
96 //        return copy;\r
97 \r
98 //        Session session = Simantics.peekSession();\r
99 //        if (session == null)\r
100 //              return;\r
101 //        session.asyncRequest(new WriteRequest(){\r
102 //                      @Override\r
103 //                      public void perform(WriteGraph g) throws DatabaseException {\r
104 //                              TranslateElement.absolute(flagInfo.getFlag(), position).perform(g);\r
105 //                      }});\r
106         }\r
107 }\r