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