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