--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.template2d.ui.function;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+import java.util.Arrays;\r
+import java.util.Map;\r
+\r
+public class TranslateFlag {\r
+ public static final float lineWidth = 0.3F;\r
+ private FlagInfo flagInfo = null;\r
+ private Map<String, FlagTableInfo> tables = null;\r
+ private double[] transform = null;\r
+ private double gridSize = 0.0;\r
+\r
+ public TranslateFlag(FlagInfo flagInfo, Map<String, FlagTableInfo> tables, double[] transform, double gridSize){\r
+ this.flagInfo = flagInfo;\r
+ this.tables = tables;\r
+ this.transform = transform;\r
+ this.gridSize = gridSize;\r
+ }\r
+\r
+ public double[] perform(){\r
+ // flag transforming\r
+ FlagTableInfo table = tables.get(flagInfo.flagTableName);\r
+ if (table == null)\r
+ return this.transform;\r
+\r
+// Double x = table.getPosition().getX()+((table.getAlignment()==TemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0);\r
+// Double y = table.getPosition().getY()+table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();\r
+// double[] copy = Arrays.copyOf(transform, transform.length);\r
+// copy[4] = x;\r
+// copy[5] = y;\r
+// return copy;\r
+\r
+\r
+ //double yy = table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();\r
+ double[] tableTrans = table.getTransform();\r
+ double scale = tableTrans[3];\r
+ double scaledRowHeigth = scale*table.getRowHeight();\r
+\r
+ // move to coordinate system of parent of iotable\r
+ double rowMiddleY = tableTrans[5]+(flagInfo.flagTableRowIndex+0.5F)*scaledRowHeigth;\r
+ if (gridSize != 0.0) {\r
+ double modulo = rowMiddleY%gridSize;\r
+ int count =(int) (rowMiddleY/gridSize);\r
+ double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;\r
+ double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;\r
+ if (diff < (scaledRowHeigth/2))\r
+ rowMiddleY = gridOnRow; \r
+ }\r
+ // move back to iotable coordinate system\r
+ rowMiddleY = (rowMiddleY-tableTrans[5])/scale;\r
+\r
+// double yy = 0.0;\r
+// yy = (flagInfo.getSlotTableRowIndex()+0.5F)*table.getRowHeigth()-lineWidth/2;\r
+// double yy2 = 0.0;\r
+//\r
+// // calculate flag position if it is snap to grid\r
+// if (gridSize != 0.0) {\r
+// double modulo = yy%gridSize;\r
+// int count =(int) (yy/gridSize);\r
+// yy2 = ((yy%gridSize) > (gridSize/2))?gridSize*(1+ (int)(yy/gridSize))-lineWidth/2:gridSize*(int)(yy/gridSize)-lineWidth/2;\r
+// }\r
+//\r
+// // check that the nearist grid layes on current row\r
+// if (yy2-yy < table.getRowHeigth()/2)\r
+// yy = yy2;\r
+\r
+ double xx = ((table.getAlignment()==DrawingTemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0); \r
+\r
+ AffineTransform trans = new AffineTransform(new double[]{tableTrans[0],tableTrans[1],tableTrans[2],tableTrans[3],0.0,0.0});\r
+ Point2D point = new Point2D.Double(xx, rowMiddleY);\r
+ trans.transform(point, point);\r
+\r
+ double[] copy = Arrays.copyOf(transform, transform.length);\r
+ copy[4] = tableTrans[4] + point.getX();\r
+ copy[5] = tableTrans[5] + point.getY();\r
+ return copy;\r
+// Double x = table.getTransform()[4]+;\r
+// Double y = table.getTransform()[5]+;\r
+// \r
+// double[] copy = Arrays.copyOf(transform, transform.length);\r
+// copy[4] = x;//transform[4];\r
+// copy[5] = y;\r
+// return copy;\r
+\r
+// Session session = Simantics.peekSession();\r
+// if (session == null)\r
+// return;\r
+// session.asyncRequest(new WriteRequest(){\r
+// @Override\r
+// public void perform(WriteGraph g) throws DatabaseException {\r
+// TranslateElement.absolute(flagInfo.getFlag(), position).perform(g);\r
+// }});\r
+ }\r
+}\r