quit saves screen size
[jalview.git] / src / jalview / gui / Desktop.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import java.awt.*;\r
22 import java.awt.datatransfer.*;\r
23 import java.awt.dnd.*;\r
24 import java.awt.event.*;\r
25 import javax.swing.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import jalview.io.*;\r
29 \r
30 public class Desktop\r
31     extends jalview.jbgui.GDesktop implements DropTargetListener\r
32 {\r
33   public static JDesktopPane desktop;\r
34   static int openFrameCount = 0;\r
35   static final int xOffset = 30;\r
36   static final int yOffset = 30;\r
37 \r
38   public Desktop()\r
39   {\r
40     Image image = null;\r
41 \r
42     try\r
43     {\r
44       java.net.URL url = getClass().getResource("/images/logo.gif");\r
45 \r
46       if (url != null)\r
47       {\r
48         image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
49 \r
50         MediaTracker mt = new MediaTracker(this);\r
51         mt.addImage(image, 0);\r
52         mt.waitForID(0);\r
53         setIconImage(image);\r
54       }\r
55     }\r
56     catch (Exception ex)\r
57     {\r
58     }\r
59 \r
60     setTitle("Jalview 2005");\r
61     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r
62     desktop = new JDesktopPane();\r
63     desktop.setBackground(Color.white);\r
64     setContentPane(desktop);\r
65     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);\r
66 \r
67     // This line prevents Windows Look&Feel resizing all new windows to maximum\r
68     // if previous window was maximised\r
69     desktop.setDesktopManager(new DefaultDesktopManager());\r
70 \r
71     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();\r
72     String x = jalview.bin.Cache.getProperty("SCREEN_X");\r
73     String y = jalview.bin.Cache.getProperty("SCREEN_Y");\r
74     String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");\r
75     String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");\r
76 \r
77     if ( (x != null) && (y != null) && (width != null) && (height != null))\r
78     {\r
79       setBounds(Integer.parseInt(x), Integer.parseInt(y),\r
80                 Integer.parseInt(width), Integer.parseInt(height));\r
81     }\r
82     else\r
83     {\r
84       setBounds( (int) (screenSize.width - 900) / 2,\r
85                 (int) (screenSize.height - 650) / 2, 900, 650);\r
86     }\r
87 \r
88     this.addWindowListener(new WindowAdapter()\r
89     {\r
90       public void windowClosing(WindowEvent evt)\r
91       {\r
92         quit();\r
93       }\r
94     });\r
95     setVisible(true);\r
96 \r
97     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));\r
98 \r
99     /////////Add a splashscreen on startup\r
100     /////////Add a splashscreen on startup\r
101     JInternalFrame frame = new JInternalFrame();\r
102 \r
103     SplashScreen splash = new SplashScreen(frame, image);\r
104     frame.setContentPane(splash);\r
105     frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
106     addInternalFrame(frame, "", 750, 160, false);\r
107     frame.setLocation( (int) ( (getWidth() - 750) / 2),\r
108                       (int) ( (getHeight() - 160) / 2));\r
109   }\r
110 \r
111   public static void addInternalFrame(final JInternalFrame frame,\r
112                                       String title, int w, int h)\r
113   {\r
114     addInternalFrame(frame, title, w, h, true);\r
115   }\r
116 \r
117   public static void addInternalFrame(final JInternalFrame frame,\r
118                                       String title, int w, int h,\r
119                                       boolean resizable)\r
120   {\r
121     desktop.add(frame);\r
122     openFrameCount++;\r
123 \r
124     try\r
125     {\r
126       frame.setSelected(true);\r
127     }\r
128     catch (java.beans.PropertyVetoException e)\r
129     {\r
130     }\r
131 \r
132     frame.setTitle(title);\r
133     frame.setSize(w, h);\r
134     frame.setClosable(true);\r
135     frame.setResizable(resizable);\r
136     frame.setMaximizable(resizable);\r
137     frame.setIconifiable(resizable);\r
138     frame.setFrameIcon(null);\r
139     frame.setLocation(xOffset * openFrameCount, yOffset * openFrameCount);\r
140     frame.toFront();\r
141 \r
142     final JMenuItem menuItem = new JMenuItem(title);\r
143     frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
144     {\r
145       public void internalFrameClosed(\r
146           javax.swing.event.InternalFrameEvent evt)\r
147       {\r
148         openFrameCount--;\r
149         windowMenu.remove(menuItem);\r
150       }\r
151       ;\r
152     });\r
153 \r
154     menuItem.addActionListener(new ActionListener()\r
155     {\r
156       public void actionPerformed(ActionEvent e)\r
157       {\r
158         try\r
159         {\r
160           frame.setSelected(true);\r
161           frame.setIcon(false);\r
162         }\r
163         catch (java.beans.PropertyVetoException ex)\r
164         {\r
165         }\r
166 \r
167         ;\r
168       }\r
169     });\r
170 \r
171     frame.setVisible(true);\r
172     windowMenu.add(menuItem);\r
173   }\r
174 \r
175   public void dragEnter(DropTargetDragEvent evt)\r
176   {\r
177   }\r
178 \r
179   public void dragExit(DropTargetEvent evt)\r
180   {\r
181   }\r
182 \r
183   public void dragOver(DropTargetDragEvent evt)\r
184   {\r
185   }\r
186 \r
187   public void dropActionChanged(DropTargetDragEvent evt)\r
188   {\r
189   }\r
190 \r
191   public void drop(DropTargetDropEvent evt)\r
192   {\r
193     Transferable t = evt.getTransferable();\r
194 \r
195     if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))\r
196     {\r
197       return;\r
198     }\r
199 \r
200     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);\r
201 \r
202     try\r
203     {\r
204       java.util.List files = (java.util.List) t.getTransferData(DataFlavor.\r
205           javaFileListFlavor);\r
206 \r
207       for (int i = 0; i < files.size(); i++)\r
208       {\r
209         String file = files.get(i).toString();\r
210         String protocol = "File";\r
211 \r
212         if (file.endsWith(".jar"))\r
213         {\r
214           Jalview2XML.LoadJalviewAlign(file);\r
215         }\r
216         else\r
217         {\r
218           String format = jalview.io.IdentifyFile.Identify(file,\r
219               protocol);\r
220           LoadFile(file, protocol, format);\r
221         }\r
222       }\r
223     }\r
224     catch (Exception ex)\r
225     {\r
226       ex.printStackTrace();\r
227     }\r
228   }\r
229 \r
230   public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
231   {\r
232     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
233         getProperty(\r
234             "LAST_DIRECTORY"),\r
235         new String[]\r
236         {\r
237         "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",\r
238         "jar"\r
239     },\r
240         new String[]\r
241         {\r
242         "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"\r
243     }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));\r
244 \r
245     chooser.setFileView(new JalviewFileView());\r
246     chooser.setDialogTitle("Open local file");\r
247     chooser.setToolTipText("Open");\r
248 \r
249     int value = chooser.showOpenDialog(this);\r
250 \r
251     if (value == JalviewFileChooser.APPROVE_OPTION)\r
252     {\r
253       String choice = chooser.getSelectedFile().getPath();\r
254       jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
255                                     chooser.getSelectedFile().getParent());\r
256 \r
257       if (chooser.getSelectedFormat().equals("Jalview"))\r
258       {\r
259         jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalivew");\r
260         Jalview2XML.LoadJalviewAlign(choice);\r
261       }\r
262       else\r
263       {\r
264         String format = IdentifyFile.Identify(choice, "File");\r
265         jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
266         LoadFile(choice, "File", format);\r
267       }\r
268     }\r
269   }\r
270 \r
271   public void LoadFile(String file, String protocol, String format)\r
272   {\r
273     LoadingThread loader = new LoadingThread(file, protocol, format);\r
274     loader.start();\r
275   }\r
276 \r
277   public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
278   {\r
279     String url = JOptionPane.showInternalInputDialog(Desktop.desktop,\r
280         "Enter url of input file", "Input alignment from URL",\r
281         JOptionPane.QUESTION_MESSAGE, null, null, "http://www.")\r
282         .toString();\r
283 \r
284     if (url == null)\r
285     {\r
286       return;\r
287     }\r
288 \r
289     String format = IdentifyFile.Identify(url, "URL");\r
290 \r
291     if (format.equals("URL NOT FOUND"))\r
292     {\r
293       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
294                                             "Couldn't locate " + url,\r
295                                             "URL not found",\r
296                                             JOptionPane.WARNING_MESSAGE);\r
297 \r
298       return;\r
299     }\r
300 \r
301     LoadFile(url, "URL", format);\r
302   }\r
303 \r
304   public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
305   {\r
306     CutAndPasteTransfer cap = new CutAndPasteTransfer();\r
307     cap.setForInput();\r
308     Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);\r
309   }\r
310 \r
311   /*\r
312    * Exit the program\r
313    */\r
314   public void quit()\r
315   {\r
316     if (jalview.bin.Jalview.applet != null)\r
317     {\r
318       jalview.bin.Jalview.applet.destroy();\r
319     }\r
320     else\r
321     {\r
322       jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x +\r
323                                     "");\r
324       jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y +\r
325                                     "");\r
326       jalview.bin.Cache.setProperty("SCREEN_WIDTH",\r
327                                     getWidth() + "");\r
328       jalview.bin.Cache.setProperty("SCREEN_HEIGHT",\r
329                                       getHeight() + "");\r
330       System.exit(0);\r
331     }\r
332   }\r
333 \r
334   public void aboutMenuItem_actionPerformed(ActionEvent e)\r
335   {\r
336     JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
337                                           "JalView 2005 version " +\r
338                                           jalview.bin.Cache.VERSION +\r
339                                           "; last updated: " +\r
340                                           jalview.bin.Cache.BUILD_DATE +\r
341                                           "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +\r
342                                           "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +\r
343                                           "\nFor all issues relating to Jalview, email help@jalview.org" +\r
344                                           "\n\nIf  you use JalView, please cite:" +\r
345                                           "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +\r
346                                           "\nBioinformatics,  2004 12;426-7.",\r
347                                           "About Jalview",\r
348                                           JOptionPane.INFORMATION_MESSAGE);\r
349   }\r
350 \r
351   public void documentationMenuItem_actionPerformed(ActionEvent e)\r
352   {\r
353     try\r
354     {\r
355       ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
356       java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");\r
357       javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);\r
358 \r
359       javax.help.HelpBroker hb = hs.createHelpBroker();\r
360       hb.setLocation(new Point(200, 50));\r
361       hb.setSize(new Dimension(800, 700));\r
362       hb.setCurrentID("home");\r
363       hb.setDisplayed(true);\r
364     }\r
365     catch (Exception ex)\r
366     {\r
367       ex.printStackTrace();\r
368     }\r
369   }\r
370 \r
371   protected void preferences_actionPerformed(ActionEvent e)\r
372   {\r
373     Preferences pref = new Preferences();\r
374   }\r
375 \r
376   public void saveState_actionPerformed(ActionEvent e)\r
377   {\r
378     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
379         getProperty(\r
380             "LAST_DIRECTORY"), new String[]\r
381         {"jar"},\r
382         new String[]\r
383         {"Jalview Project"}, "Jalview Project");\r
384 \r
385     chooser.setFileView(new JalviewFileView());\r
386     chooser.setDialogTitle("Save State");\r
387 \r
388     int value = chooser.showSaveDialog(this);\r
389 \r
390     if (value == JalviewFileChooser.APPROVE_OPTION)\r
391     {\r
392       java.io.File choice = chooser.getSelectedFile();\r
393       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
394       Jalview2XML.SaveState(choice);\r
395     }\r
396   }\r
397 \r
398   public void loadState_actionPerformed(ActionEvent e)\r
399   {\r
400     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
401         getProperty(\r
402             "LAST_DIRECTORY"), new String[]\r
403         {"jar"},\r
404         new String[]\r
405         {"Jalview Project"}, "Jalview Project");\r
406     chooser.setFileView(new JalviewFileView());\r
407     chooser.setDialogTitle("Restore state");\r
408 \r
409     int value = chooser.showOpenDialog(this);\r
410 \r
411     if (value == JalviewFileChooser.APPROVE_OPTION)\r
412     {\r
413       String choice = chooser.getSelectedFile().getAbsolutePath();\r
414       jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
415                                     chooser.getSelectedFile().getParent());\r
416       Jalview2XML.LoadJalviewAlign(choice);\r
417     }\r
418   }\r
419 \r
420   class LoadingThread\r
421       extends Thread\r
422   {\r
423     String file;\r
424     String protocol;\r
425     String format;\r
426 \r
427     public LoadingThread(String file, String protocol, String format)\r
428     {\r
429       this.file = file;\r
430       this.protocol = protocol;\r
431       this.format = format;\r
432     }\r
433 \r
434     public void run()\r
435     {\r
436       SequenceI[] sequences = null;\r
437 \r
438       if (FormatAdapter.formats.contains(format))\r
439       {\r
440         sequences = FormatAdapter.readFile(file, protocol, format);\r
441       }\r
442 \r
443       if ( (sequences != null) && (sequences.length > 0))\r
444       {\r
445         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
446         addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
447                          AlignFrame.NEW_WINDOW_HEIGHT);\r
448         af.currentFileFormat = format;\r
449         af.statusBar.setText("Successfully loaded file " + file);\r
450 \r
451         try\r
452         {\r
453           af.setMaximum(Preferences.showFullscreen);\r
454         }\r
455         catch (Exception ex)\r
456         {\r
457         }\r
458       }\r
459       else\r
460       {\r
461         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
462                                               "Couldn't open file.\n" +\r
463                                               "Formats currently supported are\n" +\r
464                                               "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
465                                               , "Error loading file",\r
466                                               JOptionPane.WARNING_MESSAGE);\r
467       }\r
468     }\r
469   }\r
470 }\r