1
14 package demo.view.ports;
15
16 import java.awt.Color;
17 import java.awt.Graphics2D;
18 import java.awt.geom.Ellipse2D;
19 import java.awt.geom.Point2D;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23
24 import y.base.ListCell;
25 import y.base.YCursor;
26 import y.base.YList;
27 import y.geom.YPoint;
28
29 import y.view.EdgeRealizer;
30 import y.view.Graph2D;
31 import y.view.ImageNodeRealizer;
32 import y.view.NodeRealizer;
33 import y.view.Port;
34
35
40 public class FixedPortsNodeRealizer extends ImageNodeRealizer
41 {
42
43 private boolean paintingPorts = true;
44
45
46 private YList portCandidates;
47
48
49 public FixedPortsNodeRealizer()
50 {
51 init();
52 }
53
54
55 public FixedPortsNodeRealizer(NodeRealizer arg)
56 {
57 super(arg);
58 if (arg instanceof FixedPortsNodeRealizer){
59 FixedPortsNodeRealizer fpnr = (FixedPortsNodeRealizer)arg;
60 this.portCandidates =
61 new YList(fpnr.portCandidates);
62 this.paintingPorts = fpnr.paintingPorts;
63 }
64 init();
65 }
66
67
70 private void init(){
71 if (portCandidates == null){
72 portCandidates = new YList();
73 portCandidates.add(new Port(0,0));
74 }
75 }
76
77
81 public YList getPortCandidates()
82 {
83 return this.portCandidates;
84 }
85
86
89 public YList getPortCandidates(double grid){
90 YList candidates = new YList();
91 for (YCursor c = getPortCandidates().cursor(); c.ok(); c.next()){
92 Port p = (Port) c.current();
93 candidates.add(new YPoint(p.getX(this), p.getY(this)));
94 }
95 return candidates;
96 }
97
98
106 public YList createCandidateList(Graph2D graph, EdgeRealizer edge, boolean source){
107 return getPortCandidates();
108 }
109
110
118 public Port snapCandidate(Graph2D graph, EdgeRealizer edge, boolean source, double x, double y){
119
120 YList ports = createCandidateList(graph, edge, source);
121
122 if (ports == null || ports.size() < 1) return null;
124 Port closest = (Port) ports.first();
126 double dist = getDistance(x,y,closest);
127
128 for (YCursor cursor = ports.cursor(); cursor.ok(); cursor.next()){
129 Port p = (Port) cursor.current();
130 double d2 = getDistance(x,y, p);
131 if (d2 < dist){
132 dist = d2;
133 closest = p;
134 }
135 }
136 return new Port(closest);
137 }
138
139
146 public static double getDistance(double x, double y, Port port){
147 return Math.sqrt((x-port.getOffsetX())*(x-port.getOffsetX())
148 + (y-port.getOffsetY()) * (y-port.getOffsetY()));
149 }
150
151
154 public NodeRealizer createCopy(NodeRealizer arg){
155 return new FixedPortsNodeRealizer(arg);
156 }
157
158
161 public byte hotSpotHit(double x, double y){
162 return HOTSPOT_NONE;
163 }
164
165 private static final Ellipse2D ellipse = new Ellipse2D.Double();
166
167 public void paintNode(Graphics2D g2d){
168 super.paintNode(g2d);
169 if (isSelected()){
170 g2d.setColor(getHotSpotColor());
171 g2d.draw(getBoundingBox());
172 }
173 if (paintingPorts){
174 g2d.setColor(getHotSpotColor());
175 for (ListCell cell = getPortCandidates().firstCell(); cell != null; cell = cell.succ()){
176 Port p = (Port) cell.getInfo();
177 ellipse.setFrame(p.getX(this)-3.0,p.getY(this)-3.0,6.0,6.0);
178 g2d.setColor(Color.red);
179 g2d.fill(ellipse);
180 g2d.setColor(Color.black);
181 g2d.draw(ellipse);
182 }
183 }
184 }
185
186
189 public void paintHotSpots(Graphics2D g2d){
190 return;
191 }
192
193
197 public boolean isPaintingPorts()
198 {
199 return this.paintingPorts;
200 }
201
202
206 public void setPaintingPorts(boolean paintingPorts)
207 {
208 this.paintingPorts = paintingPorts;
209 }
210
211
214 public boolean findIntersection(double ix, double iy, double ox, double oy, Point2D res){
215 res.setLocation(ix, iy);
216 return true;
217 }
218
219
224 public void write(ObjectOutputStream oos) throws IOException{
225 super.write(oos);
226 YList ports = getPortCandidates();
227 oos.writeInt(ports.size());
228 for (YCursor yc = ports.cursor(); yc.ok(); yc.next()){
229 Port p = (Port)yc.current();
230 p.write(oos);
231 }
232 oos.writeBoolean(isPaintingPorts());
233 }
234
235
239 public void read(ObjectInputStream ois) throws IOException, ClassNotFoundException{
240 super.read(ois);
241 YList ports = getPortCandidates();
242 ports.clear();
243 int count = ois.readInt();
244 while (count-- > 0){
245 Port p = new Port();
246 p.read(ois);
247 ports.add(p);
248 }
249 setPaintingPorts(ois.readBoolean());
250 }
251 }
252