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