chiark / gitweb /
threading fixes: be properly idempotent etc. about setup - comments and extra debug...
[jarrg-ian.git] / src / net / chiark / yarrg / MarketUploader.java
index 53b434971f92c0874fb301ada62f3aaaca8fde45..a6df4975b34441a320301edb044553dae64d8789 100644 (file)
@@ -34,7 +34,7 @@ import java.beans.*;
 *      an error dialog is shown, and processing returns, the button
 *      becoming re-enabled.
 */
-public class MarketUploader implements TopLevelWindowListener, GUIInitializedListener {
+public class MarketUploader implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        private JFrame frame = null;
        private Window window = null;
        private JButton findMarket = null;
@@ -272,13 +272,51 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                uploadToPCTB=prefs.getBoolean("uploadToPCTB", true);
                showArbitrage=prefs.getBoolean("showArbitrage", true);
 
-               EventQueueMonitor.addTopLevelWindowListener(this);
+               if (dtxt!=null) dtxt.println("main on dispatch thread: "+EventQueue.isDispatchThread());
+               EventQueue.invokeLater(this);
+       }
+
+       /*
+        * We arrange to wait for the GUI to be initialised, then look at every top-level window,
+        * and if it 
+        */
+        public void run() {
+               if (dtxt!=null) dtxt.println("MarketUploader run()...");
                if (EventQueueMonitor.isGUIInitialized()) {
-                       createGUI();
+                       if (dtxt!=null) dtxt.println("MarketUploader GUI already ready");
+                       guiInitialized();
                } else {
+                       if (dtxt!=null) dtxt.println("MarketUploader waiting for GUI");
                        EventQueueMonitor.addGUIInitializedListener(this);
                }
        }
+
+       public void guiInitialized() {
+               Window ws[]= EventQueueMonitor.getTopLevelWindows();
+               EventQueueMonitor.addTopLevelWindowListener(this);
+               for (int i=0; i<ws.length; i++) {
+                       if (dtxt!=null) dtxt.println("MarketUploader existing toplevel "+i);
+                       topLevelWindowCreated(ws[i]);
+               }
+       }
+
+       public void topLevelWindowDestroyed(Window w) {
+               if (dtxt!=null) dtxt.println("MarketUploader destroyed toplevel");
+       }
+       
+       public void topLevelWindowCreated(Window w) {
+               if (frame!=null) 
+                       // already got it
+                       return;
+               String name = w.getAccessibleContext().getAccessibleName();
+               if (dtxt!=null) dtxt.println("MarketUploader new toplevel "+name);
+               if (!name.equals("Puzzle Pirates"))
+                       return;
+               if (dtxt!=null) dtxt.println("MarketUploader found toplevel, creating gui");
+               window = w;
+               createGUI();
+               frame.setVisible(true);
+       }
        
        /**
        *       Set up the GUI, with its window and one-button
@@ -286,10 +324,6 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        *       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);
@@ -346,7 +380,10 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                // If the league tracker is there, we can skip the faff
                // and ask for its tooltip, since we're on a boat
 
-               Accessible leagueTracker = descendNodes(window,new int[] {0,1,0,0,2,1,1,1});
+               Accessible leagueTrackerContainer = descendNodes(window,new int[] {0,1,0,0,2,1});
+               Accessible leagueTrackerItself = descendByClass(leagueTrackerContainer,
+                                                               "com.threerings.yohoho.sea.client.LeagueTracker");
+               Accessible leagueTracker = descend(leagueTrackerItself, 1);
                try {
                        islandName = ((JLabel)leagueTracker).getToolTipText();
                } catch (NullPointerException e) {
@@ -692,21 +729,33 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                if (dtxt!=null) dtxt.println("DESCEND "+childNum+" "+child.getClass().getName()+" OK");
                return child;
        }
-       
-       public static void main(String[] args) {
-               new MarketUploader();
-       }
 
        /**
-       *       Set the global window variable after the YPP window is created,
-       *       remove the top level window listener, and start the GUI
+       *       Descends one level to the child which has the specified class.
+       *       
+       *       @param parent the node with children
+       *       @param classname the name of the class, as a string
+       *       @return the child or <code>null</code> if the child is not found.
        */
-       public void topLevelWindowCreated(Window w) {
-               window = w;
-               EventQueueMonitor.removeTopLevelWindowListener(this);
-               createGUI();
+       private Accessible descendByClass(Accessible parent, String classname) {
+               if (parent == null) return null;
+               AccessibleContext ac = parent.getAccessibleContext();
+               int children = ac.getAccessibleChildrenCount();
+               for (int i=0; i<children; i++) {
+                       Accessible child = ac.getAccessibleChild(i);
+                       if (child.getClass().getName() == classname) {
+                           if (dtxt!=null) dtxt.println("DESCEND CLASS "+classname+" OK");
+                           return child;
+                       }
+               }
+               if (dtxt!=null) dtxt.println("DESCEND CLASS "+classname+" NOT FOUND");
+               return null;
        }
-       
+
+       public static void main(String[] args) {
+               new MarketUploader();
+       }
+
        /**
        *       Returns true if the "Display:" menu on the commodities interface in YPP is set to "All"
        *
@@ -724,12 +773,6 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                return true;
        }
        
-       public void topLevelWindowDestroyed(Window w) {}
-
-       public void guiInitialized() {
-               createGUI();
-       }
-       
        /**
        *       Gets the list of commodities and their associated commodity ids.
        *