preferences moved to desktop
[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        try{\r
222            af.setMaximum(Preferences.showFullscreen);\r
223        }catch(Exception ex){}\r
224 \r
225      }\r
226      else\r
227        JOptionPane.showInternalMessageDialog(Desktop.desktop,  "Couldn't open file.\n"\r
228                             + "Formats currently supported are\n"\r
229                             + "Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM" // JBPNote - message should be generated through FormatAdapter!\r
230                             ,"Error loading file",\r
231                                      JOptionPane.WARNING_MESSAGE);\r
232 \r
233    }\r
234  }\r
235 \r
236 \r
237  public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
238  {\r
239    JOptionPane op = new JOptionPane();\r
240 \r
241    String url = JOptionPane.showInternalInputDialog(Desktop.desktop,"Enter url of input file",\r
242                                             "Input alignment from URL",\r
243                                             JOptionPane.QUESTION_MESSAGE,\r
244                                             null, null,\r
245                                             "http://www.").toString();\r
246    if (url == null)\r
247      return;\r
248 \r
249    String format = IdentifyFile.Identify(url, "URL");\r
250 \r
251    if (format.equals("URL NOT FOUND"))\r
252    {\r
253      JOptionPane.showInternalMessageDialog(Desktop.desktop,"Couldn't locate " + url,\r
254                                    "URL not found",\r
255                                    JOptionPane.WARNING_MESSAGE);\r
256      return;\r
257    }\r
258 \r
259     LoadFile(url, "URL", format);\r
260  }\r
261 \r
262  public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
263  {\r
264    CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
265    int accept =  JOptionPane.showInternalOptionDialog(Desktop.desktop, cap, "Cut & paste Alignment File",\r
266                                        JOptionPane.YES_NO_CANCEL_OPTION,\r
267                                        JOptionPane.PLAIN_MESSAGE,\r
268                                        null,\r
269                                        new Object[]{"Accept", "Cancel", }, null);\r
270 \r
271 \r
272   if(accept == JOptionPane.YES_OPTION)\r
273   {\r
274     String format = IdentifyFile.Identify(cap.getText(), "Paste");\r
275     SequenceI [] sequences = null;\r
276 \r
277     if (FormatProperties.contains( format ))\r
278       sequences = FormatAdapter.read(cap.getText(), "Paste", format);\r
279 \r
280 \r
281       if(sequences!=null)\r
282       {\r
283         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
284         addInternalFrame(af, "Cut & Paste input - "+format,\r
285                          AlignFrame.NEW_WINDOW_WIDTH,\r
286                          AlignFrame.NEW_WINDOW_HEIGHT);\r
287         af.statusBar.setText("Successfully pasted alignment file");\r
288       }\r
289       else\r
290         JOptionPane.showInternalMessageDialog(Desktop.desktop, "Couldn't read the pasted text.\n"\r
291                                       +"Formats currently supported are\n"\r
292                                       +"Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM",\r
293                                       "Error parsing text", JOptionPane.WARNING_MESSAGE);\r
294 \r
295    }\r
296  }\r
297 \r
298 /*\r
299 * Exit the program\r
300 */\r
301  public void quit_actionPerformed(ActionEvent e)\r
302  {\r
303    if(jalview.bin.Jalview.applet!=null)\r
304      jalview.bin.Jalview.applet.destroy();\r
305     else\r
306       System.exit(0);\r
307  }\r
308 \r
309 \r
310 \r
311  public void aboutMenuItem_actionPerformed(ActionEvent e)\r
312  {\r
313    JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
314                                          "JalView 2005 version " + jalview.bin.Cache.VERSION+"; last updated: "+jalview.bin.Cache.BUILD_DATE\r
315                                        +"\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton."\r
316                                        +"\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee."\r
317                                        +"\nIf  you use JalView, please cite:"\r
318                                        +"\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\""\r
319                                        +"\nBioinformatics,  2004 12;426-7.",\r
320                                          "About Jalview",\r
321                                          JOptionPane.INFORMATION_MESSAGE);\r
322  }\r
323 \r
324  public void documentationMenuItem_actionPerformed(ActionEvent e)\r
325  {\r
326    try\r
327    {\r
328       ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();\r
329       java.net.URL url = javax.help.HelpSet.findHelpSet(cl,"help/help");\r
330       javax.help.HelpSet hs = new javax.help.HelpSet(cl,url);\r
331 \r
332       javax.help.HelpBroker hb = hs.createHelpBroker();\r
333       hb.setLocation(new Point(200, 50));\r
334       hb.setSize(new Dimension(800,700));\r
335       hb.setCurrentID("home");\r
336       hb.setDisplayed(true);\r
337    }\r
338    catch (Exception ex)\r
339    {\r
340      ex.printStackTrace();\r
341    }\r
342  }\r
343 \r
344  protected void preferences_actionPerformed(ActionEvent e)\r
345  {\r
346    Preferences pref = new Preferences();\r
347  }\r
348 \r
349 }\r
350 \r
351 \r