Source code: junk/gui/applet/JXAppletViewer.java
1 /*
2 * @(#)JXAppletViewer.java
3 * Copyright (C) 2001-2002 Tay Hock Keong <tay_ivan@hotmail.com>
4 * This file is part of JD4X.
5 * JD4X is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 * JD4X is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with JD4X; see the file COPYING. If not, write to the Free
15 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 package junk.gui.applet;
19
20 import junk.gui.JXTheme;
21 import junk.system.jvm.JXClassLoader;
22 import java.awt.*;
23 import java.util.*;
24 import javax.swing.*;
25 import javax.swing.BorderFactory;
26 import javax.swing.border.EmptyBorder;
27 import java.io.*;
28 import java.net.URL;
29 import java.applet.*;
30
31 /**
32 * JXAppletViewer provides a simple applet viewer for the JD4X desktop to run
33 * applets. Applets hosted in this applet viewer may not be restricted to all
34 * the security constraints found in a internet browser. This will allow for
35 * more useful easy to create applet programs on the desktop.
36 * @version 0.1, 06/08/2002
37 * @since JD4X 1.0
38 * @author Tay Hock Keong
39 */
40
41 public class JXAppletViewer extends JFrame implements AppletContext, AppletStub{
42
43 /** utility data structures used. */
44 private Hashtable applets, stream_keys;
45
46 /** utility features used. */
47 private JApplet toolkit;
48
49 /** status bar of viewer. */
50 private JLabel statusBar;
51
52 /** containers of viewer. */
53 private JPanel c, appletBox;
54
55 /** GUI container. */
56 private Container ct;
57
58 /** reference to last applet added */
59 private Applet current;
60
61 /**
62 * The default constructor of the applet viewer, only used if no
63 * title is desired.
64 */
65 public JXAppletViewer(){
66 this("New AppletViewer");
67 }
68
69 /**
70 * The title constructor of the applet viewer, used if title is desired.
71 * @param title
72 * the title of the applet viewer.
73 */
74 public JXAppletViewer(String title){
75 super(title);
76 init();
77 }
78
79 /**
80 * Initializes all needed GUI components and their support classes.
81 */
82 protected void init(){
83 toolkit = new JApplet();
84 applets = new Hashtable();
85 stream_keys = new Hashtable();
86 appletBox = new JPanel();
87 statusBar = new JLabel("JXAppletViewer");
88 statusBar.setBorder(BorderFactory.createLineBorder(Color.black));
89 statusBar.setOpaque(false);
90 statusBar.setToolTipText("Status Bar");
91 statusBar.setFont(new Font("mono", Font.PLAIN, 10));
92 statusBar.setForeground(JXTheme.getForegroundColorTheme());
93 JPanel p = new JPanel(new BorderLayout());
94 p.setBorder(new EmptyBorder(2,2,2,2));
95 p.add(statusBar, BorderLayout.CENTER);
96 c = new JPanel(new BorderLayout());
97 c.add(appletBox, BorderLayout.CENTER);
98 ct = getContentPane();
99 ct.add(c, BorderLayout.CENTER);
100 ct.add(p, BorderLayout.SOUTH);
101 }
102
103 /**
104 * Use to add an applet into the applet viewer.
105 * @param appt
106 * the applet to be added to the applet viewer.
107 * @param name
108 * the name of the applet.
109 * @throws NullPointerException
110 * thrown when appt or name == null.
111 */
112 public void addApplet(Applet appt, String name) throws NullPointerException{
113 if(appt == null || name == null){
114 throw new NullPointerException("Applet=="+appt+", Name=="+name);
115 }
116 applets.put(name, appt);
117 current=appt;
118 if(applets.size() > 1){
119 setAppletSpace();
120 }
121 else{
122 appletBox.setLayout(new BorderLayout());
123 appletBox.add(appt, BorderLayout.CENTER);
124 appt.init();
125 appt.start();
126 }
127 statusBar.setText("Applet started ...");
128 }
129
130 /**
131 * Use to add an array of applets into the applet viewer.
132 * @param appts
133 * the applets to be added to the applet viewer.
134 * @param names
135 * the names of the applets.
136 * <dt><b>Precondition:</b><dd>
137 * appts and names != null. The array length of appts==names.
138 * @throws NullPointerException
139 * thrown when appts or names == null.
140 * @throws IllegalArgumentException
141 * thrown when appts.length != names.length.
142 */
143 public void addApplets(Applet[] appts, String[] names) throws
144 NullPointerException, IllegalArgumentException{
145 if(appts == null || names == null){
146 throw new NullPointerException("Applets=="+appts+", Names=="+names);
147 }
148 if(appts.length != names.length){
149 throw new IllegalArgumentException("appts.length=="+appts.length+", names.length=="+names.length);
150 }
151 for(int i=0; i<appts.length; i++){
152 applets.put(names[i], appts[i]);
153 current=appts[i];
154 }
155 setAppletSpace();
156 statusBar.setText("Applets started ...");
157 }
158
159 /**
160 * Use internally to reorganized the layout if there are more than 1
161 * applet added to the viewer.
162 */
163 protected void setAppletSpace(){
164 showStatus("Reorganizing AppletViewer space of applets");
165 ct.invalidate();
166 c.remove(appletBox);
167 appletBox.removeAll();
168 appletBox.setLayout(new GridLayout(applets.size(), 1));
169 for(Enumeration e=applets.elements(); e.hasMoreElements();){
170 Applet appt = (Applet)e.nextElement();
171 appletBox.add(appt);
172 appt.init();
173 appt.start();
174 }
175 c.add(appletBox, BorderLayout.CENTER);
176 ct.validate();
177 showStatus(" ");
178 }
179
180 /**
181 * Get the desired applet by name.
182 * @param name
183 * the name of the applet.
184 * @return
185 * the applet in the applet viewer with the given name.
186 */
187 public Applet getApplet(String name){
188 return (Applet)applets.get(name);
189 }
190
191 /**
192 * Get all the applets currently in the applet viewer.
193 * @return
194 * all the applets in the applet viewer.
195 */
196 public Enumeration getApplets(){
197 return applets.elements();
198 }
199
200 /**
201 * Get the audio clip at the given url.
202 * @param url
203 * the url where the audioclip is stored.
204 * @return
205 * the audio clip at the given url.
206 */
207 public AudioClip getAudioClip(URL url){
208 return toolkit.getAudioClip(url);
209 }
210
211 /**
212 * Get the image at the given url.
213 * @param url
214 * the url where the image is stored.
215 * @return
216 * the image at the given url.
217 */
218 public Image getImage(URL url){
219 return toolkit.getImage(url);
220 }
221
222 /**
223 * Get the input stream associated with the given key.
224 * @param key
225 * the key used to store the desired stream.
226 * @return
227 * the stream with the given key.
228 */
229 public InputStream getStream(String key){
230 return (InputStream)stream_keys.get(key);
231 }
232
233 /**
234 * Get all the input stream keys stored in the applet viewer.
235 * @return
236 * all the keys stored by the applet viewer.
237 */
238 public Iterator getStreamKeys(){
239 Collection grp = stream_keys.values();
240 return grp.iterator();
241 }
242
243 /**
244 * Store a stream with an associated key into the applet viewer.
245 * @param key
246 * the key used to store the desired stream.
247 * @param stream
248 * the stream to be stored with a given key.
249 */
250 public void setStream(String key, InputStream stream){
251 stream_keys.put(key, stream);
252 }
253
254 /**
255 * Currently does nothing.
256 * @param url
257 * the url where the document is stored.
258 */
259 public void showDocument(URL url){
260 //current not implemented.
261 /*Socket s;
262 BufferedInputStream client;
263 byte[] buff = new byte[4096];
264 try{
265 s = new Socket(url.getHost(), url.getPort());
266 client = new BufferedInputStream(s.getInputStream());
267 client.read(buff, 0, buff.length);
268 client.close();
269 }
270 catch(Exception jdxServerE){
271 JXController.printDebug("JXAppletViewer: socket connection get input stream error");
272 showStatus("socket connection get input stream error");
273 }*/
274 }
275
276 /**
277 * Currently does nothing.
278 * @param url
279 * the url where the document is stored.
280 * @param target
281 * the file name of the document is stored.
282 */
283 public void showDocument(URL url, String target){
284 //current not implemented.
285 }
286
287 /**
288 * Display a message to the status bar of the applet viewer.
289 * @param status
290 * the massage to be displayed.
291 */
292 public void showStatus(String status){
293 statusBar.setText(status);
294 }
295
296 /**
297 * Currently does nothing.
298 * @param width, height
299 * the new width and height of the applet.
300 */
301 public void appletResize(int width, int height){
302 //current not implemented.
303 }
304
305 /**
306 * Get the current applet context of the viewer.
307 * @return
308 * the context in which the applet is to be run in.
309 */
310 public AppletContext getAppletContext(){
311 return this;
312 }
313
314 /**
315 * Get the url where the applet was loaded from. Currently only
316 * local host is returned.
317 * @return
318 * the url where the applet was loaded from.
319 */
320 public URL getCodeBase(){
321 //assumes classloader knows where its loaded.
322 ClassLoader cl = this.getClass().getClassLoader();
323 if(cl instanceof JXClassLoader){
324 JXClassLoader jxcl = (JXClassLoader)cl;
325 File path = new File(jxcl.toString());
326 try{
327 return path.toURL();
328 }
329 catch(Exception e){
330 e.printStackTrace();
331 return null;
332 }
333 }
334 if(cl != null){
335 return cl.getResource("./");
336 }
337 return null;
338 }
339
340 /**
341 * Get the url where the applet was loaded from. Currently the same
342 * return value as getCodeBase().
343 * @return
344 * the url where the applet was loaded from.
345 */
346 public URL getDocumentBase(){
347 return getCodeBase();
348 }
349
350 /**
351 * Currently does nothing.
352 * @param name
353 * the tag name found with the html of the applet.
354 * @return
355 * fixed to return null.
356 */
357 public String getParameter(String name){
358 //current not implemented.
359 return null;
360 }
361
362 /**
363 * Check if the applet is currently running.
364 * @return
365 * fixed to return true.
366 */
367 public boolean isActive(){
368 return true;
369 }
370 }