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.io.graphml;
15  
16  import y.view.NodeRealizer;
17  import y.view.ShapeNodeRealizer;
18  
19  import java.awt.Color;
20  import java.awt.Graphics2D;
21  
22  import demo.view.DemoDefaults;
23  
24  /**
25   * A simple customization of {@link y.view.ShapeNodeRealizer} that holds additional
26   * fields.
27   * GraphML serialization of this realizer and its additional fields is handled by
28   * {@link CustomNodeRealizerSerializer}.
29   */
30  public class CustomNodeRealizer extends ShapeNodeRealizer {
31    // Custom value
32    private int customValue;
33    // Custom attribute
34    private String customAttribute;
35  
36    /** Creates a new instance of CustomNodeRealizer. */
37    public CustomNodeRealizer() {
38      setSize(60, 40);
39      setCustomAttribute("v1.0");
40      setCustomValue(333);
41      setFillColor(DemoDefaults.DEFAULT_NODE_COLOR);
42    }
43  
44    /** Creates a new instance of CustomNodeRealizer. */
45    public CustomNodeRealizer(NodeRealizer nr) {
46      super(nr);
47      // If the given node realizer is of this type, then apply copy semantics. 
48      if (nr instanceof CustomNodeRealizer) {
49        CustomNodeRealizer fnr = (CustomNodeRealizer) nr;
50        // Copy the values of custom attributes. 
51        setCustomValue(fnr.customValue);
52        setCustomAttribute(fnr.customAttribute);
53      }
54    }
55  
56    public NodeRealizer createCopy(NodeRealizer nr) {
57      return new CustomNodeRealizer(nr);
58    }
59  
60    public void paintText(Graphics2D gfx) {
61      super.paintText(gfx);
62      gfx.setColor(Color.blue);
63      gfx.drawString("value: " + getCustomValue(), (float) getX() + 4, (float) getY() + 12);
64      gfx.drawString("attr:  " + getCustomAttribute(), (float) getX() + 4, (float) (getY() + getHeight() - 2));
65    }
66  
67    public int getCustomValue() {
68      return customValue;
69    }
70  
71    public void setCustomValue(int customValue) {
72      this.customValue = customValue;
73    }
74  
75    public String getCustomAttribute() {
76      return customAttribute;
77    }
78  
79    public void setCustomAttribute(String customAttribute) {
80      this.customAttribute = customAttribute;
81    }
82  }
83