src/share/classes/java/awt/event/MouseEvent.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Component;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Point;
  31 import java.awt.Toolkit;
  32 import java.io.IOException;
  33 import java.io.ObjectInputStream;
  34 import java.awt.IllegalComponentStateException;
  35 import java.awt.MouseInfo;


  36 import sun.awt.SunToolkit;
  37 
  38 /**
  39  * An event which indicates that a mouse action occurred in a component.
  40  * A mouse action is considered to occur in a particular component if and only
  41  * if the mouse cursor is over the unobscured part of the component's bounds
  42  * when the action happens.
  43  * For lightweight components, such as Swing's components, mouse events
  44  * are only dispatched to the component if the mouse event type has been
  45  * enabled on the component. A mouse event type is enabled by adding the
  46  * appropriate mouse-based {@code EventListener} to the component
  47  * ({@link MouseListener} or {@link MouseMotionListener}), or by invoking
  48  * {@link Component#enableEvents(long)} with the appropriate mask parameter
  49  * ({@code AWTEvent.MOUSE_EVENT_MASK} or {@code AWTEvent.MOUSE_MOTION_EVENT_MASK}).
  50  * If the mouse event type has not been enabled on the component, the
  51  * corresponding mouse events are dispatched to the first ancestor that
  52  * has enabled the mouse event type.
  53  *<p>
  54  * For example, if a {@code MouseListener} has been added to a component, or
  55  * {@code enableEvents(AWTEvent.MOUSE_EVENT_MASK)} has been invoked, then all


 922      * Note that the <code>InputEvent.ALT_MASK</code> and
 923      * <code>InputEvent.BUTTON2_MASK</code> have equal values,
 924      * so the "Alt" string is returned for both modifiers.  Likewise,
 925      * the <code>InputEvent.META_MASK</code> and
 926      * <code>InputEvent.BUTTON3_MASK</code> have equal values,
 927      * so the "Meta" string is returned for both modifiers.
 928      * <p>
 929      * Note that passing negative parameter is incorrect,
 930      * and will cause the returning an unspecified string.
 931      * Zero parameter means that no modifiers were passed and will
 932      * cause the returning an empty string.
 933      * <p>
 934      * @param modifiers A modifier mask describing the modifier keys and
 935      *                  mouse buttons that were down during the event
 936      * @return string   string text description of the combination of modifier
 937      *                  keys and mouse buttons that were down during the event
 938      * @see InputEvent#getModifiersExText(int)
 939      * @since 1.4
 940      */
 941     public static String getMouseModifiersText(int modifiers) {
 942         StringBuilder buf = new StringBuilder();
 943         if ((modifiers & InputEvent.ALT_MASK) != 0) {
 944             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
 945             buf.append("+");
 946         }
 947         if ((modifiers & InputEvent.META_MASK) != 0) {
 948             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
 949             buf.append("+");
 950         }
 951         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
 952             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
 953             buf.append("+");
 954         }
 955         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
 956             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
 957             buf.append("+");
 958         }
 959         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
 960             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
 961             buf.append("+");
 962         }
 963         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
 964             buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
 965             buf.append("+");
 966         }
 967         if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
 968             buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
 969             buf.append("+");
 970         }
 971         if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
 972             buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
 973             buf.append("+");
 974         }
 975 
 976         int mask;
 977 
 978         // TODO: add a toolkit field that holds a number of button on the mouse.
 979         // As the method getMouseModifiersText() is static and obtain
 980         // an integer as a parameter then we may not restrict this with the number
 981         // of buttons installed on the mouse.
 982         // It's a temporary solution. We need to somehow hold the number of buttons somewhere else.


 983         for (int i = 1; i <= cachedNumberOfButtons; i++){
 984             mask = InputEvent.getMaskForButton(i);
 985             if ((modifiers & mask) != 0 &&
 986                 buf.indexOf(Toolkit.getProperty("AWT.button"+i, "Button"+i)) == -1) //1,2,3 buttons may already be there; so don't duplicate it.

 987             {
 988                 buf.append(Toolkit.getProperty("AWT.button"+i, "Button"+i));
 989                 buf.append("+");
 990             }
 991         }
 992 
 993         if (buf.length() > 0) {
 994             buf.setLength(buf.length()-1); // remove trailing '+'
 995         }
 996         return buf.toString();
 997     }
 998 
 999     /**
1000      * Returns a parameter string identifying this event.
1001      * This method is useful for event-logging and for debugging.
1002      *
1003      * @return a string identifying the event and its attributes
1004      */
1005     public String paramString() {
1006         StringBuilder str = new StringBuilder(80);
1007 
1008         switch(id) {
1009           case MOUSE_PRESSED:
1010               str.append("MOUSE_PRESSED");
1011               break;
1012           case MOUSE_RELEASED:
1013               str.append("MOUSE_RELEASED");
1014               break;
1015           case MOUSE_CLICKED:




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Component;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Point;
  31 import java.awt.Toolkit;
  32 import java.io.IOException;
  33 import java.io.ObjectInputStream;
  34 import java.awt.IllegalComponentStateException;
  35 import java.awt.MouseInfo;
  36 import java.util.StringJoiner;
  37 
  38 import sun.awt.SunToolkit;
  39 
  40 /**
  41  * An event which indicates that a mouse action occurred in a component.
  42  * A mouse action is considered to occur in a particular component if and only
  43  * if the mouse cursor is over the unobscured part of the component's bounds
  44  * when the action happens.
  45  * For lightweight components, such as Swing's components, mouse events
  46  * are only dispatched to the component if the mouse event type has been
  47  * enabled on the component. A mouse event type is enabled by adding the
  48  * appropriate mouse-based {@code EventListener} to the component
  49  * ({@link MouseListener} or {@link MouseMotionListener}), or by invoking
  50  * {@link Component#enableEvents(long)} with the appropriate mask parameter
  51  * ({@code AWTEvent.MOUSE_EVENT_MASK} or {@code AWTEvent.MOUSE_MOTION_EVENT_MASK}).
  52  * If the mouse event type has not been enabled on the component, the
  53  * corresponding mouse events are dispatched to the first ancestor that
  54  * has enabled the mouse event type.
  55  *<p>
  56  * For example, if a {@code MouseListener} has been added to a component, or
  57  * {@code enableEvents(AWTEvent.MOUSE_EVENT_MASK)} has been invoked, then all


 924      * Note that the <code>InputEvent.ALT_MASK</code> and
 925      * <code>InputEvent.BUTTON2_MASK</code> have equal values,
 926      * so the "Alt" string is returned for both modifiers.  Likewise,
 927      * the <code>InputEvent.META_MASK</code> and
 928      * <code>InputEvent.BUTTON3_MASK</code> have equal values,
 929      * so the "Meta" string is returned for both modifiers.
 930      * <p>
 931      * Note that passing negative parameter is incorrect,
 932      * and will cause the returning an unspecified string.
 933      * Zero parameter means that no modifiers were passed and will
 934      * cause the returning an empty string.
 935      * <p>
 936      * @param modifiers A modifier mask describing the modifier keys and
 937      *                  mouse buttons that were down during the event
 938      * @return string   string text description of the combination of modifier
 939      *                  keys and mouse buttons that were down during the event
 940      * @see InputEvent#getModifiersExText(int)
 941      * @since 1.4
 942      */
 943     public static String getMouseModifiersText(int modifiers) {
 944         StringJoiner buf = new StringJoiner("+");
 945         if ((modifiers & InputEvent.ALT_MASK) != 0) {
 946             buf.add(Toolkit.getProperty("AWT.alt", "Alt"));

 947         }
 948         if ((modifiers & InputEvent.META_MASK) != 0) {
 949             buf.add(Toolkit.getProperty("AWT.meta", "Meta"));

 950         }
 951         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
 952             buf.add(Toolkit.getProperty("AWT.control", "Ctrl"));

 953         }
 954         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
 955             buf.add(Toolkit.getProperty("AWT.shift", "Shift"));

 956         }
 957         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
 958             buf.add(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));

 959         }
 960         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
 961             buf.add(Toolkit.getProperty("AWT.button1", "Button1"));

 962         }
 963         if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
 964             buf.add(Toolkit.getProperty("AWT.button2", "Button2"));

 965         }
 966         if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
 967             buf.add(Toolkit.getProperty("AWT.button3", "Button3"));

 968         }
 969         String result = buf.toString();

 970 
 971         // TODO: add a toolkit field that holds a number of button on the mouse.
 972         // As the method getMouseModifiersText() is static and obtain
 973         // an integer as a parameter then we may not restrict this with the number
 974         // of buttons installed on the mouse.
 975         // It's a temporary solution. We need to somehow hold the number of buttons somewhere else.
 976         buf = new StringJoiner("+");
 977         buf.add(result);
 978         for (int i = 1; i <= cachedNumberOfButtons; i++){
 979             int mask = InputEvent.getMaskForButton(i);
 980             if ((modifiers & mask) != 0 &&
 981                 //1,2,3 buttons may already be there; so don't duplicate it.
 982                 !result.contains(Toolkit.getProperty("AWT.button" + i, "Button" + i)))
 983             {
 984                 buf.add(Toolkit.getProperty("AWT.button" + i, "Button" + i));

 985             }
 986         }




 987         return buf.toString();
 988     }
 989 
 990     /**
 991      * Returns a parameter string identifying this event.
 992      * This method is useful for event-logging and for debugging.
 993      *
 994      * @return a string identifying the event and its attributes
 995      */
 996     public String paramString() {
 997         StringBuilder str = new StringBuilder(80);
 998 
 999         switch(id) {
1000           case MOUSE_PRESSED:
1001               str.append("MOUSE_PRESSED");
1002               break;
1003           case MOUSE_RELEASED:
1004               str.append("MOUSE_RELEASED");
1005               break;
1006           case MOUSE_CLICKED: