9588cf4c2c575260e9f736930b359ba3d150bdc3
[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 \r
19 \r
20 public class Desktop extends jalview.jbgui.GDesktop\r
21 {\r
22   public static JDesktopPane desktop;\r
23   static int openFrameCount = 0;\r
24   static final int xOffset = 30, yOffset = 30;\r
25   static boolean splashScreenVisible = false;\r
26 \r
27   public Desktop()\r
28   {\r
29     Image image =null;\r
30     try{\r
31       java.net.URL url = getClass().getResource("/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     setContentPane(desktop);\r
48     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);\r
49 \r
50     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();\r
51     setBounds( (int)(screenSize.getWidth()-1200)/2,\r
52                (int)(screenSize.getHeight()-800)/2,\r
53               1200 ,\r
54               800);\r
55 \r
56 \r
57    /////////Add a splashscreen on startup\r
58     JInternalFrame frame = new JInternalFrame();\r
59 \r
60     SplashScreen splash = new SplashScreen(frame, image);\r
61     frame.setContentPane(splash);\r
62     addInternalFrame(frame, "",820,200);\r
63     splashScreenVisible = true;\r
64 \r
65     frame.setLocation( (int)((getWidth()-820) /2), (int)((getHeight()-240) /2));\r
66     frame.setClosable(false);\r
67     frame.setIconifiable(false);\r
68     frame.setMaximizable(false);\r
69     frame.setFrameIcon(null);\r
70     splashScreenVisible = false;\r
71 \r
72   }\r
73 \r
74   public static void addInternalFrame(final JInternalFrame frame, String title, int w, int h)\r
75   {\r
76    if(!splashScreenVisible)\r
77       frame.setVisible(true); //necessary as of 1.3\r
78    desktop.add(frame);\r
79    openFrameCount++;\r
80    try {\r
81        frame.setSelected(true);\r
82    } catch (java.beans.PropertyVetoException e) {}\r
83    frame.setTitle(title);\r
84    frame.setResizable(true);\r
85    frame.setSize(w,h);\r
86    frame.setClosable(true);\r
87    frame.setMaximizable(true);\r
88    frame.setIconifiable(true);\r
89    frame.setLocation(xOffset*openFrameCount, yOffset*openFrameCount);\r
90    frame.toFront();\r
91    final JMenuItem menuItem = new JMenuItem(title);\r
92    frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()\r
93                                    { public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)\r
94                                      {\r
95                                        openFrameCount--;\r
96                                        windowMenu.remove(menuItem);\r
97                                      };\r
98                                    });\r
99 \r
100    menuItem.addActionListener(new ActionListener()\r
101     {\r
102       public void actionPerformed(ActionEvent e)\r
103       {\r
104         try{  frame.setSelected(true);   frame.setIcon(false); }\r
105         catch(java.beans.PropertyVetoException ex){};\r
106       }\r
107     });\r
108 \r
109    windowMenu.add(menuItem);\r
110  }\r
111 \r
112 \r
113 \r
114  public void inputLocalFileMenuItem_actionPerformed(ActionEvent e)\r
115  {\r
116    JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY);\r
117    chooser.setDialogTitle("Open local file");\r
118    chooser.setToolTipText("Open");\r
119    int value = chooser.showOpenDialog(this);\r
120    if(value == JFileChooser.APPROVE_OPTION)\r
121    {\r
122      String choice =  chooser.getSelectedFile().getPath();\r
123      jalview.bin.Cache.LAST_DIRECTORY = choice;\r
124      String format = IdentifyFile.Identify(choice, "File");\r
125      LoadFile(choice, "File", format);\r
126    }\r
127  }\r
128 \r
129  public void LoadFile(String file, String protocol,  String format)\r
130  {\r
131    SequenceI [] sequences = null;\r
132 \r
133    if (FormatProperties.contains(format))\r
134        sequences = FormatAdapter.read(file, protocol, format);\r
135 \r
136    if (sequences != null)\r
137    {\r
138      AlignFrame af = new AlignFrame(new Alignment(sequences));\r
139      addInternalFrame(af, file, 700, 500);\r
140      af.statusBar.setText("Successfully loaded file " + file);\r
141 \r
142 \r
143    }\r
144    else\r
145      JOptionPane.showInternalMessageDialog(this,  "Couldn't open file.\n"\r
146                           + "Formats currently supported are\n"\r
147                           + "Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM"\r
148                           ,"Error loading file",\r
149                                    JOptionPane.WARNING_MESSAGE);\r
150 \r
151  }\r
152 \r
153  public void inputURLMenuItem_actionPerformed(ActionEvent e)\r
154  {\r
155    String url = JOptionPane.showInternalInputDialog(this,"Enter url of input file",\r
156                                             "Input alignment from URL",\r
157                                             JOptionPane.QUESTION_MESSAGE);\r
158    if (url == null)\r
159      return;\r
160 \r
161    String format = IdentifyFile.Identify(url, "URL");\r
162 \r
163    System.out.println(format +" format");\r
164    if (format.equals("URL NOT FOUND"))\r
165    {\r
166      JOptionPane.showInternalMessageDialog(this,"Couldn't locate " + url,\r
167                                    "URL not found",\r
168                                    JOptionPane.WARNING_MESSAGE);\r
169      return;\r
170    }\r
171 \r
172     LoadFile(url, "URL", format);\r
173  }\r
174 \r
175  public void inputTextboxMenuItem_actionPerformed(ActionEvent e)\r
176  {\r
177    CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
178    int accept =  JOptionPane.showInternalOptionDialog(desktop, cap, "Cut & paste Alignment File",\r
179                                        JOptionPane.YES_NO_CANCEL_OPTION,\r
180                                        JOptionPane.PLAIN_MESSAGE,\r
181                                        null,\r
182                                        new Object[]{"Accept", "Cancel", }, null);\r
183 \r
184 \r
185   if(accept == JOptionPane.YES_OPTION)\r
186   {\r
187     String format = IdentifyFile.Identify(cap.getText(), "Paste");\r
188     SequenceI [] sequences = null;\r
189 \r
190     if (FormatProperties.contains( format ))\r
191       sequences = FormatAdapter.read(cap.getText(), "Paste", format);\r
192 \r
193 \r
194       if(sequences!=null)\r
195       {\r
196         AlignFrame af = new AlignFrame(new Alignment(sequences));\r
197         addInternalFrame(af, "Cut & Paste input - "+format, 700, 500);\r
198         af.statusBar.setText("Successfully pasted alignment file");\r
199       }\r
200       else\r
201         JOptionPane.showInternalMessageDialog(this, "Couldn't read the pasted text.\n"\r
202                                       +"Formats currently supported are\n"\r
203                                       +"Fasta, MSF, Clustal, BLC, PIR, MSP or PFAM",\r
204                                       "Error parsing text", JOptionPane.WARNING_MESSAGE);\r
205 \r
206 \r
207    }\r
208  }\r
209 \r
210 /*\r
211 * Exit the program\r
212 */\r
213  public void quit_actionPerformed(ActionEvent e)\r
214  {\r
215    this.setVisible(false);\r
216    System.exit(0);\r
217  }\r
218 \r
219 \r
220  protected void colourIndexMenuItem_actionPerformed(ActionEvent e)\r
221  {\r
222    ColourKey colourKey = new ColourKey(null);\r
223    addInternalFrame(colourKey, "Colour scheme key", 400, 320);\r
224  }\r
225 \r
226 \r
227  public void aboutMenuItem_actionPerformed(ActionEvent e)\r
228  {\r
229    JOptionPane.showInternalMessageDialog(desktop,\r
230                                          "JalView 2005"\r
231                                        +"\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse and Geoff Barton."\r
232                                        +"\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee."\r
233                                        +"\nIf  you use JalView, please cite:  \"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"",\r
234                                          "About Jalview",\r
235                                          JOptionPane.INFORMATION_MESSAGE);\r
236  }\r
237 \r
238  public void documentationMenuItem_actionPerformed(ActionEvent e)\r
239  {\r
240    try{\r
241      jalview.util.BrowserLauncher.openURL("http://jalview.org/documentation.html");\r
242    }catch(Exception ex){}\r
243  }\r
244 \r
245 \r
246 }\r