KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > GView


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;
33
34 import org.antlr.xjlib.appkit.frame.XJView;
35 import org.antlr.xjlib.appkit.gview.base.Rect;
36 import org.antlr.xjlib.appkit.gview.base.Vector2D;
37 import org.antlr.xjlib.appkit.gview.event.*;
38 import org.antlr.xjlib.appkit.gview.object.GElement;
39 import org.antlr.xjlib.appkit.gview.object.GLink;
40 import org.antlr.xjlib.appkit.gview.timer.GTimer;
41 import org.antlr.xjlib.appkit.gview.timer.GTimerDelegate;
42 import org.antlr.xjlib.appkit.gview.utils.GAlphaVariator;
43 import org.antlr.xjlib.appkit.gview.utils.GMagnetic;
44 import org.antlr.xjlib.appkit.menu.XJMenu;
45 import org.antlr.xjlib.appkit.menu.XJMenuItem;
46 import org.antlr.xjlib.appkit.menu.XJMenuItemDelegate;
47 import org.antlr.xjlib.appkit.swing.XJGraphics2DPS;
48
49 import javax.swing.*;
50 import javax.swing.event.PopupMenuEvent JavaDoc;
51 import javax.swing.event.PopupMenuListener JavaDoc;
52 import java.awt.*;
53 import java.awt.event.MouseEvent JavaDoc;
54 import java.awt.image.*;
55 import java.util.HashSet JavaDoc;
56 import java.util.Set JavaDoc;
57
58 public class GView extends XJView implements XJMenuItemDelegate, GTimerDelegate, GEventDelegate {
59
60     public static final double DEFAULT_LINK_FLATENESS = 40;
61     public static final int SCROLL_TO_VISIBLE_MARGIN = 10;
62
63     protected GViewDelegate delegate = null;
64     protected GEventManager eventManager = new GEventManager(this);
65
66     protected GElement rootElement = null;
67
68     protected Point lastMousePosition = null;
69     protected boolean smoothGraphics = true;
70     protected float zoom = 1;
71     protected boolean autoAdjustSize = false;
72     protected boolean drawBorder = true;
73     protected int sizeMargin = 0;
74
75     // ** Selected/focused elements
76

77     protected GTimer selectionTimer = new GTimer(this);
78     protected GTimer focusTimer = new GTimer(this);
79
80     protected GAlphaVariator selectionAlphaVariator = new GAlphaVariator();
81     protected GAlphaVariator focusAlphaVariator = new GAlphaVariator();
82
83     // ** Magnetics layout
84

85     protected Set JavaDoc<GMagnetic> magnetics = new HashSet JavaDoc<GMagnetic>();
86     protected boolean magneticsVisible = false;
87
88     public GView () {
89         setFocusable(false);
90
91         setBackground(Color.gray);
92         setPreferredSize(new Dimension(1024, 600));
93
94         addDefaultEventManager();
95     }
96
97     public void addDefaultEventManager() {
98         // !!! ORDER OF THE FOLLOWING METHODS CALLS IS IMPORTANT !!!
99

100         eventManager.add(new GEventDragElement(this));
101         eventManager.add(new GEventDragRootElement(this));
102         eventManager.add(new GEventDragSelection(this));
103         eventManager.add(new GEventEditElement(this));
104         eventManager.add(new GEventCreateLinkElement(this));
105         eventManager.add(new GEventCreateElement(this));
106         eventManager.add(new GEventFocusElement(this));
107         eventManager.add(new GEventModifyLinkElement(this));
108     }
109
110     public int defaultLinkShape() {
111         return GLink.SHAPE_ARC;
112     }
113
114     public void setRootElement(GElement element) {
115         this.rootElement = element;
116         if(rootElement != null) {
117             this.rootElement.setPanel(this);
118             autoAdjustSize();
119         }
120     }
121
122     public GElement getRootElement() {
123         return rootElement;
124     }
125
126     public GEventManager getEventManager() {
127         return eventManager;
128     }
129
130     public void setDelegate(GViewDelegate delegate) {
131         this.delegate = delegate;
132     }
133
134     public void setSmoothGraphics(boolean flag) {
135         smoothGraphics = flag;
136     }
137
138     public boolean getSmoothGraphics() {
139         return smoothGraphics;
140     }
141
142     public void setAutoAdjustSize(boolean flag) {
143         this.autoAdjustSize = flag;
144     }
145
146     public boolean getAutoAdjustSize() {
147         return autoAdjustSize;
148     }
149
150     public float getSelectionAlphaValue() {
151         return selectionAlphaVariator.getAlphaValue();
152     }
153
154     public float getFocusAlphaValue() {
155         return focusAlphaVariator.getAlphaValue();
156     }
157
158     public Point getMousePosition(MouseEvent JavaDoc e) {
159         Point p = e.getPoint();
160         if(zoom != 1)
161             return new Point((int)(p.x/zoom), (int)(p.y/zoom));
162         else
163             return p;
164     }
165
166     public Point getLastMousePosition() {
167         return lastMousePosition;
168     }
169
170     public void setDrawBorder(boolean flag) {
171         this.drawBorder = flag;
172     }
173
174     public boolean getDrawBorder() {
175         return drawBorder;
176     }
177
178     public void setZoom(float zoom) {
179         this.zoom = zoom;
180         autoAdjustSize();
181     }
182
183     public float getZoom() {
184         return zoom;
185     }
186
187     public void setSizeMargin(int margin) {
188         this.sizeMargin = margin;
189     }
190
191     public void setRealSize(Dimension d) {
192         if(sizeMargin > 0) {
193             d.width += sizeMargin;
194             d.height += sizeMargin;
195         }
196         setPreferredSize(d);
197         setMaximumSize(d);
198         revalidate();
199     }
200
201     public void setRealSize(int dx, int dy) {
202         setRealSize(new Dimension(dx, dy));
203     }
204
205     public Dimension getRealSize() {
206         return getPreferredSize();
207     }
208
209     public void autoAdjustSize() {
210         if(rootElement == null || !autoAdjustSize)
211             return;
212
213         Rect bounds = rootElement.bounds();
214         setRealSize((int) ((bounds.r.x+bounds.r.width)*zoom),
215                 (int) ((bounds.r.y+bounds.r.height)*zoom));
216
217         if(delegate != null)
218             delegate.viewSizeDidChange();
219     }
220
221     public void centerAll() {
222         if(rootElement == null)
223             return;
224
225         Dimension d = getRealSize();
226         if(d.width == 0 && d.height == 0) {
227             // Note: when the view has not been displayed, the current size seems to be (0, 0)
228
d = this.getPreferredSize();
229         }
230
231         Rectangle r = rootElement.bounds().rectangle();
232
233         double x = (d.width-r.width)*0.5;
234         double y = (d.height-r.height)*0.5;
235
236         rootElement.move(x-r.x, y-r.y);
237     }
238
239     public void setMagneticsVisible(boolean flag) {
240         this.magneticsVisible = flag;
241         repaint();
242     }
243
244     public boolean isMagneticsVisible() {
245         return magneticsVisible;
246     }
247
248     public void toggleShowMagnetics() {
249         setMagneticsVisible(!isMagneticsVisible());
250     }
251
252     public void createMagnetics() {
253         magnetics.clear();
254         double f = 0;
255         for(int i=0; i<delegate.getHorizontalMagnetics(); i++) {
256             f += 1.0/(delegate.getHorizontalMagnetics()+1);
257             magnetics.add(GMagnetic.createHorizontal(f));
258         }
259
260         f = 0;
261         for(int i=0; i<delegate.getVerticalMagnetics(); i++) {
262             f += 1.0/(delegate.getVerticalMagnetics()+1);
263             magnetics.add(GMagnetic.createVertical(f));
264         }
265     }
266
267     public void showAndAjustPositionToMagnetics(Vector2D position) {
268         for (GMagnetic magnetic : magnetics) {
269             magnetic.showAndAjust(position, getRealSize());
270         }
271     }
272
273     public void hideAllMagnetics() {
274         for (GMagnetic magnetic : magnetics) {
275             magnetic.setVisible(false);
276         }
277     }
278
279     public void scrollElementToVisible(GElement element) {
280         Rectangle r;
281         Rect frame = element.getFrame();
282         if(frame == null)
283             r = new Rectangle((int)element.getPositionX(), (int)element.getPositionY(), 1, 1);
284         else
285             r = frame.r;
286
287         // Scale according to the current zoom
288
r.x *= zoom;
289         r.y *= zoom;
290         r.width *= zoom;
291         r.height *= zoom;
292
293         // Add some margin to make the element "more" visible
294
r.x -= SCROLL_TO_VISIBLE_MARGIN;
295         r.y -= SCROLL_TO_VISIBLE_MARGIN;
296         r.width += 2*SCROLL_TO_VISIBLE_MARGIN;
297         r.height += 2*SCROLL_TO_VISIBLE_MARGIN;
298
299         scrollRectToVisible(r);
300         //new XJSmoothScrolling(this, r, null);
301
}
302
303     public void addSelectedElement(GElement element) {
304         selectionTimer.add(element);
305     }
306
307     public void removeSelectedElement(GElement element) {
308         selectionTimer.remove(element);
309     }
310
311     public void addFocusedElement(GElement element) {
312         focusTimer.add(element);
313     }
314
315     public void removeFocusedElement(GElement element) {
316         focusTimer.remove(element);
317     }
318
319     public BufferedImage getImage() {
320         int width = getPreferredSize().width;
321         int height = getPreferredSize().height;
322
323         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
324 // BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
325
Graphics2D g2d = (Graphics2D)image.getGraphics();
326         super.paintComponent(g2d);
327
328         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
329
330         g2d.setColor(Color.white);
331         g2d.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
332
333         rootElement.drawRecursive(g2d);
334
335         g2d.dispose();
336
337         return image; //makeColorTransparent(image, Color.white);
338
}
339
340     public String JavaDoc getEPS() {
341         XJGraphics2DPS g2d = new XJGraphics2DPS();
342         g2d.setMargins(6, 6);
343         rootElement.drawRecursive(g2d);
344         return g2d.getPSText();
345     }
346
347     // Does not work currently - don't know why...
348
public Image makeColorTransparent(Image im, final Color color) {
349         ImageFilter filter = new RGBImageFilter() {
350             public int markerRGB = color.getRGB() | 0xFF000000;
351
352             public int filterRGB(int x, int y, int rgb) {
353                 if ( ( rgb | 0xFF000000 ) == markerRGB ) {
354                     // Mark the alpha bits as zero - transparent
355
return 0x00FFFFFF & rgb;
356                 }
357                 else {
358                     // nothing to do
359
return rgb;
360                 }
361             }
362         };
363
364         ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
365         return Toolkit.getDefaultToolkit().createImage(ip);
366     }
367
368     public void drawMagnetics(Graphics2D g2d) {
369         g2d.setColor(Color.yellow);
370
371         for (GMagnetic magnetic : magnetics) {
372             if (magneticsVisible || magnetic.isVisible())
373                 magnetic.draw(g2d, getRealSize());
374         }
375     }
376
377     public void paintComponent(Graphics g) {
378         super.paintComponent(g);
379
380         Graphics2D g2d = (Graphics2D)g;
381         if(smoothGraphics)
382             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
383
384         if(drawBorder) {
385             g2d.setColor(Color.white);
386             g2d.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
387             g2d.setColor(Color.darkGray);
388             g2d.drawRect(0, 0, getPreferredSize().width, getPreferredSize().height);
389             g2d.clipRect(0, 0, getPreferredSize().width, getPreferredSize().height);
390         }
391
392         g2d.scale(zoom, zoom);
393
394         drawMagnetics(g2d);
395         if(rootElement != null)
396             rootElement.drawRecursive(g2d);
397
398         eventManager.performEventObjects(GEventManager.EVENT_DRAW, null, null, g);
399     }
400
401     public void addMenuItem(JPopupMenu menu, String JavaDoc title, int tag, Object JavaDoc object) {
402         XJMenuItem item = new XJMenuItem();
403         item.setTitle(title);
404         item.setTag(tag);
405         item.setObject(object);
406         item.setDelegate(this);
407
408         menu.add(item.getSwingComponent());
409     }
410
411     public JPopupMenu getContextualMenu(GElement element) {
412         return null;
413     }
414
415     public java.util.List JavaDoc<GElement> getSelectedElements() {
416         return selectionTimer.getElements();
417     }
418
419     public GElement getElementAtMousePosition(MouseEvent JavaDoc e) {
420         return getElementAtPoint(getMousePosition(e));
421     }
422
423     public GElement getElementAtPoint(Point p) {
424         if(rootElement != null)
425             return rootElement.match(p);
426         else
427             return null;
428     }
429
430     public void changeDone() {
431         if(delegate == null)
432             return;
433
434         delegate.changeOccured();
435     }
436
437     public void selectElementsInRect(int x, int y, int dx, int dy) {
438         Rect rectangle = new Rect(x, y, dx, dy);
439         if(rootElement == null || rootElement.getElements() == null)
440             return;
441
442         for (GElement element : rootElement.getElements()) {
443             boolean selected = Rect.intersect(rectangle, element.bounds());
444             element.setSelected(selected);
445             if (selected)
446                 addSelectedElement(element);
447             else
448                 removeSelectedElement(element);
449         }
450     }
451
452     public void selectAllElements(boolean select) {
453         if(rootElement == null)
454             return;
455
456         for (GElement element : rootElement.getElements()) {
457             element.setSelected(select);
458             if (select)
459                 addSelectedElement(element);
460             else
461                 removeSelectedElement(element);
462         }
463     }
464
465     public void moveSelectedElements(double dx, double dy) {
466         for (GElement element : selectionTimer.getElements()) {
467             element.move(dx, dy);
468         }
469         autoAdjustSize();
470     }
471
472     public void timerFired(GTimer timer) {
473         if(timer == selectionTimer)
474             selectionAlphaVariator.run();
475         else if(timer == focusTimer)
476             focusAlphaVariator.run();
477
478         repaint();
479     }
480
481     public void processMouseEvent(MouseEvent JavaDoc e) {
482         if(e.isPopupTrigger()) {
483             JPopupMenu menu = getContextualMenu(getElementAtPoint(getMousePosition(e)));
484             if(menu != null) {
485                 this.requestFocus();
486                 menu.show(this, e.getX(), e.getY());
487                 lastMousePosition = getMousePosition(e);
488             }
489         } else
490             super.processMouseEvent(e);
491     }
492
493     public void handleMousePressed(MouseEvent JavaDoc e) {
494         eventManager.performEventObjects(GEventManager.EVENT_MOUSE_PRESSED, e, getMousePosition(e), null);
495     }
496
497     public void handleMouseReleased(MouseEvent JavaDoc e) {
498         eventManager.performEventObjects(GEventManager.EVENT_MOUSE_RELEASED, e, getMousePosition(e), null);
499     }
500
501     public void handleMouseDragged(MouseEvent JavaDoc e) {
502         eventManager.performEventObjects(GEventManager.EVENT_MOUSE_DRAGGED, e, getMousePosition(e), null);
503     }
504
505     public void handleMouseMoved(MouseEvent JavaDoc e) {
506         eventManager.performEventObjects(GEventManager.EVENT_MOUSE_MOVED, e, getMousePosition(e), null);
507         if(delegate != null)
508             delegate.contextualHelp(getElementAtPoint(getMousePosition(e)));
509     }
510
511     public void handleMouseEntered(MouseEvent JavaDoc e) {
512         selectionTimer.refresh();
513         focusTimer.refresh();
514     }
515
516     public void handleMouseExited(MouseEvent JavaDoc e) {
517         selectionTimer.stop();
518         focusTimer.stop();
519     }
520
521     public void handleMenuEvent(XJMenu menu, XJMenuItem item) {
522     }
523
524     public class MyContextualMenuListener implements PopupMenuListener JavaDoc {
525
526         public void popupMenuWillBecomeVisible(PopupMenuEvent JavaDoc event) {
527         }
528
529         public void popupMenuWillBecomeInvisible(PopupMenuEvent JavaDoc event) {
530             repaint();
531         }
532
533         public void popupMenuCanceled(PopupMenuEvent JavaDoc event) {
534         }
535     }
536
537     // *** GEventDelegate methods
538

539     public void eventChangeDone() {
540         autoAdjustSize();
541         changeDone();
542     }
543
544     public void eventShouldRepaint() {
545         repaint();
546     }
547
548     public GElement eventQueryElementAtPoint(Point p) {
549         return getElementAtPoint(p);
550     }
551
552     public GElement eventQueryRootElement() {
553         return rootElement;
554     }
555
556     public void eventSouldSelectAllElements(boolean flag) {
557         selectAllElements(flag);
558     }
559
560     public void eventMoveSelectedElements(int dx, int dy) {
561         moveSelectedElements(dx, dy);
562     }
563
564     public void eventCreateElement(Point p, boolean doubleclick) {
565
566     }
567
568     public void eventEditElement(GElement e) {
569
570     }
571
572     public boolean eventCanCreateLink() {
573         return false;
574     }
575
576     public double eventLinkFlateness() {
577         return DEFAULT_LINK_FLATENESS;
578     }
579
580     public void eventCreateLink(GElement source, String JavaDoc sourceAnchorKey, GElement target, String JavaDoc targetAnchorKey, int shape, Point p) {
581         rootElement.addElement(new GLink(source, sourceAnchorKey, target, targetAnchorKey, shape, "", p, GView.DEFAULT_LINK_FLATENESS));
582     }
583
584     public void eventSelectElementsInRect(int x, int y, int dx, int dy) {
585         selectElementsInRect(x, y, dx, dy);
586     }
587
588     public void eventAddFocusedElement(GElement element) {
589         addFocusedElement(element);
590     }
591
592     public void eventRemoveFocusedElement(GElement element) {
593         removeFocusedElement(element);
594     }
595
596     public boolean eventIsSelectedElement(GElement element) {
597         return selectionTimer.contains(element);
598     }
599
600 }
601
Popular Tags