Set background colour of applet RGB
[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 public class JalviewLite extends Applet {\r
34     static int lastFrameX = 200;\r
35     static int lastFrameY = 200;\r
36     static Applet applet;\r
37     boolean fileFound = true;\r
38     String file = "No file";\r
39     Button launcher = new Button("Start Jalview");\r
40 \r
41     public void init() {\r
42         applet = this;\r
43 \r
44         int r=255,g=255,b=255;\r
45         String param = getParameter("RGB");\r
46         if(param!=null)\r
47         {\r
48           try\r
49           {\r
50             r = Integer.parseInt(param.substring(0,2),16);\r
51             g = Integer.parseInt(param.substring(2,4),16);\r
52             b = Integer.parseInt(param.substring(4,6),16);\r
53           }\r
54           catch (Exception ex)\r
55           { r=255;g=255;b=255; }\r
56         }\r
57 \r
58         this.setBackground(new Color(r,g,b));\r
59 \r
60         file = getParameter("file");\r
61 \r
62         if (file != null) {\r
63             add(launcher);\r
64             file = applet.getCodeBase() + file;\r
65             launcher.addActionListener(new java.awt.event.ActionListener() {\r
66                     public void actionPerformed(ActionEvent e) {\r
67                         String format = jalview.io.IdentifyFile.Identify(file,\r
68                                 "URL");\r
69                         LoadFile(file, "URL", format);\r
70                     }\r
71                 });\r
72         } else {\r
73             file = "NO FILE";\r
74             fileFound = false;\r
75         }\r
76     }\r
77 \r
78     public static void showURL(String url) {\r
79         try {\r
80             applet.getAppletContext().showDocument(new java.net.URL(url),\r
81                 "HELP_WINDOW");\r
82         } catch (Exception ex) {\r
83         }\r
84     }\r
85 \r
86     public void LoadFile(String file, String protocol, String format) {\r
87         LoadingThread loader = new LoadingThread(file, protocol, format, this);\r
88         loader.start();\r
89     }\r
90 \r
91     public static void addFrame(final Frame frame, String title, int width,\r
92         int height) {\r
93         frame.setLocation(lastFrameX, lastFrameY);\r
94         lastFrameX += 40;\r
95         lastFrameY += 40;\r
96         frame.setSize(width, height);\r
97         frame.setTitle(title);\r
98         frame.addWindowListener(new WindowAdapter() {\r
99                 public void windowClosing(WindowEvent e) {\r
100                     frame.dispose();\r
101                 }\r
102             });\r
103         frame.setVisible(true);\r
104     }\r
105 \r
106     public void paint(Graphics g) {\r
107         if (!fileFound) {\r
108             g.setColor(new Color(200, 200, 200));\r
109             g.setColor(Color.cyan);\r
110             g.fillRect(0, 0, getSize().width, getSize().height);\r
111             g.setColor(Color.red);\r
112             g.drawString("Jalview can't open file", 5, 15);\r
113             g.drawString("\"" + file + "\"", 5, 30);\r
114         }\r
115     }\r
116 \r
117     class LoadingThread extends Thread {\r
118         String file;\r
119         String protocol;\r
120         String format;\r
121         JalviewLite jlapplet;\r
122 \r
123         public LoadingThread(String file, String protocol, String format,\r
124             JalviewLite applet) {\r
125             this.file = file;\r
126             this.protocol = protocol;\r
127             this.format = format;\r
128             this.jlapplet = applet;\r
129         }\r
130 \r
131         public void run() {\r
132             SequenceI[] sequences = null;\r
133             sequences = FormatAdapter.readFile(file, protocol, format);\r
134 \r
135             if ((sequences != null) && (sequences.length > 0)) {\r
136                 AlignFrame af = new AlignFrame(new Alignment(sequences),\r
137                         jlapplet);\r
138                 addFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
139                     AlignFrame.NEW_WINDOW_HEIGHT);\r
140                 af.statusBar.setText("Successfully loaded file " + file);\r
141             } else {\r
142                 fileFound = false;\r
143                 remove(launcher);\r
144                 repaint();\r
145             }\r
146         }\r
147     }\r
148 }\r