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