=== modified file 'src/com/goldencode/p2j/ui/client/WidgetRegistry.java' --- src/com/goldencode/p2j/ui/client/WidgetRegistry.java 2015-12-01 13:37:14 +0000 +++ src/com/goldencode/p2j/ui/client/WidgetRegistry.java 2016-02-12 09:18:24 +0000 @@ -2,7 +2,7 @@ ** Module : WidgetRegistry.java ** Abstract : ** -** Copyright (c) 2010-2015, Golden Code Development Corporation. +** Copyright (c) 2010-2016, Golden Code Development Corporation. ** ALL RIGHTS RESERVED. Use is subject to license terms. ** ** Golden Code Development Corporation @@ -48,13 +48,16 @@ ** 024 CA 20151026 Added support for nested frames. ** 025 CA 20151123 Replaced root frame check with dedicated API. ** SBI 20151201 Added findAncestor/findDescendant for a given widget and a target widget type. +** 026 IAS 20160211 Thread-safe access to the widgetList. */ package com.goldencode.p2j.ui.client; import java.util.*; +import java.util.concurrent.locks.*; import java.util.logging.*; +import com.codahale.metrics.*; import com.goldencode.p2j.ui.*; import com.goldencode.p2j.ui.chui.*; import com.goldencode.p2j.ui.client.widget.*; @@ -78,6 +81,9 @@ /** The list of the widgets in the current screen. */ private LinkedHashMap> widgetList = new LinkedHashMap<>(); + /** Guard lock for access to the widgetList */ + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + /** * Returns a nearest widget's ancestor having a given type. * @@ -183,7 +189,7 @@ if (comp != null) { - widgetList.put(cid, comp); + addWidget(cid, comp); if (comp instanceof WidgetWithConfig) { @@ -216,13 +222,13 @@ Widget[] list = frame.getContentPane().widgets(); - widgetList.put(frame.config().id, frame); + addWidget(frame.config().id, frame); for (Widget comp : list) { if (UiUtils.hasConfig(comp)) { - widgetList.put(comp.getId(), comp); + addWidget(comp.getId(), comp); } } } @@ -233,9 +239,17 @@ * @return Array of component. */ @SuppressWarnings("unchecked") - public Widget[] getComponents() + public synchronized Widget[] getComponents() { - return widgetList.values().toArray(new Widget[widgetList.size()]); + lock.readLock().lock(); + try + { + return widgetList.values().toArray(new Widget[widgetList.size()]); + } + finally + { + lock.readLock().unlock(); + } } /** @@ -276,7 +290,7 @@ WidgetId id = UiUtils.getWidgetId(w[i]); if (id != null) { - Widget comp = widgetList.remove(id); + Widget comp = removeWidget(id); if (comp != null) { @@ -286,7 +300,7 @@ } // remove the frame itself - widgetList.remove(base); + removeWidget(base); // destroy will detach the frame from its parent frame.destroy(); @@ -311,8 +325,18 @@ if (id.equals(WidgetId.DEFAULT_WINDOW_ID)) return (Widget) WindowManager.getDefaultWindow(); + + Widget w = null; + lock.readLock().lock(); + try + { + w = widgetList.get(id); + } + finally + { + lock.readLock().unlock(); + } - Widget w = widgetList.get(id); if (w == null) { w = WindowManager.findWindow(id.asInt()); @@ -335,6 +359,7 @@ @SuppressWarnings("unchecked") public Widget getComponent(int id) { +// System.out.printf("*** getComponent(%s)\n", id); if (id == WidgetId.DEFAULT_WINDOW_ID.asInt()) return (Widget) WindowManager.getDefaultWindow(); @@ -345,20 +370,23 @@ } int frameID = lookupFrameIdFromWidgetId(id); - +// System.out.println("*** frameID = " + frameID); if (frameID != id && frameID != -1) { Frame frame = (Frame) getComponent(frameID); WidgetId wid = Frame.resolveWidgetId(frame, id); - Widget widget = widgetList.get(wid); + Widget widget = getComponent(wid); + +// System.out.printf("*** widgetList.get(%s): %s\n", wid, widget); // search by ID only if not found... // TODO: confirm this doesn't break anything else! - return (widget == null ? widgetList.get(new WidgetId(id)) : widget); + return (widget == null ? getComponent(new WidgetId(id)) : widget); } else { - return widgetList.get(new WidgetId(id)); +// System.out.printf("*** lookup widgetLis for %s\n", id); + return getComponent(new WidgetId(id)); } } @@ -374,7 +402,7 @@ { int resultId = -1; - Widget wid = widgetList.get(new WidgetId(widgetId)); + Widget wid = getComponent(new WidgetId(widgetId)); if (wid != null) { @@ -543,7 +571,7 @@ */ public void addWidget(Widget widget) { - this.widgetList.put(widget.getId(), widget); + addWidget(widget.getId(), widget); } /** @@ -556,7 +584,15 @@ */ public void addWidget(WidgetId id, Widget widget) { - this.widgetList.put(id, widget); + lock.writeLock().lock(); + try + { + this.widgetList.put(id, widget); + } + finally + { + lock.writeLock().unlock(); + } } /** @@ -565,9 +601,17 @@ * @param id * The widget ID. */ - public void removeWidget(WidgetId id) + public synchronized Widget removeWidget(WidgetId id) { - this.widgetList.remove(id); + lock.writeLock().lock(); + try + { + return this.widgetList.remove(id); + } + finally + { + lock.writeLock().unlock(); + } } /**