-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
- */\r
-\r
-package jalview.io;\r
-\r
-import java.util.*;\r
-\r
-import javax.swing.*;\r
-\r
-import jalview.datamodel.*;\r
-import jalview.gui.*;\r
-\r
-public class FileLoader\r
- implements Runnable\r
-{\r
- String file;\r
- String protocol;\r
- String format;\r
- AlignViewport viewport;\r
- AlignFrame alignFrame;\r
-\r
- public void LoadFile(AlignViewport viewport, String file, String protocol,\r
- String format)\r
- {\r
- this.viewport = viewport;\r
- LoadFile(file, protocol, format);\r
- }\r
-\r
- public void LoadFile(String file, String protocol, String format)\r
- {\r
- this.file = file;\r
- this.protocol = protocol;\r
- this.format = format;\r
-\r
- final Thread loader = new Thread(this);\r
-\r
- SwingUtilities.invokeLater(new Runnable()\r
- {\r
- public void run()\r
- {\r
- loader.start();\r
- }\r
- });\r
- }\r
-\r
- public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,\r
- String format)\r
- {\r
- this.file = file;\r
- this.protocol = protocol;\r
- this.format = format;\r
-\r
- Thread loader = new Thread(this);\r
- loader.start();\r
-\r
- while (loader.isAlive())\r
- {\r
- try\r
- {\r
- Thread.sleep(500);\r
- }\r
- catch (Exception ex)\r
- {}\r
- }\r
-\r
- return alignFrame;\r
- }\r
-\r
- public void updateRecentlyOpened()\r
- {\r
- Vector recent = new Vector();\r
-\r
- String type = protocol.equals(FormatAdapter.FILE)\r
- ? "RECENT_FILE" : "RECENT_URL";\r
-\r
- String historyItems = jalview.bin.Cache.getProperty(type);\r
-\r
- StringTokenizer st;\r
-\r
- if (historyItems != null)\r
- {\r
- st = new StringTokenizer(historyItems, "\t");\r
-\r
- while (st.hasMoreTokens())\r
- {\r
- recent.addElement(st.nextElement().toString().trim());\r
- }\r
- }\r
-\r
- if (recent.contains(file))\r
- {\r
- recent.remove(file);\r
- }\r
-\r
- StringBuffer newHistory = new StringBuffer(file);\r
- for (int i = 0; i < recent.size() && i < 10; i++)\r
- {\r
- newHistory.append("\t");\r
- newHistory.append(recent.elementAt(i));\r
- }\r
-\r
- jalview.bin.Cache.setProperty(type, newHistory.toString());\r
-\r
- if (protocol.equals(FormatAdapter.FILE))\r
- {\r
- jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
- }\r
- }\r
-\r
- public void run()\r
- {\r
- String title = protocol.equals(AppletFormatAdapter.PASTE)\r
- ? "Copied From Clipboard" : file;\r
-\r
- try\r
- {\r
- if (Desktop.instance != null)\r
- {\r
- Desktop.instance.startLoading(file);\r
- }\r
-\r
- Alignment al = null;\r
-\r
- if (format.equalsIgnoreCase("Jalview"))\r
- {\r
- alignFrame = new Jalview2XML().LoadJalviewAlign(file);\r
- }\r
- else\r
- {\r
- String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
-\r
- if (FormatAdapter.isValidFormat(format))\r
- {\r
- try\r
- {\r
- al = new FormatAdapter().readFile(file, protocol, format);\r
- }\r
- catch (java.io.IOException ex)\r
- {\r
- error = ex.getMessage();\r
- }\r
- }\r
-\r
- if ( (al != null) && (al.getHeight() > 0))\r
- {\r
- if (viewport != null)\r
- {\r
- for (int i = 0; i < al.getHeight(); i++)\r
- {\r
- viewport.getAlignment().addSequence(al.getSequenceAt(i));\r
- }\r
- viewport.firePropertyChange("alignment", null,\r
- viewport.getAlignment().getSequences());\r
-\r
- }\r
- else\r
- {\r
- alignFrame = new AlignFrame(al,\r
- AlignFrame.DEFAULT_WIDTH,\r
- AlignFrame.DEFAULT_HEIGHT);\r
-\r
- alignFrame.statusBar.setText("Successfully loaded file " + title);\r
-\r
- if (!protocol.equals(AppletFormatAdapter.PASTE))\r
- alignFrame.setFileName(file, format);\r
-\r
- Desktop.addInternalFrame(alignFrame, title,\r
- AlignFrame.DEFAULT_WIDTH,\r
- AlignFrame.DEFAULT_HEIGHT);\r
-\r
- try\r
- {\r
- alignFrame.setMaximum(jalview.bin.Cache.getDefault(\r
- "SHOW_FULLSCREEN", false));\r
- }\r
- catch (java.beans.PropertyVetoException ex)\r
- {\r
- }\r
- }\r
- }\r
- else\r
- {\r
- if (Desktop.instance != null)\r
- {\r
- Desktop.instance.stopLoading();\r
- }\r
-\r
- final String errorMessage = "Couldn't load file " + title + "\n" +\r
- error;\r
-\r
- javax.swing.SwingUtilities.invokeLater(new Runnable()\r
- {\r
- public void run()\r
- {\r
- JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
- errorMessage,\r
- "Error loading file",\r
- JOptionPane.WARNING_MESSAGE);\r
- }\r
- });\r
- }\r
- }\r
-\r
- updateRecentlyOpened();\r
-\r
- }\r
- catch (OutOfMemoryError er)\r
- {\r
-\r
- er.printStackTrace();\r
- alignFrame = null;\r
-\r
- javax.swing.SwingUtilities.invokeLater(new Runnable()\r
- {\r
- public void run()\r
- {\r
- javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
- "Out of memory loading file " + file + "!!"\r
- +\r
- "\nSee help files for increasing Java Virtual Machine memory."\r
- , "Out of memory",\r
- javax.swing.JOptionPane.WARNING_MESSAGE);\r
- }\r
- });\r
- }\r
-\r
- System.gc();\r
- if (Desktop.instance != null)\r
- {\r
- Desktop.instance.stopLoading();\r
- }\r
-\r
- }\r
-\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2007 AM Waterhouse, J Procter, 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 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.
+ *
+ * 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
+ */
+
+package jalview.io;
+
+import java.util.*;
+
+import javax.swing.*;
+
+import jalview.datamodel.*;
+import jalview.gui.*;
+
+public class FileLoader
+ implements Runnable
+{
+ String file;
+ String protocol;
+ String format;
+ AlignViewport viewport;
+ AlignFrame alignFrame;
+
+ public void LoadFile(AlignViewport viewport, String file, String protocol,
+ String format)
+ {
+ this.viewport = viewport;
+ LoadFile(file, protocol, format);
+ }
+
+ public void LoadFile(String file, String protocol, String format)
+ {
+ this.file = file;
+ this.protocol = protocol;
+ this.format = format;
+
+ final Thread loader = new Thread(this);
+
+ SwingUtilities.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ loader.start();
+ }
+ });
+ }
+
+ public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
+ String format)
+ {
+ this.file = file;
+ this.protocol = protocol;
+ this.format = format;
+
+ Thread loader = new Thread(this);
+ loader.start();
+
+ while (loader.isAlive())
+ {
+ try
+ {
+ Thread.sleep(500);
+ }
+ catch (Exception ex)
+ {}
+ }
+
+ return alignFrame;
+ }
+
+ public void updateRecentlyOpened()
+ {
+ Vector recent = new Vector();
+
+ String type = protocol.equals(FormatAdapter.FILE)
+ ? "RECENT_FILE" : "RECENT_URL";
+
+ String historyItems = jalview.bin.Cache.getProperty(type);
+
+ StringTokenizer st;
+
+ if (historyItems != null)
+ {
+ st = new StringTokenizer(historyItems, "\t");
+
+ while (st.hasMoreTokens())
+ {
+ recent.addElement(st.nextElement().toString().trim());
+ }
+ }
+
+ if (recent.contains(file))
+ {
+ recent.remove(file);
+ }
+
+ StringBuffer newHistory = new StringBuffer(file);
+ for (int i = 0; i < recent.size() && i < 10; i++)
+ {
+ newHistory.append("\t");
+ newHistory.append(recent.elementAt(i));
+ }
+
+ jalview.bin.Cache.setProperty(type, newHistory.toString());
+
+ if (protocol.equals(FormatAdapter.FILE))
+ {
+ jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
+ }
+ }
+
+ public void run()
+ {
+ String title = protocol.equals(AppletFormatAdapter.PASTE)
+ ? "Copied From Clipboard" : file;
+
+ try
+ {
+ if (Desktop.instance != null)
+ {
+ Desktop.instance.startLoading(file);
+ }
+
+ Alignment al = null;
+
+ if (format.equalsIgnoreCase("Jalview"))
+ {
+ alignFrame = new Jalview2XML().LoadJalviewAlign(file);
+ }
+ else
+ {
+ String error = AppletFormatAdapter.SUPPORTED_FORMATS;
+
+ if (FormatAdapter.isValidFormat(format))
+ {
+ try
+ {
+ al = new FormatAdapter().readFile(file, protocol, format);
+ }
+ catch (java.io.IOException ex)
+ {
+ error = ex.getMessage();
+ }
+ } else {
+ if (format!=null && format.length()>7)
+ {
+ // ad hoc message in format.
+ error = format +"\n"+error;
+ }
+ }
+
+ if ( (al != null) && (al.getHeight() > 0))
+ {
+ if (viewport != null)
+ {
+ for (int i = 0; i < al.getHeight(); i++)
+ {
+ viewport.getAlignment().addSequence(al.getSequenceAt(i));
+ }
+ viewport.firePropertyChange("alignment", null,
+ viewport.getAlignment().getSequences());
+
+ }
+ else
+ {
+ alignFrame = new AlignFrame(al,
+ AlignFrame.DEFAULT_WIDTH,
+ AlignFrame.DEFAULT_HEIGHT);
+
+ alignFrame.statusBar.setText("Successfully loaded file " + title);
+
+ if (!protocol.equals(AppletFormatAdapter.PASTE))
+ alignFrame.setFileName(file, format);
+
+ Desktop.addInternalFrame(alignFrame, title,
+ AlignFrame.DEFAULT_WIDTH,
+ AlignFrame.DEFAULT_HEIGHT);
+
+ try
+ {
+ alignFrame.setMaximum(jalview.bin.Cache.getDefault(
+ "SHOW_FULLSCREEN", false));
+ }
+ catch (java.beans.PropertyVetoException ex)
+ {
+ }
+ }
+ }
+ else
+ {
+ if (Desktop.instance != null)
+ {
+ Desktop.instance.stopLoading();
+ }
+
+ final String errorMessage = "Couldn't load file " + title + "\n" +
+ error;
+
+ javax.swing.SwingUtilities.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ JOptionPane.showInternalMessageDialog(Desktop.desktop,
+ errorMessage,
+ "Error loading file",
+ JOptionPane.WARNING_MESSAGE);
+ }
+ });
+ }
+ }
+
+ updateRecentlyOpened();
+
+ }
+ catch (OutOfMemoryError er)
+ {
+
+ er.printStackTrace();
+ alignFrame = null;
+
+ javax.swing.SwingUtilities.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
+ "Out of memory loading file " + file + "!!"
+ +
+ "\nSee help files for increasing Java Virtual Machine memory."
+ , "Out of memory",
+ javax.swing.JOptionPane.WARNING_MESSAGE);
+ }
+ });
+ }
+
+ System.gc();
+ if (Desktop.instance != null)
+ {
+ Desktop.instance.stopLoading();
+ }
+
+ }
+
+}