]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/BasicFlagType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / BasicFlagType.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.diagram.flag;\r
13 \r
14 import java.awt.Shape;\r
15 import java.awt.geom.Path2D;\r
16 import java.awt.geom.Rectangle2D;\r
17 \r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.diagram.query.DiagramRequests;\r
22 import org.simantics.g2d.elementclass.FlagClass;\r
23 import org.simantics.g2d.elementclass.FlagClass.Mode;\r
24 import org.simantics.g2d.elementclass.FlagClass.Type;\r
25 import org.simantics.g2d.utils.Alignment;\r
26 import org.simantics.structural2.modelingRules.IModelingRules;\r
27 \r
28 /**\r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class BasicFlagType extends AbstractFlagType {\r
32 \r
33     public BasicFlagType(Resource flag, IModelingRules modelingRules) {\r
34         super(flag, modelingRules);\r
35     }\r
36 \r
37     @Override\r
38     public FlagInfo getInfo(ReadGraph graph) throws DatabaseException {\r
39         Type type = getType(graph);\r
40         Mode mode = getMode(graph);\r
41 \r
42         return FlagInfoBuilder.fill(type)\r
43         .shape(getShape(graph, type, mode))\r
44         .text(getText(graph))\r
45         .textArea(getArea(graph, type, mode))\r
46         .horizontalAlignment(Alignment.LEADING)\r
47         .verticalAlignment(Alignment.CENTER)\r
48         .create();\r
49     }\r
50 \r
51     protected Rectangle2D getArea(ReadGraph graph, Type type, Mode mode) {\r
52         return getArea(type, mode);\r
53     }\r
54 \r
55     public static Rectangle2D getArea(Type type, Mode mode) {\r
56         final double width = FlagClass.DEFAULT_WIDTH;\r
57         final double height = FlagClass.DEFAULT_HEIGHT;\r
58         final double beakLength = FlagClass.getBeakLength(height, FlagClass.DEFAULT_BEAK_ANGLE);\r
59         return getArea(type, mode, width, height, beakLength);\r
60     }\r
61 \r
62     public static Rectangle2D getArea(Type type, Mode mode, double width, double height, double beakLength) {\r
63         double x = 0;\r
64         double y = -height / 2;\r
65         double w = width;\r
66         double h = height;\r
67 \r
68         if (type == Type.In) {\r
69             if (mode instanceof FlagClass.External) {\r
70                 x = -width-beakLength;\r
71             } else if (mode == Mode.Internal) {\r
72                 x = -beakLength;\r
73                 w = beakLength;\r
74             }\r
75         } else if (type == Type.Out) {\r
76             if (mode == Mode.Internal) {\r
77                 w = beakLength;\r
78             }\r
79         }\r
80 \r
81         return new Rectangle2D.Double(x, y, w, h);\r
82     }\r
83 \r
84     protected Shape getShape(ReadGraph graph, Type type, Mode mode) throws DatabaseException {\r
85         return getShape(type, mode);\r
86     }\r
87 \r
88     public static Shape getShape(Type type, Mode mode) {\r
89         final double width = FlagClass.DEFAULT_WIDTH;\r
90         final double height = FlagClass.DEFAULT_HEIGHT;\r
91         final double beakLength = FlagClass.getBeakLength(height, FlagClass.DEFAULT_BEAK_ANGLE);\r
92         Path2D path = new Path2D.Double();\r
93         FlagClass.createFlagShape(path, type, mode, width, height, beakLength);\r
94         return path;\r
95     }\r
96 \r
97     protected String[] getText(ReadGraph graph) throws DatabaseException {\r
98         return graph.syncRequest(DiagramRequests.getFlagText(flag));\r
99     }\r
100 \r
101 }