Formatting changes
[jalview.git] / src / jalview / bin / JalviewLite.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.bin;\r
20 \r
21 import jalview.appletgui.AlignFrame;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 import jalview.io.*;\r
26 \r
27 import java.applet.*;\r
28 \r
29 import java.awt.*;\r
30 import java.awt.event.*;\r
31 \r
32 \r
33 /**\r
34  * Jalview Applet. Runs in Java 1.18 runtime\r
35  *\r
36  * @author $author$\r
37  * @version $Revision$\r
38  */\r
39 public class JalviewLite extends Applet\r
40 {\r
41     static int lastFrameX = 200;\r
42     static int lastFrameY = 200;\r
43     static Applet applet;\r
44     boolean fileFound = true;\r
45     String file = "No file";\r
46     Button launcher = new Button("Start Jalview");\r
47 \r
48     /**\r
49      * init method for Jalview Applet\r
50      */\r
51     public void init()\r
52     {\r
53         applet = this;\r
54 \r
55         int r = 255;\r
56         int g = 255;\r
57         int b = 255;\r
58         String param = getParameter("RGB");\r
59 \r
60         if (param != null)\r
61         {\r
62             try\r
63             {\r
64                 r = Integer.parseInt(param.substring(0, 2), 16);\r
65                 g = Integer.parseInt(param.substring(2, 4), 16);\r
66                 b = Integer.parseInt(param.substring(4, 6), 16);\r
67             }\r
68             catch (Exception ex)\r
69             {\r
70                 r = 255;\r
71                 g = 255;\r
72                 b = 255;\r
73             }\r
74         }\r
75 \r
76         this.setBackground(new Color(r, g, b));\r
77 \r
78         file = getParameter("file");\r
79 \r
80         if (file != null)\r
81         {\r
82             add(launcher);\r
83             file = applet.getCodeBase() + file;\r
84             launcher.addActionListener(new java.awt.event.ActionListener()\r
85                 {\r
86                     public void actionPerformed(ActionEvent e)\r
87                     {\r
88                         String format = jalview.io.IdentifyFile.Identify(file,\r
89                                 "URL");\r
90                         LoadFile(file, "URL", format);\r
91                     }\r
92                 });\r
93         }\r
94         else\r
95         {\r
96             file = "NO FILE";\r
97             fileFound = false;\r
98         }\r
99     }\r
100 \r
101     /**\r
102      * Displays the given URL in a new browser window\r
103      *\r
104      * @param url URL to display in browser window.\r
105      * <br>New window will be named "HELP_WINDOW"\r
106      */\r
107     public static void showURL(String url)\r
108     {\r
109         try\r
110         {\r
111             applet.getAppletContext().showDocument(new java.net.URL(url),\r
112                 "HELP_WINDOW");\r
113         }\r
114         catch (Exception ex)\r
115         {\r
116         }\r
117     }\r
118 \r
119     /**\r
120      * Starts a new LoadingThread for loading an alignment file\r
121      *\r
122      * @param file file name including full path to file\r
123      * @param protocol file or URL or paste\r
124      * @param format Fasta, Clustal, PFAM, MSF, PIR, BLC, Jalview\r
125      */\r
126     public void LoadFile(String file, String protocol, String format)\r
127     {\r
128         LoadingThread loader = new LoadingThread(file, protocol, format, this);\r
129         loader.start();\r
130     }\r
131 \r
132     /**\r
133      * Initialises and displays a new java.awt.Frame\r
134      *\r
135      * @param frame java.awt.Frame to be displayed\r
136      * @param title title of new frame\r
137      * @param width width if new frame\r
138      * @param height height of new frame\r
139      */\r
140     public static void addFrame(final Frame frame, String title, int width,\r
141         int height)\r
142     {\r
143         frame.setLocation(lastFrameX, lastFrameY);\r
144         lastFrameX += 40;\r
145         lastFrameY += 40;\r
146         frame.setSize(width, height);\r
147         frame.setTitle(title);\r
148         frame.addWindowListener(new WindowAdapter()\r
149             {\r
150                 public void windowClosing(WindowEvent e)\r
151                 {\r
152                     frame.dispose();\r
153                 }\r
154             });\r
155         frame.setVisible(true);\r
156     }\r
157 \r
158     /**\r
159      * This paints the background surrounding the "Launch Jalview button"\r
160      * <br>\r
161      * <br>If file given in parameter not found, displays error message\r
162      *\r
163      * @param g graphics context\r
164      */\r
165     public void paint(Graphics g)\r
166     {\r
167         if (!fileFound)\r
168         {\r
169             g.setColor(new Color(200, 200, 200));\r
170             g.setColor(Color.cyan);\r
171             g.fillRect(0, 0, getSize().width, getSize().height);\r
172             g.setColor(Color.red);\r
173             g.drawString("Jalview can't open file", 5, 15);\r
174             g.drawString("\"" + file + "\"", 5, 30);\r
175         }\r
176     }\r
177 \r
178     class LoadingThread extends Thread\r
179     {\r
180         String file;\r
181         String protocol;\r
182         String format;\r
183         JalviewLite jlapplet;\r
184 \r
185         public LoadingThread(String file, String protocol, String format,\r
186             JalviewLite applet)\r
187         {\r
188             this.file = file;\r
189             this.protocol = protocol;\r
190             this.format = format;\r
191             this.jlapplet = applet;\r
192         }\r
193 \r
194         public void run()\r
195         {\r
196             SequenceI[] sequences = null;\r
197             sequences = FormatAdapter.readFile(file, protocol, format);\r
198 \r
199             if ((sequences != null) && (sequences.length > 0))\r
200             {\r
201                 AlignFrame af = new AlignFrame(new Alignment(sequences),\r
202                         jlapplet);\r
203                 addFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
204                     AlignFrame.NEW_WINDOW_HEIGHT);\r
205                 af.statusBar.setText("Successfully loaded file " + file);\r
206             }\r
207             else\r
208             {\r
209                 fileFound = false;\r
210                 remove(launcher);\r
211                 repaint();\r
212             }\r
213         }\r
214     }\r
215 }\r