Source code: com/memoire/dja/DjaBrokenArrow.java
1 /**
2 * @modification $Date: 2002/10/09 15:08:24 $
3 * @statut unstable
4 * @file DjaBrokenArrow.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.dja;
13
14 import com.memoire.fu.*;
15 import com.memoire.dnd.*;
16 import com.memoire.bu.*;
17 import com.memoire.dja.*;
18 import com.memoire.mst.*;
19
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.border.*;
25
26 public class DjaBrokenArrow
27 extends DjaLink
28 {
29 public DjaBrokenArrow(String _text)
30 {
31 super();
32
33 if(_text!=null) addText(_text);
34 }
35
36 public DjaBrokenArrow()
37 {
38 this(null);
39 }
40
41 public Rectangle getExtendedBounds()
42 {
43 int[][] t=compute();
44
45 int x0,y0,x1,y1,x2,y2,x3,y3;
46 x0=t[0][0];
47 x1=t[0][1];
48 x2=t[0][2];
49 x3=t[0][3];
50 y0=t[1][0];
51 y1=t[1][1];
52 y2=t[1][2];
53 y3=t[1][3];
54
55 int xmin=Math.min(x0,Math.min(x1,Math.min(x2,x3)));
56 int xmax=Math.max(x0,Math.max(x1,Math.max(x2,x3)));
57 int ymin=Math.min(y0,Math.min(y1,Math.min(y2,y3)));
58 int ymax=Math.max(y0,Math.max(y1,Math.max(y2,y3)));
59 return getExtendedBounds(new Rectangle(xmin,ymin,xmax-xmin,ymax-ymin));
60 }
61
62 public boolean contains(int _x, int _y)
63 {
64 int[][] t=compute();
65
66 int x0,y0,x1,y1,x2,y2,x3,y3;
67 x0=t[0][0];
68 x1=t[0][1];
69 x2=t[0][2];
70 x3=t[0][3];
71 y0=t[1][0];
72 y1=t[1][1];
73 y2=t[1][2];
74 y3=t[1][3];
75
76 return
77 (rectangle(x0,y0,x2,y0).contains(_x,_y))
78 || (rectangle(x2,y0,x2,y2).contains(_x,_y))
79 || (rectangle(x2,y2,x3,y3).contains(_x,_y))
80 || (rectangle(x3,y3,x1,y1).contains(_x,_y));
81 }
82
83 public DjaAnchor[] getAnchors()
84 {
85 int[][] t=compute();
86
87 int x0,y0,x1,y1,x2,y2,x3,y3;
88 x0=t[0][0];
89 x1=t[0][1];
90 x2=t[0][2];
91 x3=t[0][3];
92 y0=t[1][0];
93 y1=t[1][1];
94 y2=t[1][2];
95 y3=t[1][3];
96
97 DjaAnchor[] r=new DjaAnchor[1];
98 r[0]=new DjaAnchor(this,0,ANY,(x2+x3)/2,(y2+y3)/2);
99 return r;
100 }
101
102 private int[][] compute()
103 {
104 int x0,y0,x1,y1,x2,y2,x3,y3;
105 int o0,o1;
106
107 updateXYO();
108 x0=xbegin_;
109 y0=ybegin_;
110 o0=obegin_;
111
112 x1=xend_;
113 y1=yend_;
114 o1=oend_;
115
116 x2=x3=(x0+x1)/2;
117 y2=y3=(y0+y1)/2;
118
119 if((o0==NORTH)||(o0==SOUTH)) x2=x0;
120 if((o0==EAST )||(o0==WEST )) y2=y0;
121 if((o1==NORTH)||(o1==SOUTH)) x3=x1;
122 if((o1==EAST )||(o1==WEST )) y3=y1;
123
124 if((o0==NORTH)&&(o1==NORTH)) y3=y2=Math.min(y0-2*deltaY,y1-2*deltaY);
125 if((o0==SOUTH)&&(o1==SOUTH)) y3=y2=Math.max(y0+2*deltaY,y1+2*deltaY);
126 if((o0==EAST )&&(o1==EAST )) x3=x2=Math.max(x0+2*deltaX,x1+2*deltaX);
127 if((o0==WEST )&&(o1==WEST )) x3=x2=Math.min(x0-2*deltaX,x1-2*deltaX);
128
129 if((o0==NORTH)&&(o1==WEST )) { x3=x2; y2=y3; }
130 if((o0==NORTH)&&(o1==EAST )) { x3=x2; y2=y3; }
131 if((o0==SOUTH)&&(o1==WEST )) { x3=x2; y2=y3; }
132 if((o0==SOUTH)&&(o1==EAST )) { x3=x2; y2=y3; }
133
134 if((o0==EAST )&&(o1==NORTH)) { x2=x3; y3=y2; }
135 if((o0==WEST )&&(o1==NORTH)) { x2=x3; y3=y2; }
136 if((o0==EAST )&&(o1==SOUTH)) { x2=x3; y3=y2; }
137 if((o0==WEST )&&(o1==SOUTH)) { x2=x3; y3=y2; }
138
139 int rounded=0;
140 try { rounded=Integer.parseInt(getProperty("arrondi")); }
141 catch(Exception ex) { }
142
143 if((rounded>0)&&(x2==x3)&&(y2==y3))
144 {
145 if(x0==x2)
146 { x3+=(x1>x0 ? rounded : -rounded); y2+=(y0>y1 ? rounded : -rounded); }
147 else
148 { y3+=(y1>y0 ? rounded : -rounded); x2+=(x0>x1 ? rounded : -rounded); }
149 }
150
151 return new int[][]
152 { { x0,x1,x2,x3,o0 }, { y0,y1,y2,y3,o1 } };
153 }
154
155 public void paintObject(Graphics _g)
156 {
157 int[][] t=compute();
158
159 int x0,y0,x1,y1,x2,y2,x3,y3,o0,o1;
160 x0=t[0][0];
161 x1=t[0][1];
162 x2=t[0][2];
163 x3=t[0][3];
164 y0=t[1][0];
165 y1=t[1][1];
166 y2=t[1][2];
167 y3=t[1][3];
168 o0=t[0][4];
169 o1=t[1][4];
170
171 _g.setColor(getForeground());
172 DjaGraphics.BresenhamParams bp=DjaGraphics.getBresenhamParams(this);
173
174 Polygon p=new Polygon();
175 p.addPoint(x0,y0);
176 p.addPoint(x2,y0);
177 p.addPoint(x2,y2);
178 p.addPoint(x3,y3);
179 p.addPoint(x3,y1);
180 p.addPoint(x1,y1);
181 DjaGraphics.drawPolyline(_g,p,bp);
182
183 int x,y;
184
185 switch(o0)
186 {
187 case EAST:
188 drawBracket(_g,tbegin_,o0,x0,y0-5,x0+10,y0+5);
189 break;
190 case WEST:
191 drawBracket(_g,tbegin_,o0,x0,y0-5,x0-10,y0+5);
192 break;
193 case NORTH:
194 drawBracket(_g,tbegin_,o0,x0-5,y0,x0+5,y0-10);
195 break;
196 case SOUTH:
197 drawBracket(_g,tbegin_,o0,x0-5,y0,x0+5,y0+10);
198 break;
199 }
200
201 switch(o1)
202 {
203 case EAST:
204 drawBracket(_g,tend_,o1,x1,y1-5,x1+10,y1+5);
205 break;
206 case WEST:
207 drawBracket(_g,tend_,o1,x1,y1-5,x1-10,y1+5);
208 break;
209 case NORTH:
210 drawBracket(_g,tend_,o1,x1-5,y1,x1+5,y1-10);
211 break;
212 case SOUTH:
213 drawBracket(_g,tend_,o1,x1-5,y1,x1+5,y1+10);
214 break;
215 }
216
217 super.paintObject(_g);
218 }
219 }