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.awt.geom.Point2D;
16 import java.util.Arrays;
19 public class TranslateFlag {
20 public static final float lineWidth = 0.3F;
21 private FlagInfo flagInfo = null;
22 private Map<String, FlagTableInfo> tables = null;
23 private double[] transform = null;
24 private double gridSize = 0.0;
26 public TranslateFlag(FlagInfo flagInfo, Map<String, FlagTableInfo> tables, double[] transform, double gridSize){
27 this.flagInfo = flagInfo;
29 this.transform = transform;
30 this.gridSize = gridSize;
33 public double[] perform(){
35 FlagTableInfo table = tables.get(flagInfo.flagTableName);
37 return this.transform;
39 // Double x = table.getPosition().getX()+((table.getAlignment()==TemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0);
40 // Double y = table.getPosition().getY()+table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();
41 // double[] copy = Arrays.copyOf(transform, transform.length);
47 //double yy = table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();
48 double[] tableTrans = table.getTransform();
49 double scale = tableTrans[3];
50 double scaledRowHeigth = scale*table.getRowHeight();
52 // move to coordinate system of parent of iotable
53 double rowMiddleY = tableTrans[5]+(flagInfo.flagTableRowIndex+0.5F)*scaledRowHeigth;
54 if (gridSize != 0.0) {
55 double modulo = rowMiddleY%gridSize;
56 int count =(int) (rowMiddleY/gridSize);
57 double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;
58 double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;
59 if (diff < (scaledRowHeigth/2))
60 rowMiddleY = gridOnRow;
62 // move back to iotable coordinate system
63 rowMiddleY = (rowMiddleY-tableTrans[5])/scale;
66 // yy = (flagInfo.getSlotTableRowIndex()+0.5F)*table.getRowHeigth()-lineWidth/2;
69 // // calculate flag position if it is snap to grid
70 // if (gridSize != 0.0) {
71 // double modulo = yy%gridSize;
72 // int count =(int) (yy/gridSize);
73 // yy2 = ((yy%gridSize) > (gridSize/2))?gridSize*(1+ (int)(yy/gridSize))-lineWidth/2:gridSize*(int)(yy/gridSize)-lineWidth/2;
76 // // check that the nearist grid layes on current row
77 // if (yy2-yy < table.getRowHeigth()/2)
80 double xx = ((table.getAlignment()==DrawingTemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0);
82 AffineTransform trans = new AffineTransform(new double[]{tableTrans[0],tableTrans[1],tableTrans[2],tableTrans[3],0.0,0.0});
83 Point2D point = new Point2D.Double(xx, rowMiddleY);
84 trans.transform(point, point);
86 double[] copy = Arrays.copyOf(transform, transform.length);
87 copy[4] = tableTrans[4] + point.getX();
88 copy[5] = tableTrans[5] + point.getY();
90 // Double x = table.getTransform()[4]+;
91 // Double y = table.getTransform()[5]+;
93 // double[] copy = Arrays.copyOf(transform, transform.length);
94 // copy[4] = x;//transform[4];
98 // Session session = Simantics.peekSession();
99 // if (session == null)
101 // session.asyncRequest(new WriteRequest(){
103 // public void perform(WriteGraph g) throws DatabaseException {
104 // TranslateElement.absolute(flagInfo.getFlag(), position).perform(g);