Project

General

Profile

lost_keys_input_fix_20151103_2.txt

Sergey Ivanovskiy, 11/03/2015 11:30 AM

Download (8.54 KB)

 
1
=== modified file 'src/com/goldencode/p2j/ui/client/WindowManager.java'
2
--- src/com/goldencode/p2j/ui/client/WindowManager.java	2015-10-16 12:11:54 +0000
3
+++ src/com/goldencode/p2j/ui/client/WindowManager.java	2015-11-03 14:07:35 +0000
4
@@ -310,7 +310,7 @@
5
          w.hide();
6
          if (w instanceof Window)
7
          {
8
-            removeWindow(w.getId().asInt());
9
+            remove(w);
10
          }
11
          else 
12
          {
13

    
14
=== modified file 'src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingEmulatedWindow.java'
15
--- src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingEmulatedWindow.java	2015-10-24 19:51:34 +0000
16
+++ src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingEmulatedWindow.java	2015-11-03 15:39:09 +0000
17
@@ -73,6 +73,7 @@
18
 **                  operations.  Removed save/restore of focus listeners and replaced it with
19
 **                  a global flag.
20
 ** 035 CA  20151024 Added support for WINDOW:SENSITIVE attribute.
21
+**     SBI 20151103 Added the drivers keyboard focus manager to fix the lost keys input.
22
 */
23
 
24
 package com.goldencode.p2j.ui.client.gui.driver.swing;
25
@@ -104,6 +105,7 @@
26
 import com.goldencode.p2j.ui.client.event.*;
27
 import com.goldencode.p2j.ui.client.gui.*;
28
 import com.goldencode.p2j.ui.client.gui.driver.*;
29
+
30
 import sun.java2d.*;
31
 import sun.java2d.loops.*;
32
 
33
@@ -213,7 +215,7 @@
34
             SwingEmulatedWindow.this.graphics = null;
35
          }   
36
       };
37
-
38
+      KeyboardFocusManager.setCurrentKeyboardFocusManager(new SwingEmulatedKeyboardFocusManager());
39
       // let the app above handle the decoration itself
40
       if (window instanceof JFrame)
41
       {
42
@@ -302,7 +304,22 @@
43
             }
44
          }
45
       });
46
-   
47
+
48
+      window.addComponentListener(new ComponentAdapter()
49
+      {
50
+         
51
+         @Override
52
+         public void componentShown(ComponentEvent e)
53
+         {
54
+            KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
55
+            if (kfm instanceof SwingEmulatedKeyboardFocusManager)
56
+            {
57
+               ((SwingEmulatedKeyboardFocusManager) kfm).setWindowFocused(window);
58
+            }
59
+         }
60
+         
61
+      });
62
+
63
       // setup mouse listeners
64
       pane.addMouseListener(mouseHandler);
65
       pane.addMouseMotionListener(mouseHandler);
66
@@ -375,37 +392,63 @@
67
    @Override
68
    public void quit()
69
    {
70
-      for (MouseListener l : pane.getMouseListeners())
71
-      {
72
-         pane.removeMouseListener(l);
73
-      }
74
-      for (MouseMotionListener l : pane.getMouseMotionListeners())
75
-      {
76
-         pane.removeMouseMotionListener(l);
77
-      }
78
-      for (MouseWheelListener l : pane.getMouseWheelListeners())
79
-      {
80
-         pane.removeMouseWheelListener(l);
81
-      }
82
-      for (WindowListener l : window.getWindowListeners())
83
-      {
84
-         window.removeWindowListener(l);
85
-      }
86
-      for (WindowFocusListener l : window.getWindowFocusListeners())
87
-      {
88
-         window.removeWindowFocusListener(l);
89
-      }
90
-      for (WindowStateListener l : window.getWindowStateListeners())
91
-      {
92
-         window.removeWindowStateListener(l);
93
-      }
94
-      for (KeyListener l : window.getKeyListeners())
95
-      {
96
-         window.removeKeyListener(l);
97
-      }
98
-      
99
-      // cleanup window resources
100
-      pane.quit();
101
+      Runnable r = new Runnable()
102
+      {
103
+         
104
+         @Override
105
+         public void run()
106
+         {
107
+            for (MouseListener l : pane.getMouseListeners())
108
+            {
109
+               pane.removeMouseListener(l);
110
+            }
111
+            for (MouseMotionListener l : pane.getMouseMotionListeners())
112
+            {
113
+               pane.removeMouseMotionListener(l);
114
+            }
115
+            for (MouseWheelListener l : pane.getMouseWheelListeners())
116
+            {
117
+               pane.removeMouseWheelListener(l);
118
+            }
119
+            for (WindowListener l : window.getWindowListeners())
120
+            {
121
+               window.removeWindowListener(l);
122
+            }
123
+            for (WindowFocusListener l : window.getWindowFocusListeners())
124
+            {
125
+               window.removeWindowFocusListener(l);
126
+            }
127
+            for (WindowStateListener l : window.getWindowStateListeners())
128
+            {
129
+               window.removeWindowStateListener(l);
130
+            }
131
+            for (KeyListener l : window.getKeyListeners())
132
+            {
133
+               window.removeKeyListener(l);
134
+            }
135
+            for (ComponentListener l : window.getComponentListeners())
136
+            {
137
+               window.removeComponentListener(l);
138
+            }
139
+            
140
+            // cleanup window resources
141
+            pane.quit();
142
+         }
143
+      };
144
+      if (SwingUtilities.isEventDispatchThread())
145
+      {
146
+         r.run();
147
+      }
148
+      else
149
+      {
150
+         try
151
+         {
152
+            SwingUtilities.invokeAndWait(r);
153
+         }
154
+         catch (InvocationTargetException | InterruptedException e)
155
+         {
156
+         }
157
+      }
158
    }
159
    
160
    /**
161
@@ -1448,4 +1491,28 @@
162
       
163
       highlightRestoreBuffer = null;
164
    }
165
+
166
+   /**
167
+    * The driver level keyboard focus manager.
168
+    */
169
+   class SwingEmulatedKeyboardFocusManager extends DefaultKeyboardFocusManager 
170
+   {
171
+      /**
172
+       * Sets the target window to be able to receive keys input.
173
+       * 
174
+       * @param    focusedWindow
175
+       *           The target clients window
176
+       * @throws   SecurityException
177
+       *           if this KeyboardFocusManager is not the current KeyboardFocusManager
178
+       *           for the calling thread's context and if the calling thread does not have
179
+       *           "replaceKeyboardFocusManager" permission.
180
+       */
181
+      public void setWindowFocused(Window focusedWindow) throws SecurityException
182
+      {
183
+         setGlobalFocusOwner(focusedWindow);
184
+         setGlobalFocusedWindow(focusedWindow);
185
+         setGlobalActiveWindow(focusedWindow);
186
+      }
187
+   };
188
+
189
 }
190

    
191
=== modified file 'src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingGuiDriver.java'
192
--- src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingGuiDriver.java	2015-10-22 23:14:19 +0000
193
+++ src/com/goldencode/p2j/ui/client/gui/driver/swing/SwingGuiDriver.java	2015-11-03 16:17:30 +0000
194
@@ -68,6 +68,7 @@
195
 **     HC  20151022 A fix for JWindow not able to receive focus when its owner window is not
196
 **                  visible. This resolves ALERT-BOX and DIALOG-BOX not receiving key input when
197
 **                  no main window visible.
198
+**     SBI 20151102 To do the keyboard reader a final driver's field.
199
 */
200
 
201
 package com.goldencode.p2j.ui.client.gui.driver.swing;
202
@@ -82,7 +83,6 @@
203
 import java.io.*;
204
 import java.util.*;
205
 
206
-import com.goldencode.p2j.security.*;
207
 import com.goldencode.p2j.ui.client.*;
208
 import com.goldencode.p2j.ui.client.gui.driver.*;
209
 import com.goldencode.p2j.ui.client.driver.swing.*;
210
@@ -97,14 +97,7 @@
211
     * The key reader.  This is global per the entire application, no matter how many frames are
212
     * alive.
213
     */
214
-   private static final ContextLocal<SwingKeyboardReader> keyReader = 
215
-   new ContextLocal<SwingKeyboardReader>()
216
-   {
217
-      protected SwingKeyboardReader initialValue()
218
-      {
219
-         return new WinKeyboardReader();
220
-      };
221
-   };
222
+   private final SwingKeyboardReader keyReader = new WinKeyboardReader();
223
 
224
    /** A special map containing the attributes of a underlined font. */
225
    static Map<TextAttribute, Integer> UNDERLINE_FONT = new HashMap<>();
226
@@ -246,7 +239,7 @@
227
    @Override
228
    public int readKey()                      
229
    {
230
-      return keyReader.get().read();
231
+      return keyReader.read();
232
    }
233
 
234
    /**
235
@@ -264,7 +257,7 @@
236
 
237
       try
238
       {
239
-         ews = SwingGuiClient.load(keyReader.get(), true);
240
+         ews = SwingGuiClient.load(keyReader, true);
241
          
242
          // the real window size is unknown at this time; it will be set later, when the layout
243
          // is performed
244
@@ -304,14 +297,14 @@
245
          {
246
             ews = GuiWindowContainer.load((SwingEmulatedWindow) owner, 
247
                                           windowTitle, 
248
-                                          keyReader.get());
249
+                                          keyReader);
250
          }
251
          else
252
          {
253
             // GuiWindowContainer is backed by JWindow Swing component which
254
             // requires a visible owner window in order to be focusable.
255
             // So if no visible owner, use SwingGuiClient backed by JFrame. 
256
-            ews = SwingGuiClient.load(keyReader.get(), false);
257
+            ews = SwingGuiClient.load(keyReader, false);
258
          }
259
          
260
          // the real window size is unknown at this time; it will be set later, when the layout
261