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.flowchart.painters;
15  
16  import y.view.AbstractCustomNodePainter;
17  import y.view.NodeRealizer;
18  import y.view.GenericNodeRealizer;
19  import y.view.YRenderingHints;
20  
21  import java.awt.Shape;
22  import java.awt.Graphics2D;
23  import java.awt.Color;
24  import java.awt.GradientPaint;
25  
26  /**
27   * Abstract painter class for flowchart symbols.
28   */
29  public abstract class AbstractFlowchartPainter extends AbstractCustomNodePainter implements GenericNodeRealizer.ContainsTest, FlowchartRealizerConstants {
30    protected Shape outline;
31    protected Shape innerShape;
32  
33  
34    /**
35     * Paints outline and interior of a flowchart symbol.
36     * @param context the context node
37     * @param graphics the graphics context to use
38     * @param sloppy whether to draw the node sloppily
39     */
40    protected void paintNode(NodeRealizer context, Graphics2D graphics, boolean sloppy) {
41      updateOutline(context);
42      updateInsideShape(context);
43  
44      boolean useSelectionStyle = useSelectionStyle(context, graphics);
45      graphics.setStroke(getLineStroke(context, useSelectionStyle));
46  
47      Color fillColor1 = getFillColor(context, useSelectionStyle);
48      Color fillColor2 = getFillColor2(context, useSelectionStyle);
49      Color lineColor = getLineColor(context, useSelectionStyle);
50  
51      if (fillColor1 != null) {
52        if (fillColor2 != null && useGradientStyle(graphics)) {
53          double x = context.getX();
54          double y = context.getY();
55          double width = context.getWidth();
56          double heigh = context.getHeight();
57          GradientPaint gp = new GradientPaint((float) x, (float) y, fillColor1, (float) x + (float) width,
58              (float) y + (float) heigh, fillColor2);
59          graphics.setPaint(gp);
60        } else {
61          graphics.setColor(fillColor1);
62        }
63        if (outline != null) {
64          graphics.fill(outline);
65        }
66      }
67      if (lineColor!=null){
68        graphics.setColor(lineColor);
69        if (outline != null){
70          graphics.draw(outline);
71        }
72        if (innerShape!=null){
73          graphics.draw(innerShape);
74        }
75      }
76    }
77  
78    /**
79     * Returns the outline
80     * @return The outline shape
81     */
82    Shape getOutline() {
83      return outline;
84    }
85  
86    /**
87     * Returns the current inner shape
88     * @return The inner shape
89     */
90    public Shape getInnerShape() {
91      return innerShape;
92    }
93  
94    /**
95     * Calculates the outline shape for the specified node.
96     * @param context The node context
97     */
98    protected abstract void updateOutline(NodeRealizer context);
99  
100   /**
101    * Calculates the interior shape for the specified node.
102    * @param context The node context
103    */
104   protected void updateInsideShape(NodeRealizer context) {
105   }
106 
107   public boolean contains(NodeRealizer context, double x, double y) {
108     updateOutline(context);
109     return outline != null && outline.contains(x, y);
110   }
111 
112 
113   static boolean useGradientStyle( final Graphics2D graphics ) {
114     return YRenderingHints.isGradientPaintingEnabled(graphics);
115   }
116 
117   static boolean useSelectionStyle(
118           final NodeRealizer context,
119           final Graphics2D gfx
120   ) {
121     return context.isSelected() && YRenderingHints.isSelectionPaintingEnabled(gfx);
122   }
123 }
124