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