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