KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > object > GElement


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.gview.object;
33
34 import org.antlr.xjlib.appkit.gview.GView;
35 import org.antlr.xjlib.appkit.gview.base.Anchor2D;
36 import org.antlr.xjlib.appkit.gview.base.Rect;
37 import org.antlr.xjlib.appkit.gview.base.Vector2D;
38 import org.antlr.xjlib.foundation.XJXMLSerializable;
39
40 import java.awt.*;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 public abstract class GElement implements XJXMLSerializable {
47
48     public static final String JavaDoc ANCHOR_CENTER = "CENTER";
49     public static final String JavaDoc ANCHOR_TOP = "TOP";
50     public static final String JavaDoc ANCHOR_BOTTOM = "BOTTOM";
51     public static final String JavaDoc ANCHOR_LEFT = "LEFT";
52     public static final String JavaDoc ANCHOR_RIGHT = "RIGHT";
53
54     protected transient GView view = null;
55     protected List JavaDoc<GElement> elements = new ArrayList JavaDoc<GElement>();
56
57     protected Vector2D position = new Vector2D();
58     protected transient Vector2D oldPosition = null;
59
60     protected transient Map JavaDoc<String JavaDoc,Anchor2D> anchors = new HashMap JavaDoc<String JavaDoc, Anchor2D>();
61
62     protected String JavaDoc label = null;
63     protected Color labelColor = Color.black;
64     protected boolean labelVisible = true;
65
66     protected transient boolean selected = false;
67     protected transient boolean focused = false;
68
69     protected transient Color color = Color.black;
70     protected transient int penSize = 1;
71
72     protected transient BasicStroke strokeSize = new BasicStroke(penSize);
73     protected transient BasicStroke strokeNormal = new BasicStroke(1);
74     protected transient BasicStroke strokeBold = new BasicStroke(3);
75
76     protected boolean draggable = false;
77
78     protected final Object JavaDoc lock = new Object JavaDoc();
79
80     public GElement () {
81     }
82
83     public void setPanel(GView view) {
84         this.view = view;
85         synchronized(lock) {
86             for (GElement element : elements) {
87                 element.setPanel(view);
88             }
89         }
90     }
91
92     public void setLabel(String JavaDoc label) {
93         this.label = label;
94     }
95
96     public String JavaDoc getLabel() {
97         return label;
98     }
99
100     public boolean isLabelEqualsTo(String JavaDoc otherLabel) {
101         if(label == null)
102             return otherLabel == null;
103         else
104             return label.equals(otherLabel);
105     }
106
107     public void setLabelColor(Color color) {
108         this.labelColor = color;
109     }
110
111     public Color getLabelColor() {
112         return labelColor;
113     }
114
115     public void setLabelVisible(boolean flag) {
116         this.labelVisible = flag;
117     }
118
119     public boolean isLabelVisible() {
120         return labelVisible;
121     }
122
123     public void setPosition(double x, double y) {
124         // This is the position of the center of the element
125
position.setX(x);
126         position.setY(y);
127         elementPositionDidChange();
128     }
129
130     public double getPositionX() {
131         return position.getX();
132     }
133
134     public double getPositionY() {
135         return position.getY();
136     }
137
138     public void setPosition(Vector2D position) {
139         this.position = position;
140         elementPositionDidChange();
141     }
142
143     public Vector2D getPosition() {
144         return position;
145     }
146
147     public void setElements(List JavaDoc<GElement> elements) {
148         this.elements = elements;
149     }
150
151     public List JavaDoc<GElement> getElements() {
152         return elements;
153     }
154
155     public void addElement(GElement element) {
156         element.setPanel(view);
157         synchronized(lock) {
158             elements.add(element);
159         }
160     }
161
162     public void removeElement(GElement element) {
163         synchronized(lock) {
164             elements.remove(element);
165         }
166     }
167
168     public GElement getFirstElement() {
169         if(elements == null || elements.isEmpty())
170             return null;
171         else
172             return elements.get(0);
173     }
174
175     public GElement getLastElement() {
176         if(elements == null || elements.isEmpty())
177             return null;
178         else
179             return elements.get(elements.size()-1);
180     }
181
182     public GElement findElementWithLabel(String JavaDoc label) {
183         if(isLabelEqualsTo(label))
184             return this;
185
186         if(elements == null)
187             return null;
188
189         for (GElement element : elements) {
190             if (element.isLabelEqualsTo(label))
191                 return element;
192             else {
193                 element = element.findElementWithLabel(label);
194                 if (element != null)
195                     return element;
196             }
197         }
198
199         return null;
200     }
201
202     public void updateAnchors() {
203     }
204
205     public void setAnchor(String JavaDoc key, Vector2D position, Vector2D direction) {
206         Anchor2D anchor = getAnchor(key);
207         if(anchor == null) {
208             anchor = new Anchor2D();
209             anchors.put(key, anchor);
210         }
211         anchor.setPosition(position);
212         anchor.setDirection(direction);
213     }
214
215     public double getDefaultAnchorOffset(String JavaDoc anchorKey) {
216         return 0;
217     }
218
219     public Anchor2D getAnchor(String JavaDoc key) {
220         return anchors.get(key);
221     }
222
223     public String JavaDoc getAnchorKeyClosestToPoint(Point p) {
224         Anchor2D anchor = getAnchorClosestToPoint(p);
225         for (String JavaDoc key : anchors.keySet()) {
226             if (anchors.get(key) == anchor)
227                 return key;
228         }
229         return null;
230     }
231
232     public Anchor2D getAnchorClosestToPoint(Point p) {
233         double smallest_distance = Integer.MAX_VALUE;
234         Anchor2D closest_anchor = null;
235
236         for (Anchor2D anchor : anchors.values()) {
237             double dx = anchor.position.getX() - p.x;
238             double dy = anchor.position.getY() - p.y;
239             double d = Math.sqrt(dx * dx + dy * dy);
240             if (d < smallest_distance) {
241                 smallest_distance = d;
242                 closest_anchor = anchor;
243             }
244         }
245
246         return closest_anchor;
247     }
248
249     public Rect bounds() {
250         Rect r = getFrame();
251         synchronized(lock) {
252             for (GElement element : elements) {
253                 if (element == this)
254                     continue;
255
256                 if (r == null)
257                     r = element.bounds();
258                 else
259                     r = r.union(element.bounds());
260             }
261         }
262         return r;
263     }
264
265     public Rect getFrame() {
266         return null;
267     }
268
269     public void setFocused(boolean flag) {
270         focused = flag;
271     }
272
273     public boolean isFocused() {
274         return focused;
275     }
276
277     public void setSelected(boolean flag) {
278         selected = flag;
279     }
280
281     public boolean isSelected() {
282         return selected;
283     }
284
285     public boolean acceptIncomingLink() {
286         return false;
287     }
288
289     public boolean acceptOutgoingLink() {
290         return false;
291     }
292
293     public void setDraggable(boolean flag) {
294         this.draggable = flag;
295     }
296
297     public boolean isDraggable() {
298         return draggable;
299     }
300     
301     public void setColor(Color color) {
302         this.color = color;
303     }
304
305     public void setPenSize(int size) {
306         penSize = size;
307         strokeSize = new BasicStroke(penSize);
308     }
309
310     public boolean isInside(Point p) {
311         return false;
312     }
313
314     public void move(double dx, double dy) {
315         position.shift(dx, dy);
316
317         // Recursively move every other children objects
318
synchronized(lock) {
319             for (GElement element : elements) {
320                 element.move(dx, dy);
321             }
322         }
323
324         elementPositionDidChange();
325     }
326
327     public void moveToPosition(Vector2D position) {
328         double dx = position.x-getPosition().x;
329         double dy = position.y-getPosition().y;
330         move(dx, dy);
331     }
332
333     public GElement match(Point p) {
334         synchronized(lock) {
335             for (GElement element : elements) {
336                 GElement match = element.match(p);
337                 if (match != null)
338                     return match;
339             }
340         }
341
342         if(isInside(p))
343             return this;
344         else
345             return null;
346     }
347
348     public void beginDrag() {
349         oldPosition = null;
350     }
351
352     public Vector2D dragElementPosition(Vector2D p) {
353         Vector2D ep = p.copy();
354         if(oldPosition != null) {
355             ep.x += p.x-oldPosition.x;
356             ep.y += p.y-oldPosition.y;
357         }
358         return ep;
359     }
360
361     public void drag(Vector2D p) {
362         double dx = 0;
363         double dy = 0;
364
365         if(oldPosition == null) {
366             oldPosition = new Vector2D();
367         } else {
368             dx = p.x-oldPosition.x;
369             dy = p.y-oldPosition.y;
370         }
371
372         oldPosition.x = p.x;
373         oldPosition.y = p.y;
374
375         move(dx, dy);
376     }
377
378     public void drawRecursive(Graphics2D g) {
379         synchronized(lock) {
380             for (GElement element : elements) {
381                 element.drawRecursive(g);
382             }
383         }
384
385         draw(g);
386         if(isSelected())
387             drawSelected(g);
388         else if(isFocused())
389             drawFocused(g);
390     }
391
392     public boolean isVisibleInClip(Graphics2D g) {
393         Rectangle r = getFrame().rectangle();
394         // some margin to avoid clipped drawing
395
r.width++;
396         r.height++;
397         return g.getClipBounds().intersects(r);
398     }
399
400     public void draw(Graphics2D g) {
401
402     }
403
404     public void drawShape(Graphics2D g) {
405
406     }
407
408     private void drawSelected(Graphics2D g) {
409         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, view.getSelectionAlphaValue()));
410         g.setColor(Color.gray);
411         g.setStroke(strokeBold);
412
413         drawShape(g);
414
415         g.setStroke(strokeNormal);
416         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f ));
417     }
418
419     private void drawFocused(Graphics2D g) {
420         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, view.getFocusAlphaValue()));
421         g.setColor(Color.blue);
422         g.setStroke(strokeBold);
423
424         drawShape(g);
425
426         g.setStroke(strokeNormal);
427         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f ));
428     }
429
430     // *** Notifications
431

432     public void elementPositionDidChange() {
433         updateAnchors();
434     }
435
436     public void elementDimensionDidChange() {
437         updateAnchors();
438     }
439
440     /**
441      * Method invoked when the element has been loaded from disk
442      */

443     public void elementDidLoad() {
444         synchronized(lock) {
445             for (GElement element : elements) {
446                 element.elementDidLoad();
447             }
448         }
449
450         // Update the anchors
451
updateAnchors();
452     }
453
454     public String JavaDoc toString() {
455         return getClass().getName()+": "+position.x+"/"+position.y;
456     }
457
458 }
459
Popular Tags