Formatted source
[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         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x +\r
93                                       "");\r
94         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y +\r
95                                       "");\r
96         jalview.bin.Cache.setProperty("SCREEN_WIDTH",\r
97                                       getWidth() + "");\r
98         jalview.bin.Cache.setProperty("SCREEN_HEIGHT",\r
99                                       getHeight() + "");\r
100       }\r
101     });\r
102     setVisible(true);\r
103 \r
104     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));\r
105 \r
106     /////////Add a splashscreen on startup\r
107     /////////Add a splashscreen on startup\r
108     JInternalFrame frame = new JInternalFrame();\r
109 \r
110     SplashScreen splash = new SplashScreen(frame, image);\r
111     frame.setContentPane(splash);\r
112     frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
113     addInternalFrame(frame, "", 750, 160, false);\r
114     frame.setLocation( (int) ( (getWidth() - 750) / 2),\r
115                       (int) ( (getHeight() - 160) / 2));\r
116   }\r
117 \r
118   public static void addInternalFrame(final JInternalFrame frame,\r
119                                       String title, int w, int h)\r
120   {\r
121     addInternalFrame(frame, title, w, h, true);\r
122   }\r
123 \r
124   public static void addInternalFrame(final JInternalFrame frame,\r
125                                       String title, int w, int h,\r
126                                       boolean resizable)\r
127   {\r
128     desktop.add(frame);\r
129     openFrameCount++;\r
130 \r
131     try\r
132     {\r
133       frame.setSelected(true);\r
134     }\r
135     catch (java.beans.PropertyVetoException e)\r
136     {\r
137     }\r
138 \r
139     frame.setTitle(title);\r
140     frame.setSize(w, h);\r
141     frame.setClosable(true);\r
142     frame.setResizable(resizable);\r
143     frame.setMaximizable(resizable);\r
144     frame.setIconifiable(resizable);\r
145     frame.setFrameIcon(null);\r
146     frame.setLocation(xOffset * openFrameCount, yOffset * openFrameCount);\r
147     frame.toFront();\r
148 \r
149     final JMenuItem menuItem = new JMenuItem(title);\r
150     frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
151     {\r
152       public void internalFrameClosed(\r
153           javax.swing.event.InternalFrameEvent evt)\r
154       {\r
155         openFrameCount--;\r
156         windowMenu.remove(menuItem);\r
157       }\r
158       ;\r
159     });\r
160 \r
161     menuItem.addActionListener(new ActionListener()\r
162     {\r
163       public void actionPerformed(ActionEvent e)\r
164       {\r
165         try\r
166         {\r
167           frame.setSelected(true);\r
168           frame.setIcon(false);\r
169         }\r
170         catch (java.beans.PropertyVetoException ex)\r
171         {\r
172         }\r
173 \r
174         ;\r
175       }\r
176     });\r
177 \r
178     frame.setVisible(true);\r
179     windowMenu.add(menuItem);\r
180   }\r
181 \r
182   public void dragEnter(DropTargetDragEvent evt)\r
183   {\r
184   }\r
185 \r
186   public void dragExit(DropTargetEvent evt)\r
187   {\r
188   }\r
189 \r
190   public void dragOver(DropTargetDragEvent evt)\r
191   {\r
192   }\r
193 \r
194   public void dropActionChanged(DropTargetDragEvent evt)\r
195   {\r
196   }\r
197 \r
198   public void drop(DropTargetDropEvent evt)\r
199   {\r
200     Transferable t = evt.getTransferable();\r
201 \r
202     if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))\r
203     {\r
204       return;\r
205     }\r
206 \r
207     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);\r
208 \r
209     try\r
210     {\r
211       java.util.List files = (java.util.List) t.getTransferData(DataFlavor.\r
212           javaFileListFlavor);\r
213 \r
214       for (int i = 0; i < files.size(); i++)\r
215       {\r
216         String file = files.get(i).toString();\r
217         String protocol = "File";\r
218 \r
219         if (file.endsWith(".jar"))\r
220         {\r
221           Jalview2XML.LoadJalviewAlign(file);\r
222         }\r
223         else\r
224         {\r
225           String format = jalview.io.IdentifyFile.Identify(file,\r
226               protocol);\r
227           LoadFile(file, protocol, format);\r
228         }\r
229       }\r
230     }\r
231     catch (Exception ex)\r
232     {\r
233       ex.printStackTrace();\r
234     }\r
235   }\r
236 \r
237   public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
238   {\r
239     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
240         getProperty(\r
241             "LAST_DIRECTORY"),\r
242         new String[]\r
243         {\r
244         "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",\r
245         "jar"\r
246     },\r
247         new String[]\r
248         {\r
249         "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"\r
250     }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));\r
251 \r
252     chooser.setFileView(new JalviewFileView());\r
253     chooser.setDialogTitle("Open local file");\r
254     chooser.setToolTipText("Open");\r
255 \r
256     int value = chooser.showOpenDialog(this);\r
257 \r
258     if (value == JalviewFileChooser.APPROVE_OPTION)\r
259     {\r
260       String choice = chooser.getSelectedFile().getPath();\r
261       jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
262                                     chooser.getSelectedFile().getParent());\r
263 \r
264       if (chooser.getSelectedFormat().equals("Jalview"))\r
265       {\r
266         jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", "Jalivew");\r
267         Jalview2XML.LoadJalviewAlign(choice);\r
268       }\r
269       else\r
270       {\r
271         String format = IdentifyFile.Identify(choice, "File");\r
272         jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
273         LoadFile(choice, "File", format);\r
274       }\r
275     }\r
276   }\r
277 \r
278   public void LoadFile(String file, String protocol, String format)\r
279   {\r
280     LoadingThread loader = new LoadingThread(file, protocol, format);\r
281     loader.start();\r
282   }\r
283 \r
284   public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
285   {\r
286     String url = JOptionPane.showInternalInputDialog(Desktop.desktop,\r
287         "Enter url of input file", "Input alignment from URL",\r
288         JOptionPane.QUESTION_MESSAGE, null, null, "http://www.")\r
289         .toString();\r
290 \r
291     if (url == null)\r
292     {\r
293       return;\r
294     }\r
295 \r
296     String format = IdentifyFile.Identify(url, "URL");\r
297 \r
298     if (format.equals("URL NOT FOUND"))\r
299     {\r
300       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
301                                             "Couldn't locate " + url,\r
302                                             "URL not found",\r
303                                             JOptionPane.WARNING_MESSAGE);\r
304 \r
305       return;\r
306     }\r
307 \r
308     LoadFile(url, "URL", format);\r
309   }\r
310 \r
311   public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
312   {\r
313     CutAndPasteTransfer cap = new CutAndPasteTransfer();\r
314     cap.setForInput();\r
315     Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);\r
316   }\r
317 \r
318   /*\r
319    * Exit the program\r
320    */\r
321   public void quit_actionPerformed(ActionEvent e)\r
322   {\r
323     if (jalview.bin.Jalview.applet != null)\r
324     {\r
325       jalview.bin.Jalview.applet.destroy();\r
326     }\r
327     else\r
328     {\r
329       System.exit(0);\r
330     }\r
331   }\r
332 \r
333   public void aboutMenuItem_actionPerformed(ActionEvent e)\r
334   {\r
335     JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
336                                           "JalView 2005 version " +\r
337                                           jalview.bin.Cache.VERSION +\r
338                                           "; last updated: " +\r
339                                           jalview.bin.Cache.BUILD_DATE +\r
340                                           "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +\r
341                                           "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +\r
342                                           "\nFor all issues relating to Jalview, email help@jalview.org" +\r
343                                           "\n\nIf  you use JalView, please cite:" +\r
344                                           "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +\r
345                                           "\nBioinformatics,  2004 12;426-7.",\r
346                                           "About Jalview",\r
347                                           JOptionPane.INFORMATION_MESSAGE);\r
348   }\r
349 \r
350   public void documentationMenuItem_actionPerformed(ActionEvent e)\r
351   {\r
352     try\r
353     {\r
354       ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
355       java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");\r
356       javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);\r
357 \r
358       javax.help.HelpBroker hb = hs.createHelpBroker();\r
359       hb.setLocation(new Point(200, 50));\r
360       hb.setSize(new Dimension(800, 700));\r
361       hb.setCurrentID("home");\r
362       hb.setDisplayed(true);\r
363     }\r
364     catch (Exception ex)\r
365     {\r
366       ex.printStackTrace();\r
367     }\r
368   }\r
369 \r
370   protected void preferences_actionPerformed(ActionEvent e)\r
371   {\r
372     Preferences pref = new Preferences();\r
373   }\r
374 \r
375   public void saveState_actionPerformed(ActionEvent e)\r
376   {\r
377     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
378         getProperty(\r
379             "LAST_DIRECTORY"), new String[]\r
380         {"jar"},\r
381         new String[]\r
382         {"Jalview Project"}, "Jalview Project");\r
383 \r
384     chooser.setFileView(new JalviewFileView());\r
385     chooser.setDialogTitle("Save State");\r
386 \r
387     int value = chooser.showSaveDialog(this);\r
388 \r
389     if (value == JalviewFileChooser.APPROVE_OPTION)\r
390     {\r
391       java.io.File choice = chooser.getSelectedFile();\r
392       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
393       Jalview2XML.SaveState(choice);\r
394     }\r
395   }\r
396 \r
397   public void loadState_actionPerformed(ActionEvent e)\r
398   {\r
399     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
400         getProperty(\r
401             "LAST_DIRECTORY"), new String[]\r
402         {"jar"},\r
403         new String[]\r
404         {"Jalview Project"}, "Jalview Project");\r
405     chooser.setFileView(new JalviewFileView());\r
406     chooser.setDialogTitle("Restore state");\r
407 \r
408     int value = chooser.showOpenDialog(this);\r
409 \r
410     if (value == JalviewFileChooser.APPROVE_OPTION)\r
411     {\r
412       String choice = chooser.getSelectedFile().getAbsolutePath();\r
413       jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
414                                     chooser.getSelectedFile().getParent());\r
415       Jalview2XML.LoadJalviewAlign(choice);\r
416     }\r
417   }\r
418 \r
419   class LoadingThread\r
420       extends Thread\r
421   {\r
422     String file;\r
423     String protocol;\r
424     String format;\r
425 \r
426     public LoadingThread(String file, String protocol, String format)\r
427     {\r
428       this.file = file;\r
429       this.protocol = protocol;\r
430       this.format = format;\r
431     }\r
432 \r
433     public void run()\r
434     {\r
435       SequenceI[] sequences = null;\r
436 \r
437       if (FormatAdapter.formats.contains(format))\r
438       {\r
439         sequences = FormatAdapter.readFile(file, protocol, format);\r
440       }\r
441 \r
442       if ( (sequences != null) && (sequences.length > 0))\r
443       {\r
444         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
445         addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
446                          AlignFrame.NEW_WINDOW_HEIGHT);\r
447         af.currentFileFormat = format;\r
448         af.statusBar.setText("Successfully loaded file " + file);\r
449 \r
450         try\r
451         {\r
452           af.setMaximum(Preferences.showFullscreen);\r
453         }\r
454         catch (Exception ex)\r
455         {\r
456         }\r
457       }\r
458       else\r
459       {\r
460         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
461                                               "Couldn't open file.\n" +\r
462                                               "Formats currently supported are\n" +\r
463                                               "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
464                                               , "Error loading file",\r
465                                               JOptionPane.WARNING_MESSAGE);\r
466       }\r
467     }\r
468   }\r
469 }\r