|
| 1 | +package a2s; |
| 2 | + |
| 3 | +import java.awt.AWTEvent; |
| 4 | +import java.awt.Component; |
| 5 | +import java.awt.Event; |
| 6 | +import java.awt.Point; |
| 7 | +import java.awt.event.ActionEvent; |
| 8 | +import java.awt.event.ActionListener; |
| 9 | +import java.awt.event.AdjustmentEvent; |
| 10 | +import java.awt.event.AdjustmentListener; |
| 11 | +import java.awt.event.ComponentEvent; |
| 12 | +import java.awt.event.FocusEvent; |
| 13 | +import java.awt.event.InputEvent; |
| 14 | +import java.awt.event.ItemEvent; |
| 15 | +import java.awt.event.KeyEvent; |
| 16 | +import java.awt.event.MouseEvent; |
| 17 | +import java.awt.event.WindowEvent; |
| 18 | +import java.util.EventListener; |
| 19 | + |
| 20 | +import javax.swing.AbstractButton; |
| 21 | +import javax.swing.JComboBox; |
| 22 | +import javax.swing.JScrollBar; |
| 23 | + |
| 24 | + |
| 25 | +public class A2SEvent implements Runnable { |
| 26 | + |
| 27 | + private Event e; |
| 28 | + private Component c; |
| 29 | + |
| 30 | + |
| 31 | + public A2SEvent(Component c, AWTEvent e) { |
| 32 | + this.c = c; |
| 33 | + this.e = A2SEvent.convertToOld(e); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + @SuppressWarnings("deprecation") |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + Component c = this.c; |
| 41 | + Event e = this.e; |
| 42 | + |
| 43 | + /** |
| 44 | + * Otherwise the states have not changed |
| 45 | + * |
| 46 | + * @j2sNative |
| 47 | + * |
| 48 | + * setTimeout(function() { c.handleEvent(e);}); |
| 49 | + * |
| 50 | + */ |
| 51 | + { |
| 52 | + c.handleEvent(e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + static int getOldEventKey(KeyEvent e) { |
| 58 | + int keyCode = e.getKeyCode(); |
| 59 | + for (int i = 0; i < actionKeyCodes.length; i++) { |
| 60 | + if (actionKeyCodes[i][0] == keyCode) { |
| 61 | + return actionKeyCodes[i][1]; |
| 62 | + } |
| 63 | + } |
| 64 | + return (int)e.getKeyChar(); |
| 65 | +} |
| 66 | + /* table for mapping old Event action keys to KeyEvent virtual keys. */ |
| 67 | + private static final int actionKeyCodes[][] = { |
| 68 | + /* virtual key action key */ |
| 69 | + { KeyEvent.VK_HOME, Event.HOME }, |
| 70 | + { KeyEvent.VK_END, Event.END }, |
| 71 | + { KeyEvent.VK_PAGE_UP, Event.PGUP }, |
| 72 | + { KeyEvent.VK_PAGE_DOWN, Event.PGDN }, |
| 73 | + { KeyEvent.VK_UP, Event.UP }, |
| 74 | + { KeyEvent.VK_DOWN, Event.DOWN }, |
| 75 | + { KeyEvent.VK_LEFT, Event.LEFT }, |
| 76 | + { KeyEvent.VK_RIGHT, Event.RIGHT }, |
| 77 | + { KeyEvent.VK_F1, Event.F1 }, |
| 78 | + { KeyEvent.VK_F2, Event.F2 }, |
| 79 | + { KeyEvent.VK_F3, Event.F3 }, |
| 80 | + { KeyEvent.VK_F4, Event.F4 }, |
| 81 | + { KeyEvent.VK_F5, Event.F5 }, |
| 82 | + { KeyEvent.VK_F6, Event.F6 }, |
| 83 | + { KeyEvent.VK_F7, Event.F7 }, |
| 84 | + { KeyEvent.VK_F8, Event.F8 }, |
| 85 | + { KeyEvent.VK_F9, Event.F9 }, |
| 86 | + { KeyEvent.VK_F10, Event.F10 }, |
| 87 | + { KeyEvent.VK_F11, Event.F11 }, |
| 88 | + { KeyEvent.VK_F12, Event.F12 }, |
| 89 | + { KeyEvent.VK_PRINTSCREEN, Event.PRINT_SCREEN }, |
| 90 | + { KeyEvent.VK_SCROLL_LOCK, Event.SCROLL_LOCK }, |
| 91 | + { KeyEvent.VK_CAPS_LOCK, Event.CAPS_LOCK }, |
| 92 | + { KeyEvent.VK_NUM_LOCK, Event.NUM_LOCK }, |
| 93 | + { KeyEvent.VK_PAUSE, Event.PAUSE }, |
| 94 | + { KeyEvent.VK_INSERT, Event.INSERT } |
| 95 | + }; |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Converts a new event to an old one (used for compatibility). |
| 100 | + * If the new event cannot be converted (because no old equivalent |
| 101 | + * exists) then this returns null. |
| 102 | + * |
| 103 | + * Note: this method is here instead of in each individual new |
| 104 | + * event class in java.awt.event because we don't want to make |
| 105 | + * it public and it needs to be called from java.awt. |
| 106 | + */ |
| 107 | + static Event convertToOld(AWTEvent e) { |
| 108 | + Object src = e.getSource(); |
| 109 | + int id = e.getID(); |
| 110 | + int newid = id; |
| 111 | + |
| 112 | + switch(id) { |
| 113 | + case KeyEvent.KEY_PRESSED: |
| 114 | + case KeyEvent.KEY_RELEASED: |
| 115 | + KeyEvent ke = (KeyEvent)e; |
| 116 | + if (ke.isActionKey()) { |
| 117 | + newid = (id == KeyEvent.KEY_PRESSED? |
| 118 | + Event.KEY_ACTION : Event.KEY_ACTION_RELEASE); |
| 119 | + } |
| 120 | + int keyCode = ke.getKeyCode(); |
| 121 | + if (keyCode == KeyEvent.VK_SHIFT || |
| 122 | + keyCode == KeyEvent.VK_CONTROL || |
| 123 | + keyCode == KeyEvent.VK_ALT) { |
| 124 | + return null; // suppress modifier keys in old event model. |
| 125 | + } |
| 126 | + // no mask for button1 existed in old Event - strip it out |
| 127 | + return new Event(src, ke.getWhen(), newid, 0, 0, |
| 128 | + getOldEventKey(ke), |
| 129 | + (ke.getModifiers() & ~InputEvent.BUTTON1_MASK)); |
| 130 | + |
| 131 | + case MouseEvent.MOUSE_PRESSED: |
| 132 | + case MouseEvent.MOUSE_RELEASED: |
| 133 | + case MouseEvent.MOUSE_MOVED: |
| 134 | + case MouseEvent.MOUSE_DRAGGED: |
| 135 | + case MouseEvent.MOUSE_ENTERED: |
| 136 | + case MouseEvent.MOUSE_EXITED: |
| 137 | + MouseEvent me = (MouseEvent)e; |
| 138 | + // no mask for button1 existed in old Event - strip it out |
| 139 | + Event olde = new Event(src, me.getWhen(), newid, |
| 140 | + me.getX(), me.getY(), 0, |
| 141 | + (me.getModifiers() & ~InputEvent.BUTTON1_MASK)); |
| 142 | + olde.clickCount = me.getClickCount(); |
| 143 | + return olde; |
| 144 | + |
| 145 | + case FocusEvent.FOCUS_GAINED: |
| 146 | + return new Event(src, Event.GOT_FOCUS, null); |
| 147 | + |
| 148 | + case FocusEvent.FOCUS_LOST: |
| 149 | + return new Event(src, Event.LOST_FOCUS, null); |
| 150 | + |
| 151 | + case WindowEvent.WINDOW_CLOSING: |
| 152 | + case WindowEvent.WINDOW_ICONIFIED: |
| 153 | + case WindowEvent.WINDOW_DEICONIFIED: |
| 154 | + return new Event(src, newid, null); |
| 155 | + |
| 156 | + case ComponentEvent.COMPONENT_MOVED: |
| 157 | + if (src instanceof Frame || src instanceof Dialog) { |
| 158 | + Point p = ((Component)src).getLocation(); |
| 159 | + return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0); |
| 160 | + } |
| 161 | + break; |
| 162 | + |
| 163 | + case ActionEvent.ACTION_PERFORMED: |
| 164 | + ActionEvent ae = (ActionEvent)e; |
| 165 | + String cmd; |
| 166 | + if (src instanceof AbstractButton) { |
| 167 | + cmd = ((AbstractButton)src).getText(); |
| 168 | + } else if (src instanceof MenuItem) { |
| 169 | + cmd = ((MenuItem)src).getText(); |
| 170 | + } else { |
| 171 | + cmd = ae.getActionCommand(); |
| 172 | + } |
| 173 | + return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd); |
| 174 | + |
| 175 | + case ItemEvent.ITEM_STATE_CHANGED: |
| 176 | + ItemEvent ie = (ItemEvent)e; |
| 177 | + Object arg; |
| 178 | + if (src instanceof List) { |
| 179 | + newid = (ie.getStateChange() == ItemEvent.SELECTED? |
| 180 | + Event.LIST_SELECT : Event.LIST_DESELECT); |
| 181 | + arg = ie.getItem(); |
| 182 | + } else { |
| 183 | + newid = Event.ACTION_EVENT; |
| 184 | + if (src instanceof Choice) { |
| 185 | + arg = ie.getItem(); |
| 186 | + |
| 187 | + } else { // Checkbox |
| 188 | + arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED); |
| 189 | + } |
| 190 | + } |
| 191 | + return new Event(src, newid, arg); |
| 192 | + |
| 193 | + case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED: |
| 194 | + AdjustmentEvent aje = (AdjustmentEvent)e; |
| 195 | + switch(aje.getAdjustmentType()) { |
| 196 | + case AdjustmentEvent.UNIT_INCREMENT: |
| 197 | + newid = Event.SCROLL_LINE_DOWN; |
| 198 | + break; |
| 199 | + case AdjustmentEvent.UNIT_DECREMENT: |
| 200 | + newid = Event.SCROLL_LINE_UP; |
| 201 | + break; |
| 202 | + case AdjustmentEvent.BLOCK_INCREMENT: |
| 203 | + newid = Event.SCROLL_PAGE_DOWN; |
| 204 | + break; |
| 205 | + case AdjustmentEvent.BLOCK_DECREMENT: |
| 206 | + newid = Event.SCROLL_PAGE_UP; |
| 207 | + break; |
| 208 | + case AdjustmentEvent.TRACK: |
| 209 | + if (aje.getValueIsAdjusting()) { |
| 210 | + newid = Event.SCROLL_ABSOLUTE; |
| 211 | + } |
| 212 | + else { |
| 213 | + newid = Event.SCROLL_END; |
| 214 | + } |
| 215 | + break; |
| 216 | + default: |
| 217 | + return null; |
| 218 | + } |
| 219 | + return new Event(src, newid, Integer.valueOf(aje.getValue())); |
| 220 | + |
| 221 | + default: |
| 222 | + } |
| 223 | + return null; |
| 224 | + } |
| 225 | + |
| 226 | + public static Component addComponent(EventListener listener, Component comp) { |
| 227 | + if (comp instanceof AbstractButton) { |
| 228 | + ((AbstractButton) comp).addActionListener((ActionListener) listener); |
| 229 | + } else if (comp instanceof TextField) { |
| 230 | + ((TextField) comp).addActionListener((ActionListener) listener); |
| 231 | + } else if (comp instanceof JComboBox) { |
| 232 | + ((JComboBox) comp).addActionListener((ActionListener) listener); |
| 233 | + } else if (comp instanceof JScrollBar) { |
| 234 | + ((JScrollBar) comp).addAdjustmentListener((AdjustmentListener) listener); |
| 235 | + } |
| 236 | + return comp; |
| 237 | + } |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | +} |
0 commit comments