2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.datatransfer.*;
25 import java.awt.dnd.*;
26 import java.awt.event.*;
27 import java.lang.reflect.Constructor;
31 import javax.swing.event.MenuEvent;
32 import javax.swing.event.MenuListener;
40 public class Desktop extends jalview.jbgui.GDesktop implements
41 DropTargetListener, ClipboardOwner, IProgressIndicator
44 public static Desktop instance;
46 // Need to decide if the Memory Usage is to be included in
47 // Next release or not.
48 public static MyDesktopPane desktop;
50 // public static JDesktopPane desktop;
52 static int openFrameCount = 0;
54 static final int xOffset = 30;
56 static final int yOffset = 30;
58 public static jalview.ws.Discoverer discoverer;
60 public static Object[] jalviewClipboard;
62 public static boolean internalCopy = false;
64 static int fileLoadingCount = 0;
67 * Creates a new Desktop object.
72 * A note to implementors. It is ESSENTIAL that any activities that might
73 * block are spawned off as threads rather than waited for during this
77 doVamsasClientCheck();
80 setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
81 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82 boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",
84 desktop = new MyDesktopPane(selmemusage);
85 showMemusage.setSelected(selmemusage);
86 desktop.setBackground(Color.white);
87 getContentPane().setLayout(new BorderLayout());
88 getContentPane().add(desktop, BorderLayout.CENTER);
89 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
91 // This line prevents Windows Look&Feel resizing all new windows to maximum
92 // if previous window was maximised
93 desktop.setDesktopManager(new DefaultDesktopManager());
95 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
96 String x = jalview.bin.Cache.getProperty("SCREEN_X");
97 String y = jalview.bin.Cache.getProperty("SCREEN_Y");
98 String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");
99 String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");
101 if ((x != null) && (y != null) && (width != null) && (height != null))
103 setBounds(Integer.parseInt(x), Integer.parseInt(y), Integer
104 .parseInt(width), Integer.parseInt(height));
108 setBounds((int) (screenSize.width - 900) / 2,
109 (int) (screenSize.height - 650) / 2, 900, 650);
112 this.addWindowListener(new WindowAdapter()
114 public void windowClosing(WindowEvent evt)
120 this.addMouseListener(new MouseAdapter()
122 public void mousePressed(MouseEvent evt)
124 if (SwingUtilities.isRightMouseButton(evt))
126 showPasteMenu(evt.getX(), evt.getY());
131 this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
132 // Spawn a thread that shows the splashscreen
135 discoverer = new jalview.ws.Discoverer(); // Only gets started if gui is
139 private void doVamsasClientCheck()
141 if (jalview.bin.Cache.vamsasJarsPresent())
143 setupVamsasDisconnectedGui();
144 VamsasMenu.setVisible(true);
145 final Desktop us = this;
146 VamsasMenu.addMenuListener(new MenuListener()
148 // this listener remembers when the menu was first selected, and
149 // doesn't rebuild the session list until it has been cleared and
151 boolean refresh = true;
153 public void menuCanceled(MenuEvent e)
158 public void menuDeselected(MenuEvent e)
163 public void menuSelected(MenuEvent e)
167 us.buildVamsasStMenu();
172 vamsasStart.setVisible(true);
176 void showPasteMenu(int x, int y)
178 JPopupMenu popup = new JPopupMenu();
179 JMenuItem item = new JMenuItem("Paste To New Window");
180 item.addActionListener(new ActionListener()
182 public void actionPerformed(ActionEvent evt)
189 popup.show(this, x, y);
196 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
197 Transferable contents = c.getContents(this);
199 if (contents != null)
201 String file = (String) contents
202 .getTransferData(DataFlavor.stringFlavor);
204 String format = new IdentifyFile().Identify(file,
205 FormatAdapter.PASTE);
207 new FileLoader().LoadFile(file, FormatAdapter.PASTE, format);
210 } catch (Exception ex)
213 .println("Unable to paste alignment from system clipboard:\n"
230 public static synchronized void addInternalFrame(
231 final JInternalFrame frame, String title, int w, int h)
233 addInternalFrame(frame, title, w, h, true);
250 public static synchronized void addInternalFrame(
251 final JInternalFrame frame, String title, int w, int h,
255 // TODO: consider fixing method to update entries in the window submenu with
256 // the current window title
258 frame.setTitle(title);
259 if (frame.getWidth() < 1 || frame.getHeight() < 1)
263 // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
264 // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
265 // IF JALVIEW IS RUNNING HEADLESS
266 // ///////////////////////////////////////////////
267 if (System.getProperty("java.awt.headless") != null
268 && System.getProperty("java.awt.headless").equals("true"))
275 frame.setVisible(true);
276 frame.setClosable(true);
277 frame.setResizable(resizable);
278 frame.setMaximizable(resizable);
279 frame.setIconifiable(resizable);
280 frame.setFrameIcon(null);
282 if (frame.getX() < 1 && frame.getY() < 1)
284 frame.setLocation(xOffset * openFrameCount, yOffset
285 * ((openFrameCount - 1) % 10) + yOffset);
288 final JMenuItem menuItem = new JMenuItem(title);
290 .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
292 public void internalFrameActivated(
293 javax.swing.event.InternalFrameEvent evt)
295 JInternalFrame itf = desktop.getSelectedFrame();
303 public void internalFrameClosed(
304 javax.swing.event.InternalFrameEvent evt)
306 PaintRefresher.RemoveComponent(frame);
308 windowMenu.remove(menuItem);
309 JInternalFrame itf = desktop.getSelectedFrame();
318 menuItem.addActionListener(new ActionListener()
320 public void actionPerformed(ActionEvent e)
324 frame.setSelected(true);
325 frame.setIcon(false);
326 } catch (java.beans.PropertyVetoException ex)
333 windowMenu.add(menuItem);
339 frame.setSelected(true);
340 frame.requestFocus();
341 } catch (java.beans.PropertyVetoException ve)
346 public void lostOwnership(Clipboard clipboard, Transferable contents)
350 Desktop.jalviewClipboard = null;
353 internalCopy = false;
356 public void dragEnter(DropTargetDragEvent evt)
360 public void dragExit(DropTargetEvent evt)
364 public void dragOver(DropTargetDragEvent evt)
368 public void dropActionChanged(DropTargetDragEvent evt)
378 public void drop(DropTargetDropEvent evt)
380 Transferable t = evt.getTransferable();
381 java.util.List files = null;
385 DataFlavor uriListFlavor = new DataFlavor(
386 "text/uri-list;class=java.lang.String");
387 if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
389 // Works on Windows and MacOSX
390 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
391 files = (java.util.List) t
392 .getTransferData(DataFlavor.javaFileListFlavor);
394 else if (t.isDataFlavorSupported(uriListFlavor))
396 // This is used by Unix drag system
397 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
398 String data = (String) t.getTransferData(uriListFlavor);
399 files = new java.util.ArrayList(1);
400 for (java.util.StringTokenizer st = new java.util.StringTokenizer(
401 data, "\r\n"); st.hasMoreTokens();)
403 String s = st.nextToken();
404 if (s.startsWith("#"))
406 // the line is a comment (as per the RFC 2483)
410 java.net.URI uri = new java.net.URI(s);
411 java.io.File file = new java.io.File(uri);
415 } catch (Exception e)
423 for (int i = 0; i < files.size(); i++)
425 String file = files.get(i).toString();
426 String protocol = FormatAdapter.FILE;
427 String format = null;
429 if (file.endsWith(".jar"))
436 format = new IdentifyFile().Identify(file, protocol);
439 new FileLoader().LoadFile(file, protocol, format);
442 } catch (Exception ex)
454 public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
456 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
457 .getProperty("LAST_DIRECTORY"),
458 jalview.io.AppletFormatAdapter.READABLE_EXTENSIONS,
459 jalview.io.AppletFormatAdapter.READABLE_FNAMES,
460 jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
462 chooser.setFileView(new JalviewFileView());
463 chooser.setDialogTitle("Open local file");
464 chooser.setToolTipText("Open");
466 int value = chooser.showOpenDialog(this);
468 if (value == JalviewFileChooser.APPROVE_OPTION)
470 String choice = chooser.getSelectedFile().getPath();
471 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
472 .getSelectedFile().getParent());
474 String format = null;
475 if (chooser.getSelectedFormat().equals("Jalview"))
481 format = new IdentifyFile().Identify(choice, FormatAdapter.FILE);
484 if (viewport != null)
486 new FileLoader().LoadFile(viewport, choice, FormatAdapter.FILE,
491 new FileLoader().LoadFile(choice, FormatAdapter.FILE, format);
502 public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
504 // This construct allows us to have a wider textfield
506 JLabel label = new JLabel("Enter URL of Input File");
507 final JComboBox history = new JComboBox();
509 JPanel panel = new JPanel(new GridLayout(2, 1));
512 history.setPreferredSize(new Dimension(400, 20));
513 history.setEditable(true);
514 history.addItem("http://www.");
516 String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
520 if (historyItems != null)
522 st = new StringTokenizer(historyItems, "\t");
524 while (st.hasMoreTokens())
526 history.addItem(st.nextElement());
530 int reply = JOptionPane.showInternalConfirmDialog(desktop, panel,
531 "Input Alignment From URL", JOptionPane.OK_CANCEL_OPTION);
533 if (reply != JOptionPane.OK_OPTION)
538 String url = history.getSelectedItem().toString();
540 if (url.toLowerCase().endsWith(".jar"))
542 if (viewport != null)
544 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL,
549 new FileLoader().LoadFile(url, FormatAdapter.URL, "Jalview");
554 String format = new IdentifyFile().Identify(url, FormatAdapter.URL);
556 if (format.equals("URL NOT FOUND"))
558 JOptionPane.showInternalMessageDialog(Desktop.desktop,
559 "Couldn't locate " + url, "URL not found",
560 JOptionPane.WARNING_MESSAGE);
565 if (viewport != null)
567 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, format);
571 new FileLoader().LoadFile(url, FormatAdapter.URL, format);
582 public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport)
584 CutAndPasteTransfer cap = new CutAndPasteTransfer();
585 cap.setForInput(viewport);
586 Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
594 jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
595 jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
596 jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
597 jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
607 public void aboutMenuItem_actionPerformed(ActionEvent e)
609 StringBuffer message = new StringBuffer("Jalview version "
610 + jalview.bin.Cache.getProperty("VERSION") + "; last updated: "
611 + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
613 if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
614 jalview.bin.Cache.getProperty("VERSION")))
617 .append("\n\n!! Jalview version "
618 + jalview.bin.Cache.getProperty("LATEST_VERSION")
619 + " is available for download from http://www.jalview.org !!\n");
622 // TODO: update this text for each release or centrally store it for lite
625 .append("\nAuthors: Andrew Waterhouse, Jim Procter, Michele Clamp, James Cuff, Steve Searle,\n David Martin & Geoff Barton."
626 + "\nDevelopment managed by The Barton Group, University of Dundee, Scotland, UK.\n"
627 + "\nFor help, see the FAQ at www.jalview.org and/or join the jalview-discuss@jalview.org mailing list\n"
628 + "\nIf you use Jalview, please cite:"
629 + "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\""
630 + "\nBioinformatics, 2004 20;426-7.");
632 JOptionPane.showInternalMessageDialog(Desktop.desktop,
634 message.toString(), "About Jalview", JOptionPane.INFORMATION_MESSAGE);
643 public void documentationMenuItem_actionPerformed(ActionEvent e)
647 ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
648 java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
649 javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
651 javax.help.HelpBroker hb = hs.createHelpBroker();
652 hb.setCurrentID("home");
653 hb.setDisplayed(true);
654 } catch (Exception ex)
659 public void closeAll_actionPerformed(ActionEvent e)
661 JInternalFrame[] frames = desktop.getAllFrames();
662 for (int i = 0; i < frames.length; i++)
666 frames[i].setClosed(true);
667 } catch (java.beans.PropertyVetoException ex)
671 System.out.println("ALL CLOSED");
674 // TODO clear binding to vamsas document objects on close_all
679 public void raiseRelated_actionPerformed(ActionEvent e)
681 reorderAssociatedWindows(false, false);
684 public void minimizeAssociated_actionPerformed(ActionEvent e)
686 reorderAssociatedWindows(true, false);
689 void closeAssociatedWindows()
691 reorderAssociatedWindows(false, true);
697 * @see jalview.jbgui.GDesktop#garbageCollect_actionPerformed(java.awt.event.ActionEvent)
699 protected void garbageCollect_actionPerformed(ActionEvent e)
701 // We simply collect the garbage
702 jalview.bin.Cache.log.debug("Collecting garbage...");
704 jalview.bin.Cache.log.debug("Finished garbage collection.");
710 * @see jalview.jbgui.GDesktop#showMemusage_actionPerformed(java.awt.event.ActionEvent)
712 protected void showMemusage_actionPerformed(ActionEvent e)
714 desktop.showMemoryUsage(showMemusage.isSelected());
717 void reorderAssociatedWindows(boolean minimize, boolean close)
719 JInternalFrame[] frames = desktop.getAllFrames();
720 if (frames == null || frames.length < 1)
725 AlignViewport source = null, target = null;
726 if (frames[0] instanceof AlignFrame)
728 source = ((AlignFrame) frames[0]).getCurrentView();
730 else if (frames[0] instanceof TreePanel)
732 source = ((TreePanel) frames[0]).getViewPort();
734 else if (frames[0] instanceof PCAPanel)
736 source = ((PCAPanel) frames[0]).av;
738 else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
740 source = ((PairwiseAlignPanel) frames[0].getContentPane()).av;
745 for (int i = 0; i < frames.length; i++)
748 if (frames[i] == null)
752 if (frames[i] instanceof AlignFrame)
754 target = ((AlignFrame) frames[i]).getCurrentView();
756 else if (frames[i] instanceof TreePanel)
758 target = ((TreePanel) frames[i]).getViewPort();
760 else if (frames[i] instanceof PCAPanel)
762 target = ((PCAPanel) frames[i]).av;
764 else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
766 target = ((PairwiseAlignPanel) frames[i].getContentPane()).av;
769 if (source == target)
775 frames[i].setClosed(true);
779 frames[i].setIcon(minimize);
786 } catch (java.beans.PropertyVetoException ex)
800 protected void preferences_actionPerformed(ActionEvent e)
811 public void saveState_actionPerformed(ActionEvent e)
813 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
814 .getProperty("LAST_DIRECTORY"), new String[]
815 { "jar" }, new String[]
816 { "Jalview Project" }, "Jalview Project");
818 chooser.setFileView(new JalviewFileView());
819 chooser.setDialogTitle("Save State");
821 int value = chooser.showSaveDialog(this);
823 if (value == JalviewFileChooser.APPROVE_OPTION)
825 java.io.File choice = chooser.getSelectedFile();
826 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
827 new Jalview2XML().SaveState(choice);
837 public void loadState_actionPerformed(ActionEvent e)
839 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
840 .getProperty("LAST_DIRECTORY"), new String[]
841 { "jar" }, new String[]
842 { "Jalview Project" }, "Jalview Project");
843 chooser.setFileView(new JalviewFileView());
844 chooser.setDialogTitle("Restore state");
846 int value = chooser.showOpenDialog(this);
848 if (value == JalviewFileChooser.APPROVE_OPTION)
850 String choice = chooser.getSelectedFile().getAbsolutePath();
851 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
852 .getSelectedFile().getParent());
853 new Jalview2XML().LoadJalviewAlign(choice);
857 public void inputSequence_actionPerformed(ActionEvent e)
859 new SequenceFetcher(this);
862 JPanel progressPanel;
864 public void startLoading(final String fileName)
866 if (fileLoadingCount == 0)
868 addProgressPanel("Loading File: " + fileName + " ");
874 private JProgressBar addProgressPanel(String string)
876 if (progressPanel == null)
878 progressPanel = new JPanel(new BorderLayout());
879 totalProgressCount = 0;
881 JProgressBar progressBar = new JProgressBar();
882 progressBar.setIndeterminate(true);
884 progressPanel.add(new JLabel(string), BorderLayout.WEST);
886 progressPanel.add(progressBar, BorderLayout.CENTER);
888 instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
889 totalProgressCount++;
894 int totalProgressCount = 0;
896 private void removeProgressPanel(JProgressBar progbar)
898 if (progressPanel != null)
900 progressPanel.remove(progbar);
901 if (--totalProgressCount < 1)
903 this.getContentPane().remove(progressPanel);
904 progressPanel = null;
910 public void stopLoading()
913 if (fileLoadingCount < 1)
915 if (progressPanel != null)
917 this.getContentPane().remove(progressPanel);
918 progressPanel = null;
920 fileLoadingCount = 0;
925 public static int getViewCount(String viewId)
927 AlignViewport[] aps = getViewports(viewId);
928 return (aps==null) ? 0 : aps.length;
933 * @return all AlignmentPanels concerning the viewId sequence set
935 public static AlignmentPanel[] getAlignmentPanels(String viewId)
938 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
939 ArrayList aps = new ArrayList();
940 for (int t = 0; t < frames.length; t++)
942 if (frames[t] instanceof AlignFrame)
944 AlignFrame af = (AlignFrame) frames[t];
945 for (int a = 0; a < af.alignPanels.size(); a++)
948 .equals(((AlignmentPanel) af.alignPanels.elementAt(a)).av
949 .getSequenceSetId()))
951 aps.add(af.alignPanels.elementAt(a));
960 AlignmentPanel[] vap = new AlignmentPanel[aps.size()];
961 for (int t=0;t<vap.length;t++)
963 vap[t] = (AlignmentPanel) aps.get(t);
968 * get all the viewports on an alignment.
969 * @param sequenceSetId unique alignment id
970 * @return all viewports on the alignment bound to sequenceSetId
972 public static AlignViewport[] getViewports(String sequenceSetId)
974 Vector viewp = new Vector();
977 javax.swing.JInternalFrame[] frames = instance.getAllFrames();
979 for (int t = 0; t < frames.length; t++)
981 if (frames[t] instanceof AlignFrame)
983 AlignFrame afr = ((AlignFrame) frames[t]);
984 if (afr.getViewport().getSequenceSetId()
985 .equals(sequenceSetId))
987 if (afr.alignPanels!=null)
989 for (int a = 0; a < afr.alignPanels.size(); a++)
992 .equals(((AlignmentPanel) afr.alignPanels.elementAt(a)).av
993 .getSequenceSetId()))
995 viewp.addElement(((AlignmentPanel)afr.alignPanels.elementAt(a)).av);
999 viewp.addElement(((AlignFrame) frames[t]).getViewport());
1004 if (viewp.size() > 0)
1006 AlignViewport[] vp = new AlignViewport[viewp.size()];
1014 public void explodeViews(AlignFrame af)
1016 int size = af.alignPanels.size();
1022 for (int i = 0; i < size; i++)
1024 AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(i);
1025 AlignFrame newaf = new AlignFrame(ap);
1026 if (ap.av.explodedPosition != null
1027 && !ap.av.explodedPosition.equals(af.getBounds()))
1029 newaf.setBounds(ap.av.explodedPosition);
1032 ap.av.gatherViewsHere = false;
1034 addInternalFrame(newaf, af.getTitle(), AlignFrame.DEFAULT_WIDTH,
1035 AlignFrame.DEFAULT_HEIGHT);
1038 af.alignPanels.clear();
1039 af.closeMenuItem_actionPerformed(true);
1043 public void gatherViews(AlignFrame source)
1045 source.viewport.gatherViewsHere = true;
1046 source.viewport.explodedPosition = source.getBounds();
1047 JInternalFrame[] frames = desktop.getAllFrames();
1048 String viewId = source.viewport.sequenceSetID;
1050 for (int t = 0; t < frames.length; t++)
1052 if (frames[t] instanceof AlignFrame && frames[t] != source)
1054 AlignFrame af = (AlignFrame) frames[t];
1055 boolean gatherThis = false;
1056 for (int a = 0; a < af.alignPanels.size(); a++)
1058 AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(a);
1059 if (viewId.equals(ap.av.getSequenceSetId()))
1062 ap.av.gatherViewsHere = false;
1063 ap.av.explodedPosition = af.getBounds();
1064 source.addAlignmentPanel(ap, false);
1070 af.alignPanels.clear();
1071 af.closeMenuItem_actionPerformed(true);
1078 jalview.gui.VamsasApplication v_client = null;
1080 public void vamsasImport_actionPerformed(ActionEvent e)
1082 if (v_client == null)
1084 // Load and try to start a session.
1085 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
1086 .getProperty("LAST_DIRECTORY"));
1088 chooser.setFileView(new JalviewFileView());
1089 chooser.setDialogTitle("Open a saved VAMSAS session");
1091 .setToolTipText("select a vamsas session to be opened as a new vamsas session.");
1093 int value = chooser.showOpenDialog(this);
1095 if (value == JalviewFileChooser.APPROVE_OPTION)
1099 v_client = new jalview.gui.VamsasApplication(this, chooser
1100 .getSelectedFile());
1101 } catch (Exception ex)
1103 jalview.bin.Cache.log.error(
1104 "New vamsas session from existing session file failed:",
1108 setupVamsasConnectedGui();
1109 v_client.initial_update(); // TODO: thread ?
1114 jalview.bin.Cache.log
1115 .error("Implementation error - load session from a running session is not supported.");
1119 public void vamsasStart_actionPerformed(ActionEvent e)
1121 if (v_client == null)
1124 // we just start a default session for moment.
1126 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
1127 * getProperty("LAST_DIRECTORY"));
1129 * chooser.setFileView(new JalviewFileView());
1130 * chooser.setDialogTitle("Load Vamsas file");
1131 * chooser.setToolTipText("Import");
1133 * int value = chooser.showOpenDialog(this);
1135 * if (value == JalviewFileChooser.APPROVE_OPTION) { v_client = new
1136 * jalview.gui.VamsasApplication(this, chooser.getSelectedFile());
1139 v_client = new VamsasApplication(this);
1140 setupVamsasConnectedGui();
1141 v_client.initial_update(); // TODO: thread ?
1145 // store current data in session.
1146 v_client.push_update(); // TODO: thread
1150 protected void setupVamsasConnectedGui()
1152 vamsasStart.setText("Session Update");
1153 vamsasSave.setVisible(true);
1154 vamsasStop.setVisible(true);
1155 vamsasImport.setVisible(false); // Document import to existing session is
1156 // not possible for vamsas-client-1.0.
1159 protected void setupVamsasDisconnectedGui()
1161 vamsasSave.setVisible(false);
1162 vamsasStop.setVisible(false);
1163 vamsasImport.setVisible(true);
1164 vamsasStart.setText("New Vamsas Session");
1167 public void vamsasStop_actionPerformed(ActionEvent e)
1169 if (v_client != null)
1171 v_client.end_session();
1173 setupVamsasDisconnectedGui();
1177 protected void buildVamsasStMenu()
1179 if (v_client == null)
1181 String[] sess = null;
1184 sess = VamsasApplication.getSessionList();
1185 } catch (Exception e)
1187 jalview.bin.Cache.log.warn(
1188 "Problem getting current sessions list.", e);
1193 jalview.bin.Cache.log.debug("Got current sessions list: "
1194 + sess.length + " entries.");
1195 VamsasStMenu.removeAll();
1196 for (int i = 0; i < sess.length; i++)
1198 JMenuItem sessit = new JMenuItem();
1199 sessit.setText(sess[i]);
1200 sessit.setToolTipText("Connect to session " + sess[i]);
1201 final Desktop dsktp = this;
1202 final String mysesid = sess[i];
1203 sessit.addActionListener(new ActionListener()
1206 public void actionPerformed(ActionEvent e)
1208 if (dsktp.v_client == null)
1210 Thread rthr = new Thread(new Runnable()
1215 dsktp.v_client = new VamsasApplication(dsktp, mysesid);
1216 dsktp.setupVamsasConnectedGui();
1217 dsktp.v_client.initial_update();
1225 VamsasStMenu.add(sessit);
1227 // don't show an empty menu.
1228 VamsasStMenu.setVisible(sess.length > 0);
1233 jalview.bin.Cache.log.debug("No current vamsas sessions.");
1234 VamsasStMenu.removeAll();
1235 VamsasStMenu.setVisible(false);
1240 // Not interested in the content. Just hide ourselves.
1241 VamsasStMenu.setVisible(false);
1245 public void vamsasSave_actionPerformed(ActionEvent e)
1247 if (v_client != null)
1249 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
1250 .getProperty("LAST_DIRECTORY"), new String[]
1251 { "vdj" }, // TODO: VAMSAS DOCUMENT EXTENSION is VDJ
1253 { "Vamsas Document" }, "Vamsas Document");
1255 chooser.setFileView(new JalviewFileView());
1256 chooser.setDialogTitle("Save Vamsas Document Archive");
1258 int value = chooser.showSaveDialog(this);
1260 if (value == JalviewFileChooser.APPROVE_OPTION)
1262 java.io.File choice = chooser.getSelectedFile();
1263 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
1264 String warnmsg = null;
1265 String warnttl = null;
1268 v_client.vclient.storeDocument(choice);
1271 warnttl = "Serious Problem saving Vamsas Document";
1272 warnmsg = ex.toString();
1273 jalview.bin.Cache.log.error("Error Whilst saving document to "
1276 } catch (Exception ex)
1278 warnttl = "Problem saving Vamsas Document.";
1279 warnmsg = ex.toString();
1280 jalview.bin.Cache.log.warn("Exception Whilst saving document to "
1284 if (warnmsg != null)
1286 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1288 warnmsg, warnttl, JOptionPane.ERROR_MESSAGE);
1294 JProgressBar vamUpdate = null;
1297 * hide vamsas user gui bits when a vamsas document event is being handled.
1300 * true to hide gui, false to reveal gui
1302 public void setVamsasUpdate(boolean b)
1304 jalview.bin.Cache.log.debug("Setting gui for Vamsas update "
1305 + (b ? "in progress" : "finished"));
1307 if (vamUpdate != null)
1309 this.removeProgressPanel(vamUpdate);
1313 vamUpdate = this.addProgressPanel("Updating vamsas session");
1315 vamsasStart.setVisible(!b);
1316 vamsasStop.setVisible(!b);
1317 vamsasSave.setVisible(!b);
1320 public JInternalFrame[] getAllFrames()
1322 return desktop.getAllFrames();
1326 * Checks the given url to see if it gives a response indicating that the user
1327 * should be informed of a new questionnaire.
1331 public void checkForQuestionnaire(String url)
1333 UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
1334 // javax.swing.SwingUtilities.invokeLater(jvq);
1335 new Thread(jvq).start();
1339 * Proxy class for JDesktopPane which optionally displays the current memory
1340 * usage and highlights the desktop area with a red bar if free memory runs
1345 public class MyDesktopPane extends JDesktopPane implements Runnable
1348 boolean showMemoryUsage = false;
1352 java.text.NumberFormat df;
1354 float maxMemory, allocatedMemory, freeMemory, totalFreeMemory,
1357 public MyDesktopPane(boolean showMemoryUsage)
1359 showMemoryUsage(showMemoryUsage);
1362 public void showMemoryUsage(boolean showMemoryUsage)
1364 this.showMemoryUsage = showMemoryUsage;
1365 if (showMemoryUsage)
1367 Thread worker = new Thread(this);
1372 public boolean isShowMemoryUsage()
1374 return showMemoryUsage;
1379 df = java.text.NumberFormat.getNumberInstance();
1380 df.setMaximumFractionDigits(2);
1381 runtime = Runtime.getRuntime();
1383 while (showMemoryUsage)
1388 maxMemory = runtime.maxMemory() / 1048576f;
1389 allocatedMemory = runtime.totalMemory() / 1048576f;
1390 freeMemory = runtime.freeMemory() / 1048576f;
1391 totalFreeMemory = freeMemory + (maxMemory - allocatedMemory);
1393 percentUsage = (totalFreeMemory / maxMemory) * 100;
1395 // if (percentUsage < 20)
1397 // border1 = BorderFactory.createMatteBorder(12, 12, 12, 12,
1399 // instance.set.setBorder(border1);
1403 } catch (Exception ex)
1405 ex.printStackTrace();
1410 public void paintComponent(Graphics g)
1412 if (showMemoryUsage)
1414 if (percentUsage < 20)
1415 g.setColor(Color.red);
1417 g.drawString("Total Free Memory: " + df.format(totalFreeMemory)
1418 + "MB; Max Memory: " + df.format(maxMemory) + "MB; "
1419 + df.format(percentUsage) + "%", 10, getHeight()
1420 - g.getFontMetrics().getHeight());
1426 protected JMenuItem groovyShell;
1428 public void doGroovyCheck()
1430 if (jalview.bin.Cache.groovyJarsPresent())
1432 groovyShell = new JMenuItem();
1433 groovyShell.setText("Groovy Console...");
1434 groovyShell.addActionListener(new ActionListener()
1436 public void actionPerformed(ActionEvent e)
1438 groovyShell_actionPerformed(e);
1441 toolsMenu.add(groovyShell);
1442 groovyShell.setVisible(true);
1447 * Accessor method to quickly get all the AlignmentFrames loaded.
1449 public static AlignFrame[] getAlignframes()
1451 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1457 Vector avp = new Vector();
1461 for (int i = frames.length - 1; i > -1; i--)
1463 if (frames[i] instanceof AlignFrame)
1465 AlignFrame af = (AlignFrame) frames[i];
1469 } catch (Exception ex)
1471 ex.printStackTrace();
1473 if (avp.size() == 0)
1477 AlignFrame afs[] = new AlignFrame[avp.size()];
1478 for (int i = 0, j = avp.size(); i < j; i++)
1480 afs[i] = (AlignFrame) avp.elementAt(i);
1487 * Add Groovy Support to Jalview
1489 public void groovyShell_actionPerformed(ActionEvent e)
1491 // use reflection to avoid creating compilation dependency.
1492 if (!jalview.bin.Cache.groovyJarsPresent())
1495 "Implementation Error. Cannot create groovyShell without Groovy on the classpath!");
1499 Class gcClass = Desktop.class.getClassLoader().loadClass(
1500 "groovy.ui.Console");
1501 Constructor gccons = gcClass.getConstructor(null);
1502 java.lang.reflect.Method setvar = gcClass.getMethod("setVariable",
1504 { String.class, Object.class });
1505 java.lang.reflect.Method run = gcClass.getMethod("run", null);
1506 Object gc = gccons.newInstance(null);
1507 setvar.invoke(gc, new Object[]
1508 { "Jalview", this });
1509 run.invoke(gc, null);
1510 } catch (Exception ex)
1512 jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex);
1514 .showInternalMessageDialog(
1517 "Couldn't create the groovy Shell. Check the error log for the details of what went wrong.",
1518 "Jalview Groovy Support Failed",
1519 JOptionPane.ERROR_MESSAGE);
1524 * Progress bars managed by the IProgressIndicator method.
1526 private Hashtable progressBars, progressBarHandlers;
1531 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
1533 public void setProgressBar(String message, long id)
1535 if (progressBars == null)
1537 progressBars = new Hashtable();
1538 progressBarHandlers = new Hashtable();
1541 if (progressBars.get(new Long(id)) != null)
1543 JProgressBar progressPanel = (JProgressBar) progressBars
1544 .remove(new Long(id));
1545 if (progressBarHandlers.contains(new Long(id)))
1547 progressBarHandlers.remove(new Long(id));
1549 removeProgressPanel(progressPanel);
1553 progressBars.put(new Long(id), addProgressPanel(message));
1557 * @see jalview.gui.IProgressIndicator#registerHandler(long, jalview.gui.IProgressIndicatorHandler)
1559 public void registerHandler(final long id, final IProgressIndicatorHandler handler)
1561 if (progressBarHandlers==null || !progressBars.contains(new Long(id)))
1563 throw new Error("call setProgressBar before registering the progress bar's handler.");
1565 progressBarHandlers.put(new Long(id), handler);
1566 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
1567 if (handler.canCancel())
1569 JButton cancel = new JButton("Cancel");
1570 final IProgressIndicator us=this;
1571 cancel.addActionListener(new ActionListener() {
1573 public void actionPerformed(ActionEvent e)
1575 handler.cancelActivity(id);
1576 us.setProgressBar("Cancelled "+((JLabel)progressPanel.getComponent(0)).getText(), id);
1579 progressPanel.add(cancel, BorderLayout.EAST);
1583 * This will return the first AlignFrame viewing AlignViewport av. It will
1584 * break if there are more than one AlignFrames viewing a particular av. This
1587 * @return alignFrame for av
1589 public static AlignFrame getAlignFrameFor(AlignViewport av)
1591 if (desktop != null)
1593 AlignmentPanel[] aps = getAlignmentPanels(av.getSequenceSetId());
1594 for (int panel=0;aps!=null && panel<aps.length;panel++)
1595 { if (aps[panel]!=null && aps[panel].av==av) {
1596 return aps[panel].alignFrame;