1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Michael Danilov
19 * @version $Revision$
20 */
21package java.awt.event;
22
23import java.awt.Component;
24import java.awt.Point;
25import java.awt.Toolkit;
26
27import org.apache.harmony.awt.internal.nls.Messages;
28
29/**
30 * This class is not supported in Android 1.0. It is merely provided to maintain
31 * interface compatibility with desktop Java implementations.
32 *
33 * @since Android 1.0
34 */
35public class MouseEvent extends InputEvent {
36
37    private static final long serialVersionUID = -991214153494842848L;
38
39    public static final int MOUSE_FIRST = 500;
40
41    public static final int MOUSE_LAST = 507;
42
43    public static final int MOUSE_CLICKED = 500;
44
45    public static final int MOUSE_PRESSED = 501;
46
47    public static final int MOUSE_RELEASED = 502;
48
49    public static final int MOUSE_MOVED = 503;
50
51    public static final int MOUSE_ENTERED = 504;
52
53    public static final int MOUSE_EXITED = 505;
54
55    public static final int MOUSE_DRAGGED = 506;
56
57    public static final int MOUSE_WHEEL = 507;
58
59    public static final int NOBUTTON = 0;
60
61    public static final int BUTTON1 = 1;
62
63    public static final int BUTTON2 = 2;
64
65    public static final int BUTTON3 = 3;
66
67    private boolean popupTrigger;
68    private int clickCount;
69    private int button;
70    private int x;
71    private int y;
72
73    public static String getMouseModifiersText(int modifiers) {
74        final StringBuffer text = new StringBuffer();
75
76        if ((modifiers & META_MASK) != 0) {
77            text.append(Toolkit.getProperty("AWT.meta", "Meta")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
78        }
79        if ((modifiers & SHIFT_MASK) != 0) {
80            text.append(Toolkit.getProperty("AWT.shift", "Shift")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
81        }
82        if ((modifiers & CTRL_MASK) != 0) {
83            text.append(Toolkit.getProperty("AWT.control", "Ctrl")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
84        }
85        if ((modifiers & ALT_MASK) != 0) {
86            text.append(Toolkit.getProperty("AWT.alt", "Alt")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
87        }
88        if ((modifiers & ALT_GRAPH_MASK) != 0) {
89            text.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
90        }
91        if ((modifiers & BUTTON1_MASK) != 0) {
92            text.append(Toolkit.getProperty("AWT.button1", "Button1")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
93        }
94        if ((modifiers & BUTTON2_MASK) != 0) {
95            text.append(Toolkit.getProperty("AWT.button2", "Button2")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
96        }
97        if ((modifiers & BUTTON3_MASK) != 0) {
98            text.append(Toolkit.getProperty("AWT.button3", "Button3")).append("+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
99        }
100
101        return text.length() == 0 ? text.toString() : text.substring(0, text
102                .length() - 1);
103    }
104
105    static String addMouseModifiersExText(String text, int modifiersEx) {
106        if ((modifiersEx & InputEvent.BUTTON1_DOWN_MASK) != 0) {
107            text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
108                    Toolkit.getProperty("AWT.button1", "Button1"); //$NON-NLS-1$ //$NON-NLS-2$
109        }
110        if ((modifiersEx & InputEvent.BUTTON2_DOWN_MASK) != 0) {
111            text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
112                    Toolkit.getProperty("AWT.button2", "Button2"); //$NON-NLS-1$ //$NON-NLS-2$
113        }
114        if ((modifiersEx & InputEvent.BUTTON3_DOWN_MASK) != 0) {
115            text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
116                    Toolkit.getProperty("AWT.button3", "Button3"); //$NON-NLS-1$ //$NON-NLS-2$
117        }
118
119        return text;
120    }
121
122    public MouseEvent(Component source, int id, long when,
123                      int modifiers, int x, int y,
124                      int clickCount, boolean popupTrigger) {
125        this(source, id, when, modifiers, x, y,
126             clickCount, popupTrigger, NOBUTTON);
127    }
128
129    public MouseEvent(Component source, int id, long when,
130                      int modifiers, int x, int y,
131                      int clickCount, boolean popupTrigger, int button) {
132        super(source, id, when, modifiers);
133
134        if ((button != NOBUTTON) && (button != BUTTON1) &&
135                (button != BUTTON2) && (button != BUTTON3)) {
136            // awt.18B=Invalid button value
137            throw new IllegalArgumentException(Messages.getString("awt.18B")); //$NON-NLS-1$
138        }
139
140        this.popupTrigger = popupTrigger;
141        this.clickCount = clickCount;
142        this.button = button;
143        this.x = x;
144        this.y = y;
145    }
146
147    public int getButton() {
148        return button;
149    }
150
151    public int getClickCount() {
152        return clickCount;
153    }
154
155    public Point getPoint() {
156        return new Point(x, y);
157    }
158
159    public int getX() {
160        return x;
161    }
162
163    public int getY() {
164        return y;
165    }
166
167    public boolean isPopupTrigger() {
168        return popupTrigger;
169    }
170
171    public void translatePoint(int x, int y) {
172        this.x += x;
173        this.y += y;
174    }
175
176    @Override
177    public String paramString() {
178        /* The format is based on 1.5 release behavior
179         * which can be revealed by the following code:
180         *
181         * MouseEvent e = new MouseEvent(new Component(){},
182         *          MouseEvent.MOUSE_PRESSED, 0,
183         *          MouseEvent.BUTTON1_DOWN_MASK|MouseEvent.CTRL_DOWN_MASK,
184         *          10, 20, 1, false, MouseEvent.BUTTON1);
185         * System.out.println(e);
186         */
187
188        String idString = null;
189        String paramString = null;
190
191        switch (id) {
192        case MOUSE_MOVED:
193            idString = "MOUSE_MOVED"; //$NON-NLS-1$
194            break;
195        case MOUSE_CLICKED:
196            idString = "MOUSE_CLICKED"; //$NON-NLS-1$
197            break;
198        case MOUSE_PRESSED:
199            idString = "MOUSE_PRESSED"; //$NON-NLS-1$
200            break;
201        case MOUSE_RELEASED:
202            idString = "MOUSE_RELEASED"; //$NON-NLS-1$
203            break;
204        case MOUSE_DRAGGED:
205            idString = "MOUSE_DRAGGED"; //$NON-NLS-1$
206            break;
207        case MOUSE_ENTERED:
208            idString = "MOUSE_ENTERED"; //$NON-NLS-1$
209            break;
210        case MOUSE_EXITED:
211            idString = "MOUSE_EXITED"; //$NON-NLS-1$
212            break;
213        case MOUSE_WHEEL:
214            idString = "MOUSE_WHEEL"; //$NON-NLS-1$
215            break;
216        default:
217            idString = "unknown type"; //$NON-NLS-1$
218        }
219
220        paramString = idString + ",(" + getX() + "," + getY() + ")" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
221                ",button=" + button; //$NON-NLS-1$
222        if (getModifiersEx() > 0) {
223            paramString +=
224                    ",modifiers=" + getModifiersExText(getModifiersEx()) + //$NON-NLS-1$
225                    ",extModifiers=" + getModifiersExText(getModifiersEx()); //$NON-NLS-1$
226        }
227        paramString += ",clickCount=" + getClickCount(); //$NON-NLS-1$
228
229        return paramString;
230    }
231
232}
233