KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > event > GEventCreateLinkElement


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.event;
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.Vector2D;
37 import org.antlr.xjlib.appkit.gview.object.GElement;
38 import org.antlr.xjlib.appkit.gview.object.GLink;
39 import org.antlr.xjlib.appkit.gview.shape.SLink;
40 import org.antlr.xjlib.appkit.gview.shape.SLinkArc;
41 import org.antlr.xjlib.appkit.gview.shape.SLinkElbow;
42
43 import java.awt.*;
44 import java.awt.event.InputEvent JavaDoc;
45 import java.awt.event.MouseEvent JavaDoc;
46
47 public class GEventCreateLinkElement extends GAbstractEvent {
48
49     public GElement startElement = null;
50     public String JavaDoc startAnchorKey = null;
51     public SLink linkElement = null;
52     public SLinkArc linkArc = null;
53     public SLinkElbow linkElbow = null;
54
55     public GEventCreateLinkElement(GView view) {
56         super(view);
57     }
58
59     public void mousePressed(MouseEvent JavaDoc e, Point mousePosition) {
60         GElement selectedElement = delegate.eventQueryElementAtPoint(mousePosition);
61
62         if(startElement != null) {
63             if(selectedElement != null) {
64                 int type;
65                 if(linkElement instanceof SLinkElbow)
66                     type = GLink.SHAPE_ELBOW;
67                 else
68                     type = GLink.SHAPE_ARC;
69
70                 delegate.eventCreateLink(startElement, startAnchorKey,
71                                             selectedElement, selectedElement.getAnchorKeyClosestToPoint(mousePosition),
72                                             type, mousePosition);
73             }
74             removeExclusiveValue(GEventManager.EXCLUSIVE_CREATE_LINK_VALUE);
75             startElement = null;
76             linkElement = null;
77             delegate.eventShouldRepaint();
78             return;
79         }
80
81         if(selectedElement == null || !selectedElement.acceptOutgoingLink()) {
82             return;
83         }
84
85         int mask = InputEvent.SHIFT_DOWN_MASK + InputEvent.BUTTON1_DOWN_MASK;
86         if((e.getModifiersEx() & mask) == mask || delegate.eventCanCreateLink()) {
87             startElement = selectedElement;
88             startAnchorKey = startElement.getAnchorKeyClosestToPoint(mousePosition);
89
90             linkArc = new SLinkArc();
91             linkArc.setStartTangentOffset(startElement.getDefaultAnchorOffset(startAnchorKey));
92
93             linkElbow = new SLinkElbow();
94
95             if(view.defaultLinkShape() == GLink.SHAPE_ARC)
96                 linkElement = linkArc;
97             else
98                 linkElement = linkElbow;
99
100             linkElement.setFlateness(delegate.eventLinkFlateness());
101             addExclusiveValue(GEventManager.EXCLUSIVE_CREATE_LINK_VALUE);
102         }
103     }
104
105     public void mouseMoved(MouseEvent JavaDoc e, Point mousePosition) {
106         if(startElement == null)
107             return;
108
109         updateLink(mousePosition);
110         delegate.eventShouldRepaint();
111     }
112
113     public boolean shouldFocusOnElement(GElement element) {
114         if(startElement == null)
115             return true;
116         else
117             return element.acceptIncomingLink();
118     }
119
120     public void updateLink(Point mouse) {
121         GElement ce = delegate.eventQueryElementAtPoint(mouse);
122
123         boolean selfLoop = ce == startElement;
124         setLinkStartAnchor(startElement.getAnchor(startAnchorKey));
125
126         if(ce == null || ce instanceof GLink) {
127             setLinkEnd(Vector2D.vector(mouse), Anchor2D.DIRECTION_BOTTOM);
128         } else {
129             Anchor2D anchor = ce.getAnchorClosestToPoint(mouse);
130             String JavaDoc anchorKey = ce.getAnchorKeyClosestToPoint(mouse);
131             setLinkEnd(anchor.position, anchor.direction);
132
133             if(selfLoop) {
134                 if(anchor.direction == Anchor2D.DIRECTION_FREE)
135                     linkArc.setMouse(mouse);
136                 else
137                     linkArc.setMouse(anchor.position.add(anchor.direction));
138                 linkArc.setEndTangentOffset(startElement.getDefaultAnchorOffset(anchorKey));
139             } else {
140                 linkArc.setMouse(mouse);
141                 linkArc.setEndTangentOffset(ce.getDefaultAnchorOffset(anchorKey));
142             }
143
144             if(selfLoop && view.defaultLinkShape() == GLink.SHAPE_ELBOW
145                 && startElement.getAnchor(startAnchorKey).equals(anchor))
146             {
147                 linkElement = linkArc;
148             } else if(view.defaultLinkShape() == GLink.SHAPE_ELBOW)
149                 linkElement = linkElbow;
150             else if(view.defaultLinkShape() == GLink.SHAPE_ARC)
151                 linkElement = linkArc;
152         }
153         setLinkSelfLoop(selfLoop);
154
155         linkElement.update();
156     }
157
158     private void setLinkSelfLoop(boolean selfLoop) {
159         linkArc.setSelfLoop(selfLoop);
160         linkElbow.setSelfLoop(selfLoop);
161     }
162
163     private void setLinkStartAnchor(Anchor2D anchor) {
164         linkArc.setStartAnchor(anchor);
165         linkElbow.setStartAnchor(anchor);
166     }
167
168     private void setLinkEnd(Vector2D position, Vector2D direction) {
169         linkArc.setEnd(position);
170         linkArc.setEndDirection(direction);
171
172         linkElbow.setEnd(position);
173         linkElbow.setEndDirection(direction);
174     }
175
176     public void draw(Graphics g) {
177         if(linkElement == null)
178             return;
179
180         linkElement.draw((Graphics2D)g);
181     }
182 }
183
Popular Tags