001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package java.awt.event;
021:
022: import java.awt.Component;
023: import java.awt.Point;
024: import java.awt.Toolkit;
025:
026: import org.apache.harmony.awt.internal.nls.Messages;
027:
028: public class MouseEvent extends InputEvent {
029:
030: private static final long serialVersionUID = -991214153494842848L;
031:
032: public static final int MOUSE_FIRST = 500;
033:
034: public static final int MOUSE_LAST = 507;
035:
036: public static final int MOUSE_CLICKED = 500;
037:
038: public static final int MOUSE_PRESSED = 501;
039:
040: public static final int MOUSE_RELEASED = 502;
041:
042: public static final int MOUSE_MOVED = 503;
043:
044: public static final int MOUSE_ENTERED = 504;
045:
046: public static final int MOUSE_EXITED = 505;
047:
048: public static final int MOUSE_DRAGGED = 506;
049:
050: public static final int MOUSE_WHEEL = 507;
051:
052: public static final int NOBUTTON = 0;
053:
054: public static final int BUTTON1 = 1;
055:
056: public static final int BUTTON2 = 2;
057:
058: public static final int BUTTON3 = 3;
059:
060: private boolean popupTrigger;
061: private int clickCount;
062: private int button;
063: private int x;
064: private int y;
065:
066: public MouseEvent(Component source, int id, long when,
067: int modifiers, int x, int y, int clickCount,
068: boolean popupTrigger) {
069: this (source, id, when, modifiers, x, y, clickCount,
070: popupTrigger, NOBUTTON);
071: }
072:
073: public MouseEvent(Component source, int id, long when,
074: int modifiers, int x, int y, int clickCount,
075: boolean popupTrigger, int button) {
076: super (source, id, when, modifiers);
077:
078: if ((button < NOBUTTON) || (button > BUTTON3)) {
079: // awt.18B=Invalid button value
080: throw new IllegalArgumentException(Messages
081: .getString("awt.18B")); //$NON-NLS-1$
082: }
083:
084: this .popupTrigger = popupTrigger;
085: this .clickCount = clickCount;
086: this .button = button;
087: this .x = x;
088: this .y = y;
089:
090: if (getModifiers() != 0) {
091: setModifiersEx();
092: } else if (getModifiersEx() != 0) {
093: setModifiers();
094: } else if ((button != NOBUTTON)
095: && ((id >= MOUSE_CLICKED) && (id <= MOUSE_RELEASED))) {
096: setButtonModifiers();
097: }
098: }
099:
100: public static String getMouseModifiersText(int modifiers) {
101: final StringBuffer text = new StringBuffer();
102:
103: if ((modifiers & META_MASK) != 0) {
104: text
105: .append(Toolkit.getProperty("AWT.meta", "Meta")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
106: }
107: if ((modifiers & SHIFT_MASK) != 0) {
108: text
109: .append(Toolkit.getProperty("AWT.shift", "Shift")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
110: }
111: if ((modifiers & CTRL_MASK) != 0) {
112: text
113: .append(Toolkit.getProperty("AWT.control", "Ctrl")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
114: }
115: if ((modifiers & ALT_MASK) != 0) {
116: // XXX: The following hook is required to avoid conflicts between
117: // ALT_MASK and BUTTON2_MASK which have the same value.
118: if ((modifiers & BUTTON2_DOWN_MASK) != 0) {
119: if ((modifiers & ALT_DOWN_MASK) != 0) {
120: text
121: .append(
122: Toolkit.getProperty(
123: "AWT.alt", "Alt")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
124: }
125: } else {
126: text
127: .append(Toolkit.getProperty("AWT.alt", "Alt")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
128: }
129: }
130: if ((modifiers & ALT_GRAPH_MASK) != 0) {
131: text
132: .append(
133: Toolkit.getProperty(
134: "AWT.altGraph", "Alt Graph")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
135: }
136: if ((modifiers & BUTTON1_MASK) != 0) {
137: text
138: .append(
139: Toolkit.getProperty(
140: "AWT.button1", "Button1")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
141: }
142: if ((modifiers & BUTTON2_MASK) != 0) {
143: text
144: .append(
145: Toolkit.getProperty(
146: "AWT.button2", "Button2")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
147: }
148: if ((modifiers & BUTTON3_MASK) != 0) {
149: text
150: .append(
151: Toolkit.getProperty(
152: "AWT.button3", "Button3")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
153: }
154:
155: return text.length() == 0 ? text.toString() : text.substring(0,
156: text.length() - 1);
157: }
158:
159: static String addMouseModifiersExText(String text, int modifiersEx) {
160: if ((modifiersEx & InputEvent.BUTTON1_DOWN_MASK) != 0) {
161: text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
162: Toolkit.getProperty("AWT.button1", "Button1"); //$NON-NLS-1$ //$NON-NLS-2$
163: }
164: if ((modifiersEx & InputEvent.BUTTON2_DOWN_MASK) != 0) {
165: text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
166: Toolkit.getProperty("AWT.button2", "Button2"); //$NON-NLS-1$ //$NON-NLS-2$
167: }
168: if ((modifiersEx & InputEvent.BUTTON3_DOWN_MASK) != 0) {
169: text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
170: Toolkit.getProperty("AWT.button3", "Button3"); //$NON-NLS-1$ //$NON-NLS-2$
171: }
172:
173: return text;
174: }
175:
176: public int getButton() {
177: return button;
178: }
179:
180: public int getClickCount() {
181: return clickCount;
182: }
183:
184: public Point getPoint() {
185: return new Point(x, y);
186: }
187:
188: public int getX() {
189: return x;
190: }
191:
192: public int getY() {
193: return y;
194: }
195:
196: public boolean isPopupTrigger() {
197: return popupTrigger;
198: }
199:
200: public void translatePoint(int x, int y) {
201: this .x += x;
202: this .y += y;
203: }
204:
205: @Override
206: public String paramString() {
207: /* The format is based on 1.5 release behavior
208: * which can be revealed by the following code:
209: *
210: * MouseEvent e = new MouseEvent(new Component(){},
211: * MouseEvent.MOUSE_PRESSED, 0,
212: * MouseEvent.BUTTON1_DOWN_MASK|MouseEvent.CTRL_DOWN_MASK,
213: * 10, 20, 1, false, MouseEvent.BUTTON1);
214: * System.out.println(e);
215: */
216:
217: String idString = null;
218: String paramString = null;
219:
220: switch (id) {
221: case MOUSE_MOVED:
222: idString = "MOUSE_MOVED"; //$NON-NLS-1$
223: break;
224: case MOUSE_CLICKED:
225: idString = "MOUSE_CLICKED"; //$NON-NLS-1$
226: break;
227: case MOUSE_PRESSED:
228: idString = "MOUSE_PRESSED"; //$NON-NLS-1$
229: break;
230: case MOUSE_RELEASED:
231: idString = "MOUSE_RELEASED"; //$NON-NLS-1$
232: break;
233: case MOUSE_DRAGGED:
234: idString = "MOUSE_DRAGGED"; //$NON-NLS-1$
235: break;
236: case MOUSE_ENTERED:
237: idString = "MOUSE_ENTERED"; //$NON-NLS-1$
238: break;
239: case MOUSE_EXITED:
240: idString = "MOUSE_EXITED"; //$NON-NLS-1$
241: break;
242: case MOUSE_WHEEL:
243: idString = "MOUSE_WHEEL"; //$NON-NLS-1$
244: break;
245: default:
246: idString = "unknown type"; //$NON-NLS-1$
247: }
248:
249: paramString = idString + ",(" + getX() + "," + getY() + ")" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
250: ",button=" + button; //$NON-NLS-1$
251: if (getModifiersEx() > 0) {
252: paramString += ",modifiers=" + getMouseModifiersText(modifiers) + //$NON-NLS-1$
253: ",extModifiers=" + getModifiersExText(modifiers); //$NON-NLS-1$
254: }
255: paramString += ",clickCount=" + getClickCount(); //$NON-NLS-1$
256:
257: return paramString;
258: }
259:
260: private void setModifiers() {
261: modifiers &= ~MASKS; // clear modifiers
262:
263: if ((modifiers & SHIFT_DOWN_MASK) != 0) {
264: modifiers |= SHIFT_MASK;
265: }
266: if ((modifiers & CTRL_DOWN_MASK) != 0) {
267: modifiers |= CTRL_MASK;
268: }
269: if ((modifiers & META_DOWN_MASK) != 0) {
270: modifiers |= META_MASK;
271: }
272: if ((modifiers & ALT_DOWN_MASK) != 0) {
273: modifiers |= ALT_MASK;
274: }
275: if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
276: modifiers |= ALT_GRAPH_MASK;
277: }
278:
279: if ((id >= MOUSE_CLICKED) && (id <= MOUSE_RELEASED)) {
280: switch (button) {
281: case BUTTON1:
282: modifiers |= BUTTON1_MASK;
283: break;
284: case BUTTON2:
285: modifiers |= BUTTON2_MASK;
286: break;
287: case BUTTON3:
288: modifiers |= BUTTON3_MASK;
289: break;
290: }
291: } else {
292: if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
293: modifiers |= BUTTON1_MASK;
294: }
295: if ((modifiers & BUTTON2_DOWN_MASK) != 0) {
296: modifiers |= BUTTON2_MASK;
297: }
298: if ((modifiers & BUTTON3_DOWN_MASK) != 0) {
299: modifiers |= BUTTON3_MASK;
300: }
301: }
302: }
303:
304: private void setModifiersEx() {
305: modifiers &= ~DOWN_MASKS; // clear ex modifiers
306:
307: if ((modifiers & SHIFT_MASK) != 0) {
308: modifiers |= SHIFT_DOWN_MASK;
309: }
310: if ((modifiers & CTRL_MASK) != 0) {
311: modifiers |= CTRL_DOWN_MASK;
312: }
313: if ((modifiers & META_MASK) != 0) {
314: modifiers |= META_DOWN_MASK;
315: }
316: if ((modifiers & ALT_MASK) != 0) {
317: modifiers |= ALT_DOWN_MASK;
318: }
319: if ((modifiers & ALT_GRAPH_MASK) != 0) {
320: modifiers |= ALT_GRAPH_DOWN_MASK;
321: }
322:
323: if ((id >= MOUSE_CLICKED) && (id <= MOUSE_RELEASED)) {
324: if ((modifiers & BUTTON1_MASK) != 0) {
325: button = BUTTON1;
326:
327: if (id == MOUSE_PRESSED) {
328: modifiers |= BUTTON1_DOWN_MASK;
329: }
330: } else if ((modifiers & BUTTON2_MASK) != 0) {
331: button = BUTTON2;
332:
333: if (id == MOUSE_PRESSED) {
334: modifiers |= BUTTON2_DOWN_MASK;
335: }
336: } else if ((modifiers & BUTTON3_MASK) != 0) {
337: button = BUTTON3;
338:
339: if (id == MOUSE_PRESSED) {
340: modifiers |= BUTTON3_DOWN_MASK;
341: }
342: }
343: } else {
344: if ((modifiers & BUTTON1_MASK) != 0) {
345: modifiers |= BUTTON1_DOWN_MASK;
346: }
347: if ((modifiers & BUTTON2_MASK) != 0) {
348: modifiers |= BUTTON2_DOWN_MASK;
349: }
350: if ((modifiers & BUTTON3_MASK) != 0) {
351: modifiers |= BUTTON3_DOWN_MASK;
352: }
353: }
354: }
355:
356: private void setButtonModifiers() {
357: switch (button) {
358: case BUTTON1:
359: modifiers |= BUTTON1_MASK;
360: if (id == MOUSE_PRESSED) {
361: modifiers |= BUTTON1_DOWN_MASK;
362: }
363: break;
364: case BUTTON2:
365: modifiers |= BUTTON2_MASK;
366: if (id == MOUSE_PRESSED) {
367: modifiers |= BUTTON2_DOWN_MASK;
368: }
369: break;
370: case BUTTON3:
371: modifiers |= BUTTON3_MASK;
372: if (id == MOUSE_PRESSED) {
373: modifiers |= BUTTON3_DOWN_MASK;
374: }
375: break;
376: }
377: }
378: }
|