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