KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > graph > MyEdgeHandle


1 /*====================================================================
2
3  Objectweb Explorer Framework
4  Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21
22  Initial developer(s): ________________________________.
23  Contributor(s): ______________________________________.
24
25  ====================================================================*/

26 package org.objectweb.util.explorer.swing.graph;
27
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.geom.Line2D JavaDoc;
32 import java.awt.geom.Point2D JavaDoc;
33 import java.awt.geom.Rectangle2D JavaDoc;
34
35 import org.jgraph.graph.EdgeView;
36 import org.jgraph.graph.GraphConstants;
37 import org.jgraph.graph.GraphContext;
38
39 /**
40  *
41  * @version 0.2
42  */

43 public class MyEdgeHandle extends EdgeView.EdgeHandle {
44
45     public MyEdgeHandle(EdgeView edge, GraphContext ctx) {
46         super(edge, ctx);
47     }
48
49     // Handle mouse pressed event.
50
public void mousePressed(MouseEvent JavaDoc event) {
51         System.out.println("mouse event");
52         /* INV: currentPoint = null; source = target = label = false; */
53         boolean bendable =
54             graph.isBendable()
55                 && GraphConstants.isBendable(edge.getAllAttributes());
56         boolean disconnectable =
57             graph.isDisconnectable()
58                 && GraphConstants.isDisconnectable(orig.getAllAttributes());
59         int x = event.getX();
60         int y = event.getY();
61         // Detect hit on control point
62
int index = 0;
63         for (index = 0; index < r.length; index++) {
64             if (r[index].contains(x, y)) {
65                 currentPoint = edge.getPoint(index);
66                 source =
67                     index == 0
68                         && (edge.getSource() == null
69                             || (disconnectable
70                                 && GraphConstants.isDisconnectable(
71                                     edge
72                                         .getSource()
73                                         .getParentView()
74                                         .getAllAttributes())));
75                 target =
76                     index == r.length - 1
77                         && (edge.getTarget() == null
78                             || (disconnectable
79                                 && GraphConstants.isDisconnectable(
80                                     edge
81                                         .getTarget()
82                                         .getParentView()
83                                         .getAllAttributes())));
84                 break;
85             }
86         }
87         // Detect hit on label
88
if (!isEditing()
89             && graph.isMoveable()
90             && GraphConstants.isMoveable(edge.getAllAttributes())
91             && loc != null
92             && loc.contains(x, y)
93             && !isAddPointEvent(event)
94             && !isRemovePointEvent(event)) {
95             if (event.getClickCount() == graph.getEditClickCount())
96                 graph.startEditingAtCell(edge);
97             else
98                 label = true;
99             // Remove Point
100
}
101         if (isRemovePointEvent(event)
102             && currentPoint != null
103             && !source && !target
104             && bendable) {
105             edge.removePoint(index);
106             mouseReleased(event);
107             // Add Point
108
} else if (isAddPointEvent(event) && !isEditing() && bendable) {
109             int s = graph.getHandleSize();
110             Rectangle2D JavaDoc rect =
111                 graph.fromScreen(new Rectangle JavaDoc(x - s, y - s, 2 * s, 2 * s));
112             if (edge.intersects(graph.getGraphics(), rect)) {
113                 Point2D JavaDoc point =
114                     graph.fromScreen(
115                         graph.snap(new Point JavaDoc(event.getPoint())));
116                 double min = Double.MAX_VALUE, dist = 0;
117                 for (int i = 0; i < edge.getPointCount() - 1; i++) {
118                     Point2D JavaDoc p = edge.getPoint(i);
119                     Point2D JavaDoc p1 = edge.getPoint(i + 1);
120                     dist = new Line2D.Double JavaDoc(p, p1).ptLineDistSq(point);
121                     if (dist < min) {
122                         min = dist;
123                         index = i + 1;
124                     }
125                 }
126                 edge.addPoint(index, point);
127                 currentPoint = point;
128                 reloadPoints(edge);
129                 paint(graph.getGraphics());
130             }
131         }
132         if (isEditing())
133             event.consume();
134     }
135
136 }
Popular Tags