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