chiark / gitweb /
threading fixes: be properly idempotent etc. about setup
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 2 Apr 2011 12:33:37 +0000 (13:33 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 2 Apr 2011 12:33:37 +0000 (13:33 +0100)
src/net/chiark/yarrg/MarketUploader.java

index eb97225e54ca1eadfffe3bf9535326a4ea5c4b18..864cf997d42fa6d481a00fe94377b39b29c236e7 100644 (file)
@@ -278,13 +278,33 @@ public class MarketUploader implements Runnable, TopLevelWindowListener, GUIInit
 
         public void run() {
                if (dtxt!=null) dtxt.println("MarketUploader run()...");
-               EventQueueMonitor.addTopLevelWindowListener(this);
                if (EventQueueMonitor.isGUIInitialized()) {
-                       createGUI();
+                       guiInitialized();
                } else {
                        EventQueueMonitor.addGUIInitializedListener(this);
                }
        }
+
+       public void guiInitialized() {
+               Window ws[]= EventQueueMonitor.getTopLevelWindows();
+               EventQueueMonitor.addTopLevelWindowListener(this);
+               for (int i=0; i<ws.length; i++)
+                       topLevelWindowCreated(ws[i]);
+       }
+
+       public void topLevelWindowDestroyed(Window w) {}
+       
+       public void topLevelWindowCreated(Window w) {
+               if (frame!=null) 
+                       // already got it
+                       return;
+               if (!w.getAccessibleContext().getAccessibleName().equals("Puzzle Pirates"))
+                       return;
+               // found it
+               window = w;
+               createGUI();
+               frame.setVisible(true);
+       }
        
        /**
        *       Set up the GUI, with its window and one-button
@@ -292,10 +312,6 @@ public class MarketUploader implements Runnable, TopLevelWindowListener, GUIInit
        *       a Window named "Puzzle Pirates" though.
        */
        private void createGUI() {
-               if (frame != null && window != null) {
-                       if (window.getAccessibleContext().getAccessibleName().equals("Puzzle Pirates")) frame.setVisible(true);
-                       return;
-               }
                frame = new JFrame("Jarrg Uploader");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                GridLayout layout = new GridLayout(2,1);
@@ -728,16 +744,6 @@ public class MarketUploader implements Runnable, TopLevelWindowListener, GUIInit
                new MarketUploader();
        }
 
-       /**
-       *       Set the global window variable after the YPP window is created,
-       *       remove the top level window listener, and start the GUI
-       */
-       public void topLevelWindowCreated(Window w) {
-               window = w;
-               EventQueueMonitor.removeTopLevelWindowListener(this);
-               createGUI();
-       }
-       
        /**
        *       Returns true if the "Display:" menu on the commodities interface in YPP is set to "All"
        *
@@ -755,12 +761,6 @@ public class MarketUploader implements Runnable, TopLevelWindowListener, GUIInit
                return true;
        }
        
-       public void topLevelWindowDestroyed(Window w) {}
-
-       public void guiInitialized() {
-               createGUI();
-       }
-       
        /**
        *       Gets the list of commodities and their associated commodity ids.
        *