more focus fixing (JAL-943)
[jalview.git] / src / jalview / gui / Desktop.java
index 03b2005..5dadeb3 100644 (file)
@@ -43,6 +43,8 @@ import javax.swing.*;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 
+import com.sun.xml.internal.ws.streaming.TidyXMLStreamReader;
+
 /**
  * Jalview Desktop
  * 
@@ -57,6 +59,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
 
   /**
+   * news reader - null if it was never started.
+   */
+  private BlogReader jvnews=null;
+
+  /**
    * @param listener
    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
    */
@@ -267,6 +274,18 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     showConsole(showjconsole);
 
+    showNews.setVisible(false);
+    final Desktop me = this;
+    // Thread off the news reader, in case there are connection problems.
+    new Thread( new Runnable() {
+      @Override
+      public void run()
+      {
+        jvnews = new BlogReader(me);
+        showNews.setVisible(true);
+      }
+    }).start();
+    
     this.addWindowListener(new WindowAdapter()
     {
       public void windowClosing(WindowEvent evt)
@@ -285,7 +304,24 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         }
       }
     });
-
+    this.addFocusListener(new FocusListener()
+    {
+      
+      @Override
+      public void focusLost(FocusEvent e)
+      {
+        // TODO Auto-generated method stub
+        
+      }
+      
+      @Override
+      public void focusGained(FocusEvent e)
+      {
+        Cache.log.debug("Relaying windows after focus gain");
+        // make sure that we sort windows properly after we gain focus
+        instance.relayerWindows();
+      }
+    });
     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
     // Spawn a thread that shows the splashscreen
     SwingUtilities.invokeLater(new Runnable()
@@ -328,6 +364,35 @@ public class Desktop extends jalview.jbgui.GDesktop implements
             });
   }
 
+  protected void showNews_actionPerformed(ActionEvent e)
+  {
+    showNews(showNews.isSelected());
+  }
+  void showNews(boolean visible)
+  {
+    {
+      jvnews.setVisible(visible);
+      showNews.setSelected(visible);
+      if (visible)
+      {
+        jvnews.refreshNews();
+        jvnews.toFront();
+        // try real hard to get focus when the news is first opened
+        new Thread(new Runnable() {
+          @Override
+          public void run()
+          {
+            while (jvnews.isVisible() && !jvnews.requestFocusInWindow())
+            {
+              try { Thread.sleep(50); } catch (InterruptedException x) {}; 
+            }
+            
+          }
+        }).start();
+      }
+    }
+  }
+
   /**
    * recover the last known dimensions for a jalview window
    * 
@@ -635,6 +700,10 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     } catch (java.beans.PropertyVetoException ve)
     {
     }
+    catch (java.lang.ClassCastException cex)
+    {
+      Cache.log.warn("Squashed a possible GUI implementation error. If you can recreate this, please look at http://issues.jalview.org/browse/JAL-869",cex);
+    }
   }
 
   public void lostOwnership(Clipboard clipboard, Transferable contents)
@@ -910,6 +979,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       storeLastKnownDimensions("JAVA_CONSOLE_", jconsole.getBounds());
       jconsole.stopConsole();
     }
+    if (jvnews!=null)
+    {
+      storeLastKnownDimensions("JALVIEW_RSS_WINDOW_", jvnews.getBounds());
+      
+    }
+      
     System.exit(0);
   }
 
@@ -942,7 +1017,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     {
       message.append("\n\n!! Jalview version "
               + jalview.bin.Cache.getProperty("LATEST_VERSION")
-              + " is available for download from http://www.jalview.org !!\n");
+              + " is available for download from "+jalview.bin.Cache.getDefault("www.jalview.org","http://www.jalview.org")+" !!\n");
 
     }
     // TODO: update this text for each release or centrally store it for lite
@@ -1178,11 +1253,18 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
-      java.io.File choice = chooser.getSelectedFile();
+      final Desktop me = this;
+      final java.io.File choice = chooser.getSelectedFile();
+      new Thread(new Runnable()
+      {
+        public void run()
+        {
+
       setProgressBar("Saving jalview project " + choice.getName(),
               choice.hashCode());
       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
       // TODO catch and handle errors for savestate
+      // TODO prevent user from messing with the Desktop whilst we're saving
       try
       {
         new Jalview2XML().SaveState(choice);
@@ -1195,12 +1277,13 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         Cache.log
                 .error("Problems whilst trying to save to "
                         + choice.getName(), ex);
-        JOptionPane.showMessageDialog(this,
+        JOptionPane.showMessageDialog(me,
                 "Error whilst saving current state to " + choice.getName(),
                 "Couldn't save project", JOptionPane.WARNING_MESSAGE);
       }
       setProgressBar(null, choice.hashCode());
-
+        }
+      }).start();
     }
   }
 
@@ -1258,44 +1341,47 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   }
 
   JPanel progressPanel;
-
+  ArrayList<JPanel> fileLoadingPanels=new ArrayList<JPanel>();
   public void startLoading(final String fileName)
   {
     if (fileLoadingCount == 0)
     {
-      addProgressPanel("Loading File: " + fileName + "   ");
-
+      fileLoadingPanels.add(addProgressPanel("Loading File: " + fileName + "   "));
     }
     fileLoadingCount++;
   }
 
-  private JProgressBar addProgressPanel(String string)
+  private JPanel addProgressPanel(String string)
   {
     if (progressPanel == null)
     {
-      progressPanel = new JPanel(new BorderLayout());
+      progressPanel = new JPanel(new GridLayout(1,1));
       totalProgressCount = 0;
+      instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
     }
+    JPanel thisprogress=new JPanel(new BorderLayout(10,5));
     JProgressBar progressBar = new JProgressBar();
     progressBar.setIndeterminate(true);
 
-    progressPanel.add(new JLabel(string), BorderLayout.WEST);
+    thisprogress.add(new JLabel(string), BorderLayout.WEST);
 
-    progressPanel.add(progressBar, BorderLayout.CENTER);
-
-    instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
-    totalProgressCount++;
-    validate();
-    return progressBar;
+    thisprogress.add(progressBar, BorderLayout.CENTER);
+    progressPanel.add(thisprogress);
+    ((GridLayout)progressPanel.getLayout()).setRows(((GridLayout)progressPanel.getLayout()).getRows()+1);
+    ++totalProgressCount;
+    instance.validate();
+    return thisprogress;
   }
 
   int totalProgressCount = 0;
 
-  private void removeProgressPanel(JProgressBar progbar)
+  private void removeProgressPanel(JPanel progbar)
   {
     if (progressPanel != null)
     {
       progressPanel.remove(progbar);
+      GridLayout gl = (GridLayout) progressPanel.getLayout();
+      gl.setRows(gl.getRows()-1);
       if (--totalProgressCount < 1)
       {
         this.getContentPane().remove(progressPanel);
@@ -1310,11 +1396,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     fileLoadingCount--;
     if (fileLoadingCount < 1)
     {
-      if (progressPanel != null)
+      for (JPanel flp : fileLoadingPanels)
       {
-        this.getContentPane().remove(progressPanel);
-        progressPanel = null;
+        removeProgressPanel(flp);
       }
+      fileLoadingPanels.clear();
       fileLoadingCount = 0;
     }
     validate();
@@ -1761,7 +1847,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       if (value == JalviewFileChooser.APPROVE_OPTION)
       {
         java.io.File choice = chooser.getSelectedFile();
-        JProgressBar progpanel = addProgressPanel("Saving VAMSAS Document to "
+        JPanel progpanel = addProgressPanel("Saving VAMSAS Document to "
                 + choice.getName());
         jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
         String warnmsg = null;
@@ -1795,7 +1881,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     }
   }
 
-  JProgressBar vamUpdate = null;
+  JPanel vamUpdate = null;
 
   /**
    * hide vamsas user gui bits when a vamsas document event is being handled.
@@ -1931,6 +2017,17 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
   }
 
+  /**
+   * fixes stacking order after a modal dialog to ensure windows that should be on top actually are
+   */
+  public void relayerWindows()
+  {
+    if (jvnews!=null && jvnews.isVisible())
+    {
+      jvnews.toFront();
+    }
+  }
+
   protected JMenuItem groovyShell;
 
   public void doGroovyCheck()
@@ -2067,7 +2164,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   /**
    * Progress bars managed by the IProgressIndicator method.
    */
-  private Hashtable progressBars, progressBarHandlers;
+  private Hashtable<Long,JPanel> progressBars;
+  private Hashtable<Long,IProgressIndicatorHandler> progressBarHandlers;
 
   /*
    * (non-Javadoc)
@@ -2078,13 +2176,13 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   {
     if (progressBars == null)
     {
-      progressBars = new Hashtable();
-      progressBarHandlers = new Hashtable();
+      progressBars = new Hashtable<Long,JPanel>();
+      progressBarHandlers = new Hashtable<Long,IProgressIndicatorHandler>();
     }
 
     if (progressBars.get(new Long(id)) != null)
     {
-      JProgressBar progressPanel = (JProgressBar) progressBars
+      JPanel progressPanel = progressBars
               .remove(new Long(id));
       if (progressBarHandlers.contains(new Long(id)))
       {
@@ -2205,7 +2303,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       // register PCS handler for desktop.
       discoverer.addPropertyChangeListener(changeSupport);
     }
-    if (Cache.getDefault("SHOW_JWS1_SERVICES", true))
+    // JAL-940 - disabled JWS1 service configuration - always start discoverer until we phase out completely
+    if (true)
     {
       (t0 = new Thread(discoverer)).start();
     }