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