1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.realizer;
15  
16  
17  import java.awt.geom.RoundRectangle2D;
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.io.ObjectOutputStream;
21  
22  import y.util.YVersion;
23  import y.view.EditMode;
24  import y.view.Graph2DView;
25  import y.view.NodeRealizer;
26  import y.view.ShapeNodeRealizer;
27  
28  import javax.swing.JFrame;
29  import javax.swing.JRootPane;
30  
31  /**
32   * This class represents a custom ShapeNodeRealizer that represents a
33   * rounded rectangle with custom rounding radii.
34   */
35  public class RoundRectNodeRealizer extends ShapeNodeRealizer
36  {
37    private RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0,0,10,10,20,30);
38  
39  
40    public RoundRectNodeRealizer()
41    {
42      super();
43      this.setShapeType(ROUND_RECT);
44    }
45  
46    protected RoundRectangle2D.Double getRect(){
47        if (rect == null){
48            rect = new RoundRectangle2D.Double(0,0, 10,10,20, 30);
49        }
50        return rect;
51    }
52  
53    /**
54     * overwrite setShapeType to set specialized shape...
55     */
56    public void setShapeType(byte type){
57        super.setShapeType(type);
58        RoundRectangle2D.Double rect = getRect();
59        if (type == ROUND_RECT){
60            rect.x = x;
61            rect.y = y;
62            rect.width = width;
63            rect.height = height;
64            shape = rect;
65        }
66    }
67  
68    public RoundRectNodeRealizer(NodeRealizer r)
69    {
70      super(r);
71      setShapeType(ROUND_RECT);
72      if (r instanceof RoundRectNodeRealizer){
73          setArcWidth(((RoundRectNodeRealizer)r).getArcWidth());
74          setArcHeight(((RoundRectNodeRealizer)r).getArcHeight());
75      }
76    }
77  
78  
79    public NodeRealizer createCopy(NodeRealizer r)
80    {
81      return new RoundRectNodeRealizer(r);
82    }
83  
84    /**
85     * Writes out this realizer in a serialized form. This method 
86     * will be used by YGFIOHandler to serialize this NodeRealizer.
87     */
88    public void write(ObjectOutputStream out) throws IOException
89    {
90      //write out a version tag. version tags help to provide future
91      //serialization compatibility when node realizer features
92      //change.    
93      out.writeByte(YVersion.VERSION_1);
94      out.writeDouble(getArcHeight());
95      out.writeDouble(getArcWidth());
96      //write out the shape node realizer features
97      super.write(out);
98    }
99  
100   /**
101    * Reads in the serialized form of this realizer. The realizer must have been
102    * written out before by it's {@link #write(ObjectOutputStream)} method.
103    * This method will be used by YGFIOHandler to deserialize this NodeRealizer.
104    */
105   public void read(ObjectInputStream in) throws IOException,
106                                                 ClassNotFoundException
107   {
108     switch(in.readByte()) {
109     case YVersion.VERSION_1:
110       super.read(in);
111       setArcHeight(in.readDouble());
112       setArcWidth(in.readDouble());
113       break;
114     default:
115       //trouble
116     }
117   }
118 
119   /** Getter for property arcHeight.
120    * @return Value of property arcHeight.
121    *
122    */
123   public double getArcHeight() {
124       return getRect().archeight;
125   }
126 
127   /** Setter for property arcHeight.
128    * @param arcHeight New value of property arcHeight.
129    *
130    */
131   public void setArcHeight(double arcHeight) {
132       getRect().archeight = arcHeight;
133   }
134 
135   /** Getter for property arcWidth.
136    * @return Value of property arcWidth.
137    *
138    */
139   public double getArcWidth() {
140       return getRect().arcwidth;
141   }
142 
143   /** Setter for property arcWidth.
144    * @param arcWidth New value of property arcWidth.
145    *
146    */
147   public void setArcWidth(double arcWidth) {
148       getRect().arcwidth = arcWidth;
149   }
150 
151 
152 
153   public static void addContentTo( final JRootPane rootPane )
154   {
155     final RoundRectNodeRealizer r = new RoundRectNodeRealizer();
156     r.setArcHeight(20);
157     r.setArcWidth(40);
158     r.setSize(100,40);
159     r.setLabelText("Arc=" + r.getArcWidth() + ':' + r.getArcHeight());
160 
161     final Graph2DView view = new Graph2DView();
162     view.getGraph2D().setDefaultNodeRealizer(r.createCopy());
163     view.setAntialiasedPainting(true);
164     view.getGraph2D().createNode();
165     view.addViewMode(new EditMode());
166     view.fitContent();
167 
168     rootPane.setContentPane(view);
169   }
170 
171   /**
172    * Launcher method. Execute this class to see a sample instantiation of
173    * this node realizer in action.
174    */
175   public static void main(String[] args)
176   {
177     final JFrame frame = new JFrame();
178     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
179     addContentTo(frame.getRootPane());
180     frame.pack();
181     frame.setLocationRelativeTo(null);
182     frame.setVisible(true);
183   }
184 }
185