Dont setSelected on new frames
[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      * DOCUMENT ME!
126      *
127      * @param frame DOCUMENT ME!
128      * @param title DOCUMENT ME!
129      * @param w DOCUMENT ME!
130      * @param h DOCUMENT ME!
131      */
132     public static synchronized void addInternalFrame(final JInternalFrame frame,
133         String title, int w, int h)
134     {
135         addInternalFrame(frame, title, w, h, true);
136     }
137
138     /**
139      * DOCUMENT ME!
140      *
141      * @param frame DOCUMENT ME!
142      * @param title DOCUMENT ME!
143      * @param w DOCUMENT ME!
144      * @param h DOCUMENT ME!
145      * @param resizable DOCUMENT ME!
146      */
147     public static synchronized void addInternalFrame(final JInternalFrame frame,
148         String title, int w, int h, boolean resizable)
149     {
150
151       frame.setTitle(title);
152       if(frame.getWidth()<1 || frame.getHeight()<1)
153       {
154         frame.setSize(w, h);
155       }
156       // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
157       // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
158       // IF JALVIEW IS RUNNING HEADLESS
159       /////////////////////////////////////////////////
160       if (System.getProperty("java.awt.headless") != null
161           && System.getProperty("java.awt.headless").equals("true"))
162       {
163         return;
164       }
165
166
167         openFrameCount++;
168
169         frame.setVisible(true);
170         frame.setClosable(true);
171         frame.setResizable(resizable);
172         frame.setMaximizable(resizable);
173         frame.setIconifiable(resizable);
174         frame.setFrameIcon(null);
175
176         if (frame.getX()<1 && frame.getY()<1)
177        {
178          frame.setLocation(xOffset * openFrameCount, yOffset * ((openFrameCount-1)%10)+yOffset);
179        }
180
181        desktop.add(frame);
182        frame.toFront();
183
184
185         final JMenuItem menuItem = new JMenuItem(title);
186         frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
187             {
188                 public void internalFrameClosed(
189                     javax.swing.event.InternalFrameEvent evt)
190                 {
191                     openFrameCount--;
192                     windowMenu.remove(menuItem);
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
216     /**
217      * DOCUMENT ME!
218      *
219      * @param evt DOCUMENT ME!
220      */
221     public void dragEnter(DropTargetDragEvent evt)
222     {
223     }
224
225     /**
226      * DOCUMENT ME!
227      *
228      * @param evt DOCUMENT ME!
229      */
230     public void dragExit(DropTargetEvent evt)
231     {
232     }
233
234     /**
235      * DOCUMENT ME!
236      *
237      * @param evt DOCUMENT ME!
238      */
239     public void dragOver(DropTargetDragEvent evt)
240     {
241     }
242
243     /**
244      * DOCUMENT ME!
245      *
246      * @param evt DOCUMENT ME!
247      */
248     public void dropActionChanged(DropTargetDragEvent evt)
249     {
250     }
251
252     /**
253      * DOCUMENT ME!
254      *
255      * @param evt DOCUMENT ME!
256      */
257     public void drop(DropTargetDropEvent evt)
258     {
259         Transferable t = evt.getTransferable();
260
261         if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
262         {
263             return;
264         }
265
266         evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
267
268         try
269         {
270             java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
271
272             for (int i = 0; i < files.size(); i++)
273             {
274                 String file = files.get(i).toString();
275                 String protocol = "File";
276                 String format = null;
277
278                 if (file.endsWith(".jar"))
279                 {
280                   format = "Jalview";
281
282                 }
283                 else
284                 {
285                     format = jalview.io.IdentifyFile.Identify(file,
286                             protocol);
287                 }
288                 LoadFile(file, protocol, format);
289             }
290         }
291         catch (Exception ex)
292         {
293             ex.printStackTrace();
294         }
295     }
296
297     /**
298      * DOCUMENT ME!
299      *
300      * @param e DOCUMENT ME!
301      */
302     public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)
303     {
304         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
305                     "LAST_DIRECTORY"),
306                 new String[]
307                 {
308                     "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
309                     "jar"
310                 },
311                 new String[]
312                 {
313                     "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
314                 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
315
316         chooser.setFileView(new JalviewFileView());
317         chooser.setDialogTitle("Open local file");
318         chooser.setToolTipText("Open");
319
320         int value = chooser.showOpenDialog(this);
321
322         if (value == JalviewFileChooser.APPROVE_OPTION)
323         {
324             String choice = chooser.getSelectedFile().getPath();
325             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
326                 chooser.getSelectedFile().getParent());
327
328             String format = null;
329             if (chooser.getSelectedFormat().equals("Jalview"))
330             {
331               format = "Jalview";
332             }
333             else
334             {
335                 format = IdentifyFile.Identify(choice, "File");
336             }
337
338             jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
339             LoadFile(choice, "File", format);
340         }
341     }
342
343     /**
344      * DOCUMENT ME!
345      *
346      * @param file DOCUMENT ME!
347      * @param protocol DOCUMENT ME!
348      * @param format DOCUMENT ME!
349      */
350     public void LoadFile(String file, String protocol, String format)
351     {
352       FileLoader fileLoader = new FileLoader();
353       fileLoader.LoadFile(file, protocol, format);
354     }
355
356     /**
357      * DOCUMENT ME!
358      *
359      * @param e DOCUMENT ME!
360      */
361     public void inputURLMenuItem_actionPerformed(ActionEvent e)
362     {
363         String url = JOptionPane.showInternalInputDialog(Desktop.desktop,
364                 "Enter url of input file", "Input alignment from URL",
365                 JOptionPane.QUESTION_MESSAGE, null, null, "http://www.")
366                                 .toString();
367
368         if (url == null)
369         {
370             return;
371         }
372
373         if (url.trim().toLowerCase().endsWith(".jar"))
374         {
375                jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalview");
376                Jalview2XML.LoadJalviewAlign(url);
377         }
378         else
379         {
380
381           String format = IdentifyFile.Identify(url, "URL");
382
383           if (format.equals("URL NOT FOUND"))
384           {
385             JOptionPane.showInternalMessageDialog(Desktop.desktop,
386                                                   "Couldn't locate " + url,
387                                                   "URL not found",
388                                                   JOptionPane.WARNING_MESSAGE);
389
390             return;
391           }
392
393           LoadFile(url, "URL", format);
394         }
395     }
396
397     /**
398      * DOCUMENT ME!
399      *
400      * @param e DOCUMENT ME!
401      */
402     public void inputTextboxMenuItem_actionPerformed(ActionEvent e)
403     {
404         CutAndPasteTransfer cap = new CutAndPasteTransfer();
405         cap.setForInput();
406         Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
407     }
408
409     /*
410      * Exit the program
411      */
412     public void quit()
413     {
414         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
415         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
416         jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
417         jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
418         System.exit(0);
419     }
420
421     /**
422      * DOCUMENT ME!
423      *
424      * @param e DOCUMENT ME!
425      */
426     public void aboutMenuItem_actionPerformed(ActionEvent e)
427     {
428       StringBuffer message = new StringBuffer("JalView 2005 version " +
429                                               jalview.bin.Cache.getProperty(
430           "VERSION") +
431                                               "; last updated: " +
432                                               jalview.bin.
433                                               Cache.getDefault("BUILD_DATE", "unknown"));
434
435       if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
436           jalview.bin.Cache.getProperty("VERSION")))
437       {
438         message.append("\n\n!! Jalview version "
439                        + jalview.bin.Cache.getProperty("LATEST_VERSION")
440                        + " is available for download from http://www.jalview.org !!\n");
441
442       }
443
444       message.append( "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
445             "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
446             "\nFor all issues relating to Jalview, email help@jalview.org" +
447             "\n\nIf  you use JalView, please cite:" +
448             "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
449             "\nBioinformatics,  2004 12;426-7.");
450
451         JOptionPane.showInternalMessageDialog(Desktop.desktop,
452
453            message.toString(), "About Jalview",
454             JOptionPane.INFORMATION_MESSAGE);
455     }
456
457     /**
458      * DOCUMENT ME!
459      *
460      * @param e DOCUMENT ME!
461      */
462     public void documentationMenuItem_actionPerformed(ActionEvent e)
463     {
464         try
465         {
466             ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
467             java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
468             javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
469
470             javax.help.HelpBroker hb = hs.createHelpBroker();
471             hb.setLocation(new Point(200, 50));
472             hb.setSize(new Dimension(800, 700));
473             hb.setCurrentID("home");
474             hb.setDisplayed(true);
475         }
476         catch (Exception ex)
477         {
478             ex.printStackTrace();
479         }
480     }
481
482     /**
483      * DOCUMENT ME!
484      *
485      * @param e DOCUMENT ME!
486      */
487     protected void preferences_actionPerformed(ActionEvent e)
488     {
489         new Preferences();
490     }
491
492     /**
493      * DOCUMENT ME!
494      *
495      * @param e DOCUMENT ME!
496      */
497     public void saveState_actionPerformed(ActionEvent e)
498     {
499         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
500                     "LAST_DIRECTORY"), new String[] { "jar" },
501                 new String[] { "Jalview Project" }, "Jalview Project");
502
503         chooser.setFileView(new JalviewFileView());
504         chooser.setDialogTitle("Save State");
505
506         int value = chooser.showSaveDialog(this);
507
508         if (value == JalviewFileChooser.APPROVE_OPTION)
509         {
510             java.io.File choice = chooser.getSelectedFile();
511             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
512             Jalview2XML.SaveState(choice);
513         }
514     }
515
516     /**
517      * DOCUMENT ME!
518      *
519      * @param e DOCUMENT ME!
520      */
521     public void loadState_actionPerformed(ActionEvent e)
522     {
523         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
524                     "LAST_DIRECTORY"), new String[] { "jar" },
525                 new String[] { "Jalview Project" }, "Jalview Project");
526         chooser.setFileView(new JalviewFileView());
527         chooser.setDialogTitle("Restore state");
528
529         int value = chooser.showOpenDialog(this);
530
531         if (value == JalviewFileChooser.APPROVE_OPTION)
532         {
533             String choice = chooser.getSelectedFile().getAbsolutePath();
534             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
535                 chooser.getSelectedFile().getParent());
536             Jalview2XML.LoadJalviewAlign(choice);
537         }
538     }
539
540 }
541