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