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