/Users/lyon/j4p/src/classUtils/gui/ComponentEventHandler.java

1    package classUtils.gui; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5    import java.awt.event.MouseEvent; 
6    import java.awt.event.MouseListener; 
7    import java.awt.event.MouseMotionListener; 
8     
9     
10   abstract class MouseController implements MouseListener, MouseMotionListener { 
11       private int x = 0; 
12       private int y = 0; 
13    
14       public void mouseMoved(MouseEvent e) { 
15       } 
16    
17       public void mouseReleased(MouseEvent e) { 
18           released(e.getX(), e.getY()); 
19       } 
20    
21       public void mouseEntered(MouseEvent e) { 
22       } 
23    
24       public void mouseExited(MouseEvent e) { 
25       } 
26    
27       public void mouseClicked(MouseEvent e) { 
28           if (e.isControlDown()) { 
29               clickControl(new Point(e.getX(), e.getY())); 
30           } else { 
31               clicked(new Point(e.getX(), e.getY())); 
32           } 
33    
34       } 
35    
36       public void mousePressed(MouseEvent e) { 
37           x = e.getX(); 
38           y = e.getY(); 
39           pressed(new Point(x, y)); 
40       } 
41    
42       public void mouseDragged(MouseEvent e) { 
43           if (e.isShiftDown()) { 
44               dragShift(new Point(x, y), new Point(e.getX(), e.getY())); 
45           } else { 
46               dragged(new Point(x, y), new Point(e.getX(), e.getY())); 
47           } 
48       } 
49    
50       public abstract void released(int x, int y); 
51    
52       public abstract void dragged(Point p1, Point p2); 
53    
54       public abstract void dragShift(Point p1, Point p2); 
55    
56       public abstract void pressed(Point p); 
57    
58       public abstract void clicked(Point p); 
59    
60       public abstract void clickControl(Point p); 
61    
62    
63   } 
64    
65    
66   class MouseComponentMover extends MouseController { 
67    
68       Component c; 
69    
70       MouseComponentMover(Component _c) { 
71           c = _c; 
72    
73           c.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
74           c.addMouseMotionListener(this); 
75           c.addMouseListener(this); 
76       } 
77    
78       public void dragShift(Point p1, Point p2) { 
79           SwingUtilities.convertPointToScreen(p2, c); 
80           c.setSize(p2.x - c.getBounds().x, p2.y - c.getBounds().y); 
81           c.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); 
82       } 
83    
84       public void dragged(Point p1, Point p2) { 
85           c.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
86           Point p3 = c.getLocation(); 
87           c.setLocation(p2.x + p3.x - p1.x, p2.y + p3.y - p1.y); 
88       } 
89    
90       public void clicked(Point p) { 
91       } 
92    
93       public void clickControl(Point p) { 
94           EditProperties e = new EditProperties(c); 
95       } 
96    
97       public void shiftClicked(Point p) { 
98       } 
99    
100      public void pressed(Point p) { 
101      } 
102   
103      public void doubleClicked(Point p) { 
104      } 
105   
106      public void released(int x, int y) { 
107          c.setCursor( 
108                  Cursor.getPredefinedCursor( 
109                          Cursor.HAND_CURSOR) 
110          ); 
111      } 
112   
113   
114  }