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