ensure input data can be recovered for tree and PCA even if alignment view was closed
[jalview.git] / src / jalview / gui / Desktop.java
index f61e7a8..2be8770 100755 (executable)
@@ -19,6 +19,7 @@
 package jalview.gui;
 
 import jalview.io.*;
+
 import java.awt.*;
 import java.awt.datatransfer.*;
 import java.awt.dnd.*;
@@ -38,15 +39,15 @@ import javax.swing.event.MenuListener;
  */
 public class Desktop
     extends jalview.jbgui.GDesktop implements DropTargetListener,
-    ClipboardOwner
+    ClipboardOwner, IProgressIndicator
 {
   /** DOCUMENT ME!! */
   public static Desktop instance;
 
   //Need to decide if the Memory Usage is to be included in
   //Next release or not.
- // public static MyDesktopPane desktop;
-   public static JDesktopPane desktop;
+  public static MyDesktopPane desktop;
+  // public static JDesktopPane desktop;
 
 
   static int openFrameCount = 0;
@@ -64,6 +65,11 @@ public class Desktop
    */
   public Desktop()
   {
+    /**
+     * A note to implementors. It is ESSENTIAL that any 
+     * activities that might block are spawned off as threads rather 
+     * than waited for during this constructor.
+     */
     instance = this;
     doVamsasClientCheck();
     doGroovyCheck();
@@ -71,8 +77,9 @@ public class Desktop
 
     setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-    desktop = new JDesktopPane();
+    boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",false);
+    desktop = new MyDesktopPane(selmemusage);
+    showMemusage.setSelected(selmemusage);
     desktop.setBackground(Color.white);
     getContentPane().setLayout(new BorderLayout());
     getContentPane().add(desktop, BorderLayout.CENTER);
@@ -120,10 +127,9 @@ public class Desktop
 
 
     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
+    // Spawn a thread that shows the splashscreen
+    new SplashScreen();     
 
-    /////////Add a splashscreen on startup
-    /////////Add a splashscreen on startup
-    new SplashScreen();
 
 
     discoverer = new jalview.ws.Discoverer(); // Only gets started if gui is displayed.
@@ -432,15 +438,9 @@ public class Desktop
     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
         getProperty(
             "LAST_DIRECTORY"),
-        new String[]
-        {
-        "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
-        "jar"
-    },
-        new String[]
-        {
-        "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
-    }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
+            jalview.io.AppletFormatAdapter.READABLE_EXTENSIONS,
+            jalview.io.AppletFormatAdapter.READABLE_FNAMES, 
+            jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
 
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle("Open local file");
@@ -669,6 +669,25 @@ public class Desktop
     reorderAssociatedWindows(false, true);
   }
 
+  /* (non-Javadoc)
+   * @see jalview.jbgui.GDesktop#garbageCollect_actionPerformed(java.awt.event.ActionEvent)
+   */
+  protected void garbageCollect_actionPerformed(ActionEvent e)
+  {
+    // We simply collect the garbage
+    jalview.bin.Cache.log.debug("Collecting garbage...");
+    System.gc();
+    jalview.bin.Cache.log.debug("Finished garbage collection.");
+  }
+
+  /* (non-Javadoc)
+   * @see jalview.jbgui.GDesktop#showMemusage_actionPerformed(java.awt.event.ActionEvent)
+   */
+  protected void showMemusage_actionPerformed(ActionEvent e)
+  {
+    desktop.showMemoryUsage(showMemusage.isSelected());
+  }
+
   void reorderAssociatedWindows(boolean minimize, boolean close)
   {
     JInternalFrame[] frames = desktop.getAllFrames();
@@ -745,7 +764,6 @@ public class Desktop
       }
     }
   }
-
   /**
    * DOCUMENT ME!
    *
@@ -812,7 +830,7 @@ public class Desktop
 
   public void inputSequence_actionPerformed(ActionEvent e)
   {
-    new SequenceFetcher(null);
+    new SequenceFetcher(this);
   }
 
   JPanel progressPanel;
@@ -821,21 +839,45 @@ public class Desktop
   {
     if (fileLoadingCount == 0)
     {
+      addProgressPanel("Loading File: " + fileName + "   ");
+      
+    }
+    fileLoadingCount++;
+  }
+  private JProgressBar addProgressPanel(String string)
+  {
+    if (progressPanel==null)
+    {
       progressPanel = new JPanel(new BorderLayout());
-      JProgressBar progressBar = new JProgressBar();
-      progressBar.setIndeterminate(true);
+      totalProgressCount=0;
+    }
+    JProgressBar progressBar = new JProgressBar();
+    progressBar.setIndeterminate(true);
 
-      progressPanel.add(new JLabel("Loading File: " + fileName + "   "),
-                        BorderLayout.WEST);
+    progressPanel.add(new JLabel(string),
+                      BorderLayout.WEST);
 
-      progressPanel.add(progressBar, BorderLayout.CENTER);
+    progressPanel.add(progressBar, BorderLayout.CENTER);
 
-      instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
+    instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
+    totalProgressCount++;
+    validate();
+    return progressBar;
+  }
+  int totalProgressCount=0;
+  private void removeProgressPanel(JProgressBar progbar)
+  {
+    if (progressPanel!=null)
+    {
+      progressPanel.remove(progbar);
+      if (--totalProgressCount<1)
+      {
+        this.getContentPane().remove(progressPanel);
+        progressPanel = null;
+      }
     }
-    fileLoadingCount++;
     validate();
   }
-
   public void stopLoading()
   {
     fileLoadingCount--;
@@ -850,7 +892,6 @@ public class Desktop
     }
     validate();
   }
-
   public static int getViewCount(String viewId)
   {
     int count = 0;
@@ -1019,7 +1060,7 @@ public class Desktop
     vamsasSave.setVisible(false);
     vamsasStop.setVisible(false);
     vamsasImport.setVisible(true);
-    vamsasStart.setText("New Vamsas Session...");
+    vamsasStart.setText("New Vamsas Session");
   }
 
   public void vamsasStop_actionPerformed(ActionEvent e)
@@ -1064,15 +1105,25 @@ public class Desktop
             {
               if (dsktp.v_client == null)
               {
-                dsktp.v_client = new VamsasApplication(dsktp, mysesid);
-                dsktp.setupVamsasConnectedGui();
-                dsktp.v_client.initial_update();
+                Thread rthr = new Thread(new Runnable() {
+
+                  public void run()
+                  {
+                    dsktp.v_client = new VamsasApplication(dsktp, mysesid);
+                    dsktp.setupVamsasConnectedGui();
+                    dsktp.v_client.initial_update();
+                  }
+                  
+                });
+                rthr.start();
               }
             };
           });
           VamsasStMenu.add(sessit);
         }
-        VamsasStMenu.setVisible(true);
+        // don't show an empty menu.
+        VamsasStMenu.setVisible(sess.length>0);
+        
       }
       else
       {
@@ -1134,6 +1185,7 @@ public class Desktop
         }
     }
   }
+  JProgressBar vamUpdate = null;
   /**
    * hide vamsas user gui bits when a vamsas document event is being handled.
    * @param b true to hide gui, false to reveal gui
@@ -1142,6 +1194,15 @@ public class Desktop
   {
     jalview.bin.Cache.log.debug("Setting gui for Vamsas update " +
                                 (b ? "in progress" : "finished"));
+    
+    if (vamUpdate!=null)
+    {
+      this.removeProgressPanel(vamUpdate);
+    }
+    if (b)
+    {
+      vamUpdate = this.addProgressPanel("Updating vamsas session");
+    }
     vamsasStart.setVisible(!b);
     vamsasStop.setVisible(!b);
     vamsasSave.setVisible(!b);
@@ -1161,12 +1222,18 @@ public class Desktop
   public void checkForQuestionnaire(String url)
   {
     UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
-    javax.swing.SwingUtilities.invokeLater(jvq);
+    //javax.swing.SwingUtilities.invokeLater(jvq);
+    new Thread(jvq).start();
   }
-
-  /*DISABLED
-   class  MyDesktopPane extends JDesktopPane implements Runnable
+  /**
+   * Proxy class for JDesktopPane which optionally 
+   * displays the current memory usage and highlights 
+   * the desktop area with a red bar if free memory runs low.
+   * @author AMW
+   */
+  public class  MyDesktopPane extends JDesktopPane implements Runnable
   {
+    
     boolean showMemoryUsage = false;
     Runtime runtime;
     java.text.NumberFormat df;
@@ -1187,7 +1254,10 @@ public class Desktop
         worker.start();
       }
     }
-
+    public boolean isShowMemoryUsage()
+    {
+      return showMemoryUsage;
+    }
     public void run()
     {
       df = java.text.NumberFormat.getNumberInstance();
@@ -1234,13 +1304,16 @@ public class Desktop
                      getHeight() - g.getFontMetrics().getHeight());
       }
     }
-  }*/
+
+    
+    
+  }
   protected JMenuItem groovyShell;
   public void doGroovyCheck() {
     if (jalview.bin.Cache.groovyJarsPresent())
     {
       groovyShell = new JMenuItem();
-      groovyShell.setText("Groovy Shell...");
+      groovyShell.setText("Groovy Console...");
       groovyShell.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e) {
@@ -1255,7 +1328,7 @@ public class Desktop
    * Accessor method to quickly get all the AlignmentFrames
    * loaded.
    */
-  protected AlignFrame[] getAlignframes() {
+  public static AlignFrame[] getAlignframes() {
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
     if (frames == null)
@@ -1318,4 +1391,27 @@ public class Desktop
               JOptionPane.ERROR_MESSAGE);
     }
   }
+
+  /**
+   *  Progress bars managed by the IProgressIndicator method.
+   */
+  private Hashtable progressBars;
+  /* (non-Javadoc)
+   * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
+   */
+  public void setProgressBar(String message, long id)
+  {
+    if(progressBars == null)
+    {
+      progressBars = new Hashtable();
+    }
+
+    if(progressBars.get( new Long(id) )!=null)
+    {
+      JProgressBar progressPanel = (JProgressBar)progressBars.remove( new Long(id) );
+       removeProgressPanel(progressPanel);
+    } else {
+      progressBars.put(new Long(id), addProgressPanel(message));
+    }
+  }
 }