Make URL input window bigger
[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       // This construct allows us to have a wider textfield
395       // for viewing
396       JLabel label = new JLabel("Enter URL of Input File");
397       JTextField textinput = new JTextField("http://www.", 40);
398       JPanel panel = new JPanel(new BorderLayout());
399       panel.add(label, BorderLayout.NORTH);
400       panel.add(textinput, BorderLayout.SOUTH);
401
402
403        int reply = JOptionPane.showInternalConfirmDialog(desktop,
404           panel, "Input Alignment From URL",
405           JOptionPane.OK_CANCEL_OPTION );
406
407
408         if (reply != JOptionPane.OK_OPTION )
409         {
410             return;
411         }
412
413         String url = textinput.getText();
414
415         if (url.toLowerCase().endsWith(".jar"))
416         {
417                jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalview");
418                Jalview2XML.LoadJalviewAlign(url);
419         }
420         else
421         {
422
423           String format = IdentifyFile.Identify(url, "URL");
424
425           if (format.equals("URL NOT FOUND"))
426           {
427             JOptionPane.showInternalMessageDialog(Desktop.desktop,
428                                                   "Couldn't locate " + url,
429                                                   "URL not found",
430                                                   JOptionPane.WARNING_MESSAGE);
431
432             return;
433           }
434
435           LoadFile(url, "URL", format);
436         }
437     }
438
439     /**
440      * DOCUMENT ME!
441      *
442      * @param e DOCUMENT ME!
443      */
444     public void inputTextboxMenuItem_actionPerformed(ActionEvent e)
445     {
446         CutAndPasteTransfer cap = new CutAndPasteTransfer();
447         cap.setForInput();
448         Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
449     }
450
451     /*
452      * Exit the program
453      */
454     public void quit()
455     {
456         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
457         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
458         jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
459         jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
460         System.exit(0);
461     }
462
463     /**
464      * DOCUMENT ME!
465      *
466      * @param e DOCUMENT ME!
467      */
468     public void aboutMenuItem_actionPerformed(ActionEvent e)
469     {
470       StringBuffer message = new StringBuffer("JalView version " +
471                                               jalview.bin.Cache.getProperty(
472           "VERSION") +
473                                               "; last updated: " +
474                                               jalview.bin.
475                                               Cache.getDefault("BUILD_DATE", "unknown"));
476
477       if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
478           jalview.bin.Cache.getProperty("VERSION")))
479       {
480         message.append("\n\n!! Jalview version "
481                        + jalview.bin.Cache.getProperty("LATEST_VERSION")
482                        + " is available for download from http://www.jalview.org !!\n");
483
484       }
485
486       message.append( "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
487             "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
488             "\nFor all issues relating to Jalview, email help@jalview.org" +
489             "\n\nIf  you use JalView, please cite:" +
490             "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
491             "\nBioinformatics,  2004 12;426-7.");
492
493         JOptionPane.showInternalMessageDialog(Desktop.desktop,
494
495            message.toString(), "About Jalview",
496             JOptionPane.INFORMATION_MESSAGE);
497     }
498
499     /**
500      * DOCUMENT ME!
501      *
502      * @param e DOCUMENT ME!
503      */
504     public void documentationMenuItem_actionPerformed(ActionEvent e)
505     {
506         try
507         {
508             ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
509             java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
510             javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
511
512             javax.help.HelpBroker hb = hs.createHelpBroker();
513             hb.setCurrentID("home");
514             hb.setDisplayed(true);
515         }
516         catch (Exception ex)
517         {
518             ex.printStackTrace();
519         }
520     }
521
522     /**
523      * DOCUMENT ME!
524      *
525      * @param e DOCUMENT ME!
526      */
527     protected void preferences_actionPerformed(ActionEvent e)
528     {
529         new Preferences();
530     }
531
532     /**
533      * DOCUMENT ME!
534      *
535      * @param e DOCUMENT ME!
536      */
537     public void saveState_actionPerformed(ActionEvent e)
538     {
539         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
540                     "LAST_DIRECTORY"), new String[] { "jar" },
541                 new String[] { "Jalview Project" }, "Jalview Project");
542
543         chooser.setFileView(new JalviewFileView());
544         chooser.setDialogTitle("Save State");
545
546         int value = chooser.showSaveDialog(this);
547
548         if (value == JalviewFileChooser.APPROVE_OPTION)
549         {
550             java.io.File choice = chooser.getSelectedFile();
551             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
552             Jalview2XML.SaveState(choice);
553         }
554     }
555
556     /**
557      * DOCUMENT ME!
558      *
559      * @param e DOCUMENT ME!
560      */
561     public void loadState_actionPerformed(ActionEvent e)
562     {
563         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
564                     "LAST_DIRECTORY"), new String[] { "jar" },
565                 new String[] { "Jalview Project" }, "Jalview Project");
566         chooser.setFileView(new JalviewFileView());
567         chooser.setDialogTitle("Restore state");
568
569         int value = chooser.showOpenDialog(this);
570
571         if (value == JalviewFileChooser.APPROVE_OPTION)
572         {
573             String choice = chooser.getSelectedFile().getAbsolutePath();
574             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
575                 chooser.getSelectedFile().getParent());
576             Jalview2XML.LoadJalviewAlign(choice);
577         }
578     }
579
580   /*  public void vamsasLoad_actionPerformed(ActionEvent e)
581     {
582       JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
583           getProperty("LAST_DIRECTORY"));
584
585       chooser.setFileView(new JalviewFileView());
586       chooser.setDialogTitle("Load Vamsas file");
587       chooser.setToolTipText("Import");
588
589       int value = chooser.showOpenDialog(this);
590
591       if (value == JalviewFileChooser.APPROVE_OPTION)
592       {
593         jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
594         vs.load(
595             chooser.getSelectedFile().getAbsolutePath()
596             );
597       }
598
599     }*/
600
601
602     public void inputSequence_actionPerformed(ActionEvent e)
603     {
604       SequenceFetcher sf = new SequenceFetcher(null);
605     }
606 }
607