Mapping to PDB added
[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     boolean fileFound = true;\r
44     String file = "No file";\r
45     Button launcher = new Button("Start Jalview");\r
46     public static boolean AWT1 = false;\r
47 \r
48 \r
49     /**\r
50      * init method for Jalview Applet\r
51      */\r
52     public void init()\r
53     {\r
54         String version = System.getProperty("java.version");\r
55         if (version.indexOf("1.1") == 0)\r
56           AWT1 = true;\r
57 \r
58         int r = 255;\r
59         int g = 255;\r
60         int b = 255;\r
61         String param = getParameter("RGB");\r
62 \r
63         if (param != null)\r
64         {\r
65             try\r
66             {\r
67                 r = Integer.parseInt(param.substring(0, 2), 16);\r
68                 g = Integer.parseInt(param.substring(2, 4), 16);\r
69                 b = Integer.parseInt(param.substring(4, 6), 16);\r
70             }\r
71             catch (Exception ex)\r
72             {\r
73                 r = 255;\r
74                 g = 255;\r
75                 b = 255;\r
76             }\r
77         }\r
78 \r
79         this.setBackground(new Color(r, g, b));\r
80 \r
81 \r
82         param = getParameter("features");\r
83         if(param!=null)\r
84         {\r
85           if(param.indexOf("://")==-1)\r
86                param = getCodeBase() + param;\r
87         }\r
88         final String featuresFile = param;\r
89 \r
90         file = getParameter("file");\r
91         final JalviewLite applet = this;\r
92         if (file != null)\r
93         {\r
94             add(launcher);\r
95             if(file.indexOf("://")==-1)\r
96                file = getCodeBase() + file;\r
97 \r
98             launcher.addActionListener(new java.awt.event.ActionListener()\r
99                 {\r
100                     public void actionPerformed(ActionEvent e)\r
101                     {\r
102                         String format = jalview.io.IdentifyFile.Identify(file,\r
103                                 "URL");\r
104                         LoadingThread loader = new LoadingThread(file,\r
105                             "URL",\r
106                             format,\r
107                             featuresFile,\r
108                             applet);\r
109                         loader.start();\r
110                       }\r
111                 });\r
112         }\r
113         else\r
114         {\r
115             file = "NO FILE";\r
116             fileFound = false;\r
117         }\r
118     }\r
119 \r
120     public static void main(String [] args)\r
121     {\r
122       if(args.length!=1)\r
123       {\r
124         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
125         System.exit(1);\r
126       }\r
127 \r
128       String format = jalview.io.IdentifyFile.Identify(args[0],"File");\r
129       SequenceI[] sequences = new AppletFormatAdapter().readFile(args[0], "File", format);\r
130 \r
131       if ( (sequences != null) && (sequences.length > 0))\r
132       {\r
133         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);\r
134         af.statusBar.setText("Successfully loaded file " + args[0]);\r
135       }\r
136     }\r
137 \r
138 \r
139     /**\r
140      * Initialises and displays a new java.awt.Frame\r
141      *\r
142      * @param frame java.awt.Frame to be displayed\r
143      * @param title title of new frame\r
144      * @param width width if new frame\r
145      * @param height height of new frame\r
146      */\r
147     public static void addFrame(final Frame frame, String title, int width,\r
148         int height)\r
149     {\r
150         frame.setLocation(lastFrameX, lastFrameY);\r
151         lastFrameX += 40;\r
152         lastFrameY += 40;\r
153         frame.setSize(width, height);\r
154         frame.setTitle(title);\r
155         frame.addWindowListener(new WindowAdapter()\r
156             {\r
157                 public void windowClosing(WindowEvent e)\r
158                 {\r
159                     frame.dispose();\r
160                 }\r
161             });\r
162         frame.setVisible(true);\r
163     }\r
164 \r
165     /**\r
166      * This paints the background surrounding the "Launch Jalview button"\r
167      * <br>\r
168      * <br>If file given in parameter not found, displays error message\r
169      *\r
170      * @param g graphics context\r
171      */\r
172     public void paint(Graphics g)\r
173     {\r
174         if (!fileFound)\r
175         {\r
176             g.setColor(new Color(200, 200, 200));\r
177             g.setColor(Color.cyan);\r
178             g.fillRect(0, 0, getSize().width, getSize().height);\r
179             g.setColor(Color.red);\r
180             g.drawString("Jalview can't open file", 5, 15);\r
181             g.drawString("\"" + file + "\"", 5, 30);\r
182         }\r
183     }\r
184 \r
185     class LoadingThread extends Thread\r
186     {\r
187         String file;\r
188         String protocol;\r
189         String format;\r
190         String featuresFile;\r
191         JalviewLite applet;\r
192 \r
193         public LoadingThread(String file,\r
194                              String protocol,\r
195                              String format,\r
196                              String features,\r
197                              JalviewLite applet)\r
198         {\r
199             this.file = file;\r
200             this.protocol = protocol;\r
201             this.format = format;\r
202             featuresFile = features;\r
203             this.applet = applet;\r
204         }\r
205 \r
206         public void run()\r
207         {\r
208             SequenceI[] sequences = null;\r
209             sequences = new AppletFormatAdapter().readFile(file, protocol, format);\r
210 \r
211             if ((sequences != null) && (sequences.length > 0))\r
212             {\r
213                 AlignFrame af = new AlignFrame(new Alignment(sequences),\r
214                         applet, file);\r
215                 af.statusBar.setText("Successfully loaded file " + file);\r
216 \r
217                 if(featuresFile!=null)\r
218                   af.parseFeaturesFile(featuresFile);\r
219             }\r
220             else\r
221             {\r
222                 fileFound = false;\r
223                 remove(launcher);\r
224                 repaint();\r
225             }\r
226         }\r
227     }\r
228 }\r