immutable TMouseEvent
authorKevin Lamonte <kevin.lamonte@gmail.com>
Wed, 11 Mar 2015 23:51:29 +0000 (19:51 -0400)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Wed, 11 Mar 2015 23:51:29 +0000 (19:51 -0400)
README.md
src/jexer/TApplication.java
src/jexer/TCommand.java
src/jexer/event/TCommandEvent.java
src/jexer/event/TMenuEvent.java
src/jexer/event/TMouseEvent.java
src/jexer/event/TResizeEvent.java
src/jexer/io/ECMA48Terminal.java

index 4166dc775457de9da5177b2c5aeb552bd5e5584b..c045f048ba706537f4082712a9955562fdd3e3a6 100644 (file)
--- a/README.md
+++ b/README.md
@@ -59,11 +59,6 @@ version 1.0:
 
 0.0.1:
 
-- Base classes:
-  - TCommand use getters/setters
-  - TCommand.Type: switch to int so that subclasses can make more
-    kinds of commands
-  - TMouseEvent use getters/setters to make immutable
 - Get a movable window on screen
   - TWidget
   - TWindow
index 91d0ea735d1f409b3c43a6e98e45ea22652fb0cc..88249abc3541273ca2e4a686e3776f8dd9ed06f1 100644 (file)
@@ -365,9 +365,9 @@ public class TApplication {
             // Peek at the mouse position
             if (event instanceof TMouseEvent) {
                 TMouseEvent mouse = (TMouseEvent) event;
-                if ((mouseX != mouse.x) || (mouseY != mouse.y)) {
-                    mouseX = mouse.x;
-                    mouseY = mouse.y;
+                if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) {
+                    mouseX = mouse.getX();
+                    mouseY = mouse.getY();
                     drawMouse();
                 }
             }
index c51e570e783191b989607a2151e335b8172c040d..62ff2bec9e22f37e09ef149c3a1ffab553e650f7 100644 (file)
@@ -38,106 +38,97 @@ package jexer;
 public class TCommand {
 
     /**
-     * The following types are predefined for the entire system.
-     *
-     * TODO: Switch this to int so that TCommand can be subclassed so that
-     * applications can add more.
-     */
-    public enum Type {
-        /**
-         * Immediately abort the application (e.g. remote side closed
-         * connection).
-         */
-        ABORT,
-
-        /**
-         * File open dialog.
-         */
-        OPEN,
-
-        /**
-         * Exit application.
-         */
-        EXIT,
-
-        /**
-         * Spawn OS shell window.
-         */
-        SHELL,
-
-        /**
-         * Cut selected text and copy to the clipboard.
-         */
-        CUT,
-
-        /**
-         * Copy selected text to clipboard.
-         */
-        COPY,
-
-        /**
-         * Paste from clipboard.
-         */
-        PASTE,
-
-        /**
-         * Clear selected text without copying it to the clipboard.
-         */
-        CLEAR,
-
-        /**
-         * Tile windows.
-         */
-        TILE,
-
-        /**
-         * Cascade windows.
-         */
-        CASCADE,
-
-        /**
-         * Close all windows.
-         */
-        CLOSE_ALL,
-
-        /**
-         * Move (move/resize) window.
-         */
-        WINDOW_MOVE,
-
-        /**
-         * Zoom (maximize/restore) window.
-         */
-        WINDOW_ZOOM,
-
-        /**
-         * Next window (like Alt-TAB).
-         */
-        WINDOW_NEXT,
-
-        /**
-         * Previous window (like Shift-Alt-TAB).
-         */
-        WINDOW_PREVIOUS,
-
-        /**
-         * Close window.
-         */
-        WINDOW_CLOSE,
+     * Immediately abort the application (e.g. remote side closed
+     * connection).
+     */
+    public static final int ABORT       = 1;
 
-    }
+    /**
+     * File open dialog.
+     */
+    public static final int OPEN        = 2;
+
+    /**
+     * Exit application.
+     */
+    public static final int EXIT        = 3;
+
+    /**
+     * Spawn OS shell window.
+     */
+    public static final int SHELL       = 4;
+
+    /**
+     * Cut selected text and copy to the clipboard.
+     */
+    public static final int CUT         = 5;
+
+    /**
+     * Copy selected text to clipboard.
+     */
+    public static final int COPY        = 6;
+
+    /**
+     * Paste from clipboard.
+     */
+    public static final int PASTE       = 7;
+
+    /**
+     * Clear selected text without copying it to the clipboard.
+     */
+    public static final int CLEAR       = 8;
+
+    /**
+     * Tile windows.
+     */
+    public static final int TILE        = 9;
+
+    /**
+     * Cascade windows.
+     */
+    public static final int CASCADE     = 10;
+
+    /**
+     * Close all windows.
+     */
+    public static final int CLOSE_ALL   = 11;
+
+    /**
+     * Move (move/resize) window.
+     */
+    public static final int WINDOW_MOVE = 12;
+
+    /**
+     * Zoom (maximize/restore) window.
+     */
+    public static final int WINDOW_ZOOM = 13;
+
+    /**
+     * Next window (like Alt-TAB).
+     */
+    public static final int WINDOW_NEXT = 14;
+
+    /**
+     * Previous window (like Shift-Alt-TAB).
+     */
+    public static final int WINDOW_PREVIOUS = 15;
+
+    /**
+     * Close window.
+     */
+    public static final int WINDOW_CLOSE = 16;
 
     /**
      * Type of command, one of EXIT, CASCADE, etc.
      */
-    private Type type;
+    private int type;
 
     /**
-     * Public constructor.
+     * Protected constructor.  Subclasses can be used to define new commands.
      *
      * @param type the Type of command, one of EXIT, CASCADE, etc.
      */
-    public TCommand(final Type type) {
+    protected TCommand(final int type) {
         this.type = type;
     }
 
@@ -167,22 +158,22 @@ public class TCommand {
         return (type == that.type);
     }
 
-    public static final TCommand cmAbort      = new TCommand(TCommand.Type.ABORT);
-    public static final TCommand cmExit       = new TCommand(TCommand.Type.EXIT);
-    public static final TCommand cmQuit       = new TCommand(TCommand.Type.EXIT);
-    public static final TCommand cmOpen       = new TCommand(TCommand.Type.OPEN);
-    public static final TCommand cmShell      = new TCommand(TCommand.Type.SHELL);
-    public static final TCommand cmCut        = new TCommand(TCommand.Type.CUT);
-    public static final TCommand cmCopy       = new TCommand(TCommand.Type.COPY);
-    public static final TCommand cmPaste      = new TCommand(TCommand.Type.PASTE);
-    public static final TCommand cmClear      = new TCommand(TCommand.Type.CLEAR);
-    public static final TCommand cmTile       = new TCommand(TCommand.Type.TILE);
-    public static final TCommand cmCascade    = new TCommand(TCommand.Type.CASCADE);
-    public static final TCommand cmCloseAll   = new TCommand(TCommand.Type.CLOSE_ALL);
-    public static final TCommand cmWindowMove = new TCommand(TCommand.Type.WINDOW_MOVE);
-    public static final TCommand cmWindowZoom = new TCommand(TCommand.Type.WINDOW_ZOOM);
-    public static final TCommand cmWindowNext = new TCommand(TCommand.Type.WINDOW_NEXT);
-    public static final TCommand cmWindowPrevious = new TCommand(TCommand.Type.WINDOW_PREVIOUS);
-    public static final TCommand cmWindowClose = new TCommand(TCommand.Type.WINDOW_CLOSE);
+    public static final TCommand cmAbort      = new TCommand(ABORT);
+    public static final TCommand cmExit       = new TCommand(EXIT);
+    public static final TCommand cmQuit       = new TCommand(EXIT);
+    public static final TCommand cmOpen       = new TCommand(OPEN);
+    public static final TCommand cmShell      = new TCommand(SHELL);
+    public static final TCommand cmCut        = new TCommand(CUT);
+    public static final TCommand cmCopy       = new TCommand(COPY);
+    public static final TCommand cmPaste      = new TCommand(PASTE);
+    public static final TCommand cmClear      = new TCommand(CLEAR);
+    public static final TCommand cmTile       = new TCommand(TILE);
+    public static final TCommand cmCascade    = new TCommand(CASCADE);
+    public static final TCommand cmCloseAll   = new TCommand(CLOSE_ALL);
+    public static final TCommand cmWindowMove = new TCommand(WINDOW_MOVE);
+    public static final TCommand cmWindowZoom = new TCommand(WINDOW_ZOOM);
+    public static final TCommand cmWindowNext = new TCommand(WINDOW_NEXT);
+    public static final TCommand cmWindowPrevious = new TCommand(WINDOW_PREVIOUS);
+    public static final TCommand cmWindowClose = new TCommand(WINDOW_CLOSE);
 
 }
index cdb5e2d6a70140232fa006d9fc59d0ac15588f37..fb3050b1dcc71dc2fbb99977f116f5f29dcbd616 100644 (file)
@@ -62,6 +62,30 @@ public final class TCommandEvent extends TInputEvent {
         this.cmd = cmd;
     }
 
+    /**
+     * Comparison check.  All fields must match to return true.
+     *
+     * @param rhs another TCommandEvent or TCommand instance
+     * @return true if all fields are equal
+     */
+    @Override
+    public boolean equals(final Object rhs) {
+        if (!(rhs instanceof TCommandEvent)
+            && !(rhs instanceof TCommand)
+        ) {
+            return false;
+        }
+
+        if (rhs instanceof TCommandEvent) {
+            TCommandEvent that = (TCommandEvent) rhs;
+            return (cmd.equals(that.cmd)
+                && (getTime().equals(that.getTime())));
+        }
+
+        TCommand that = (TCommand) rhs;
+        return (cmd.equals(that));
+    }
+
     /**
      * Make human-readable description of this TCommandEvent.
      *
index 0cdca0ea31543d5f42a5712c4b2d549b83f15336..2fc8dc56e6befe63d72adc9e8a1d76b4e3240494 100644 (file)
@@ -35,7 +35,7 @@ package jexer.event;
  * TApplication.getMenuItem(id) can be used to obtain the TMenuItem itself,
  * say for setting enabled/disabled/checked/etc.
  */
-public class TMenuEvent extends TInputEvent {
+public final class TMenuEvent extends TInputEvent {
 
     /**
      * MenuItem ID.
@@ -47,7 +47,7 @@ public class TMenuEvent extends TInputEvent {
      *
      * @return the ID
      */
-    public final short getId() {
+    public short getId() {
         return id;
     }
 
@@ -66,7 +66,7 @@ public class TMenuEvent extends TInputEvent {
      * @return displayable String
      */
     @Override
-    public final String toString() {
+    public String toString() {
         return String.format("MenuEvent: %d", id);
     }
 }
index 09ace1da0d97a20ff925857ad0b288c5b1be34e4..29e0c1b42c51ccd775a348146679145cdc9dd615 100644 (file)
@@ -33,7 +33,7 @@ package jexer.event;
 /**
  * This class encapsulates several kinds of mouse input events.
  */
-public class TMouseEvent extends TInputEvent {
+public final class TMouseEvent extends TInputEvent {
 
     /**
      * The type of event generated.
@@ -59,60 +59,172 @@ public class TMouseEvent extends TInputEvent {
      * Type of event, one of MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN, or
      * KEYPRESS.
      */
-    public Type type;
+    private Type type;
+
+    /**
+     * Get type.
+     *
+     * @return type
+     */
+    public Type getType() {
+        return type;
+    }
 
     /**
      * Mouse X - relative coordinates.
      */
-    public int x;
+    private int x;
+
+    /**
+     * Get x.
+     *
+     * @return x
+     */
+    public int getX() {
+        return x;
+    }
 
     /**
      * Mouse Y - relative coordinates.
      */
-    public int y;
+    private int y;
+
+    /**
+     * Get y.
+     *
+     * @return y
+     */
+    public int getY() {
+        return y;
+    }
 
     /**
      * Mouse X - absolute screen coordinates.
      */
-    public int absoluteX;
+    private int absoluteX;
+
+    /**
+     * Get absoluteX.
+     *
+     * @return absoluteX
+     */
+    public int getAbsoluteX() {
+        return absoluteX;
+    }
 
     /**
      * Mouse Y - absolute screen coordinate.
      */
-    public int absoluteY;
+    private int absoluteY;
+
+    /**
+     * Get absoluteY.
+     *
+     * @return absoluteY
+     */
+    public int getAbsoluteY() {
+        return absoluteY;
+    }
 
     /**
      * Mouse button 1 (left button).
      */
-    public boolean mouse1;
+    private boolean mouse1;
+
+    /**
+     * Get mouse1.
+     *
+     * @return mouse1
+     */
+    public boolean getMouse1() {
+        return mouse1;
+    }
 
     /**
      * Mouse button 2 (right button).
      */
-    public boolean mouse2;
+    private boolean mouse2;
+
+    /**
+     * Get mouse2.
+     *
+     * @return mouse2
+     */
+    public boolean getMouse2() {
+        return mouse2;
+    }
 
     /**
      * Mouse button 3 (middle button).
      */
-    public boolean mouse3;
+    private boolean mouse3;
+
+    /**
+     * Get mouse3.
+     *
+     * @return mouse3
+     */
+    public boolean getMouse3() {
+        return mouse3;
+    }
 
     /**
      * Mouse wheel UP (button 4).
      */
-    public boolean mouseWheelUp;
+    private boolean mouseWheelUp;
+
+    /**
+     * Get mouseWheelUp.
+     *
+     * @return mouseWheelUp
+     */
+    public boolean getMouseWheelUp() {
+        return mouseWheelUp;
+    }
 
     /**
      * Mouse wheel DOWN (button 5).
      */
-    public boolean mouseWheelDown;
+    private boolean mouseWheelDown;
+
+    /**
+     * Get mouseWheelDown.
+     *
+     * @return mouseWheelDown
+     */
+    public boolean getMouseWheelDown() {
+        return mouseWheelDown;
+    }
 
     /**
      * Public contructor.
      *
      * @param type the type of event, MOUSE_MOTION, MOUSE_DOWN, or MOUSE_UP
+     * @param x relative column
+     * @param y relative row
+     * @param absoluteX absolute column
+     * @param absoluteY absolute row
+     * @param mouse1 if true, left button is down
+     * @param mouse2 if true, right button is down
+     * @param mouse3 if true, middle button is down
+     * @param mouseWheelUp if true, mouse wheel (button 4) is down
+     * @param mouseWheelDown if true, mouse wheel (button 5) is down
      */
-    public TMouseEvent(final Type type) {
-        this.type = type;
+    public TMouseEvent(final Type type, final int x, final int y,
+        final int absoluteX, final int absoluteY,
+        final boolean mouse1, final boolean mouse2, final boolean mouse3,
+        final boolean mouseWheelUp, final boolean mouseWheelDown) {
+
+        this.type               = type;
+        this.x                  = x;
+        this.y                  = y;
+        this.absoluteX          = absoluteX;
+        this.absoluteY          = absoluteY;
+        this.mouse1             = mouse1;
+        this.mouse2             = mouse2;
+        this.mouse3             = mouse3;
+        this.mouseWheelUp       = mouseWheelUp;
+        this.mouseWheelDown     = mouseWheelDown;
     }
 
     /**
@@ -121,7 +233,7 @@ public class TMouseEvent extends TInputEvent {
      * @return displayable String
      */
     @Override
-    public final String toString() {
+    public String toString() {
         return String.format("Mouse: %s x %d y %d absoluteX %d absoluteY %d 1 %s 2 %s 3 %s DOWN %s UP %s",
             type,
             x, y,
index e0dcb6dec3fcab8c04d82798bf4818df431aa23c..7977fcef8d9f334b5ab8322f10cef056f3aedcb0 100644 (file)
@@ -33,7 +33,7 @@ package jexer.event;
 /**
  * This class encapsulates a screen or window resize event.
  */
-public class TResizeEvent extends TInputEvent {
+public final class TResizeEvent extends TInputEvent {
 
     /**
      * Resize events can be generated for either a total screen resize or a
@@ -61,7 +61,7 @@ public class TResizeEvent extends TInputEvent {
      *
      * @return SCREEN or WIDGET
      */
-    public final Type getType() {
+    public Type getType() {
         return type;
     }
 
@@ -75,7 +75,7 @@ public class TResizeEvent extends TInputEvent {
      *
      * @return width
      */
-    public final int getWidth() {
+    public int getWidth() {
         return width;
     }
 
@@ -89,8 +89,8 @@ public class TResizeEvent extends TInputEvent {
      *
      * @return height
      */
-    public final int getHeight() {
-        return width;
+    public int getHeight() {
+        return height;
     }
 
     /**
@@ -112,7 +112,7 @@ public class TResizeEvent extends TInputEvent {
      * @return displayable String
      */
     @Override
-    public final String toString() {
+    public String toString() {
         return String.format("Resize: %s width = %d height = %d",
             type, width, height);
     }
index b8fabda389b5253d10bf5ed0a1b91199eee37bff..064c4ec99f98b7d85963adc1429316751bcb4601 100644 (file)
@@ -474,7 +474,7 @@ public class ECMA48Terminal implements Runnable {
             // Unknown modifier, bail out
             return null;
         }
-        
+
         switch (key) {
         case 1:
             return new TKeypressEvent(kbHome, alt, ctrl, shift);
@@ -530,97 +530,100 @@ public class ECMA48Terminal implements Runnable {
             y = windowResize.getHeight() - 1;
         }
 
-        TMouseEvent event = new TMouseEvent(TMouseEvent.Type.MOUSE_DOWN);
-        event.x = x;
-        event.y = y;
-        event.absoluteX = x;
-        event.absoluteY = y;
+        TMouseEvent.Type eventType = TMouseEvent.Type.MOUSE_DOWN;
+        boolean eventMouse1 = false;
+        boolean eventMouse2 = false;
+        boolean eventMouse3 = false;
+        boolean eventMouseWheelUp = false;
+        boolean eventMouseWheelDown = false;
 
         // System.err.printf("buttons: %04x\r\n", buttons);
 
         switch (buttons) {
         case 0:
-            event.mouse1 = true;
+            eventMouse1 = true;
             mouse1 = true;
             break;
         case 1:
-            event.mouse2 = true;
+            eventMouse2 = true;
             mouse2 = true;
             break;
         case 2:
-            event.mouse3 = true;
+            eventMouse3 = true;
             mouse3 = true;
             break;
         case 3:
             // Release or Move
             if (!mouse1 && !mouse2 && !mouse3) {
-                event.type = TMouseEvent.Type.MOUSE_MOTION;
+                eventType = TMouseEvent.Type.MOUSE_MOTION;
             } else {
-                event.type = TMouseEvent.Type.MOUSE_UP;
+                eventType = TMouseEvent.Type.MOUSE_UP;
             }
             if (mouse1) {
                 mouse1 = false;
-                event.mouse1 = true;
+                eventMouse1 = true;
             }
             if (mouse2) {
                 mouse2 = false;
-                event.mouse2 = true;
+                eventMouse2 = true;
             }
             if (mouse3) {
                 mouse3 = false;
-                event.mouse3 = true;
+                eventMouse3 = true;
             }
             break;
 
         case 32:
             // Dragging with mouse1 down
-            event.mouse1 = true;
+            eventMouse1 = true;
             mouse1 = true;
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
 
         case 33:
             // Dragging with mouse2 down
-            event.mouse2 = true;
+            eventMouse2 = true;
             mouse2 = true;
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
 
         case 34:
             // Dragging with mouse3 down
-            event.mouse3 = true;
+            eventMouse3 = true;
             mouse3 = true;
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
 
         case 96:
             // Dragging with mouse2 down after wheelUp
-            event.mouse2 = true;
+            eventMouse2 = true;
             mouse2 = true;
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
 
         case 97:
             // Dragging with mouse2 down after wheelDown
-            event.mouse2 = true;
+            eventMouse2 = true;
             mouse2 = true;
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
 
         case 64:
-            event.mouseWheelUp = true;
+            eventMouseWheelUp = true;
             break;
 
         case 65:
-            event.mouseWheelDown = true;
+            eventMouseWheelDown = true;
             break;
 
         default:
             // Unknown, just make it motion
-            event.type = TMouseEvent.Type.MOUSE_MOTION;
+            eventType = TMouseEvent.Type.MOUSE_MOTION;
             break;
         }
-        return event;
+        return new TMouseEvent(eventType, x, y, x, y,
+            eventMouse1, eventMouse2, eventMouse3,
+            eventMouseWheelUp, eventMouseWheelDown);
     }
 
     /**