Unix drag and drop implemented
[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 internalFrameActivated(javax.swing.event.
191                                                  InternalFrameEvent evt)
192               {
193                 JInternalFrame itf = desktop.getSelectedFrame();
194                 if (itf != null)
195                   itf.requestFocus();
196
197               }
198
199                 public void internalFrameClosed(
200                     javax.swing.event.InternalFrameEvent evt)
201                 {
202                     openFrameCount--;
203                     windowMenu.remove(menuItem);
204                     JInternalFrame itf = desktop.getSelectedFrame();
205                        if (itf != null)
206                         itf.requestFocus();
207                 }
208                 ;
209             });
210
211         menuItem.addActionListener(new ActionListener()
212             {
213                 public void actionPerformed(ActionEvent e)
214                 {
215                     try
216                     {
217                         frame.setSelected(true);
218                         frame.setIcon(false);
219                     }
220                     catch (java.beans.PropertyVetoException ex)
221                     {
222
223                     }
224                 }
225             });
226
227         windowMenu.add(menuItem);
228
229         desktop.add(frame);
230         frame.toFront();
231         try{
232           frame.setSelected(true);
233           frame.requestFocus();
234         }catch(java.beans.PropertyVetoException ve)
235         {}
236     }
237
238     public void dragEnter(DropTargetDragEvent evt)
239     {}
240
241     public void dragExit(DropTargetEvent evt)
242     {}
243
244     public void dragOver(DropTargetDragEvent evt)
245     {}
246
247     public void dropActionChanged(DropTargetDragEvent evt)
248     {}
249
250     /**
251      * DOCUMENT ME!
252      *
253      * @param evt DOCUMENT ME!
254      */
255     public void drop(DropTargetDropEvent evt)
256     {
257         Transferable t = evt.getTransferable();
258         java.util.List files = null;
259
260         try
261         {
262           DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
263           if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
264           {
265             //Works on Windows and MacOSX
266             evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
267             files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
268           }
269           else if (t.isDataFlavorSupported(uriListFlavor))
270           {
271             // This is used by Unix drag system
272             evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
273             String data = (String) t.getTransferData(uriListFlavor);
274             files = new java.util.ArrayList(1);
275             for (java.util.StringTokenizer st = new java.util.StringTokenizer(
276                 data,
277                 "\r\n");
278                  st.hasMoreTokens(); )
279             {
280               String s = st.nextToken();
281               if (s.startsWith("#"))
282               {
283                 // the line is a comment (as per the RFC 2483)
284                 continue;
285               }
286
287               java.net.URI uri = new java.net.URI(s);
288               java.io.File file = new java.io.File(uri);
289               files.add(file);
290             }
291           }
292         }
293         catch (Exception e)
294         {
295           e.printStackTrace();
296         }
297
298         if (files != null)
299         {
300           try
301           {
302             for (int i = 0; i < files.size(); i++)
303             {
304               String file = files.get(i).toString();
305               String protocol = "File";
306               String format = null;
307
308               if (file.endsWith(".jar"))
309               {
310                 format = "Jalview";
311
312               }
313               else
314               {
315                 format = jalview.io.IdentifyFile.Identify(file,
316                                                           protocol);
317               }
318               LoadFile(file, protocol, format);
319             }
320           }
321           catch (Exception ex)
322           {
323             ex.printStackTrace();
324           }
325         }
326     }
327
328     /**
329      * DOCUMENT ME!
330      *
331      * @param e DOCUMENT ME!
332      */
333     public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)
334     {
335         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
336                     "LAST_DIRECTORY"),
337                 new String[]
338                 {
339                     "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
340                     "jar"
341                 },
342                 new String[]
343                 {
344                     "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
345                 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
346
347         chooser.setFileView(new JalviewFileView());
348         chooser.setDialogTitle("Open local file");
349         chooser.setToolTipText("Open");
350
351         int value = chooser.showOpenDialog(this);
352
353         if (value == JalviewFileChooser.APPROVE_OPTION)
354         {
355             String choice = chooser.getSelectedFile().getPath();
356             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
357                 chooser.getSelectedFile().getParent());
358
359             String format = null;
360             if (chooser.getSelectedFormat().equals("Jalview"))
361             {
362               format = "Jalview";
363             }
364             else
365             {
366                 format = IdentifyFile.Identify(choice, "File");
367             }
368
369             jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
370             LoadFile(choice, "File", format);
371         }
372     }
373
374     /**
375      * DOCUMENT ME!
376      *
377      * @param file DOCUMENT ME!
378      * @param protocol DOCUMENT ME!
379      * @param format DOCUMENT ME!
380      */
381     public void LoadFile(String file, String protocol, String format)
382     {
383       FileLoader fileLoader = new FileLoader();
384       fileLoader.LoadFile(file, protocol, format);
385     }
386
387     /**
388      * DOCUMENT ME!
389      *
390      * @param e DOCUMENT ME!
391      */
392     public void inputURLMenuItem_actionPerformed(ActionEvent e)
393     {
394         Object reply = JOptionPane.showInternalInputDialog(Desktop.desktop,
395                 "Enter url of input file", "Input alignment from URL",
396                 JOptionPane.QUESTION_MESSAGE, null, null, "http://www.");
397
398         if (reply == null)
399         {
400             return;
401         }
402
403         String url = reply.toString().trim();
404
405         if (url.toLowerCase().endsWith(".jar"))
406         {
407                jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalview");
408                Jalview2XML.LoadJalviewAlign(url);
409         }
410         else
411         {
412
413           String format = IdentifyFile.Identify(url, "URL");
414
415           if (format.equals("URL NOT FOUND"))
416           {
417             JOptionPane.showInternalMessageDialog(Desktop.desktop,
418                                                   "Couldn't locate " + url,
419                                                   "URL not found",
420                                                   JOptionPane.WARNING_MESSAGE);
421
422             return;
423           }
424
425           LoadFile(url, "URL", format);
426         }
427     }
428
429     /**
430      * DOCUMENT ME!
431      *
432      * @param e DOCUMENT ME!
433      */
434     public void inputTextboxMenuItem_actionPerformed(ActionEvent e)
435     {
436         CutAndPasteTransfer cap = new CutAndPasteTransfer();
437         cap.setForInput();
438         Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
439     }
440
441     /*
442      * Exit the program
443      */
444     public void quit()
445     {
446         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
447         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
448         jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
449         jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
450         System.exit(0);
451     }
452
453     /**
454      * DOCUMENT ME!
455      *
456      * @param e DOCUMENT ME!
457      */
458     public void aboutMenuItem_actionPerformed(ActionEvent e)
459     {
460       StringBuffer message = new StringBuffer("JalView version " +
461                                               jalview.bin.Cache.getProperty(
462           "VERSION") +
463                                               "; last updated: " +
464                                               jalview.bin.
465                                               Cache.getDefault("BUILD_DATE", "unknown"));
466
467       if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
468           jalview.bin.Cache.getProperty("VERSION")))
469       {
470         message.append("\n\n!! Jalview version "
471                        + jalview.bin.Cache.getProperty("LATEST_VERSION")
472                        + " is available for download from http://www.jalview.org !!\n");
473
474       }
475
476       message.append( "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
477             "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
478             "\nFor all issues relating to Jalview, email help@jalview.org" +
479             "\n\nIf  you use JalView, please cite:" +
480             "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
481             "\nBioinformatics,  2004 12;426-7.");
482
483         JOptionPane.showInternalMessageDialog(Desktop.desktop,
484
485            message.toString(), "About Jalview",
486             JOptionPane.INFORMATION_MESSAGE);
487     }
488
489     /**
490      * DOCUMENT ME!
491      *
492      * @param e DOCUMENT ME!
493      */
494     public void documentationMenuItem_actionPerformed(ActionEvent e)
495     {
496         try
497         {
498             ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
499             java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
500             javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
501
502             javax.help.HelpBroker hb = hs.createHelpBroker();
503             hb.setCurrentID("home");
504             hb.setDisplayed(true);
505         }
506         catch (Exception ex)
507         {
508             ex.printStackTrace();
509         }
510     }
511
512     /**
513      * DOCUMENT ME!
514      *
515      * @param e DOCUMENT ME!
516      */
517     protected void preferences_actionPerformed(ActionEvent e)
518     {
519         new Preferences();
520     }
521
522     /**
523      * DOCUMENT ME!
524      *
525      * @param e DOCUMENT ME!
526      */
527     public void saveState_actionPerformed(ActionEvent e)
528     {
529         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
530                     "LAST_DIRECTORY"), new String[] { "jar" },
531                 new String[] { "Jalview Project" }, "Jalview Project");
532
533         chooser.setFileView(new JalviewFileView());
534         chooser.setDialogTitle("Save State");
535
536         int value = chooser.showSaveDialog(this);
537
538         if (value == JalviewFileChooser.APPROVE_OPTION)
539         {
540             java.io.File choice = chooser.getSelectedFile();
541             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
542             Jalview2XML.SaveState(choice);
543         }
544     }
545
546     /**
547      * DOCUMENT ME!
548      *
549      * @param e DOCUMENT ME!
550      */
551     public void loadState_actionPerformed(ActionEvent e)
552     {
553         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
554                     "LAST_DIRECTORY"), new String[] { "jar" },
555                 new String[] { "Jalview Project" }, "Jalview Project");
556         chooser.setFileView(new JalviewFileView());
557         chooser.setDialogTitle("Restore state");
558
559         int value = chooser.showOpenDialog(this);
560
561         if (value == JalviewFileChooser.APPROVE_OPTION)
562         {
563             String choice = chooser.getSelectedFile().getAbsolutePath();
564             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
565                 chooser.getSelectedFile().getParent());
566             Jalview2XML.LoadJalviewAlign(choice);
567         }
568     }
569
570   /*  public void vamsasLoad_actionPerformed(ActionEvent e)
571     {
572       JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
573           getProperty("LAST_DIRECTORY"));
574
575       chooser.setFileView(new JalviewFileView());
576       chooser.setDialogTitle("Load Vamsas file");
577       chooser.setToolTipText("Import");
578
579       int value = chooser.showOpenDialog(this);
580
581       if (value == JalviewFileChooser.APPROVE_OPTION)
582       {
583         jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
584         vs.load(
585             chooser.getSelectedFile().getAbsolutePath()
586             );
587       }
588
589     }*/
590
591
592     public void inputSequence_actionPerformed(ActionEvent e)
593     {
594       SequenceFetcher sf = new SequenceFetcher(null);
595     }
596 }
597