1
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
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
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
88 public void write(ObjectOutputStream out) throws IOException
89 {
90 out.writeByte(YVersion.VERSION_1);
94 out.writeDouble(getArcHeight());
95 out.writeDouble(getArcWidth());
96 super.write(out);
98 }
99
100
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 }
117 }
118
119
123 public double getArcHeight() {
124 return getRect().archeight;
125 }
126
127
131 public void setArcHeight(double arcHeight) {
132 getRect().archeight = arcHeight;
133 }
134
135
139 public double getArcWidth() {
140 return getRect().arcwidth;
141 }
142
143
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
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