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