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
24 import java.awt.datatransfer.*;
25 import java.awt.dnd.*;
26 import java.awt.event.*;
38 public class Desktop extends jalview.jbgui.GDesktop
39 implements DropTargetListener, ClipboardOwner
42 public static Desktop instance;
43 public static JDesktopPane desktop;
44 static int openFrameCount = 0;
45 static final int xOffset = 30;
46 static final int yOffset = 30;
47 public static jalview.ws.Discoverer discoverer;
49 public static Object [] jalviewClipboard;
50 public static boolean internalCopy = false;
52 static int fileLoadingCount= 0;
55 * Creates a new Desktop object.
60 doVamsasClientCheck();
65 java.net.URL url = getClass().getResource("/images/logo.gif");
69 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
71 MediaTracker mt = new MediaTracker(this);
72 mt.addImage(image, 0);
81 setTitle("Jalview "+jalview.bin.Cache.getProperty("VERSION"));
82 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
83 desktop = new JDesktopPane();
84 desktop.setBackground(Color.white);
85 getContentPane().setLayout(new BorderLayout());
86 getContentPane().add(desktop, BorderLayout.CENTER);
87 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
89 // This line prevents Windows Look&Feel resizing all new windows to maximum
90 // if previous window was maximised
91 desktop.setDesktopManager(new DefaultDesktopManager());
93 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
94 String x = jalview.bin.Cache.getProperty("SCREEN_X");
95 String y = jalview.bin.Cache.getProperty("SCREEN_Y");
96 String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");
97 String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");
99 if ((x != null) && (y != null) && (width != null) && (height != null))
101 setBounds(Integer.parseInt(x), Integer.parseInt(y),
102 Integer.parseInt(width), Integer.parseInt(height));
106 setBounds((int) (screenSize.width - 900) / 2,
107 (int) (screenSize.height - 650) / 2, 900, 650);
110 this.addWindowListener(new WindowAdapter()
112 public void windowClosing(WindowEvent evt)
118 this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
120 /////////Add a splashscreen on startup
121 /////////Add a splashscreen on startup
122 JInternalFrame frame = new JInternalFrame();
124 SplashScreen splash = new SplashScreen(frame, image);
125 frame.setContentPane(splash);
126 frame.setLayer(JLayeredPane.PALETTE_LAYER);
127 frame.setLocation((int) ((getWidth() - 750) / 2),
128 (int) ((getHeight() - 160) / 2));
130 addInternalFrame(frame, "", 750, 160, false);
132 discoverer=new jalview.ws.Discoverer(); // Only gets started if gui is displayed.
135 private void doVamsasClientCheck() {
136 if (jalview.bin.Cache.vamsasJarsPresent()) {
137 VamsasMenu.setVisible(true);
138 vamsasLoad.setVisible(true);
146 * @param frame DOCUMENT ME!
147 * @param title DOCUMENT ME!
148 * @param w DOCUMENT ME!
149 * @param h DOCUMENT ME!
151 public static synchronized void addInternalFrame(final JInternalFrame frame,
152 String title, int w, int h)
154 addInternalFrame(frame, title, w, h, true);
160 * @param frame DOCUMENT ME!
161 * @param title DOCUMENT ME!
162 * @param w DOCUMENT ME!
163 * @param h DOCUMENT ME!
164 * @param resizable DOCUMENT ME!
166 public static synchronized void addInternalFrame(final JInternalFrame frame,
167 String title, int w, int h, boolean resizable)
170 frame.setTitle(title);
171 if(frame.getWidth()<1 || frame.getHeight()<1)
175 // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
176 // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
177 // IF JALVIEW IS RUNNING HEADLESS
178 /////////////////////////////////////////////////
179 if (System.getProperty("java.awt.headless") != null
180 && System.getProperty("java.awt.headless").equals("true"))
188 frame.setVisible(true);
189 frame.setClosable(true);
190 frame.setResizable(resizable);
191 frame.setMaximizable(resizable);
192 frame.setIconifiable(resizable);
193 frame.setFrameIcon(null);
195 if (frame.getX()<1 && frame.getY()<1)
197 frame.setLocation(xOffset * openFrameCount, yOffset * ((openFrameCount-1)%10)+yOffset);
200 final JMenuItem menuItem = new JMenuItem(title);
201 frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
203 public void internalFrameActivated(javax.swing.event.
204 InternalFrameEvent evt)
206 JInternalFrame itf = desktop.getSelectedFrame();
213 public void internalFrameClosed(
214 javax.swing.event.InternalFrameEvent evt)
216 PaintRefresher.RemoveComponent(frame);
218 windowMenu.remove(menuItem);
219 JInternalFrame itf = desktop.getSelectedFrame();
226 menuItem.addActionListener(new ActionListener()
228 public void actionPerformed(ActionEvent e)
232 frame.setSelected(true);
233 frame.setIcon(false);
235 catch (java.beans.PropertyVetoException ex)
242 windowMenu.add(menuItem);
247 frame.setSelected(true);
248 frame.requestFocus();
249 }catch(java.beans.PropertyVetoException ve)
253 public void lostOwnership(Clipboard clipboard, Transferable contents)
256 Desktop.jalviewClipboard = null;
258 internalCopy = false;
261 public void dragEnter(DropTargetDragEvent evt)
264 public void dragExit(DropTargetEvent evt)
267 public void dragOver(DropTargetDragEvent evt)
270 public void dropActionChanged(DropTargetDragEvent evt)
276 * @param evt DOCUMENT ME!
278 public void drop(DropTargetDropEvent evt)
280 Transferable t = evt.getTransferable();
281 java.util.List files = null;
285 DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
286 if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
288 //Works on Windows and MacOSX
289 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
290 files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
292 else if (t.isDataFlavorSupported(uriListFlavor))
294 // This is used by Unix drag system
295 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
296 String data = (String) t.getTransferData(uriListFlavor);
297 files = new java.util.ArrayList(1);
298 for (java.util.StringTokenizer st = new java.util.StringTokenizer(
301 st.hasMoreTokens(); )
303 String s = st.nextToken();
304 if (s.startsWith("#"))
306 // the line is a comment (as per the RFC 2483)
310 java.net.URI uri = new java.net.URI(s);
311 java.io.File file = new java.io.File(uri);
323 for (int i = 0; i < files.size(); i++)
325 String file = files.get(i).toString();
326 String protocol = FormatAdapter.FILE;
327 String format = null;
329 if (file.endsWith(".jar"))
336 format = new IdentifyFile().Identify(file,
341 new FileLoader().LoadFile(file, protocol, format);
353 * @param e DOCUMENT ME!
355 public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
357 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
361 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
366 "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
367 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
369 chooser.setFileView(new JalviewFileView());
370 chooser.setDialogTitle("Open local file");
371 chooser.setToolTipText("Open");
373 int value = chooser.showOpenDialog(this);
375 if (value == JalviewFileChooser.APPROVE_OPTION)
377 String choice = chooser.getSelectedFile().getPath();
378 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
379 chooser.getSelectedFile().getParent());
381 String format = null;
382 if (chooser.getSelectedFormat().equals("Jalview"))
388 format = new IdentifyFile().Identify(choice, FormatAdapter.FILE);
391 if (viewport != null)
392 new FileLoader().LoadFile(viewport, choice, FormatAdapter.FILE, format);
394 new FileLoader().LoadFile(choice, FormatAdapter.FILE, format);
403 * @param e DOCUMENT ME!
405 public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
407 // This construct allows us to have a wider textfield
409 JLabel label = new JLabel("Enter URL of Input File");
410 final JComboBox history = new JComboBox();
412 JPanel panel = new JPanel(new GridLayout(2,1));
415 history.setPreferredSize(new Dimension(400,20));
416 history.setEditable(true);
417 history.addItem("http://www.");
419 String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
423 if (historyItems != null)
425 st = new StringTokenizer(historyItems, "\t");
427 while (st.hasMoreTokens())
429 history.addItem(st.nextElement());
433 int reply = JOptionPane.showInternalConfirmDialog(desktop,
434 panel, "Input Alignment From URL",
435 JOptionPane.OK_CANCEL_OPTION );
438 if (reply != JOptionPane.OK_OPTION )
443 String url = history.getSelectedItem().toString();
445 if (url.toLowerCase().endsWith(".jar"))
447 if (viewport != null)
448 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, "Jalview");
450 new FileLoader().LoadFile(url, FormatAdapter.URL, "Jalview");
454 String format = new IdentifyFile().Identify(url, FormatAdapter.URL);
456 if (format.equals("URL NOT FOUND"))
458 JOptionPane.showInternalMessageDialog(Desktop.desktop,
459 "Couldn't locate " + url,
461 JOptionPane.WARNING_MESSAGE);
466 if (viewport != null)
467 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, format);
469 new FileLoader().LoadFile(url, FormatAdapter.URL, format);
476 * @param e DOCUMENT ME!
478 public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport)
480 CutAndPasteTransfer cap = new CutAndPasteTransfer();
481 cap.setForInput(viewport);
482 Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
490 jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
491 jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
492 jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
493 jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
500 * @param e DOCUMENT ME!
502 public void aboutMenuItem_actionPerformed(ActionEvent e)
504 StringBuffer message = new StringBuffer("JalView version " +
505 jalview.bin.Cache.getProperty(
509 Cache.getDefault("BUILD_DATE", "unknown"));
511 if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
512 jalview.bin.Cache.getProperty("VERSION")))
514 message.append("\n\n!! Jalview version "
515 + jalview.bin.Cache.getProperty("LATEST_VERSION")
516 + " is available for download from http://www.jalview.org !!\n");
520 message.append( "\nAuthors: Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
521 "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
522 "\nFor all issues relating to Jalview, email help@jalview.org" +
523 "\n\nIf you use JalView, please cite:" +
524 "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
525 "\nBioinformatics, 2004 20;426-7.");
527 JOptionPane.showInternalMessageDialog(Desktop.desktop,
529 message.toString(), "About Jalview",
530 JOptionPane.INFORMATION_MESSAGE);
536 * @param e DOCUMENT ME!
538 public void documentationMenuItem_actionPerformed(ActionEvent e)
542 ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
543 java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
544 javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
546 javax.help.HelpBroker hb = hs.createHelpBroker();
547 hb.setCurrentID("home");
548 hb.setDisplayed(true);
554 public void closeAll_actionPerformed(ActionEvent e)
556 JInternalFrame [] frames = desktop.getAllFrames();
557 for(int i=0; i<frames.length; i++)
560 frames[i].setClosed(true);
561 }catch(java.beans.PropertyVetoException ex){}
565 public void raiseRelated_actionPerformed(ActionEvent e)
567 reorderAssociatedWindows(false, false);
570 public void minimizeAssociated_actionPerformed(ActionEvent e)
572 reorderAssociatedWindows(true, false);
575 void closeAssociatedWindows()
577 reorderAssociatedWindows(false, true);
580 void reorderAssociatedWindows(boolean minimize, boolean close)
582 JInternalFrame[] frames = desktop.getAllFrames();
583 if (frames == null || frames.length<1)
586 AlignViewport source = null, target = null;
587 if (frames[0] instanceof AlignFrame)
588 source = ( (AlignFrame) frames[0]).getCurrentView();
589 else if (frames[0] instanceof TreePanel)
590 source = ( (TreePanel) frames[0]).getViewPort();
591 else if (frames[0] instanceof PCAPanel)
592 source = ( (PCAPanel) frames[0]).av;
593 else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
594 source = ((PairwiseAlignPanel)frames[0].getContentPane()).av;
598 for (int i = 0; i < frames.length; i++)
605 if (frames[i] instanceof AlignFrame)
606 target = ( (AlignFrame) frames[i]).getCurrentView();
607 else if (frames[i] instanceof TreePanel)
608 target = ( (TreePanel) frames[i]).getViewPort();
609 else if (frames[i] instanceof PCAPanel)
610 target = ( (PCAPanel) frames[i]).av;
611 else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
612 target = ( (PairwiseAlignPanel) frames[i].getContentPane()).av;
614 if (source == target)
619 frames[i].setClosed(true);
622 frames[i].setIcon(minimize);
630 catch (java.beans.PropertyVetoException ex)
641 * @param e DOCUMENT ME!
643 protected void preferences_actionPerformed(ActionEvent e)
651 * @param e DOCUMENT ME!
653 public void saveState_actionPerformed(ActionEvent e)
655 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
656 "LAST_DIRECTORY"), new String[] { "jar" },
657 new String[] { "Jalview Project" }, "Jalview Project");
659 chooser.setFileView(new JalviewFileView());
660 chooser.setDialogTitle("Save State");
662 int value = chooser.showSaveDialog(this);
664 if (value == JalviewFileChooser.APPROVE_OPTION)
666 java.io.File choice = chooser.getSelectedFile();
667 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
668 new Jalview2XML().SaveState(choice);
675 * @param e DOCUMENT ME!
677 public void loadState_actionPerformed(ActionEvent e)
679 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
680 "LAST_DIRECTORY"), new String[] { "jar" },
681 new String[] { "Jalview Project" }, "Jalview Project");
682 chooser.setFileView(new JalviewFileView());
683 chooser.setDialogTitle("Restore state");
685 int value = chooser.showOpenDialog(this);
687 if (value == JalviewFileChooser.APPROVE_OPTION)
689 String choice = chooser.getSelectedFile().getAbsolutePath();
690 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
691 chooser.getSelectedFile().getParent());
692 new Jalview2XML().LoadJalviewAlign(choice);
696 /* public void vamsasLoad_actionPerformed(ActionEvent e)
698 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
699 getProperty("LAST_DIRECTORY"));
701 chooser.setFileView(new JalviewFileView());
702 chooser.setDialogTitle("Load Vamsas file");
703 chooser.setToolTipText("Import");
705 int value = chooser.showOpenDialog(this);
707 if (value == JalviewFileChooser.APPROVE_OPTION)
709 jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
711 chooser.getSelectedFile().getAbsolutePath()
718 public void inputSequence_actionPerformed(ActionEvent e)
720 new SequenceFetcher(null);
723 JPanel progressPanel;
725 public void startLoading(final String fileName)
727 if (fileLoadingCount == 0)
729 progressPanel = new JPanel(new BorderLayout());
730 JProgressBar progressBar = new JProgressBar();
731 progressBar.setIndeterminate(true);
733 progressPanel.add(new JLabel("Loading File: " + fileName + " "),
736 progressPanel.add(progressBar, BorderLayout.CENTER);
738 instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
744 public void stopLoading()
747 if (fileLoadingCount < 1)
749 if(progressPanel!=null)
751 this.getContentPane().remove(progressPanel);
752 progressPanel = null;
754 fileLoadingCount = 0;
759 public static int getViewCount(String viewId)
762 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
763 for (int t = 0; t < frames.length; t++)
765 if (frames[t] instanceof AlignFrame)
767 AlignFrame af = (AlignFrame) frames[t];
768 for(int a=0; a<af.alignPanels.size(); a++)
771 ((AlignmentPanel)af.alignPanels.elementAt(a)).av.getSequenceSetId() )
781 public void explodeViews(AlignFrame af)
783 int size = af.alignPanels.size();
787 for(int i=0; i<size; i++)
789 AlignmentPanel ap = (AlignmentPanel)af.alignPanels.elementAt(i);
790 AlignFrame newaf = new AlignFrame(ap);
791 if(ap.av.explodedPosition!=null)
792 newaf.setBounds(ap.av.explodedPosition);
794 ap.av.gatherViewsHere = false;
796 addInternalFrame(newaf, af.getTitle(),
797 AlignFrame.DEFAULT_WIDTH,
798 AlignFrame.DEFAULT_HEIGHT);
801 af.alignPanels.clear();
802 af.closeMenuItem_actionPerformed(true);
806 public void gatherViews(AlignFrame source)
808 source.viewport.gatherViewsHere = true;
809 source.viewport.explodedPosition = source.getBounds();
810 JInternalFrame[] frames = desktop.getAllFrames();
811 String viewId = source.viewport.sequenceSetID;
813 for (int t = 0; t < frames.length; t++)
815 if (frames[t] instanceof AlignFrame && frames[t] != source)
817 AlignFrame af = (AlignFrame) frames[t];
818 boolean gatherThis = false;
819 for (int a = 0; a < af.alignPanels.size(); a++)
821 AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(a);
822 if (viewId.equals(ap.av.getSequenceSetId()))
825 ap.av.gatherViewsHere = false;
826 ap.av.explodedPosition = af.getBounds();
827 source.addAlignmentPanel(ap, false);
833 af.alignPanels.clear();
834 af.closeMenuItem_actionPerformed(true);
841 jalview.gui.VamsasClient v_client=null;
842 public void vamsasLoad_actionPerformed(ActionEvent e)
844 if (v_client==null) {
846 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
847 getProperty("LAST_DIRECTORY"));
849 chooser.setFileView(new JalviewFileView());
850 chooser.setDialogTitle("Load Vamsas file");
851 chooser.setToolTipText("Import");
853 int value = chooser.showOpenDialog(this);
855 if (value == JalviewFileChooser.APPROVE_OPTION)
857 v_client = new jalview.gui.VamsasClient(this,
858 chooser.getSelectedFile());
859 this.vamsasLoad.setText("Session Update");
860 this.vamsasStop.setVisible(true);
861 v_client.initial_update();
862 v_client.startWatcher();
865 // store current data in session.
866 v_client.push_update();
869 public void vamsasStop_actionPerformed(ActionEvent e) {
870 if (v_client!=null) {
871 v_client.end_session();
873 this.vamsasStop.setVisible(false);
874 this.vamsasLoad.setText("Start Vamsas Session...");
878 * hide vamsas user gui bits when a vamsas document event is being handled.
879 * @param b true to hide gui, false to reveal gui
881 public void setVamsasUpdate(boolean b) {
882 jalview.bin.Cache.log.debug("Setting gui for Vamsas update "+(b ? "in progress" : "finished"));
883 vamsasLoad.setVisible(!b);
884 vamsasStop.setVisible(!b);