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