DEFAULT_FILE_FORMAT added
[jalview.git] / src / jalview / gui / Desktop.java
index f92f01c..36183bb 100755 (executable)
@@ -15,9 +15,13 @@ import jalview.datamodel.*;
 import javax.swing.*;\r
 import java.awt.*;\r
 import java.awt.event.*;\r
+import java.awt.dnd.*;\r
+import javax.swing.*;\r
+import java.awt.datatransfer.*;\r
+import jalview.bin.Cache;\r
 \r
 \r
-public class Desktop extends jalview.jbgui.GDesktop\r
+public class Desktop extends jalview.jbgui.GDesktop implements DropTargetListener\r
 {\r
   public static JDesktopPane desktop;\r
   static int openFrameCount = 0;\r
@@ -25,11 +29,12 @@ public class Desktop extends jalview.jbgui.GDesktop
 \r
   public Desktop()\r
   {\r
+    Image image =null;\r
     try{\r
-      java.net.URL url = getClass().getResource("/BartonGroupBanner.gif");\r
+      java.net.URL url = getClass().getResource("/images/logo.gif");\r
       if(url!=null)\r
       {\r
-        Image image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
+        image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
         MediaTracker mt = new MediaTracker(this);\r
         mt.addImage(image, 0);\r
         mt.waitForID(0);\r
@@ -40,41 +45,93 @@ public class Desktop extends jalview.jbgui.GDesktop
 \r
     setTitle("Jalview 2005");\r
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r
-    setVisible(true);\r
     desktop = new JDesktopPane();\r
+    desktop.setBackground(Color.white);\r
     setContentPane(desktop);\r
     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);\r
-    int inset = 150;\r
+\r
+\r
+    // This line prevents Windows Look&Feel resizing all new windows to maximum\r
+    // if previous window was maximised\r
+    desktop.setDesktopManager( new DefaultDesktopManager() );\r
+\r
+\r
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();\r
-    setBounds(inset, inset,\r
-              screenSize.width  - inset*2,\r
-              screenSize.height - inset*2);\r
-  }\r
+    String x = jalview.bin.Cache.getProperty("SCREEN_X");\r
+    String y = jalview.bin.Cache.getProperty("SCREEN_Y");\r
+    String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");\r
+    String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");\r
 \r
+    if(x!=null && y!=null && width!=null && height!=null)\r
+    {\r
+      setBounds( Integer.parseInt(x), Integer.parseInt(y),\r
+                 Integer.parseInt(width), Integer.parseInt(height));\r
+    }\r
+    else\r
+      setBounds( (int)(screenSize.width-900)/2,\r
+           (int)(screenSize.height-650)/2,\r
+          900 ,\r
+          650);\r
+\r
+this.addWindowListener(new WindowAdapter()\r
+{\r
+public void windowClosing(WindowEvent evt)\r
+{\r
+ jalview.bin.Cache.setProperty("SCREEN_X",\r
+                               getBounds().x + "");\r
+ jalview.bin.Cache.setProperty("SCREEN_Y",\r
+                               getBounds().y + "");\r
+ jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");\r
+ jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");\r
+}\r
+});\r
+setVisible(true);\r
+\r
+this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));\r
+\r
+/////////Add a splashscreen on startup\r
+   /////////Add a splashscreen on startup\r
+    JInternalFrame frame = new JInternalFrame();\r
+\r
+    SplashScreen splash = new SplashScreen(frame, image);\r
+    frame.setContentPane(splash);\r
+    frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
+    addInternalFrame(frame, "", 750,160, false);\r
+    frame.setLocation( (int)((getWidth()-750) /2), (int)((getHeight()-160) /2));\r
+  }\r
   public static void addInternalFrame(final JInternalFrame frame, String title, int w, int h)\r
   {\r
-   frame.setVisible(true); //necessary as of 1.3\r
+    addInternalFrame(frame, title, w, h, true);\r
+  }\r
+\r
+  public static void addInternalFrame(final JInternalFrame frame, String title,\r
+                                      int w,\r
+                                      int h,\r
+                                      boolean resizable )\r
+  {\r
    desktop.add(frame);\r
    openFrameCount++;\r
    try {\r
        frame.setSelected(true);\r
    } catch (java.beans.PropertyVetoException e) {}\r
    frame.setTitle(title);\r
-   frame.setResizable(true);\r
    frame.setSize(w,h);\r
    frame.setClosable(true);\r
-   frame.setMaximizable(true);\r
-   frame.setIconifiable(true);\r
+   frame.setResizable(resizable);\r
+   frame.setMaximizable(resizable);\r
+   frame.setIconifiable(resizable);\r
+   frame.setFrameIcon(null);\r
    frame.setLocation(xOffset*openFrameCount, yOffset*openFrameCount);\r
    frame.toFront();\r
    final JMenuItem menuItem = new JMenuItem(title);\r
    frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
-                                   { public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)\r
-                                     {\r
-                                       openFrameCount--;\r
-                                       windowMenu.remove(menuItem);\r
-                                     };\r
-                                   });\r
+   {\r
+     public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)\r
+     {\r
+       openFrameCount--;\r
+       windowMenu.remove(menuItem);\r
+     };\r
+   });\r
 \r
    menuItem.addActionListener(new ActionListener()\r
     {\r
@@ -85,63 +142,129 @@ public class Desktop extends jalview.jbgui.GDesktop
       }\r
     });\r
 \r
+   frame.setVisible(true);\r
    windowMenu.add(menuItem);\r
  }\r
 \r
+ public void dragEnter(DropTargetDragEvent evt){}\r
+ public void dragExit(DropTargetEvent evt){}\r
+ public void dragOver(DropTargetDragEvent evt){}\r
+ public void dropActionChanged(DropTargetDragEvent evt){}\r
+ public void drop(DropTargetDropEvent evt)\r
+{\r
+   Transferable t = evt.getTransferable();\r
+   if(!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))\r
+     return;\r
+\r
+   evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);\r
+   try{\r
+     java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);\r
+\r
+     for (int i = 0; i < files.size(); i++)\r
+     {\r
+       String file = files.get(i).toString();\r
+       String protocol = "File";\r
+       if(file.endsWith(".jar"))\r
+                Jalview2XML.LoadJalviewAlign(file);\r
+      else\r
+      {\r
+        String format = jalview.io.IdentifyFile.Identify(file, protocol);\r
+        LoadFile(file, protocol, format);\r
+      }\r
+     }\r
+\r
+   }catch(Exception ex){ex.printStackTrace();}\r
+  }\r
 \r
 \r
  public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
  {\r
-   JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY);\r
+   JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
+       new String[]{"fa, fasta, fastq", "aln",  "pfam", "msf", "pir","blc","jar"},\r
+       new String[]{"Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"}\r
+       ,jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));\r
+\r
+   chooser.setFileView(new JalviewFileView());\r
    chooser.setDialogTitle("Open local file");\r
    chooser.setToolTipText("Open");\r
    int value = chooser.showOpenDialog(this);\r
-   if(value == JFileChooser.APPROVE_OPTION)\r
+   if(value == JalviewFileChooser.APPROVE_OPTION)\r
    {\r
+     jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", chooser.getSelectedFormat());\r
      String choice =  chooser.getSelectedFile().getPath();\r
-     jalview.bin.Cache.LAST_DIRECTORY = choice;\r
-     String format = IdentifyFile.Identify(choice, "File");\r
-     LoadFile(choice, "File", format);\r
+     jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser.getSelectedFile().getParent());\r
+     if(chooser.getSelectedFormat().equals("Jalview"))\r
+     {\r
+       Jalview2XML.LoadJalviewAlign(choice);\r
+     }\r
+     else\r
+     {\r
+       String format = IdentifyFile.Identify(choice, "File");\r
+       LoadFile(choice, "File", format);\r
+     }\r
    }\r
  }\r
 \r
  public void LoadFile(String file, String protocol,  String format)\r
  {\r
-   SequenceI [] sequences = null;\r
-\r
+   LoadingThread loader = new LoadingThread(file, protocol, format);\r
+   loader.start();\r
+ }\r
 \r
-   if (FormatProperties.contains(format))\r
-       sequences = FormatAdapter.read(file, protocol, format);\r
+ class LoadingThread extends Thread\r
+ {\r
+   String file, protocol, format;\r
 \r
-   if (sequences != null)\r
+   public LoadingThread(String file, String protocol,  String format)\r
    {\r
-     AlignFrame af = new AlignFrame(new Alignment(sequences));\r
-     addInternalFrame(af, file, 700, 500);\r
-     af.statusBar.setText("Successfully loaded file " + file);\r
+     this.file = file;\r
+     this.protocol = protocol;\r
+     this.format = format;\r
    }\r
-   else\r
-     JOptionPane.showMessageDialog(this,  "Couldn't open file.\n"\r
-                          + "Formats currently supported are\n"\r
-                          + "Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM"\r
-                          ,"Error loading file",\r
-                                   JOptionPane.WARNING_MESSAGE);\r
+   public void run()\r
+   {\r
+     SequenceI [] sequences = null;\r
+\r
+     if (FormatAdapter.formats.contains(format))\r
+         sequences = FormatAdapter.readFile(file, protocol, format);\r
+\r
+     if (sequences != null && sequences.length>0)\r
+     {\r
+       AlignFrame af = new AlignFrame(new Alignment(sequences));\r
+       addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH, AlignFrame.NEW_WINDOW_HEIGHT);\r
+       af.currentFileFormat = format;\r
+       af.statusBar.setText("Successfully loaded file " + file);\r
+       try{\r
+           af.setMaximum(Preferences.showFullscreen);\r
+       }catch(Exception ex){}\r
+\r
+     }\r
+     else\r
+       JOptionPane.showInternalMessageDialog(Desktop.desktop,  "Couldn't open file.\n"\r
+                            + "Formats currently supported are\n"\r
+                            + "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
+                            ,"Error loading file",\r
+                                     JOptionPane.WARNING_MESSAGE);\r
 \r
+   }\r
  }\r
 \r
+\r
  public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
  {\r
-   String url = JOptionPane.showInputDialog(this,"Enter url of input file",\r
+   String url = JOptionPane.showInternalInputDialog(Desktop.desktop,"Enter url of input file",\r
                                             "Input alignment from URL",\r
-                                            JOptionPane.QUESTION_MESSAGE);\r
+                                            JOptionPane.QUESTION_MESSAGE,\r
+                                            null, null,\r
+                                            "http://www.").toString();\r
    if (url == null)\r
      return;\r
 \r
    String format = IdentifyFile.Identify(url, "URL");\r
 \r
-   System.out.println(format +" format");\r
    if (format.equals("URL NOT FOUND"))\r
    {\r
-     JOptionPane.showMessageDialog(this,"Couldn't locate " + url,\r
+     JOptionPane.showInternalMessageDialog(Desktop.desktop,"Couldn't locate " + url,\r
                                    "URL not found",\r
                                    JOptionPane.WARNING_MESSAGE);\r
      return;\r
@@ -153,7 +276,7 @@ public class Desktop extends jalview.jbgui.GDesktop
  public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
  {\r
    CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
-   int accept =  JOptionPane.showInternalOptionDialog(desktop, cap, "Cut & paste Alignment File",\r
+   int accept =  JOptionPane.showInternalOptionDialog(Desktop.desktop, cap, "Cut & paste Alignment File",\r
                                        JOptionPane.YES_NO_CANCEL_OPTION,\r
                                        JOptionPane.PLAIN_MESSAGE,\r
                                        null,\r
@@ -165,23 +288,25 @@ public class Desktop extends jalview.jbgui.GDesktop
     String format = IdentifyFile.Identify(cap.getText(), "Paste");\r
     SequenceI [] sequences = null;\r
 \r
-    if (FormatProperties.contains( format ))\r
-      sequences = FormatAdapter.read(cap.getText(), "Paste", format);\r
+    if (FormatAdapter.formats.contains( format ))\r
+      sequences = FormatAdapter.readFile(cap.getText(), "Paste", format);\r
 \r
 \r
       if(sequences!=null)\r
       {\r
         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
-        addInternalFrame(af, "Cut & Paste input - "+format, 700, 500);\r
+        af.currentFileFormat = format;\r
+        addInternalFrame(af, "Cut & Paste input - "+format,\r
+                         AlignFrame.NEW_WINDOW_WIDTH,\r
+                         AlignFrame.NEW_WINDOW_HEIGHT);\r
         af.statusBar.setText("Successfully pasted alignment file");\r
       }\r
       else\r
-        JOptionPane.showMessageDialog(this, "Couldn't read the pasted text.\n"\r
+        JOptionPane.showInternalMessageDialog(Desktop.desktop, "Couldn't read the pasted text.\n"\r
                                       +"Formats currently supported are\n"\r
-                                      +"Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM",\r
+                                      +"Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM",\r
                                       "Error parsing text", JOptionPane.WARNING_MESSAGE);\r
 \r
-\r
    }\r
  }\r
 \r
@@ -190,33 +315,86 @@ public class Desktop extends jalview.jbgui.GDesktop
 */\r
  public void quit_actionPerformed(ActionEvent e)\r
  {\r
-   this.setVisible(false);\r
-   System.exit(0);\r
+   if(jalview.bin.Jalview.applet!=null)\r
+     jalview.bin.Jalview.applet.destroy();\r
+    else\r
+      System.exit(0);\r
  }\r
 \r
 \r
- protected void colourIndexMenuItem_actionPerformed(ActionEvent e)\r
- {\r
-   ColourKey colourKey = new ColourKey(null);\r
-   addInternalFrame(colourKey, "Colour scheme key", 400, 320);\r
- }\r
-\r
 \r
  public void aboutMenuItem_actionPerformed(ActionEvent e)\r
  {\r
-   JOptionPane.showInternalMessageDialog(desktop,\r
-                                         "Jalview 1998-2005\n"\r
-                                        +"Sequence Alignment viewer & editor",\r
+   JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                                         "JalView 2005 version " + jalview.bin.Cache.VERSION+"; last updated: "+jalview.bin.Cache.BUILD_DATE\r
+                                       +"\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton."\r
+                                       +"\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee."\r
+                                       +"\nIf  you use JalView, please cite:"\r
+                                       +"\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\""\r
+                                       +"\nBioinformatics,  2004 12;426-7.",\r
                                          "About Jalview",\r
                                          JOptionPane.INFORMATION_MESSAGE);\r
  }\r
 \r
  public void documentationMenuItem_actionPerformed(ActionEvent e)\r
  {\r
-   try{\r
-     jalview.util.BrowserLauncher.openURL("http://jalview.org/documentation.html");\r
-   }catch(Exception ex){}\r
+   try\r
+   {\r
+      ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
+      java.net.URL url = javax.help.HelpSet.findHelpSet(cl,"help/help");\r
+      javax.help.HelpSet hs = new javax.help.HelpSet(cl,url);\r
+\r
+      javax.help.HelpBroker hb = hs.createHelpBroker();\r
+      hb.setLocation(new Point(200, 50));\r
+      hb.setSize(new Dimension(800,700));\r
+      hb.setCurrentID("home");\r
+      hb.setDisplayed(true);\r
+   }\r
+   catch (Exception ex)\r
+   {\r
+     ex.printStackTrace();\r
+   }\r
+ }\r
+\r
+ protected void preferences_actionPerformed(ActionEvent e)\r
+ {\r
+   Preferences pref = new Preferences();\r
+ }\r
+\r
+ public void saveState_actionPerformed(ActionEvent e)\r
+ {\r
+   JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
+       getProperty("LAST_DIRECTORY"),\r
+       new String[]\r
+       {"xml"}, new String[]{"Jalview Project"}, "Jalview Project");\r
+\r
+   chooser.setFileView(new JalviewFileView());\r
+   chooser.setDialogTitle("Save State");\r
+   int value = chooser.showSaveDialog(this);\r
+   if (value == JalviewFileChooser.APPROVE_OPTION)\r
+   {\r
+     java.io.File choice = chooser.getSelectedFile();\r
+     jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
+     Jalview2XML.SaveState(choice);\r
+   }\r
+\r
  }\r
 \r
 \r
+ public void loadState_actionPerformed(ActionEvent e)\r
+ {\r
+   JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
+       new String[]{"xml"}, new String[]{"Jalview Project"}, "Jalview Project");\r
+   chooser.setFileView(new JalviewFileView());\r
+   chooser.setDialogTitle("Restore state");\r
+   int value = chooser.showOpenDialog(this);\r
+   if(value == JalviewFileChooser.APPROVE_OPTION)\r
+   {\r
+     String choice =  chooser.getSelectedFile().getAbsolutePath();\r
+     jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser.getSelectedFile().getParent());\r
+     Jalview2XML.LoadState(choice);\r
+   }\r
+ }\r
 }\r
+\r
+\r