ensure enfin service is started in background when desktop is displayed
[jalview.git] / src / jalview / gui / Desktop.java
index e00f993..5031f72 100755 (executable)
@@ -1,20 +1,19 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
- * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This file is part of Jalview.
  * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
 package jalview.gui;
 
@@ -71,7 +70,84 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   public static boolean internalCopy = false;
 
   static int fileLoadingCount = 0;
-
+  class MyDesktopManager implements DesktopManager {
+         
+          private DesktopManager delegate;
+        
+          public MyDesktopManager(DesktopManager delegate) {
+             this.delegate = delegate;
+          }
+        
+          public void activateFrame(JInternalFrame f) {
+             try {
+                delegate.activateFrame(f);
+             } catch (NullPointerException npe) {
+                 Point p = getMousePosition();
+                 instance.showPasteMenu(p.x,p.y);
+             }
+          }
+
+       public void beginDraggingFrame(JComponent f) {
+               delegate.beginDraggingFrame(f);
+       }
+
+       public void beginResizingFrame(JComponent f, int direction) {
+               delegate.beginResizingFrame(f, direction);
+       }
+
+       public void closeFrame(JInternalFrame f) {
+               delegate.closeFrame(f);
+       }
+
+       public void deactivateFrame(JInternalFrame f) {
+               delegate.deactivateFrame(f);
+       }
+
+       public void deiconifyFrame(JInternalFrame f) {
+               delegate.deiconifyFrame(f);
+       }
+
+       public void dragFrame(JComponent f, int newX, int newY) {
+               delegate.dragFrame(f, newX, newY);
+       }
+
+       public void endDraggingFrame(JComponent f) {
+               delegate.endDraggingFrame(f);
+       }
+
+       public void endResizingFrame(JComponent f) {
+               delegate.endResizingFrame(f);
+       }
+
+       public void iconifyFrame(JInternalFrame f) {
+               delegate.iconifyFrame(f);
+       }
+
+       public void maximizeFrame(JInternalFrame f) {
+               delegate.maximizeFrame(f);
+       }
+
+       public void minimizeFrame(JInternalFrame f) {
+               delegate.minimizeFrame(f);
+       }
+
+       public void openFrame(JInternalFrame f) {
+               delegate.openFrame(f);
+       }
+
+       public void resizeFrame(JComponent f, int newX, int newY, int newWidth,
+                       int newHeight) {
+               delegate.resizeFrame(f, newX, newY, newWidth, newHeight);
+       }
+
+       public void setBoundsForFrame(JComponent f, int newX, int newY,
+                       int newWidth, int newHeight) {
+               delegate.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
+       }
+        
+          // All other methods, simply delegate
+        
+       }
   /**
    * Creates a new Desktop object.
    */
@@ -101,11 +177,10 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     // This line prevents Windows Look&Feel resizing all new windows to maximum
     // if previous window was maximised
-    desktop.setDesktopManager(new DefaultDesktopManager());
-
+    desktop.setDesktopManager(new MyDesktopManager(new DefaultDesktopManager()));
     Rectangle dims = getLastKnownDimensions("");
-    if (dims!=null)
-    {      
+    if (dims != null)
+    {
       setBounds(dims);
     }
     else
@@ -114,16 +189,17 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       setBounds((int) (screenSize.width - 900) / 2,
               (int) (screenSize.height - 650) / 2, 900, 650);
     }
-    jconsole = new Console(this);
-    // immediately output essential build information
-    System.out.println("Jalview Desktop "+jalview.bin.Cache.getProperty("VERSION")+"\n"
-            +"Build Date: "
-            + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")+"\n"
-            + "Java version: " + System.getProperty("java.version")
-            + "\n"+System.getProperty("os.arch") + " "
+    jconsole = new Console(this, showjconsole);
+    // add essential build information
+    jconsole.setHeader("Jalview Desktop "
+            + jalview.bin.Cache.getProperty("VERSION") + "\n"
+            + "Build Date: "
+            + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown") + "\n"
+            + "Java version: " + System.getProperty("java.version") + "\n"
+            + System.getProperty("os.arch") + " "
             + System.getProperty("os.name") + " "
             + System.getProperty("os.version"));
-    
+
     showConsole(showjconsole);
 
     this.addWindowListener(new WindowAdapter()
@@ -157,10 +233,26 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     discoverer = new jalview.ws.Discoverer(); // Only gets started if gui is
     // displayed.
+    // Thread off a new instance of the file chooser - this reduces the time it
+    // takes to open it later on.
+    new Thread(new Runnable()
+    {
+      public void run()
+      {
+        Cache.log.debug("Filechooser init thread started.");
+        JalviewFileChooser chooser = new JalviewFileChooser(
+                jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
+                jalview.io.AppletFormatAdapter.READABLE_EXTENSIONS,
+                jalview.io.AppletFormatAdapter.READABLE_FNAMES,
+                jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
+        Cache.log.debug("Filechooser init thread finished.");
+      }
+    }).start();
   }
 
   /**
    * recover the last known dimensions for a jalview window
+   * 
    * @param windowName
    *          - empty string is desktop, all other windows have unique prefix
    * @return null or last known dimensions scaled to current geometry (if last
@@ -185,21 +277,22 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         // attempt #1 - try to cope with change in screen geometry - this
         // version doesn't preserve original jv aspect ratio.
         // take ratio of current screen size vs original screen size.
-        double sw = ( (1f*screenSize.width)
-                / (1f*Integer.parseInt(jalview.bin.Cache
-                        .getProperty("SCREENGEOMETRY_WIDTH"))))
-                        ;
-        double sh = ( (1f*screenSize.height)
-                / (1f*Integer.parseInt(jalview.bin.Cache
+        double sw = ((1f * screenSize.width) / (1f * Integer
+                .parseInt(jalview.bin.Cache
+                        .getProperty("SCREENGEOMETRY_WIDTH"))));
+        double sh = ((1f * screenSize.height) / (1f * Integer
+                .parseInt(jalview.bin.Cache
                         .getProperty("SCREENGEOMETRY_HEIGHT"))));
         // rescale the bounds depending upon the current screen geometry.
         ix = (int) (ix * sw);
         iw = (int) (iw * sw);
         iy = (int) (iy * sh);
         ih = (int) (ih * sh);
-        jalview.bin.Cache.log.debug("Got last known dimensions for "+windowName+": x:"+ix + " y:"+iy + " width:"+iw+" height:"+ih);
+        jalview.bin.Cache.log.debug("Got last known dimensions for "
+                + windowName + ": x:" + ix + " y:" + iy + " width:" + iw
+                + " height:" + ih);
       }
-      // return dimensions for new instance 
+      // return dimensions for new instance
       return new Rectangle(ix, iy, iw, ih);
     }
     return null;
@@ -285,7 +378,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   }
 
   /**
-   * Adds and opens the given frame to the desktop 
+   * Adds and opens the given frame to the desktop
    * 
    * @param frame
    *          DOCUMENT ME!
@@ -321,7 +414,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements
           boolean resizable)
   {
 
-    //TODO: allow callers to determine X and Y position of frame (eg. via bounds object).
+    // TODO: allow callers to determine X and Y position of frame (eg. via
+    // bounds object).
     // TODO: consider fixing method to update entries in the window submenu with
     // the current window title
 
@@ -678,11 +772,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
             .setProperty("SCREENGEOMETRY_WIDTH", screen.width + "");
     jalview.bin.Cache.setProperty("SCREENGEOMETRY_HEIGHT", screen.height
             + "");
-    storeLastKnownDimensions("",new Rectangle(getBounds().x,getBounds().y, getWidth(), getHeight()));
+    storeLastKnownDimensions("", new Rectangle(getBounds().x,
+            getBounds().y, getWidth(), getHeight()));
 
     if (jconsole != null)
     {
-      storeLastKnownDimensions("JAVA_CONSOLE_",jconsole.getBounds());
+      storeLastKnownDimensions("JAVA_CONSOLE_", jconsole.getBounds());
       jconsole.stopConsole();
     }
     System.exit(0);
@@ -690,13 +785,14 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
   private void storeLastKnownDimensions(String string, Rectangle jc)
   {
-    jalview.bin.Cache.log.debug("Storing last known dimensions for "+string+": x:"+jc.x + " y:"+jc.y + " width:"+jc.width+" height:"+jc.height);
-    
-    jalview.bin.Cache.setProperty(string+"SCREEN_X",jc.x + "");
-    jalview.bin.Cache.setProperty(string+"SCREEN_Y", jc.y + "");
-    jalview.bin.Cache.setProperty(string+"SCREEN_WIDTH", jc.width + "");
-    jalview.bin.Cache.setProperty(string+"SCREEN_HEIGHT", jc.height
-            + "");
+    jalview.bin.Cache.log.debug("Storing last known dimensions for "
+            + string + ": x:" + jc.x + " y:" + jc.y + " width:" + jc.width
+            + " height:" + jc.height);
+
+    jalview.bin.Cache.setProperty(string + "SCREEN_X", jc.x + "");
+    jalview.bin.Cache.setProperty(string + "SCREEN_Y", jc.y + "");
+    jalview.bin.Cache.setProperty(string + "SCREEN_WIDTH", jc.width + "");
+    jalview.bin.Cache.setProperty(string + "SCREEN_HEIGHT", jc.height + "");
   }
 
   /**
@@ -1661,11 +1757,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         if (percentUsage < 20)
           g.setColor(Color.red);
         FontMetrics fm = g.getFontMetrics();
-        if (fm!=null)
-        { g.drawString("Total Free Memory: " + df.format(totalFreeMemory)
-                + "MB; Max Memory: " + df.format(maxMemory) + "MB; "
-                + df.format(percentUsage) + "%", 10, getHeight()
-                - fm.getHeight());
+        if (fm != null)
+        {
+          g.drawString("Total Free Memory: " + df.format(totalFreeMemory)
+                  + "MB; Max Memory: " + df.format(maxMemory) + "MB; "
+                  + df.format(percentUsage) + "%", 10, getHeight()
+                  - fm.getHeight());
         }
       }
     }
@@ -1865,4 +1962,17 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     return v_client;
 
   }
+
+  public void startServiceDiscovery()
+  {
+    discoverer.start();
+    
+    try {
+      new Thread(jalview.ws.EnfinEnvision2OneWay.getInstance()).start();
+    } catch (Exception e)
+    {
+      Cache.log.info("Exception when trying to launch Envision2 workflow discovery.",e);
+      Cache.log.info(e.getStackTrace());
+    }
+  }
 }