/Users/lyon/j4p/src/graphics/graph/MouseEventHandler.java
|
1 package graphics.graph;
2
3 import java.awt.*;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseListener;
6 import java.awt.event.MouseMotionListener;
7
8 public class MouseEventHandler
9 implements MouseListener, MouseMotionListener
10 /**
11 * This used to be the class called MousePanel.
12 * The responsibility of this class is to listen for MouseEvents
13 * including clicks, drags, etc. and handle them appropriately.
14 * A Component will be passed in as part of the Constructor.
15 * It is on that component that the gui.mouse.m2.mouse events will be handled.
16 * When the appropriate response has been calculated, a message
17 * is sent to repaint the component and the gui.mouse.m2.mouse event is consumed.
18 */ {
19 private PickBean pb = PickBean.getPickBean();
20 private PickFixedBean pfb = PickFixedBean.getPickFixedBean();
21 private Nodes nodes = GraphFactory.getNodes();
22 private Component component;
23
24 public MouseEventHandler(Component c) {
25 component = c;
26 }
27
28 //1.1 event handling
29 public void mouseClicked(MouseEvent e) {
30 }
31
32 public void mouseEntered(MouseEvent e) {
33 }
34
35 public void mouseExited(MouseEvent e) {
36 }
37
38 public void mouseMoved(MouseEvent e) {
39 }
40
41 public void mousePressed(MouseEvent e) {
42 component.addMouseMotionListener(this);
43 double bestdist = Double.MAX_VALUE;
44 int x = e.getX();
45 int y = e.getY();
46 for (int i = 0; i < nodes.getNumberOfNodes(); i++) {
47 Node n = nodes.getNode(i);
48 double dist = (n.getX() - x) * (n.getX() - x)
49 + (n.getY() - y) * (n.getY() - y);
50 if (dist < bestdist) {
51 pb.setPick(n);
52 bestdist = dist;
53 }
54 }
55 Node pickedNode = pb.getPick();
56 pfb.setPickFixed(pickedNode.isFixed());
57 pickedNode.setFixed(true);
58 pickedNode.setX(x);
59 pickedNode.setY(y);
60 component.repaint();
61 e.consume();
62 }
63
64 public void mouseReleased(MouseEvent e) {
65 component.removeMouseMotionListener(this);
66 Node n = pb.getPick();
67 if (n != null) {
68 n.setX(e.getX());
69 n.setY(e.getY());
70 if (e.isShiftDown())
71 n.setFixed(true);
72 else
73 n.setFixed(pfb.getPickFixed());
74 if (e.isAltDown())
75 n.setFixed(false);
76
77 pb.setPick(null);
78 }
79 component.repaint();
80 e.consume();
81 }
82
83 public void mouseDragged(MouseEvent e) {
84 if (pb.getPick() == null) return;
85 pb.getPick().setX(e.getX());
86 pb.getPick().setY(e.getY());
87 component.repaint();
88 e.consume();
89 }
90 }