SequenceFetcher new class
[jalview.git] / src / jalview / gui / Desktop.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.gui;
20
21 import jalview.io.*;
22
23 import java.awt.*;
24 import java.awt.datatransfer.*;
25 import java.awt.dnd.*;
26 import java.awt.event.*;
27
28 import javax.swing.*;
29
30
31 /**
32  * DOCUMENT ME!
33  *
34  * @author $author$
35  * @version $Revision$
36  */
37 public class Desktop extends jalview.jbgui.GDesktop
38     implements DropTargetListener
39 {
40     /** DOCUMENT ME!! */
41     public static JDesktopPane desktop;
42     static int openFrameCount = 0;
43     static final int xOffset = 30;
44     static final int yOffset = 30;
45     public static jalview.ws.Discoverer discoverer;
46
47     public static Object [] jalviewClipboard;
48
49
50     /**
51      * Creates a new Desktop object.
52      */
53     public Desktop()
54     {
55         Image image = null;
56
57
58         try
59         {
60             java.net.URL url = getClass().getResource("/images/logo.gif");
61
62             if (url != null)
63             {
64                 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
65
66                 MediaTracker mt = new MediaTracker(this);
67                 mt.addImage(image, 0);
68                 mt.waitForID(0);
69                 setIconImage(image);
70             }
71         }
72         catch (Exception ex)
73         {
74         }
75
76         setTitle("Jalview "+jalview.bin.Cache.getProperty("VERSION"));
77         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
78         desktop = new JDesktopPane();
79         desktop.setBackground(Color.white);
80         setContentPane(desktop);
81         desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
82
83         // This line prevents Windows Look&Feel resizing all new windows to maximum
84         // if previous window was maximised
85         desktop.setDesktopManager(new DefaultDesktopManager());
86
87         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
88         String x = jalview.bin.Cache.getProperty("SCREEN_X");
89         String y = jalview.bin.Cache.getProperty("SCREEN_Y");
90         String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");
91         String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");
92
93         if ((x != null) && (y != null) && (width != null) && (height != null))
94         {
95             setBounds(Integer.parseInt(x), Integer.parseInt(y),
96                 Integer.parseInt(width), Integer.parseInt(height));
97         }
98         else
99         {
100             setBounds((int) (screenSize.width - 900) / 2,
101                 (int) (screenSize.height - 650) / 2, 900, 650);
102         }
103
104         this.addWindowListener(new WindowAdapter()
105             {
106                 public void windowClosing(WindowEvent evt)
107                 {
108                     quit();
109                 }
110             });
111
112         this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
113
114         /////////Add a splashscreen on startup
115         /////////Add a splashscreen on startup
116         JInternalFrame frame = new JInternalFrame();
117
118         SplashScreen splash = new SplashScreen(frame, image);
119         frame.setContentPane(splash);
120         frame.setLayer(JLayeredPane.PALETTE_LAYER);
121         frame.setLocation((int) ((getWidth() - 750) / 2),
122             (int) ((getHeight() - 160) / 2));
123
124         addInternalFrame(frame, "", 750, 160, false);
125
126         discoverer=new jalview.ws.Discoverer(); // Only gets started if gui is displayed.
127
128     }
129
130     /**
131      * DOCUMENT ME!
132      *
133      * @param frame DOCUMENT ME!
134      * @param title DOCUMENT ME!
135      * @param w DOCUMENT ME!
136      * @param h DOCUMENT ME!
137      */
138     public static synchronized void addInternalFrame(final JInternalFrame frame,
139         String title, int w, int h)
140     {
141         addInternalFrame(frame, title, w, h, true);
142     }
143
144     /**
145      * DOCUMENT ME!
146      *
147      * @param frame DOCUMENT ME!
148      * @param title DOCUMENT ME!
149      * @param w DOCUMENT ME!
150      * @param h DOCUMENT ME!
151      * @param resizable DOCUMENT ME!
152      */
153     public static synchronized void addInternalFrame(final JInternalFrame frame,
154         String title, int w, int h, boolean resizable)
155     {
156
157       frame.setTitle(title);
158       if(frame.getWidth()<1 || frame.getHeight()<1)
159       {
160         frame.setSize(w, h);
161       }
162       // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
163       // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
164       // IF JALVIEW IS RUNNING HEADLESS
165       /////////////////////////////////////////////////
166       if (System.getProperty("java.awt.headless") != null
167           && System.getProperty("java.awt.headless").equals("true"))
168       {
169         return;
170       }
171
172
173         openFrameCount++;
174
175         frame.setVisible(true);
176         frame.setClosable(true);
177         frame.setResizable(resizable);
178         frame.setMaximizable(resizable);
179         frame.setIconifiable(resizable);
180         frame.setFrameIcon(null);
181
182         if (frame.getX()<1 && frame.getY()<1)
183        {
184          frame.setLocation(xOffset * openFrameCount, yOffset * ((openFrameCount-1)%10)+yOffset);
185        }
186
187         final JMenuItem menuItem = new JMenuItem(title);
188         frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
189             {
190                 public void internalFrameClosed(
191                     javax.swing.event.InternalFrameEvent evt)
192                 {
193                     openFrameCount--;
194                     windowMenu.remove(menuItem);
195                     JInternalFrame itf = desktop.getSelectedFrame();
196                        if (itf != null)
197                         itf.requestFocus();
198                 }
199                 ;
200             });
201
202         menuItem.addActionListener(new ActionListener()
203             {
204                 public void actionPerformed(ActionEvent e)
205                 {
206                     try
207                     {
208                         frame.setSelected(true);
209                         frame.setIcon(false);
210                     }
211                     catch (java.beans.PropertyVetoException ex)
212                     {
213
214                     }
215                 }
216             });
217
218         windowMenu.add(menuItem);
219
220         desktop.add(frame);
221         frame.toFront();
222         try{
223           frame.setSelected(true);
224           frame.requestFocus();
225         }catch(java.beans.PropertyVetoException ve)
226         {}
227     }
228
229
230     /**
231      * DOCUMENT ME!
232      *
233      * @param evt DOCUMENT ME!
234      */
235     public void dragEnter(DropTargetDragEvent evt)
236     {
237     }
238
239     /**
240      * DOCUMENT ME!
241      *
242      * @param evt DOCUMENT ME!
243      */
244     public void dragExit(DropTargetEvent evt)
245     {
246     }
247
248     /**
249      * DOCUMENT ME!
250      *
251      * @param evt DOCUMENT ME!
252      */
253     public void dragOver(DropTargetDragEvent evt)
254     {
255     }
256
257     /**
258      * DOCUMENT ME!
259      *
260      * @param evt DOCUMENT ME!
261      */
262     public void dropActionChanged(DropTargetDragEvent evt)
263     {
264     }
265
266     /**
267      * DOCUMENT ME!
268      *
269      * @param evt DOCUMENT ME!
270      */
271     public void drop(DropTargetDropEvent evt)
272     {
273         Transferable t = evt.getTransferable();
274
275         if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
276         {
277             return;
278         }
279
280         evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
281
282         try
283         {
284             java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
285
286             for (int i = 0; i < files.size(); i++)
287             {
288                 String file = files.get(i).toString();
289                 String protocol = "File";
290                 String format = null;
291
292                 if (file.endsWith(".jar"))
293                 {
294                   format = "Jalview";
295
296                 }
297                 else
298                 {
299                     format = jalview.io.IdentifyFile.Identify(file,
300                             protocol);
301                 }
302                 LoadFile(file, protocol, format);
303             }
304         }
305         catch (Exception ex)
306         {
307             ex.printStackTrace();
308         }
309     }
310
311     /**
312      * DOCUMENT ME!
313      *
314      * @param e DOCUMENT ME!
315      */
316     public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)
317     {
318         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
319                     "LAST_DIRECTORY"),
320                 new String[]
321                 {
322                     "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
323                     "jar"
324                 },
325                 new String[]
326                 {
327                     "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
328                 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
329
330         chooser.setFileView(new JalviewFileView());
331         chooser.setDialogTitle("Open local file");
332         chooser.setToolTipText("Open");
333
334         int value = chooser.showOpenDialog(this);
335
336         if (value == JalviewFileChooser.APPROVE_OPTION)
337         {
338             String choice = chooser.getSelectedFile().getPath();
339             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
340                 chooser.getSelectedFile().getParent());
341
342             String format = null;
343             if (chooser.getSelectedFormat().equals("Jalview"))
344             {
345               format = "Jalview";
346             }
347             else
348             {
349                 format = IdentifyFile.Identify(choice, "File");
350             }
351
352             jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
353             LoadFile(choice, "File", format);
354         }
355     }
356
357     /**
358      * DOCUMENT ME!
359      *
360      * @param file DOCUMENT ME!
361      * @param protocol DOCUMENT ME!
362      * @param format DOCUMENT ME!
363      */
364     public void LoadFile(String file, String protocol, String format)
365     {
366       FileLoader fileLoader = new FileLoader();
367       fileLoader.LoadFile(file, protocol, format);
368     }
369
370     /**
371      * DOCUMENT ME!
372      *
373      * @param e DOCUMENT ME!
374      */
375     public void inputURLMenuItem_actionPerformed(ActionEvent e)
376     {
377         Object reply = JOptionPane.showInternalInputDialog(Desktop.desktop,
378                 "Enter url of input file", "Input alignment from URL",
379                 JOptionPane.QUESTION_MESSAGE, null, null, "http://www.");
380
381         if (reply == null)
382         {
383             return;
384         }
385
386         String url = reply.toString().trim();
387
388         if (url.toLowerCase().endsWith(".jar"))
389         {
390                jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalview");
391                Jalview2XML.LoadJalviewAlign(url);
392         }
393         else
394         {
395
396           String format = IdentifyFile.Identify(url, "URL");
397
398           if (format.equals("URL NOT FOUND"))
399           {
400             JOptionPane.showInternalMessageDialog(Desktop.desktop,
401                                                   "Couldn't locate " + url,
402                                                   "URL not found",
403                                                   JOptionPane.WARNING_MESSAGE);
404
405             return;
406           }
407
408           LoadFile(url, "URL", format);
409         }
410     }
411
412     /**
413      * DOCUMENT ME!
414      *
415      * @param e DOCUMENT ME!
416      */
417     public void inputTextboxMenuItem_actionPerformed(ActionEvent e)
418     {
419         CutAndPasteTransfer cap = new CutAndPasteTransfer();
420         cap.setForInput();
421         Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
422     }
423
424     /*
425      * Exit the program
426      */
427     public void quit()
428     {
429         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
430         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
431         jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
432         jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
433         System.exit(0);
434     }
435
436     /**
437      * DOCUMENT ME!
438      *
439      * @param e DOCUMENT ME!
440      */
441     public void aboutMenuItem_actionPerformed(ActionEvent e)
442     {
443       StringBuffer message = new StringBuffer("JalView 2005 version " +
444                                               jalview.bin.Cache.getProperty(
445           "VERSION") +
446                                               "; last updated: " +
447                                               jalview.bin.
448                                               Cache.getDefault("BUILD_DATE", "unknown"));
449
450       if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
451           jalview.bin.Cache.getProperty("VERSION")))
452       {
453         message.append("\n\n!! Jalview version "
454                        + jalview.bin.Cache.getProperty("LATEST_VERSION")
455                        + " is available for download from http://www.jalview.org !!\n");
456
457       }
458
459       message.append( "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
460             "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
461             "\nFor all issues relating to Jalview, email help@jalview.org" +
462             "\n\nIf  you use JalView, please cite:" +
463             "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
464             "\nBioinformatics,  2004 12;426-7.");
465
466         JOptionPane.showInternalMessageDialog(Desktop.desktop,
467
468            message.toString(), "About Jalview",
469             JOptionPane.INFORMATION_MESSAGE);
470     }
471
472     /**
473      * DOCUMENT ME!
474      *
475      * @param e DOCUMENT ME!
476      */
477     public void documentationMenuItem_actionPerformed(ActionEvent e)
478     {
479         try
480         {
481             ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
482             java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
483             javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
484
485             javax.help.HelpBroker hb = hs.createHelpBroker();
486             hb.setCurrentID("home");
487             hb.setDisplayed(true);
488         }
489         catch (Exception ex)
490         {
491             ex.printStackTrace();
492         }
493     }
494
495     /**
496      * DOCUMENT ME!
497      *
498      * @param e DOCUMENT ME!
499      */
500     protected void preferences_actionPerformed(ActionEvent e)
501     {
502         new Preferences();
503     }
504
505     /**
506      * DOCUMENT ME!
507      *
508      * @param e DOCUMENT ME!
509      */
510     public void saveState_actionPerformed(ActionEvent e)
511     {
512         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
513                     "LAST_DIRECTORY"), new String[] { "jar" },
514                 new String[] { "Jalview Project" }, "Jalview Project");
515
516         chooser.setFileView(new JalviewFileView());
517         chooser.setDialogTitle("Save State");
518
519         int value = chooser.showSaveDialog(this);
520
521         if (value == JalviewFileChooser.APPROVE_OPTION)
522         {
523             java.io.File choice = chooser.getSelectedFile();
524             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
525             Jalview2XML.SaveState(choice);
526         }
527     }
528
529     /**
530      * DOCUMENT ME!
531      *
532      * @param e DOCUMENT ME!
533      */
534     public void loadState_actionPerformed(ActionEvent e)
535     {
536         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
537                     "LAST_DIRECTORY"), new String[] { "jar" },
538                 new String[] { "Jalview Project" }, "Jalview Project");
539         chooser.setFileView(new JalviewFileView());
540         chooser.setDialogTitle("Restore state");
541
542         int value = chooser.showOpenDialog(this);
543
544         if (value == JalviewFileChooser.APPROVE_OPTION)
545         {
546             String choice = chooser.getSelectedFile().getAbsolutePath();
547             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
548                 chooser.getSelectedFile().getParent());
549             Jalview2XML.LoadJalviewAlign(choice);
550         }
551     }
552
553     public void vamsasLoad_actionPerformed(ActionEvent e)
554     {
555       JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
556           getProperty("LAST_DIRECTORY"));
557
558       chooser.setFileView(new JalviewFileView());
559       chooser.setDialogTitle("Load Vamsas file");
560       chooser.setToolTipText("Import");
561
562       int value = chooser.showOpenDialog(this);
563
564       if (value == JalviewFileChooser.APPROVE_OPTION)
565       {
566         jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
567         vs.load(
568             chooser.getSelectedFile().getAbsolutePath()
569             );
570       }
571
572     }
573
574
575     public void inputSequence_actionPerformed(ActionEvent e)
576     {
577       SequenceFetcher sf = new SequenceFetcher(null);
578     }
579 }
580