| ErdSmallEntityNodePainter.java |
1 /****************************************************************************
2 * This demo file is part of yFiles for Java 2.14.
3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4 * 72070 Tuebingen, Germany. All rights reserved.
5 *
6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7 * of demo files in source code or binary form, with or without
8 * modification, is not permitted.
9 *
10 * Owners of a valid software license for a yFiles for Java version that this
11 * demo is shipped with are allowed to use the demo source code as basis
12 * for their own yFiles for Java powered applications. Use of such programs is
13 * governed by the rights and conditions as set out in the yFiles for Java
14 * license agreement.
15 *
16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 ***************************************************************************/
28 package demo.view.entityrelationship.painters;
29
30 import demo.view.flowchart.painters.FlowchartProcessPainter;
31 import y.view.GenericNodeRealizer;
32 import y.view.LineType;
33 import y.view.NodeRealizer;
34
35 import java.awt.Shape;
36 import java.awt.geom.Rectangle2D;
37
38 /**
39 * This is a painter to display small entity node for entity relationship diagrams (ERD).
40 *
41 * A small entity is used in the Chen notation and is displayed as a rectangle with a label.
42 * Weak entities are drawn with a double border.
43 */
44 public class ErdSmallEntityNodePainter extends FlowchartProcessPainter {
45 /**
46 * Calculates the interior shape for the specified node.
47 * @param context The node context
48 */
49 protected Shape newDecoration( final NodeRealizer context ) {
50 if (hasDoubleBorder(context)) {
51 final LineType lineType = context.getLineType();
52 final float lw = lineType.getLineWidth();
53
54 final double offset = 2 + lw;
55
56 final double x = context.getX();
57 final double y = context.getY();
58 final double width = context.getWidth();
59 final double height = context.getHeight();
60
61 if (offset + lw < width * 0.5 && offset + lw < height * 0.5) {
62 return new Rectangle2D.Double(x + offset, y + offset, width - 2 * offset, height - 2 * offset);
63 } else {
64 return new Rectangle2D.Double(x, y, 0, 0);
65 }
66 } else {
67 return null;
68 }
69 }
70
71 /**
72 * Tests if the style property {@link ErdRealizerFactory#DOUBLE_BORDER} is set for the context realizer.
73 * @param context The context node
74 * @return <code>true</code>, if style property border is set, <code>false</code> otherwise
75 */
76 protected boolean hasDoubleBorder(NodeRealizer context) {
77 return Boolean.TRUE.equals(((GenericNodeRealizer) context).getStyleProperty(ErdRealizerFactory.DOUBLE_BORDER));
78 }
79 }
80