Saves screen size on exit
[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/BartonGroupBanner.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(845, 200);\r
100     frame.setLocation( (int)((getWidth()-845) /2), (int)((getHeight()-240) /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.setLocation(xOffset*openFrameCount, yOffset*openFrameCount);\r
123    frame.toFront();\r
124    final JMenuItem menuItem = new JMenuItem(title);\r
125    frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
126                                    { public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)\r
127                                      {\r
128                                        openFrameCount--;\r
129                                        windowMenu.remove(menuItem);\r
130                                      };\r
131                                    });\r
132 \r
133    menuItem.addActionListener(new ActionListener()\r
134     {\r
135       public void actionPerformed(ActionEvent e)\r
136       {\r
137         try{  frame.setSelected(true);   frame.setIcon(false); }\r
138         catch(java.beans.PropertyVetoException ex){};\r
139       }\r
140     });\r
141 \r
142    windowMenu.add(menuItem);\r
143  }\r
144 \r
145 \r
146 \r
147  public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
148  {\r
149    JFileChooser chooser = new JFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));\r
150    chooser.setFileView(new JalviewFileView());\r
151    chooser.setDialogTitle("Open local file");\r
152    chooser.setToolTipText("Open");\r
153    int value = chooser.showOpenDialog(this);\r
154    if(value == JFileChooser.APPROVE_OPTION)\r
155    {\r
156      String choice =  chooser.getSelectedFile().getPath();\r
157      jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);\r
158      String format = IdentifyFile.Identify(choice, "File");\r
159      LoadFile(choice, "File", format);\r
160    }\r
161  }\r
162 \r
163  public void LoadFile(String file, String protocol,  String format)\r
164  {\r
165    SequenceI [] sequences = null;\r
166 \r
167    if (FormatProperties.contains(format))\r
168        sequences = FormatAdapter.read(file, protocol, format);\r
169 \r
170    if (sequences != null)\r
171    {\r
172      AlignFrame af = new AlignFrame(new Alignment(sequences));\r
173      addInternalFrame(af, file, 700, 500);\r
174      af.statusBar.setText("Successfully loaded file " + file);\r
175 \r
176 \r
177    }\r
178    else\r
179      JOptionPane.showInternalMessageDialog(Desktop.desktop,  "Couldn't open file.\n"\r
180                           + "Formats currently supported are\n"\r
181                           + "Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM"\r
182                           ,"Error loading file",\r
183                                    JOptionPane.WARNING_MESSAGE);\r
184 \r
185  }\r
186 \r
187  public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
188  {\r
189    String url = JOptionPane.showInternalInputDialog(Desktop.desktop,"Enter url of input file",\r
190                                             "Input alignment from URL",\r
191                                             JOptionPane.QUESTION_MESSAGE);\r
192    if (url == null)\r
193      return;\r
194 \r
195    String format = IdentifyFile.Identify(url, "URL");\r
196 \r
197    System.out.println(format +" format");\r
198    if (format.equals("URL NOT FOUND"))\r
199    {\r
200      JOptionPane.showInternalMessageDialog(Desktop.desktop,"Couldn't locate " + url,\r
201                                    "URL not found",\r
202                                    JOptionPane.WARNING_MESSAGE);\r
203      return;\r
204    }\r
205 \r
206     LoadFile(url, "URL", format);\r
207  }\r
208 \r
209  public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
210  {\r
211    CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
212    int accept =  JOptionPane.showInternalOptionDialog(Desktop.desktop, cap, "Cut & paste Alignment File",\r
213                                        JOptionPane.YES_NO_CANCEL_OPTION,\r
214                                        JOptionPane.PLAIN_MESSAGE,\r
215                                        null,\r
216                                        new Object[]{"Accept", "Cancel", }, null);\r
217 \r
218 \r
219   if(accept == JOptionPane.YES_OPTION)\r
220   {\r
221     String format = IdentifyFile.Identify(cap.getText(), "Paste");\r
222     SequenceI [] sequences = null;\r
223 \r
224     if (FormatProperties.contains( format ))\r
225       sequences = FormatAdapter.read(cap.getText(), "Paste", format);\r
226 \r
227 \r
228       if(sequences!=null)\r
229       {\r
230         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
231         addInternalFrame(af, "Cut & Paste input - "+format, 700, 500);\r
232         af.statusBar.setText("Successfully pasted alignment file");\r
233       }\r
234       else\r
235         JOptionPane.showInternalMessageDialog(Desktop.desktop, "Couldn't read the pasted text.\n"\r
236                                       +"Formats currently supported are\n"\r
237                                       +"Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM",\r
238                                       "Error parsing text", JOptionPane.WARNING_MESSAGE);\r
239 \r
240    }\r
241  }\r
242 \r
243 /*\r
244 * Exit the program\r
245 */\r
246  public void quit_actionPerformed(ActionEvent e)\r
247  {\r
248    if(jalview.bin.Jalview.applet!=null)\r
249      jalview.bin.Jalview.applet.destroy();\r
250     else\r
251       System.exit(0);\r
252  }\r
253 \r
254 \r
255 \r
256  public void aboutMenuItem_actionPerformed(ActionEvent e)\r
257  {\r
258    JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
259                                          "JalView 2005"\r
260                                        +"\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton."\r
261                                        +"\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee."\r
262                                        +"\nIf  you use JalView, please cite:  \"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"",\r
263                                          "About Jalview",\r
264                                          JOptionPane.INFORMATION_MESSAGE);\r
265  }\r
266 \r
267  public void documentationMenuItem_actionPerformed(ActionEvent e)\r
268  {\r
269    try\r
270    {\r
271       ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
272       java.net.URL url = javax.help.HelpSet.findHelpSet(cl,"help/help");\r
273       javax.help.HelpSet hs = new javax.help.HelpSet(cl,url);\r
274 \r
275       javax.help.HelpBroker hb = hs.createHelpBroker();\r
276       hb.setLocation(new Point(200, 50));\r
277       hb.setSize(new Dimension(800,700));\r
278       hb.setCurrentID("home");\r
279       hb.setDisplayed(true);\r
280    }\r
281    catch (Exception ex)\r
282    {\r
283      ex.printStackTrace();\r
284    }\r
285  }\r
286 \r
287  static JInternalFrame conservationSlider;\r
288  static JInternalFrame PIDSlider;\r
289  public static int setConservationSliderSource(AlignmentPanel ap, ColourSchemeI cs, String source)\r
290  {\r
291    SliderPanel sp = null;\r
292 \r
293    if(conservationSlider == null)\r
294    {\r
295      sp = new SliderPanel(ap, 30, true, cs);\r
296      conservationSlider = new JInternalFrame();\r
297      conservationSlider.setContentPane(sp);\r
298      conservationSlider.setResizable(false);\r
299      addInternalFrame(conservationSlider, "Conservation Colour Increment  ("+source+")", 420, 90);\r
300      conservationSlider.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
301                   {\r
302                     public void internalFrameClosed(javax.swing.event.InternalFrameEvent e)\r
303                     {\r
304                       conservationSlider = null;\r
305                     }\r
306                   });\r
307    }\r
308    else\r
309    {\r
310        conservationSlider.setTitle("Conservation Colour Increment  ("+source+")");\r
311        sp = (SliderPanel)conservationSlider.getContentPane();\r
312        sp.cs = cs;\r
313    }\r
314 \r
315    if(ap.av.alignment.getGroups()!=null)\r
316      sp.setAllGroupsCheckEnabled( true );\r
317    else\r
318      sp.setAllGroupsCheckEnabled( false);\r
319 \r
320    return sp.getValue();\r
321 \r
322  }\r
323 \r
324  public static void hideConservationSlider()\r
325  {\r
326   try{\r
327     conservationSlider.setClosed(true);\r
328     conservationSlider = null;\r
329   }catch(Exception ex){}\r
330 }\r
331 \r
332 \r
333  public static void hidePIDSlider()\r
334  {\r
335    try{\r
336      PIDSlider.setClosed(true);\r
337      PIDSlider = null;\r
338    }catch(Exception ex){}\r
339  }\r
340 \r
341  public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs, String source)\r
342  {\r
343    SliderPanel pid = null;\r
344    if(PIDSlider == null)\r
345    {\r
346      pid = new SliderPanel(ap, 50, false, cs);\r
347      PIDSlider = new JInternalFrame();\r
348      PIDSlider.setContentPane(pid);\r
349      PIDSlider.setResizable(false);\r
350      addInternalFrame(PIDSlider, "Percentage Identity Threshold ("+source+")", 420, 90);\r
351    }\r
352    else\r
353    {\r
354        PIDSlider.setTitle("Percentage Identity Threshold ("+source+")");\r
355        pid = (SliderPanel)PIDSlider.getContentPane();\r
356        pid.cs = cs;\r
357    }\r
358 \r
359    if (ap.av.alignment.getGroups() != null)\r
360      pid.setAllGroupsCheckEnabled(true);\r
361    else\r
362      pid.setAllGroupsCheckEnabled(false);\r
363 \r
364 \r
365    return pid.getValue();\r
366 \r
367  }\r
368 \r
369 }\r