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;
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();
211 public void internalFrameClosed(
212 javax.swing.event.InternalFrameEvent evt)
215 windowMenu.remove(menuItem);
216 JInternalFrame itf = desktop.getSelectedFrame();
223 menuItem.addActionListener(new ActionListener()
225 public void actionPerformed(ActionEvent e)
229 frame.setSelected(true);
230 frame.setIcon(false);
232 catch (java.beans.PropertyVetoException ex)
239 windowMenu.add(menuItem);
244 frame.setSelected(true);
245 frame.requestFocus();
246 }catch(java.beans.PropertyVetoException ve)
250 public void lostOwnership(Clipboard clipboard, Transferable contents)
252 Desktop.jalviewClipboard = null;
255 public void dragEnter(DropTargetDragEvent evt)
258 public void dragExit(DropTargetEvent evt)
261 public void dragOver(DropTargetDragEvent evt)
264 public void dropActionChanged(DropTargetDragEvent evt)
270 * @param evt DOCUMENT ME!
272 public void drop(DropTargetDropEvent evt)
274 Transferable t = evt.getTransferable();
275 java.util.List files = null;
279 DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
280 if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
282 //Works on Windows and MacOSX
283 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
284 files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
286 else if (t.isDataFlavorSupported(uriListFlavor))
288 // This is used by Unix drag system
289 evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
290 String data = (String) t.getTransferData(uriListFlavor);
291 files = new java.util.ArrayList(1);
292 for (java.util.StringTokenizer st = new java.util.StringTokenizer(
295 st.hasMoreTokens(); )
297 String s = st.nextToken();
298 if (s.startsWith("#"))
300 // the line is a comment (as per the RFC 2483)
304 java.net.URI uri = new java.net.URI(s);
305 java.io.File file = new java.io.File(uri);
319 for (int i = 0; i < files.size(); i++)
321 String file = files.get(i).toString();
322 String protocol = FormatAdapter.FILE;
323 String format = null;
325 if (file.endsWith(".jar"))
332 format = new IdentifyFile().Identify(file,
337 new FileLoader().LoadFile(file, protocol, format);
343 ex.printStackTrace();
351 * @param e DOCUMENT ME!
353 public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
355 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
359 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
364 "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
365 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
367 chooser.setFileView(new JalviewFileView());
368 chooser.setDialogTitle("Open local file");
369 chooser.setToolTipText("Open");
371 int value = chooser.showOpenDialog(this);
373 if (value == JalviewFileChooser.APPROVE_OPTION)
375 String choice = chooser.getSelectedFile().getPath();
376 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
377 chooser.getSelectedFile().getParent());
379 String format = null;
380 if (chooser.getSelectedFormat().equals("Jalview"))
386 format = new IdentifyFile().Identify(choice, FormatAdapter.FILE);
389 if (viewport != null)
390 new FileLoader().LoadFile(viewport, choice, FormatAdapter.FILE, format);
392 new FileLoader().LoadFile(choice, FormatAdapter.FILE, format);
401 * @param e DOCUMENT ME!
403 public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
405 // This construct allows us to have a wider textfield
407 JLabel label = new JLabel("Enter URL of Input File");
408 final JComboBox history = new JComboBox();
410 JPanel panel = new JPanel(new GridLayout(2,1));
413 history.setPreferredSize(new Dimension(400,20));
414 history.setEditable(true);
415 history.addItem("http://www.");
417 String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
421 if (historyItems != null)
423 st = new StringTokenizer(historyItems, "\t");
425 while (st.hasMoreTokens())
427 history.addItem(st.nextElement());
431 int reply = JOptionPane.showInternalConfirmDialog(desktop,
432 panel, "Input Alignment From URL",
433 JOptionPane.OK_CANCEL_OPTION );
436 if (reply != JOptionPane.OK_OPTION )
441 String url = history.getSelectedItem().toString();
443 if (url.toLowerCase().endsWith(".jar"))
445 if (viewport != null)
446 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, "Jalview");
448 new FileLoader().LoadFile(url, FormatAdapter.URL, "Jalview");
452 String format = new IdentifyFile().Identify(url, FormatAdapter.URL);
454 if (format.equals("URL NOT FOUND"))
456 JOptionPane.showInternalMessageDialog(Desktop.desktop,
457 "Couldn't locate " + url,
459 JOptionPane.WARNING_MESSAGE);
464 if (viewport != null)
465 new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, format);
467 new FileLoader().LoadFile(url, FormatAdapter.URL, format);
474 * @param e DOCUMENT ME!
476 public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport)
478 CutAndPasteTransfer cap = new CutAndPasteTransfer();
479 cap.setForInput(viewport);
480 Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
488 jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
489 jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
490 jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
491 jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
498 * @param e DOCUMENT ME!
500 public void aboutMenuItem_actionPerformed(ActionEvent e)
502 StringBuffer message = new StringBuffer("JalView version " +
503 jalview.bin.Cache.getProperty(
507 Cache.getDefault("BUILD_DATE", "unknown"));
509 if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
510 jalview.bin.Cache.getProperty("VERSION")))
512 message.append("\n\n!! Jalview version "
513 + jalview.bin.Cache.getProperty("LATEST_VERSION")
514 + " is available for download from http://www.jalview.org !!\n");
518 message.append( "\nAuthors: Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
519 "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
520 "\nFor all issues relating to Jalview, email help@jalview.org" +
521 "\n\nIf you use JalView, please cite:" +
522 "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
523 "\nBioinformatics, 2004 20;426-7.");
525 JOptionPane.showInternalMessageDialog(Desktop.desktop,
527 message.toString(), "About Jalview",
528 JOptionPane.INFORMATION_MESSAGE);
534 * @param e DOCUMENT ME!
536 public void documentationMenuItem_actionPerformed(ActionEvent e)
540 ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
541 java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
542 javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
544 javax.help.HelpBroker hb = hs.createHelpBroker();
545 hb.setCurrentID("home");
546 hb.setDisplayed(true);
550 ex.printStackTrace();
557 * @param e DOCUMENT ME!
559 protected void preferences_actionPerformed(ActionEvent e)
567 * @param e DOCUMENT ME!
569 public void saveState_actionPerformed(ActionEvent e)
571 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
572 "LAST_DIRECTORY"), new String[] { "jar" },
573 new String[] { "Jalview Project" }, "Jalview Project");
575 chooser.setFileView(new JalviewFileView());
576 chooser.setDialogTitle("Save State");
578 int value = chooser.showSaveDialog(this);
580 if (value == JalviewFileChooser.APPROVE_OPTION)
582 java.io.File choice = chooser.getSelectedFile();
583 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
584 new Jalview2XML().SaveState(choice);
591 * @param e DOCUMENT ME!
593 public void loadState_actionPerformed(ActionEvent e)
595 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
596 "LAST_DIRECTORY"), new String[] { "jar" },
597 new String[] { "Jalview Project" }, "Jalview Project");
598 chooser.setFileView(new JalviewFileView());
599 chooser.setDialogTitle("Restore state");
601 int value = chooser.showOpenDialog(this);
603 if (value == JalviewFileChooser.APPROVE_OPTION)
605 String choice = chooser.getSelectedFile().getAbsolutePath();
606 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
607 chooser.getSelectedFile().getParent());
608 new Jalview2XML().LoadJalviewAlign(choice);
612 /* public void vamsasLoad_actionPerformed(ActionEvent e)
614 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
615 getProperty("LAST_DIRECTORY"));
617 chooser.setFileView(new JalviewFileView());
618 chooser.setDialogTitle("Load Vamsas file");
619 chooser.setToolTipText("Import");
621 int value = chooser.showOpenDialog(this);
623 if (value == JalviewFileChooser.APPROVE_OPTION)
625 jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
627 chooser.getSelectedFile().getAbsolutePath()
634 public void inputSequence_actionPerformed(ActionEvent e)
636 new SequenceFetcher(null);
639 JPanel progressPanel;
641 public void startLoading(final String fileName)
643 if (fileLoadingCount == 0)
645 progressPanel = new JPanel(new BorderLayout());
646 JProgressBar progressBar = new JProgressBar();
647 progressBar.setIndeterminate(true);
649 progressPanel.add(new JLabel("Loading File: " + fileName + " "),
652 progressPanel.add(progressBar, BorderLayout.CENTER);
654 instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
660 public void stopLoading()
663 if (fileLoadingCount < 1)
665 if(progressPanel!=null)
667 this.getContentPane().remove(progressPanel);
668 progressPanel = null;
670 fileLoadingCount = 0;
675 public static int getViewCount(String viewId)
678 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
679 for (int t = 0; t < frames.length; t++)
681 if (frames[t] instanceof AlignFrame)
683 AlignFrame af = (AlignFrame) frames[t];
684 for(int a=0; a<af.alignPanels.size(); a++)
687 ((AlignmentPanel)af.alignPanels.elementAt(a)).av.getSequenceSetId() )
697 public void explodeViews(AlignFrame af)
699 int size = af.alignPanels.size();
702 af.closeMenuItem_actionPerformed(null);
704 for(int i=0; i<size; i++)
706 AlignmentPanel ap = (AlignmentPanel)af.alignPanels.elementAt(i);
707 AlignFrame newaf = new AlignFrame(ap);
708 if(ap.av.explodedPosition!=null)
709 newaf.setBounds(ap.av.explodedPosition);
711 ap.av.gatherViewsHere = false;
713 PaintRefresher.Register(ap.seqPanel.seqCanvas, ap.av.getSequenceSetId());
714 PaintRefresher.Register(ap.idPanel.idCanvas, ap.av.getSequenceSetId());
715 PaintRefresher.Register(ap, ap.av.getSequenceSetId());
718 newaf.viewport = ap.av;
719 addInternalFrame(newaf, af.getTitle(),
720 AlignFrame.DEFAULT_WIDTH,
721 AlignFrame.DEFAULT_HEIGHT);
726 public void gatherViews(AlignFrame source)
728 source.viewport.gatherViewsHere = true;
729 source.viewport.explodedPosition = source.getBounds();
730 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
731 String viewId = source.viewport.sequenceSetID;
733 for (int t = 0; t < frames.length; t++)
735 if (frames[t] instanceof AlignFrame && frames[t] != source)
737 AlignFrame af = (AlignFrame) frames[t];
738 boolean gatherThis = false;
739 for (int a = 0; a < af.alignPanels.size(); a++)
741 AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(a);
742 if (viewId.equals(ap.av.getSequenceSetId()))
745 ap.av.gatherViewsHere = false;
746 ap.av.explodedPosition = af.getBounds();
747 source.addAlignmentPanel(ap);
751 af.closeMenuItem_actionPerformed(null);
757 jalview.gui.VamsasClient v_client=null;
758 public void vamsasLoad_actionPerformed(ActionEvent e)
760 if (v_client==null) {
762 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
763 getProperty("LAST_DIRECTORY"));
765 chooser.setFileView(new JalviewFileView());
766 chooser.setDialogTitle("Load Vamsas file");
767 chooser.setToolTipText("Import");
769 int value = chooser.showOpenDialog(this);
771 if (value == JalviewFileChooser.APPROVE_OPTION)
773 v_client = new jalview.gui.VamsasClient(this,
774 chooser.getSelectedFile());
775 this.vamsasLoad.setText("Session Update");
776 this.vamsasStop.setVisible(true);
777 v_client.initial_update();
778 v_client.startWatcher();
781 // store current data in session.
782 v_client.push_update();
785 public void vamsasStop_actionPerformed(ActionEvent e) {
786 if (v_client!=null) {
787 v_client.end_session();
789 this.vamsasStop.setVisible(false);
790 this.vamsasLoad.setText("Start Vamsas Session...");
794 * hide vamsas user gui bits when a vamsas document event is being handled.
795 * @param b true to hide gui, false to reveal gui
797 public void setVamsasUpdate(boolean b) {
798 jalview.bin.Cache.log.debug("Setting gui for Vamsas update "+(b ? "in progress" : "finished"));
799 vamsasLoad.setVisible(!b);
800 vamsasStop.setVisible(!b);