1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.flag;
14 import java.awt.Shape;
15 import java.awt.geom.Path2D;
16 import java.awt.geom.Rectangle2D;
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;
29 * @author Tuukka Lehtonen
31 public class BasicFlagType extends AbstractFlagType {
33 public BasicFlagType(Resource flag, IModelingRules modelingRules) {
34 super(flag, modelingRules);
38 public FlagInfo getInfo(ReadGraph graph) throws DatabaseException {
39 Type type = getType(graph);
40 Mode mode = getMode(graph);
42 return FlagInfoBuilder.fill(type)
43 .shape(getShape(graph, type, mode))
45 .textArea(getArea(graph, type, mode))
46 .horizontalAlignment(Alignment.LEADING)
47 .verticalAlignment(Alignment.CENTER)
51 protected Rectangle2D getArea(ReadGraph graph, Type type, Mode mode) {
52 return getArea(type, mode);
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);
62 public static Rectangle2D getArea(Type type, Mode mode, double width, double height, double beakLength) {
64 double y = -height / 2;
68 if (type == Type.In) {
69 if (mode instanceof FlagClass.External) {
70 x = -width-beakLength;
71 } else if (mode == Mode.Internal) {
75 } else if (type == Type.Out) {
76 if (mode == Mode.Internal) {
81 return new Rectangle2D.Double(x, y, w, h);
84 protected Shape getShape(ReadGraph graph, Type type, Mode mode) throws DatabaseException {
85 return getShape(type, mode);
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);
97 protected String[] getText(ReadGraph graph) throws DatabaseException {
98 return graph.syncRequest(DiagramRequests.getFlagText(flag));