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.flowchart.painters;
29  
30  import y.view.AbstractCustomNodePainter;
31  import y.view.NodeRealizer;
32  import y.view.GenericNodeRealizer;
33  
34  import java.awt.Paint;
35  import java.awt.Shape;
36  import java.awt.Graphics2D;
37  import java.awt.Color;
38  import java.awt.GradientPaint;
39  
40  /**
41   * Abstract painter class for flowchart symbols.
42   */
43  public abstract class AbstractFlowchartPainter extends AbstractCustomNodePainter implements GenericNodeRealizer.ContainsTest, FlowchartRealizerConstants {
44    /**
45     * Paints outline shape and interior decoration of a flowchart symbol.
46     * @param context the context node
47     * @param graphics the graphics context to use
48     * @param sloppy whether to draw the node sloppily
49     */
50    protected void paintNode(NodeRealizer context, Graphics2D graphics, boolean sloppy) {
51      final Shape outline = newShape(context);
52      if (initializeFill(context, graphics)) {
53        graphics.fill(outline);
54      }
55      if (initializeLine(context, graphics)) {
56        graphics.draw(outline);
57  
58        final Shape decoration = newDecoration(context);
59        if (decoration != null) {
60          graphics.draw(decoration);
61        }
62      }
63    }
64  
65    protected Paint getFillPaint( final NodeRealizer context, final boolean selected ) {
66      final Color fc1 = getFillColor(context, selected);
67      if (fc1 != null) {
68        final Color fc2 = getFillColor2(context, selected);
69        if (fc2 != null) {
70          final double x = context.getX();
71          final double y = context.getY();
72          return new GradientPaint(
73                  (float) x, (float) y, fc1,
74                  (float)(x + context.getWidth()), (float) (y + context.getHeight()), fc2,
75                  true);
76        } else {
77          return fc1;
78        }
79      } else {
80        return null;
81      }
82    }
83  
84    /**
85     * Calculates the outline shape for the specified node.
86     * @param context The node context
87     */
88    protected abstract Shape newShape(NodeRealizer context);
89  
90    /**
91     * Calculates the interior decoration for the specified node.
92     * @param context The node context
93     */
94    protected Shape newDecoration(NodeRealizer context) {
95      return null;
96    }
97  
98    public boolean contains(NodeRealizer context, double x, double y) {
99      return newShape(context).contains(x, y);
100   }
101 
102 
103   protected double getBorderDistance(NodeRealizer context, double defaultValue) {
104     GenericNodeRealizer gnr = (GenericNodeRealizer) context;
105     Object value = gnr.getStyleProperty(PROPERTY_BORDER_DISTANCE);
106     if (value instanceof Number) {
107       return ((Number) value).doubleValue();
108     } else {
109       return defaultValue;
110     }
111   }
112 
113   protected double getRadius(NodeRealizer context, double defaultValue) {
114     GenericNodeRealizer gnr = (GenericNodeRealizer) context;
115     Object value = gnr.getStyleProperty(PROPERTY_RADIUS);
116     if (value instanceof Number) {
117       return ((Number) value).doubleValue();
118     } else {
119       return defaultValue;
120     }
121   }
122 }
123