add all suffixes to file open
[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 \r
20 \r
21 public class Desktop extends jalview.jbgui.GDesktop\r
22 {\r
23   public static JDesktopPane desktop;\r
24   static int openFrameCount = 0;\r
25   static final int xOffset = 30, yOffset = 30;\r
26 \r
27   public Desktop()\r
28   {\r
29     Image image =null;\r
30     try{\r
31       java.net.URL url = getClass().getResource("/images/logo.gif");\r
32       if(url!=null)\r
33       {\r
34         image = java.awt.Toolkit.getDefaultToolkit().createImage(url);\r
35         MediaTracker mt = new MediaTracker(this);\r
36         mt.addImage(image, 0);\r
37         mt.waitForID(0);\r
38         setIconImage(image);\r
39       }\r
40 \r
41     }catch(Exception ex){}\r
42 \r
43     setTitle("Jalview 2005");\r
44     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r
45     setVisible(true);\r
46     desktop = new JDesktopPane();\r
47     desktop.setBackground(Color.white);\r
48     setContentPane(desktop);\r
49     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);\r
50 \r
51     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();\r
52     String x = jalview.bin.Cache.getProperty("SCREEN_X");\r
53     String y = jalview.bin.Cache.getProperty("SCREEN_Y");\r
54     String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");\r
55     String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");\r
56 \r
57     if(x!=null && y!=null && width!=null && height!=null)\r
58     {\r
59       setBounds( Integer.parseInt(x), Integer.parseInt(y),\r
60                  Integer.parseInt(width), Integer.parseInt(height));\r
61     }\r
62     else\r
63       setBounds( (int)(screenSize.width-900)/2,\r
64            (int)(screenSize.height-650)/2,\r
65           900 ,\r
66           650);\r
67 \r
68 this.addWindowListener(new WindowAdapter()\r
69 {\r
70 public void windowClosing(WindowEvent evt)\r
71 {\r
72  jalview.bin.Cache.setProperty("SCREEN_X",\r
73                                getBounds().x + "");\r
74  jalview.bin.Cache.setProperty("SCREEN_Y",\r
75                                getBounds().y + "");\r
76  jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");\r
77  jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");\r
78 }\r
79 });\r
80 \r
81 /////////Add a splashscreen on startup\r
82 \r
83 \r
84    /////////Add a splashscreen on startup\r
85     JInternalFrame frame = new JInternalFrame();\r
86 \r
87     SplashScreen splash = new SplashScreen(frame, image);\r
88     frame.setContentPane(splash);\r
89     desktop.add(frame);\r
90     frame.setVisible(true);\r
91     openFrameCount++;\r
92     try\r
93     {\r
94       frame.setSelected(true);\r
95     }\r
96     catch (java.beans.PropertyVetoException e)\r
97     {}\r
98     frame.setResizable(true);\r
99     frame.setSize(750, 160);\r
100     frame.setLocation( (int)((getWidth()-750) /2), (int)((getHeight()-160) /2));\r
101     frame.setClosable(false);\r
102     frame.setIconifiable(false);\r
103     frame.setMaximizable(false);\r
104     frame.setFrameIcon(null);\r
105 \r
106   }\r
107 \r
108   public static void addInternalFrame(final JInternalFrame frame, String title, int w, int h)\r
109   {\r
110    desktop.add(frame);\r
111    frame.setVisible(true);\r
112    openFrameCount++;\r
113    try {\r
114        frame.setSelected(true);\r
115    } catch (java.beans.PropertyVetoException e) {}\r
116    frame.setTitle(title);\r
117    frame.setResizable(true);\r
118    frame.setSize(w,h);\r
119    frame.setClosable(true);\r
120    frame.setMaximizable(true);\r
121    frame.setIconifiable(true);\r
122    frame.setFrameIcon(null);\r
123    frame.setLocation(xOffset*openFrameCount, yOffset*openFrameCount);\r
124    frame.toFront();\r
125    final JMenuItem menuItem = new JMenuItem(title);\r
126    frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
127                                    { public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)\r
128                                      {\r
129                                        openFrameCount--;\r
130                                        windowMenu.remove(menuItem);\r
131                                      };\r
132                                    });\r
133 \r
134    menuItem.addActionListener(new ActionListener()\r
135     {\r
136       public void actionPerformed(ActionEvent e)\r
137       {\r
138         try{  frame.setSelected(true);   frame.setIcon(false); }\r
139         catch(java.beans.PropertyVetoException ex){};\r
140       }\r
141     });\r
142 \r
143    windowMenu.add(menuItem);\r
144  }\r
145 \r
146 \r
147 \r
148  public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
149  {\r
150    JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
151        new String[]{"fa", "fasta", "fastq", "blc", "msf", "pfam", "aln", "pir"}, "Alignment files");\r
152    chooser.setFileView(new JalviewFileView());\r
153    chooser.setDialogTitle("Open local file");\r
154    chooser.setToolTipText("Open");\r
155    int value = chooser.showOpenDialog(this);\r
156    if(value == JalviewFileChooser.APPROVE_OPTION)\r
157    {\r
158      String choice =  chooser.getSelectedFile().getPath();\r
159      jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);\r
160      String format = IdentifyFile.Identify(choice, "File");\r
161      LoadFile(choice, "File", format);\r
162    }\r
163  }\r
164 \r
165  public void LoadFile(String file, String protocol,  String format)\r
166  {\r
167    SequenceI [] sequences = null;\r
168 \r
169    if (FormatProperties.contains(format))\r
170        sequences = FormatAdapter.read(file, protocol, format);\r
171 \r
172    if (sequences != null)\r
173    {\r
174      AlignFrame af = new AlignFrame(new Alignment(sequences));\r
175      addInternalFrame(af, file, 700, 500);\r
176      af.statusBar.setText("Successfully loaded file " + file);\r
177 \r
178 \r
179    }\r
180    else\r
181      JOptionPane.showInternalMessageDialog(Desktop.desktop,  "Couldn't open file.\n"\r
182                           + "Formats currently supported are\n"\r
183                           + "Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM"\r
184                           ,"Error loading file",\r
185                                    JOptionPane.WARNING_MESSAGE);\r
186 \r
187  }\r
188 \r
189  public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
190  {\r
191    String url = JOptionPane.showInternalInputDialog(Desktop.desktop,"Enter url of input file",\r
192                                             "Input alignment from URL",\r
193                                             JOptionPane.QUESTION_MESSAGE);\r
194    if (url == null)\r
195      return;\r
196 \r
197    String format = IdentifyFile.Identify(url, "URL");\r
198 \r
199    System.out.println(format +" format");\r
200    if (format.equals("URL NOT FOUND"))\r
201    {\r
202      JOptionPane.showInternalMessageDialog(Desktop.desktop,"Couldn't locate " + url,\r
203                                    "URL not found",\r
204                                    JOptionPane.WARNING_MESSAGE);\r
205      return;\r
206    }\r
207 \r
208     LoadFile(url, "URL", format);\r
209  }\r
210 \r
211  public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
212  {\r
213    CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
214    int accept =  JOptionPane.showInternalOptionDialog(Desktop.desktop, cap, "Cut & paste Alignment File",\r
215                                        JOptionPane.YES_NO_CANCEL_OPTION,\r
216                                        JOptionPane.PLAIN_MESSAGE,\r
217                                        null,\r
218                                        new Object[]{"Accept", "Cancel", }, null);\r
219 \r
220 \r
221   if(accept == JOptionPane.YES_OPTION)\r
222   {\r
223     String format = IdentifyFile.Identify(cap.getText(), "Paste");\r
224     SequenceI [] sequences = null;\r
225 \r
226     if (FormatProperties.contains( format ))\r
227       sequences = FormatAdapter.read(cap.getText(), "Paste", format);\r
228 \r
229 \r
230       if(sequences!=null)\r
231       {\r
232         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
233         addInternalFrame(af, "Cut & Paste input - "+format, 700, 500);\r
234         af.statusBar.setText("Successfully pasted alignment file");\r
235       }\r
236       else\r
237         JOptionPane.showInternalMessageDialog(Desktop.desktop, "Couldn't read the pasted text.\n"\r
238                                       +"Formats currently supported are\n"\r
239                                       +"Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM",\r
240                                       "Error parsing text", JOptionPane.WARNING_MESSAGE);\r
241 \r
242    }\r
243  }\r
244 \r
245 /*\r
246 * Exit the program\r
247 */\r
248  public void quit_actionPerformed(ActionEvent e)\r
249  {\r
250    if(jalview.bin.Jalview.applet!=null)\r
251      jalview.bin.Jalview.applet.destroy();\r
252     else\r
253       System.exit(0);\r
254  }\r
255 \r
256 \r
257 \r
258  public void aboutMenuItem_actionPerformed(ActionEvent e)\r
259  {\r
260    JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
261                                          "JalView 2005 version " + jalview.bin.Cache.VERSION+"; last updated: "+jalview.bin.Cache.BUILD_DATE\r
262                                        +"\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton."\r
263                                        +"\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee."\r
264                                        +"\nIf  you use JalView, please cite:  \"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"",\r
265                                          "About Jalview",\r
266                                          JOptionPane.INFORMATION_MESSAGE);\r
267  }\r
268 \r
269  public void documentationMenuItem_actionPerformed(ActionEvent e)\r
270  {\r
271    try\r
272    {\r
273       ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
274       java.net.URL url = javax.help.HelpSet.findHelpSet(cl,"help/help");\r
275       javax.help.HelpSet hs = new javax.help.HelpSet(cl,url);\r
276 \r
277       javax.help.HelpBroker hb = hs.createHelpBroker();\r
278       hb.setLocation(new Point(200, 50));\r
279       hb.setSize(new Dimension(800,700));\r
280       hb.setCurrentID("home");\r
281       hb.setDisplayed(true);\r
282    }\r
283    catch (Exception ex)\r
284    {\r
285      ex.printStackTrace();\r
286    }\r
287  }\r
288 \r
289  static JInternalFrame conservationSlider;\r
290  static JInternalFrame PIDSlider;\r
291  public static int setConservationSliderSource(AlignmentPanel ap, ColourSchemeI cs, String source)\r
292  {\r
293    SliderPanel sp = null;\r
294 \r
295    if(conservationSlider == null)\r
296    {\r
297      sp = new SliderPanel(ap, 30, true, cs);\r
298      conservationSlider = new JInternalFrame();\r
299      conservationSlider.setContentPane(sp);\r
300      conservationSlider.setResizable(false);\r
301      addInternalFrame(conservationSlider, "Conservation Colour Increment  ("+source+")", 420, 90);\r
302      conservationSlider.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
303                   {\r
304                     public void internalFrameClosed(javax.swing.event.InternalFrameEvent e)\r
305                     {\r
306                       conservationSlider = null;\r
307                     }\r
308                   });\r
309    }\r
310    else\r
311    {\r
312        conservationSlider.setTitle("Conservation Colour Increment  ("+source+")");\r
313        sp = (SliderPanel)conservationSlider.getContentPane();\r
314        sp.cs = cs;\r
315    }\r
316 \r
317    if(ap.av.alignment.getGroups()!=null)\r
318      sp.setAllGroupsCheckEnabled( true );\r
319    else\r
320      sp.setAllGroupsCheckEnabled( false);\r
321 \r
322    return sp.getValue();\r
323 \r
324  }\r
325 \r
326  public static void hideConservationSlider()\r
327  {\r
328   try{\r
329     conservationSlider.setClosed(true);\r
330     conservationSlider = null;\r
331   }catch(Exception ex){}\r
332 }\r
333 \r
334 \r
335  public static void hidePIDSlider()\r
336  {\r
337    try{\r
338      PIDSlider.setClosed(true);\r
339      PIDSlider = null;\r
340    }catch(Exception ex){}\r
341  }\r
342 \r
343  public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs, String source)\r
344  {\r
345    SliderPanel pid = null;\r
346    if(PIDSlider == null)\r
347    {\r
348      pid = new SliderPanel(ap, 50, false, cs);\r
349      PIDSlider = new JInternalFrame();\r
350      PIDSlider.setContentPane(pid);\r
351      PIDSlider.setResizable(false);\r
352      addInternalFrame(PIDSlider, "Percentage Identity Threshold ("+source+")", 420, 90);\r
353    }\r
354    else\r
355    {\r
356        PIDSlider.setTitle("Percentage Identity Threshold ("+source+")");\r
357        pid = (SliderPanel)PIDSlider.getContentPane();\r
358        pid.cs = cs;\r
359    }\r
360 \r
361    if (ap.av.alignment.getGroups() != null)\r
362      pid.setAllGroupsCheckEnabled(true);\r
363    else\r
364      pid.setAllGroupsCheckEnabled(false);\r
365 \r
366 \r
367    return pid.getValue();\r
368 \r
369  }\r
370 \r
371 }\r