1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.drawing; |
16 | |
17 | import java.awt.event.MouseEvent; |
18 | |
19 | import javax.swing.JComponent; |
20 | import javax.swing.event.MouseInputAdapter; |
21 | |
22 | import org.apache.commons.logging.Log; |
23 | import org.apache.commons.logging.LogFactory; |
24 | |
25 | import net.sourceforge.hiveboard.Board; |
26 | import net.sourceforge.hiveboard.main.IdentityHolder; |
27 | |
28 | public class ToolMouseListener extends MouseInputAdapter |
29 | { |
30 | static private final Log _logger = LogFactory.getLog(ToolMouseListener.class); |
31 | |
32 | public ToolMouseListener(IdentityHolder identity, DrawingActionSender sender) |
33 | { |
34 | _identity = identity; |
35 | _sender = sender; |
36 | } |
37 | |
38 | public void setArea(JComponent area) |
39 | { |
40 | _area = area; |
41 | } |
42 | |
43 | public void setBoard(int id, Board board) |
44 | { |
45 | _board = board; |
46 | _idBoard = id; |
47 | } |
48 | |
49 | public void setCurrentTool(Tool tool) |
50 | { |
51 | if (tool instanceof MouseDragTool) |
52 | { |
53 | _currentDragTool = (MouseDragTool) tool; |
54 | _currentClickTool = null; |
55 | } |
56 | else if (tool instanceof MouseClickTool) |
57 | { |
58 | _currentDragTool = null; |
59 | _currentClickTool = (MouseClickTool) tool; |
60 | } |
61 | else |
62 | { |
63 | _currentClickTool = null; |
64 | _currentDragTool = null; |
65 | } |
66 | } |
67 | |
68 | @Override public void mousePressed(MouseEvent e) |
69 | { |
70 | if ( e.getButton() == MouseEvent.BUTTON1 |
71 | && _currentDragTool != null |
72 | && _board != null |
73 | && _board.getWriter() == _identity.getId()) |
74 | { |
75 | _logger.debug("mousePressed() [_currentDragTool != null]"); |
76 | _dragToolStarted = true; |
77 | _currentDragTool.start(_area); |
78 | _currentDragTool.mouseMotion(e.getPoint()); |
79 | } |
80 | } |
81 | |
82 | @Override public void mouseDragged(MouseEvent e) |
83 | { |
84 | if (_dragToolStarted) |
85 | { |
86 | _currentDragTool.mouseMotion(e.getPoint()); |
87 | } |
88 | } |
89 | |
90 | @Override public void mouseReleased(MouseEvent e) |
91 | { |
92 | if ( e.getButton() == MouseEvent.BUTTON1 |
93 | && _dragToolStarted) |
94 | { |
95 | _logger.debug("mouseReleased() [_dragToolStarted]"); |
96 | |
97 | // This is the end of the current editing task |
98 | _dragToolStarted = false; |
99 | _currentDragTool.mouseMotion(e.getPoint()); |
100 | execute(_currentDragTool.stop()); |
101 | } |
102 | } |
103 | |
104 | @Override public void mouseClicked(MouseEvent e) |
105 | { |
106 | if ( e.getButton() == MouseEvent.BUTTON1 |
107 | && _currentClickTool != null |
108 | && _board != null |
109 | && _board.getWriter() == _identity.getId()) |
110 | { |
111 | _logger.debug("mouseClicked() [_currentClickTool != null]"); |
112 | execute(_currentClickTool.execute(_area, e.getPoint())); |
113 | } |
114 | } |
115 | |
116 | private void execute(final DrawingAction action) |
117 | { |
118 | // Send the action to the server asynchroneously |
119 | _sender.send(action, _idBoard, _area); |
120 | } |
121 | |
122 | final private IdentityHolder _identity; |
123 | final private DrawingActionSender _sender; |
124 | |
125 | // The current user-chosen tool for mouse-drag operations |
126 | private MouseDragTool _currentDragTool; |
127 | // Indicates if the current mouse-driven tool is currently "in action" (drag movement) |
128 | private boolean _dragToolStarted; |
129 | |
130 | // The current user-chosen tool for mouse-click operations |
131 | private MouseClickTool _currentClickTool; |
132 | |
133 | // The current target board for all drawing actions |
134 | private Board _board = null; |
135 | private int _idBoard = -1; |
136 | // The board drawing area that is the current target of all actions |
137 | private JComponent _area; |
138 | } |