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.GenericNodeRealizer;
31  import y.view.NodeRealizer;
32  import y.view.Graph2D;
33  import y.base.Node;
34  import y.base.EdgeCursor;
35  import y.base.Edge;
36  
37  /**
38   * The {@link y.view.GenericNodeRealizer.LayerHandler} implementation for flowchart annotation node.
39   * The implementation puts the annotation symbol in the {@link y.view.Graph2DView#FG_LAYER foreground layer},
40   * if the edges of the annotation node are located in the foreground layer. This ensures the repainting of the symbol,
41   * if edges (or neighbour nodes) has been moved.
42   */
43  public class FlowchartAnnotationLayerHandler implements GenericNodeRealizer.LayerHandler {
44  
45    private static final String LAYER_STYLE_PROPERTY_KEY = "LAYER_STYLE_PROPERTY_KEY";
46  
47    /**
48     * Sets the logical graphical layer for this realizer. Layer information can be used by viewers to optimize redraws.
49     *
50     * @see y.view.Graph2DView#FG_LAYER
51     * @see y.view.Graph2DView#BG_LAYER
52     */
53    public void setLayer(NodeRealizer context, byte l) {
54      if (l == y.view.Graph2DView.FG_LAYER) {
55        ((GenericNodeRealizer) context).removeStyleProperty(LAYER_STYLE_PROPERTY_KEY);
56      } else {
57        ((GenericNodeRealizer)context).setStyleProperty(LAYER_STYLE_PROPERTY_KEY, new Byte(l));
58      }
59    }
60  
61    /**
62     * Returns the logical graphical layer for this realizer. Layer information can be used by viewers to optimize
63     * redraws.
64     *
65     * @see y.view.Graph2DView#FG_LAYER
66     * @see y.view.Graph2DView#BG_LAYER
67     */
68    public byte getLayer(NodeRealizer context) {
69      final Node node = context.getNode();
70      final Graph2D graph2D = (Graph2D) node.getGraph();
71      for (EdgeCursor edgeCursor = node.edges(); edgeCursor.ok(); edgeCursor.next()) {
72        Edge edge = edgeCursor.edge();
73        if (graph2D.getRealizer(edge).getLayer() == y.view.Graph2DView.FG_LAYER) {
74          return y.view.Graph2DView.FG_LAYER;
75        }
76      }
77      if (context instanceof GenericNodeRealizer && ((GenericNodeRealizer)context).getStyleProperty(LAYER_STYLE_PROPERTY_KEY) instanceof Byte){
78        Byte layer = (Byte) ((GenericNodeRealizer)context).getStyleProperty(LAYER_STYLE_PROPERTY_KEY);
79        return layer.byteValue();
80      } else {
81        return y.view.Graph2DView.FG_LAYER;
82      }
83    }
84  }
85