Project

General

Profile

wr.txt

Greg Shah, 02/17/2016 11:01 AM

Download (5.89 KB)

 
1
=== modified file 'src/com/goldencode/p2j/ui/client/WidgetRegistry.java'
2
--- src/com/goldencode/p2j/ui/client/WidgetRegistry.java	2015-12-01 13:37:14 +0000
3
+++ src/com/goldencode/p2j/ui/client/WidgetRegistry.java	2016-02-12 09:18:24 +0000
4
@@ -2,7 +2,7 @@
5
 ** Module   : WidgetRegistry.java
6
 ** Abstract :
7
 **
8
-** Copyright (c) 2010-2015, Golden Code Development Corporation.
9
+** Copyright (c) 2010-2016, Golden Code Development Corporation.
10
 ** ALL RIGHTS RESERVED. Use is subject to license terms.
11
 **
12
 **           Golden Code Development Corporation
13
@@ -48,13 +48,16 @@
14
 ** 024 CA  20151026 Added support for nested frames.
15
 ** 025 CA  20151123 Replaced root frame check with dedicated API.
16
 **     SBI 20151201 Added findAncestor/findDescendant for a given widget and a target widget type.
17
+** 026 IAS 20160211 Thread-safe access to the widgetList.
18
 */
19
 
20
 package com.goldencode.p2j.ui.client;
21
 
22
 import java.util.*;
23
+import java.util.concurrent.locks.*;
24
 import java.util.logging.*;
25
 
26
+import com.codahale.metrics.*;
27
 import com.goldencode.p2j.ui.*;
28
 import com.goldencode.p2j.ui.chui.*;
29
 import com.goldencode.p2j.ui.client.widget.*;
30
@@ -78,6 +81,9 @@
31
    /** The list of the widgets in the current screen. */
32
    private LinkedHashMap<WidgetId, Widget<O>> widgetList = new LinkedHashMap<>();
33
    
34
+   /** Guard lock for access to the widgetList */
35
+   private final ReadWriteLock lock = new ReentrantReadWriteLock();
36
+   
37
    /**
38
     * Returns a nearest widget's ancestor having a given type.
39
     * 
40
@@ -183,7 +189,7 @@
41
 
42
          if (comp != null)
43
          {
44
-            widgetList.put(cid, comp);
45
+            addWidget(cid, comp);
46
             
47
             if (comp instanceof WidgetWithConfig)
48
             {
49
@@ -216,13 +222,13 @@
50
    
51
       Widget<O>[] list = frame.getContentPane().widgets();
52
       
53
-      widgetList.put(frame.config().id, frame);
54
+      addWidget(frame.config().id, frame);
55
    
56
       for (Widget<O> comp : list)
57
       {
58
          if (UiUtils.hasConfig(comp))
59
          {
60
-            widgetList.put(comp.getId(), comp);
61
+            addWidget(comp.getId(), comp);
62
          }
63
       }
64
    }
65
@@ -233,9 +239,17 @@
66
     * @return  Array of component.
67
     */
68
    @SuppressWarnings("unchecked")
69
-   public Widget<O>[] getComponents()
70
+   public synchronized Widget<O>[] getComponents()
71
    {
72
-      return widgetList.values().toArray(new Widget[widgetList.size()]);
73
+      lock.readLock().lock();
74
+      try
75
+      {
76
+         return widgetList.values().toArray(new Widget[widgetList.size()]);
77
+      }
78
+      finally
79
+      {
80
+         lock.readLock().unlock();
81
+      }
82
    }
83
 
84
    /**
85
@@ -276,7 +290,7 @@
86
             WidgetId id = UiUtils.getWidgetId(w[i]);
87
             if (id != null)
88
             {
89
-               Widget<?> comp = widgetList.remove(id);
90
+               Widget<?> comp = removeWidget(id);
91
                
92
                if (comp != null)
93
                {
94
@@ -286,7 +300,7 @@
95
          }
96
 
97
          // remove the frame itself
98
-         widgetList.remove(base);
99
+         removeWidget(base);
100
          
101
          // destroy will detach the frame from its parent
102
          frame.destroy();
103
@@ -311,8 +325,18 @@
104
 
105
       if (id.equals(WidgetId.DEFAULT_WINDOW_ID))
106
          return (Widget<O>) WindowManager.getDefaultWindow();
107
+
108
+      Widget<O> w = null;
109
+      lock.readLock().lock();
110
+      try
111
+      {
112
+         w = widgetList.get(id);
113
+      }
114
+      finally
115
+      {
116
+         lock.readLock().unlock();
117
+      }
118
       
119
-      Widget<O> w = widgetList.get(id);
120
       if (w == null)
121
       {
122
          w = WindowManager.findWindow(id.asInt());
123
@@ -335,6 +359,7 @@
124
    @SuppressWarnings("unchecked")
125
    public Widget<O> getComponent(int id)
126
    {
127
+//      System.out.printf("*** getComponent(%s)\n", id);
128
       if (id == WidgetId.DEFAULT_WINDOW_ID.asInt())
129
          return (Widget<O>) WindowManager.getDefaultWindow();
130
       
131
@@ -345,20 +370,23 @@
132
       }
133
 
134
       int frameID = lookupFrameIdFromWidgetId(id);
135
-      
136
+//      System.out.println("*** frameID = " + frameID);
137
       if (frameID != id && frameID != -1)
138
       {
139
          Frame<?> frame = (Frame<?>) getComponent(frameID);
140
          WidgetId wid = Frame.resolveWidgetId(frame, id);
141
-         Widget<O> widget = widgetList.get(wid);
142
+         Widget<O> widget = getComponent(wid);
143
+
144
+//         System.out.printf("*** widgetList.get(%s): %s\n", wid, widget);
145
 
146
          // search by ID only if not found... 
147
          // TODO: confirm this doesn't break anything else!
148
-         return (widget == null ? widgetList.get(new WidgetId(id)) : widget);
149
+         return (widget == null ? getComponent(new WidgetId(id)) : widget);
150
       }
151
       else
152
       {
153
-         return widgetList.get(new WidgetId(id));
154
+//         System.out.printf("*** lookup widgetLis for %s\n", id);
155
+         return getComponent(new WidgetId(id));
156
       }
157
    }
158
 
159
@@ -374,7 +402,7 @@
160
    {
161
       int resultId = -1;
162
       
163
-      Widget<?> wid = widgetList.get(new WidgetId(widgetId));
164
+      Widget<?> wid = getComponent(new WidgetId(widgetId));
165
       
166
       if (wid != null)
167
       {
168
@@ -543,7 +571,7 @@
169
     */
170
    public void addWidget(Widget<O> widget)
171
    {
172
-      this.widgetList.put(widget.getId(), widget);
173
+      addWidget(widget.getId(), widget);
174
    }
175
 
176
    /**
177
@@ -556,7 +584,15 @@
178
     */
179
    public void addWidget(WidgetId id, Widget<O> widget)
180
    {
181
-      this.widgetList.put(id, widget);
182
+      lock.writeLock().lock();
183
+      try
184
+      {
185
+         this.widgetList.put(id, widget);
186
+      }
187
+      finally
188
+      {
189
+         lock.writeLock().unlock();
190
+      }
191
    }
192
    
193
    /**
194
@@ -565,9 +601,17 @@
195
     * @param    id
196
     *           The widget ID.
197
     */
198
-   public void removeWidget(WidgetId id)
199
+   public synchronized Widget<?> removeWidget(WidgetId id)
200
    {
201
-      this.widgetList.remove(id);
202
+      lock.writeLock().lock();
203
+      try
204
+      {
205
+         return this.widgetList.remove(id);
206
+      }
207
+      finally
208
+      {
209
+         lock.writeLock().unlock();
210
+      }
211
    }
212
 
213
    /**
214