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.FlowchartStart1Painter;
17  import y.view.GenericNodeRealizer;
18  import y.view.LineType;
19  import y.view.NodeRealizer;
20  
21  import java.awt.geom.Ellipse2D;
22  
23  /**
24   * This is a painter to display an attribute node for entity relationship diagrams (ERD).
25   *
26   * An attribute is represented by an elliptic shape. It is possible to display a weak
27   * attribute by drawing a double border.
28   */
29  public class ErdAttributeNodePainter extends FlowchartStart1Painter {
30  
31    /** The border of the elliptic shaped node */
32    private Ellipse2D ellipse;
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      if (hasDoubleBorder(context)) {
40        if (ellipse == null) {
41          ellipse = new Ellipse2D.Double();
42        }
43        innerShape = ellipse;
44        updateInsideShapeImpl(context);
45      } else {
46        innerShape = null;
47      }
48    }
49  
50    /**
51     * Calculates the ellipse for the interior shape.
52     * @param context The node context
53     */
54    private void updateInsideShapeImpl( final NodeRealizer context ) {
55      final Ellipse2D shape = (Ellipse2D) getInnerShape();
56  
57      final LineType lineType = context.getLineType();
58      final float lw = lineType.getLineWidth();
59  
60      final double offset = 2 + lw;
61  
62      final double x = context.getX();
63      final double y = context.getY();
64      final double width = context.getWidth();
65      final double height = context.getHeight();
66  
67      if (offset + lw < width * 0.5 && offset + lw < height * 0.5) {
68        shape.setFrame(x + offset, y + offset, width - 2 * offset, height - 2 * offset);
69      } else {
70        shape.setFrame(x, y + height * 0.5, 0, 0);
71      }
72    }
73  
74    /**
75     * Tests if the style property {@link ErdRealizerFactory#DOUBLE_BORDER} is set for the context realizer.
76     * @param context The context node
77     * @return <code>true</code>, if style property border is set, <code>false</code> otherwise
78     */
79    private boolean hasDoubleBorder(NodeRealizer context) {
80      return Boolean.TRUE.equals(((GenericNodeRealizer) context).getStyleProperty(ErdRealizerFactory.DOUBLE_BORDER));
81    }
82  
83  }
84