1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.entityrelationship.painters;
15  
16  import demo.view.flowchart.painters.FlowchartProcessPainter;
17  import y.view.GenericNodeRealizer;
18  import y.view.LineType;
19  import y.view.NodeRealizer;
20  
21  import java.awt.geom.Rectangle2D;
22  
23  /**
24   * This is a painter to display small entity node for entity relationship diagrams (ERD).
25   *
26   * A small entity is used in the Chen notation and is displayed as a rectangle with a label.
27   * Weak entities are drawn with a double border.
28   */
29  public class ErdSmallEntityNodePainter extends FlowchartProcessPainter {
30  
31    /** The border of the rectangular shaped node */
32    private Rectangle2D rectangle;
33  
34    /**
35     * Calculates the interior shape for the specified node.
36     * @param context The node context
37     */
38    protected void updateInsideShape( final NodeRealizer context ) {
39  
40      if (hasDoubleBorder(context)) {
41        if (rectangle == null) {
42          rectangle = new Rectangle2D.Double();
43        }
44        innerShape = rectangle;
45        updateInsideShapeImpl(context);
46      } else {
47        innerShape = null;
48      }
49  
50    }
51  
52    /**
53     * Calculates the rectangular for the interior shape.
54     * @param context The node context
55     */
56    private void updateInsideShapeImpl( final NodeRealizer context ) {
57      final Rectangle2D shape = (Rectangle2D) getInnerShape();
58  
59      final LineType lineType = context.getLineType();
60      final float lw = lineType.getLineWidth();
61  
62      final double offset = 2 + lw;
63  
64      final double x = context.getX();
65      final double y = context.getY();
66      final double width = context.getWidth();
67      final double height = context.getHeight();
68  
69      if (offset + lw < width * 0.5 && offset + lw < height * 0.5) {
70        shape.setFrame(x + offset, y + offset, width - 2 * offset, height - 2 * offset);
71      } else {
72        shape.setFrame(x, y, 0, 0);
73      }
74    }
75  
76    /**
77     * Tests if the style property {@link ErdRealizerFactory#DOUBLE_BORDER} is set for the context realizer.
78     * @param context The context node
79     * @return <code>true</code>, if style property border is set, <code>false</code> otherwise
80     */
81    private boolean hasDoubleBorder(NodeRealizer context) {
82      return Boolean.TRUE.equals(((GenericNodeRealizer) context).getStyleProperty(ErdRealizerFactory.DOUBLE_BORDER));
83    }
84  
85  }
86