2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
23 import java.awt.datatransfer.*;
24 import java.awt.dnd.*;
25 import java.awt.event.*;
37 public class Desktop extends jalview.jbgui.GDesktop
38 implements DropTargetListener, ClipboardOwner
41 public static Desktop instance;
42 public static JDesktopPane desktop;
43 static int openFrameCount = 0;
44 static final int xOffset = 30;
45 static final int yOffset = 30;
46 public static jalview.ws.Discoverer discoverer;
48 public static Object [] jalviewClipboard;
49 public static boolean internalCopy = false;
51 static int fileLoadingCount= 0;
54 * Creates a new Desktop object.
59 doVamsasClientCheck();
64 java.net.URL url = getClass().getResource("/images/logo.gif");
68 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
70 MediaTracker mt = new MediaTracker(this);
71 mt.addImage(image, 0);
80 setTitle("Jalview "+jalview.bin.Cache.getProperty("VERSION"));
81 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82 desktop = new JDesktopPane();
83 desktop.setBackground(Color.white);
84 getContentPane().setLayout(new BorderLayout());
85 getContentPane().add(desktop, BorderLayout.CENTER);
86 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
88 // This line prevents Windows Look&Feel resizing all new windows to maximum
89 // if previous window was maximised
90 desktop.setDesktopManager(new DefaultDesktopManager());
92 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
93 String x = jalview.bin.Cache.getProperty("SCREEN_X");
94 String y = jalview.bin.Cache.getProperty("SCREEN_Y");
95 String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");
96 String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");
98 if ((x != null) && (y != null) && (width != null) && (height != null))
100 setBounds(Integer.parseInt(x), Integer.parseInt(y),
101 Integer.parseInt(width), Integer.parseInt(height));
105 setBounds((int) (screenSize.width - 900) / 2,
106 (int) (screenSize.height - 650) / 2, 900, 650);
109 this.addWindowListener(new WindowAdapter()
111 public void windowClosing(WindowEvent evt)
117 this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
119 /////////Add a splashscreen on startup
120 /////////Add a splashscreen on startup
121 JInternalFrame frame = new JInternalFrame();
123 SplashScreen splash = new SplashScreen(frame, image);
124 frame.setContentPane(splash);
125 frame.setLayer(JLayeredPane.PALETTE_LAYER);
126 frame.setLocation((int) ((getWidth() - 750) / 2),
127 (int) ((getHeight() - 160) / 2));
129 addInternalFrame(frame, "", 750, 160, false);
131 discoverer=new jalview.ws.Discoverer(); // Only gets started if gui is displayed.
134 private void doVamsasClientCheck() {
135 if (jalview.bin.Cache.vamsasJarsPresent()) {
136 VamsasMenu.setVisible(true);
137 vamsasLoad.setVisible(true);
145 * @param frame DOCUMENT ME!
146 * @param title DOCUMENT ME!
147 * @param w DOCUMENT ME!
148 * @param h DOCUMENT ME!
150 public static synchronized void addInternalFrame(final JInternalFrame frame,
151 String title, int w, int h)
153 addInternalFrame(frame, title, w, h, true);
159 * @param frame DOCUMENT ME!
160 * @param title DOCUMENT ME!
161 * @param w DOCUMENT ME!
162 * @param h DOCUMENT ME!
163 * @param resizable DOCUMENT ME!
165 public static synchronized void addInternalFrame(final JInternalFrame frame,
166 String title, int w, int h, boolean resizable)
169 frame.setTitle(title);
170 if(frame.getWidth()<1 || frame.getHeight()<1)
174 // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
175 // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
176 // IF JALVIEW IS RUNNING HEADLESS
177 /////////////////////////////////////////////////
178 if (System.getProperty("java.awt.headless") != null
179 && System.getProperty("java.awt.headless").equals("true"))
187 frame.setVisible(true);
188 frame.setClosable(true);
189 frame.setResizable(resizable);
190 frame.setMaximizable(resizable);
191 frame.setIconifiable(resizable);
192 frame.setFrameIcon(null);
194 if (frame.getX()<1 && frame.getY()<1)
196 frame.setLocation(xOffset * openFrameCount, yOffset * ((openFrameCount-1)%10)+yOffset);
199 final JMenuItem menuItem = new JMenuItem(title);
200 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
202 public void internalFrameActivated(javax.swing.event.
203 InternalFrameEvent evt)
205 JInternalFrame itf = desktop.getSelectedFrame();
212 public void internalFrameClosed(
213 javax.swing.event.InternalFrameEvent evt)
215 PaintRefresher.RemoveComponent(frame);
217 windowMenu.remove(menuItem);
218 JInternalFrame itf = desktop.getSelectedFrame();
225 menuItem.addActionListener(new ActionListener()
227 public void actionPerformed(ActionEvent e)
231 frame.setSelected(true);
232 frame.setIcon(false);
234 catch (java.beans.PropertyVetoException ex)
241 windowMenu.add(menuItem);
246 frame.setSelected(true);
247 frame.requestFocus();
248 }catch(java.beans.PropertyVetoException ve)
252 public void lostOwnership(Clipboard clipboard, Transferable contents)
255 Desktop.jalviewClipboard = null;
257 internalCopy = false;
260 public void dragEnter(DropTargetDragEvent evt)
263 public void dragExit(DropTargetEvent evt)
266 public void dragOver(DropTargetDragEvent evt)
269 public void dropActionChanged(DropTargetDragEvent evt)
275 * @param evt DOCUMENT ME!
277 public void drop(DropTargetDropEvent evt)
279 Transferable t = evt.getTransferable();
280 java.util.List files = null;
284 DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
285 if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
287 //Works on Windows and MacOSX
288 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
289 files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
291 else if (t.isDataFlavorSupported(uriListFlavor))
293 // This is used by Unix drag system
294 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
295 String data = (String) t.getTransferData(uriListFlavor);
296 files = new java.util.ArrayList(1);
297 for (java.util.StringTokenizer st = new java.util.StringTokenizer(
300 st.hasMoreTokens(); )
302 String s = st.nextToken();
303 if (s.startsWith("#"))
305 // the line is a comment (as per the RFC 2483)
309 java.net.URI uri = new java.net.URI(s);
310 java.io.File file = new java.io.File(uri);
322 for (int i = 0; i < files.size(); i++)
324 String file = files.get(i).toString();
325 String protocol = FormatAdapter.FILE;
326 String format = null;
328 if (file.endsWith(".jar"))
335 format = new IdentifyFile().Identify(file,
340 new FileLoader().LoadFile(file, protocol, format);
352 * @param e DOCUMENT ME!
354 public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
356 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
360 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
365 "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
366 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
368 chooser.setFileView(new JalviewFileView());
369 chooser.setDialogTitle("Open local file");
370 chooser.setToolTipText("Open");
372 int value = chooser.showOpenDialog(this);
374 if (value == JalviewFileChooser.APPROVE_OPTION)
376 String choice = chooser.getSelectedFile().getPath();
377 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
378 chooser.getSelectedFile().getParent());
380 String format = null;
381 if (chooser.getSelectedFormat().equals("Jalview"))
387 format = new IdentifyFile().Identify(choice, FormatAdapter.FILE);
390 if (viewport != null)
391 new FileLoader().LoadFile(viewport, choice, FormatAdapter.FILE, format);
393 new FileLoader().LoadFile(choice, FormatAdapter.FILE, format);
402 * @param e DOCUMENT ME!
404 public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
406 // This construct allows us to have a wider textfield
408 JLabel label = new JLabel("Enter URL of Input File");
409 final JComboBox history = new JComboBox();
411 JPanel panel = new JPanel(new GridLayout(2,1));
414 history.setPreferredSize(new Dimension(400,20));
415 history.setEditable(true);
416 history.addItem("http://www.");
418 String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
422 if (historyItems != null)
424 st = new StringTokenizer(historyItems, "\t");
426 while (st.hasMoreTokens())
428 history.addItem(st.nextElement());
432 int reply = JOptionPane.showInternalConfirmDialog(desktop,
433 panel, "Input Alignment From URL",
434 JOptionPane.OK_CANCEL_OPTION );
437 if (reply != JOptionPane.OK_OPTION )
442 String url = history.getSelectedItem().toString();
444 if (url.toLowerCase().endsWith(".jar"))
446 if (viewport != null)
447 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, "Jalview");
449 new FileLoader().LoadFile(url, FormatAdapter.URL, "Jalview");
453 String format = new IdentifyFile().Identify(url, FormatAdapter.URL);
455 if (format.equals("URL NOT FOUND"))
457 JOptionPane.showInternalMessageDialog(Desktop.desktop,
458 "Couldn't locate " + url,
460 JOptionPane.WARNING_MESSAGE);
465 if (viewport != null)
466 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, format);
468 new FileLoader().LoadFile(url, FormatAdapter.URL, format);
475 * @param e DOCUMENT ME!
477 public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport)
479 CutAndPasteTransfer cap = new CutAndPasteTransfer();
480 cap.setForInput(viewport);
481 Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
489 jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
490 jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
491 jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
492 jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
499 * @param e DOCUMENT ME!
501 public void aboutMenuItem_actionPerformed(ActionEvent e)
503 StringBuffer message = new StringBuffer("JalView version " +
504 jalview.bin.Cache.getProperty(
508 Cache.getDefault("BUILD_DATE", "unknown"));
510 if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
511 jalview.bin.Cache.getProperty("VERSION")))
513 message.append("\n\n!! Jalview version "
514 + jalview.bin.Cache.getProperty("LATEST_VERSION")
515 + " is available for download from http://www.jalview.org !!\n");
519 message.append( "\nAuthors: Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
520 "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
521 "\nFor all issues relating to Jalview, email help@jalview.org" +
522 "\n\nIf you use JalView, please cite:" +
523 "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
524 "\nBioinformatics, 2004 20;426-7.");
526 JOptionPane.showInternalMessageDialog(Desktop.desktop,
528 message.toString(), "About Jalview",
529 JOptionPane.INFORMATION_MESSAGE);
535 * @param e DOCUMENT ME!
537 public void documentationMenuItem_actionPerformed(ActionEvent e)
541 ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
542 java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
543 javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
545 javax.help.HelpBroker hb = hs.createHelpBroker();
546 hb.setCurrentID("home");
547 hb.setDisplayed(true);
553 public void closeAll_actionPerformed(ActionEvent e)
555 JInternalFrame [] frames = desktop.getAllFrames();
556 for(int i=0; i<frames.length; i++)
559 frames[i].setClosed(true);
560 }catch(java.beans.PropertyVetoException ex){}
564 public void raiseRelated_actionPerformed(ActionEvent e)
566 reorderAssociatedWindows(false, false);
569 public void minimizeAssociated_actionPerformed(ActionEvent e)
571 reorderAssociatedWindows(true, false);
574 void closeAssociatedWindows()
576 reorderAssociatedWindows(false, true);
579 void reorderAssociatedWindows(boolean minimize, boolean close)
581 JInternalFrame[] frames = desktop.getAllFrames();
582 if (frames == null || frames.length<1)
585 AlignViewport source = null, target = null;
586 if (frames[0] instanceof AlignFrame)
587 source = ( (AlignFrame) frames[0]).getCurrentView();
588 else if (frames[0] instanceof TreePanel)
589 source = ( (TreePanel) frames[0]).getViewPort();
590 else if (frames[0] instanceof PCAPanel)
591 source = ( (PCAPanel) frames[0]).av;
592 else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
593 source = ((PairwiseAlignPanel)frames[0].getContentPane()).av;
597 for (int i = 0; i < frames.length; i++)
604 if (frames[i] instanceof AlignFrame)
605 target = ( (AlignFrame) frames[i]).getCurrentView();
606 else if (frames[i] instanceof TreePanel)
607 target = ( (TreePanel) frames[i]).getViewPort();
608 else if (frames[i] instanceof PCAPanel)
609 target = ( (PCAPanel) frames[i]).av;
610 else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
611 target = ( (PairwiseAlignPanel) frames[i].getContentPane()).av;
613 if (source == target)
618 frames[i].setClosed(true);
621 frames[i].setIcon(minimize);
629 catch (java.beans.PropertyVetoException ex)
640 * @param e DOCUMENT ME!
642 protected void preferences_actionPerformed(ActionEvent e)
650 * @param e DOCUMENT ME!
652 public void saveState_actionPerformed(ActionEvent e)
654 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
655 "LAST_DIRECTORY"), new String[] { "jar" },
656 new String[] { "Jalview Project" }, "Jalview Project");
658 chooser.setFileView(new JalviewFileView());
659 chooser.setDialogTitle("Save State");
661 int value = chooser.showSaveDialog(this);
663 if (value == JalviewFileChooser.APPROVE_OPTION)
665 java.io.File choice = chooser.getSelectedFile();
666 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
667 new Jalview2XML().SaveState(choice);
674 * @param e DOCUMENT ME!
676 public void loadState_actionPerformed(ActionEvent e)
678 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
679 "LAST_DIRECTORY"), new String[] { "jar" },
680 new String[] { "Jalview Project" }, "Jalview Project");
681 chooser.setFileView(new JalviewFileView());
682 chooser.setDialogTitle("Restore state");
684 int value = chooser.showOpenDialog(this);
686 if (value == JalviewFileChooser.APPROVE_OPTION)
688 String choice = chooser.getSelectedFile().getAbsolutePath();
689 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
690 chooser.getSelectedFile().getParent());
691 new Jalview2XML().LoadJalviewAlign(choice);
695 /* public void vamsasLoad_actionPerformed(ActionEvent e)
697 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
698 getProperty("LAST_DIRECTORY"));
700 chooser.setFileView(new JalviewFileView());
701 chooser.setDialogTitle("Load Vamsas file");
702 chooser.setToolTipText("Import");
704 int value = chooser.showOpenDialog(this);
706 if (value == JalviewFileChooser.APPROVE_OPTION)
708 jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
710 chooser.getSelectedFile().getAbsolutePath()
717 public void inputSequence_actionPerformed(ActionEvent e)
719 new SequenceFetcher(null);
722 JPanel progressPanel;
724 public void startLoading(final String fileName)
726 if (fileLoadingCount == 0)
728 progressPanel = new JPanel(new BorderLayout());
729 JProgressBar progressBar = new JProgressBar();
730 progressBar.setIndeterminate(true);
732 progressPanel.add(new JLabel("Loading File: " + fileName + " "),
735 progressPanel.add(progressBar, BorderLayout.CENTER);
737 instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
743 public void stopLoading()
746 if (fileLoadingCount < 1)
748 if(progressPanel!=null)
750 this.getContentPane().remove(progressPanel);
751 progressPanel = null;
753 fileLoadingCount = 0;
758 public static int getViewCount(String viewId)
761 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
762 for (int t = 0; t < frames.length; t++)
764 if (frames[t] instanceof AlignFrame)
766 AlignFrame af = (AlignFrame) frames[t];
767 for(int a=0; a<af.alignPanels.size(); a++)
770 ((AlignmentPanel)af.alignPanels.elementAt(a)).av.getSequenceSetId() )
780 public void explodeViews(AlignFrame af)
782 int size = af.alignPanels.size();
786 for(int i=0; i<size; i++)
788 AlignmentPanel ap = (AlignmentPanel)af.alignPanels.elementAt(i);
789 AlignFrame newaf = new AlignFrame(ap);
790 if(ap.av.explodedPosition!=null)
791 newaf.setBounds(ap.av.explodedPosition);
793 ap.av.gatherViewsHere = false;
795 addInternalFrame(newaf, af.getTitle(),
796 AlignFrame.DEFAULT_WIDTH,
797 AlignFrame.DEFAULT_HEIGHT);
800 af.alignPanels.clear();
801 af.closeMenuItem_actionPerformed(true);
805 public void gatherViews(AlignFrame source)
807 source.viewport.gatherViewsHere = true;
808 source.viewport.explodedPosition = source.getBounds();
809 JInternalFrame[] frames = desktop.getAllFrames();
810 String viewId = source.viewport.sequenceSetID;
812 for (int t = 0; t < frames.length; t++)
814 if (frames[t] instanceof AlignFrame && frames[t] != source)
816 AlignFrame af = (AlignFrame) frames[t];
817 boolean gatherThis = false;
818 for (int a = 0; a < af.alignPanels.size(); a++)
820 AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(a);
821 if (viewId.equals(ap.av.getSequenceSetId()))
824 ap.av.gatherViewsHere = false;
825 ap.av.explodedPosition = af.getBounds();
826 source.addAlignmentPanel(ap, false);
832 af.alignPanels.clear();
833 af.closeMenuItem_actionPerformed(true);
840 jalview.gui.VamsasClient v_client=null;
841 public void vamsasLoad_actionPerformed(ActionEvent e)
843 if (v_client==null) {
845 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
846 getProperty("LAST_DIRECTORY"));
848 chooser.setFileView(new JalviewFileView());
849 chooser.setDialogTitle("Load Vamsas file");
850 chooser.setToolTipText("Import");
852 int value = chooser.showOpenDialog(this);
854 if (value == JalviewFileChooser.APPROVE_OPTION)
856 v_client = new jalview.gui.VamsasClient(this,
857 chooser.getSelectedFile());
858 this.vamsasLoad.setText("Session Update");
859 this.vamsasStop.setVisible(true);
860 v_client.initial_update();
861 v_client.startWatcher();
864 // store current data in session.
865 v_client.push_update();
868 public void vamsasStop_actionPerformed(ActionEvent e) {
869 if (v_client!=null) {
870 v_client.end_session();
872 this.vamsasStop.setVisible(false);
873 this.vamsasLoad.setText("Start Vamsas Session...");
877 * hide vamsas user gui bits when a vamsas document event is being handled.
878 * @param b true to hide gui, false to reveal gui
880 public void setVamsasUpdate(boolean b) {
881 jalview.bin.Cache.log.debug("Setting gui for Vamsas update "+(b ? "in progress" : "finished"));
882 vamsasLoad.setVisible(!b);
883 vamsasStop.setVisible(!b);
887 * Checks the given url to see if it gives a response indicating that
888 * the user should be informed of a new questionnaire.
891 public void checkForQuestionnaire(String url) {
892 UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
893 javax.swing.SwingUtilities.invokeLater(jvq);