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.mindmap;
29  
30  import demo.view.mindmap.StateIconProvider.StateIcon;
31  
32  import y.view.GenericNodeRealizer;
33  import y.view.LineType;
34  import y.view.NodeRealizer;
35  import y.view.YRenderingHints;
36  
37  import java.awt.Color;
38  import java.awt.Graphics2D;
39  import java.awt.RenderingHints;
40  import java.awt.geom.Line2D;
41  
42  /**
43   * Implementation of {@link y.view.GenericNodeRealizer.Painter} and {@link y.view.GenericNodeRealizer.ContainsTest}
44   * for mind map items.
45   */
46  class MindMapNodePainter implements GenericNodeRealizer.Painter{
47    private static final String KEY_STATE_ICON = "MindMapNodePainter.StateIcon";
48  
49    /**
50     * Paints a node with just an underline and its label on top of this line
51     * @param context current <code>NodeRealizer</code>
52     * @param graphics current <code>Graphics2D</code>
53     */
54    public void paint(final NodeRealizer context, Graphics2D graphics) {
55      paintImpl(context, graphics, false);
56    }
57  
58    /**
59     * Paints a node with few details.
60     * @param context current <code>NodeRealizer</code>
61     * @param graphics current <code>Graphics2D</code>
62     */
63    public void paintSloppy(final NodeRealizer context, final Graphics2D graphics) {
64      paintImpl(context, graphics, true);
65    }
66  
67    private void paintImpl(final NodeRealizer context, Graphics2D graphics, final boolean sloppy) {
68      graphics = (Graphics2D) graphics.create();
69  
70      final Color fc = context.getFillColor();
71      if (fc != null) {
72        final double x = context.getX();
73        final double y = context.getY() + context.getHeight();
74        graphics.setColor(fc);
75        graphics.setStroke(context.getLineType());
76        graphics.draw(new Line2D.Double(x, y, x + context.getWidth(), y));
77      }
78  
79      final Color lc = context.getLineColor();
80      if (lc != null && context.isSelected() && YRenderingHints.isSelectionPaintingEnabled(graphics)) {
81        graphics.setColor(lc);
82        graphics.setStroke(LineType.LINE_1);
83        graphics.draw(context.getBoundingBox());
84      }
85  
86      if (!sloppy) {
87        final StateIcon icon = getStateIcon(context);
88        if (icon != null) {
89          int xoffset = 0;
90          if (ViewModel.instance.isLeft(context.getNode())) {
91            xoffset = (int) context.getWidth() - 16;
92          }
93          graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
94          graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
95          graphics.translate(context.getX() + xoffset, context.getY());
96          icon.paintIcon(null, graphics, 0, 0);
97        }
98      }
99      graphics.dispose();
100   }
101 
102   /**
103    * Returns the state icon for the specified node realizer.
104    * @param nr the visual representation of a node in the mind map. 
105    * @return the state icon for the specified node realizer or <code>null</code>
106    * if the specified node realizer does not display a state icon.
107    */
108   static StateIcon getStateIcon( final NodeRealizer nr ) {
109     if (nr instanceof GenericNodeRealizer) {
110       final Object name = ((GenericNodeRealizer) nr).getStyleProperty(KEY_STATE_ICON);
111       if (name instanceof String) {
112         return StateIconProvider.instance.getIcon((String) name);
113       }
114     }
115     return null;
116   }
117 
118   /**
119    * Specifies the state icon for the specified node realizer.
120    * Does nothing if the specified node realizer is not an instance of
121    * {@link GenericNodeRealizer}.
122    * @param nr the visual representation of a node in the mind map.
123    * @param icon the state icon to display.
124    */
125   static void setStateIcon( final NodeRealizer nr, final StateIcon icon ) {
126     if (nr instanceof GenericNodeRealizer) {
127       if (icon == null || icon.getName() == null) {
128         ((GenericNodeRealizer) nr).removeStyleProperty(KEY_STATE_ICON);
129       } else {
130         ((GenericNodeRealizer) nr).setStyleProperty(KEY_STATE_ICON, icon.getName());
131       }
132     }
133   }
134 }
135