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.*;
18  import java.awt.geom.*;
19  import java.io.*;
20  
21  import y.view.*;
22  import y.util.YVersion;
23  
24  import javax.swing.JRootPane;
25  import javax.swing.JFrame;
26  
27  /**
28   * This class represents a customized ShapeNodeRealizer similar to
29   * the ellipse type, but restricts the aspect ratio to be 1, i.e. the
30   * ellipse to be a circle at all times.
31   */
32  public class CircleNodeRealizer extends ShapeNodeRealizer
33  {
34    private Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 10,10);
35  
36    public CircleNodeRealizer()
37    {
38      super();
39      this.setShapeType(HEXAGON); //anything but ELLIPSE OR RECT, since intersection handling
40                                  //in superclass handles these cases specially.
41      updateShape();
42    }
43  
44    protected void updateShape(){
45        this.shape = circle;
46        double radius = Math.min(getWidth(), getHeight());
47        circle.width = radius;
48        circle.height = radius;
49        circle.x = getCenterX() - radius/2;
50        circle.y = getCenterY() - radius/2;
51    }
52  
53    public CircleNodeRealizer(NodeRealizer r)
54    {
55      super(r);
56      setShapeType(HEXAGON);
57    }
58  
59    /**
60     * Paints the node on the given graphics context.
61     */
62    public void paintNode(Graphics2D gfx)
63    {
64      updateShape();
65  
66      super.paintNode(gfx);
67    }
68  
69    public void setLocation(double x, double y){
70        super.setLocation(x,y);
71        updateShape();
72    }
73  
74    public void setSize(double w, double h){
75        super.setSize(w, h);
76        updateShape();
77    }
78  
79    /**
80     * Returns the radius of this realizer.
81     */
82    public double getRadius()
83    {
84      return Math.min(getWidth(),getHeight())/2.0;
85    }
86  
87    /**
88     * Sets the radius of this node. This will effectively change the
89     * size of this node.
90     */
91    public void setRadius(double r)
92    {
93      setSize(2*r, 2*r);
94    }
95  
96    public NodeRealizer createCopy(NodeRealizer r)
97    {
98      return new CircleNodeRealizer(r);
99    }
100 
101   /**
102    * Writes out this realizer in a serialized form. This method 
103    * will be used by YGFIOHandler to serialize this NodeRealizer.
104    */
105   public void write(ObjectOutputStream out) throws IOException
106   {
107     //write out a version tag. version tags help to provide future
108     //serialization compatibility when node realizer features
109     //change.    
110     out.writeByte(YVersion.VERSION_1);
111     //write out the shape node realzier features
112     super.write(out);
113   }
114 
115   /**
116    * Reads in the serialized form of this realizer. The realizer must have been
117    * written out before by it's {@link #write(ObjectOutputStream)} method.
118    * This method will be used by YGFIOHandler to deserialize this NodeRealizer.
119    */
120   public void read(ObjectInputStream in) throws IOException,
121                                                 ClassNotFoundException
122   {
123     switch(in.readByte()) {
124     case YVersion.VERSION_1:
125       super.read(in);
126       break;
127     default:
128       //trouble
129     }
130   }
131 
132   /** Sets the width of this realizer. This method delegates to
133    *  {@link #setSize(double, double)}.
134    *
135    */
136   public void setWidth(double width) {
137       super.setWidth(width);
138       updateShape();
139   }
140 
141   /** Sets the height of this realizer. This method delegates to
142    *  {@link #setSize(double,double)}.
143    *
144    */
145   public void setHeight(double height) {
146       super.setHeight(height);
147       updateShape();
148   }
149 
150   /**
151    * Set the x coordinate of the center of the node.
152    *
153    */
154   public void setCenterX(double x) {
155       super.setCenterX(x);
156       updateShape();
157   }
158 
159   /**
160    * Set the coordinates of the center of the node.
161    *
162    */
163   public void setCenter(double x, double y) {
164       super.setCenter(x, y);
165       updateShape();
166   }
167 
168   /**
169    * Set the y coordinate of the center of the node.
170    *
171    */
172   public void setCenterY(double y) {
173       super.setCenterY(y);
174       updateShape();
175   }
176 
177   /**
178    * Sets the frame of the realizer, i.e. its size and
179    * its location.
180    *
181    */
182   public void setFrame(Rectangle2D frame) {
183       super.setFrame(frame);
184       updateShape();
185   }
186 
187   /**
188    * Sets the Y-Coordinate of the upper left corner of the node.
189    *
190    */
191   public void setY(double yp) {
192       super.setY(yp);
193       updateShape();
194   }
195 
196   /**
197    * Sets the frame of the realizer, i.e. its size and
198    * its location.
199    *
200    */
201   public void setFrame(double x, double y, double width, double height) {
202       super.setFrame(x, y, width, height);
203       updateShape();
204   }
205 
206   /**
207    * Sets the X-Coordinate of the upper left corner of the node.
208    *
209    */
210   public void setX(double xp) {
211       super.setX(xp);
212       updateShape();
213   }
214 
215 
216 
217   public static void addContentTo( final JRootPane rootPane )
218   {
219     final CircleNodeRealizer r = new CircleNodeRealizer();
220     r.setRadius(50);
221     r.setLabelText("Circle");
222 
223     final Graph2DView view = new Graph2DView();
224     view.getGraph2D().setDefaultNodeRealizer(r.createCopy());
225     view.setAntialiasedPainting(true);
226     view.getGraph2D().createNode();
227     view.addViewMode(new EditMode());
228     view.fitContent();
229 
230     rootPane.setContentPane(view);
231   }
232 
233   /**
234    * Launcher method. Execute this class to see a sample instantiation of
235    * this node realizer in action. Try to change the size of this realizer by 
236    * dragging the 8 hotspot handles that are visible around the node when selected.  
237    */
238   public static void main(String[] args)
239   {
240     final JFrame frame = new JFrame();
241     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
242     addContentTo(frame.getRootPane());
243     frame.pack();
244     frame.setLocationRelativeTo(null);
245     frame.setVisible(true);
246   }
247 }
248 
249