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