001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.starter;
025:
026: import jacareto.system.Environment;
027: import jacareto.toolkit.awt.AppletFrame;
028:
029: import java.applet.Applet;
030:
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033:
034: /**
035: * This class represents an applet starter.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.03
039: */
040: public class AppletStarter extends Starter {
041: /** The width of the applet. */
042: private int width;
043:
044: /** The height of the applet. */
045: private int height;
046:
047: /** The code base. */
048: private String codeBase;
049:
050: /** The document base. */
051: private String documentBase;
052:
053: /** The parameters. */
054: private Hashtable parameters;
055:
056: /** The applet frame. */
057: private AppletFrame frame;
058:
059: /**
060: * Creates a new applet starter with the specified values.
061: *
062: * @param env the environment
063: * @param name the starter's name
064: * @param classname the starter's classname
065: * @param initClassname the initializer's classname
066: * @param classpathext the class path extension
067: * @param duration the start up duration
068: * @param detectDuration whether or not the startup duration should be detected automatically
069: * @param width the width of the applet
070: * @param height the applet's height
071: * @param codeBase the code base
072: * @param documentBase the document base
073: */
074: public AppletStarter(Environment env, String name,
075: String classname, String initClassname,
076: String classpathext, long duration, boolean detectDuration,
077: int width, int height, String codeBase, String documentBase) {
078: super (env, name, classname, initClassname, classpathext,
079: duration, detectDuration, 0);
080: setWidth(width);
081: setHeight(height);
082: setCodeBase(codeBase);
083: setDocumentBase(documentBase);
084: parameters = new Hashtable();
085: }
086:
087: /**
088: * Creates a new applet starter with the specified values.
089: *
090: * @param env the environment
091: * @param name the starter's name
092: * @param classname the starter's classname
093: * @param initClassname the initializer's classname
094: * @param duration the start up duration
095: * @param detectDuration whether or not the startup duration should be detected automatically
096: * @param width the width of the applet
097: * @param height the applet's height
098: * @param codeBase the code base
099: * @param documentBase the document base
100: */
101: public AppletStarter(Environment env, String name,
102: String classname, String initClassname, long duration,
103: boolean detectDuration, int width, int height,
104: String codeBase, String documentBase) {
105: this (env, name, classname, initClassname, null, duration,
106: detectDuration, width, height, codeBase, documentBase);
107: }
108:
109: /**
110: * Creates a new applet starter with uninitialized values.
111: *
112: * @param env the environment
113: */
114: public AppletStarter(Environment env) {
115: this (env, "", "", "", "", 0L, true, 0, 0, "", "");
116: }
117:
118: /**
119: * Creates a new applet starter with default values and no environment. The environment should
120: * be defined with the method {@link
121: * jacareto.system.EnvironmentMember#setEnvironment(Environment)} before environment instances
122: * will be accessed.
123: */
124: public AppletStarter() {
125: this (null);
126: }
127:
128: /**
129: * Sets the width of the applet.
130: *
131: * @param width the width of the applet.
132: */
133: public void setWidth(int width) {
134: this .width = width;
135: fireValuesChanged();
136: }
137:
138: /**
139: * Sets the height of the applet.
140: *
141: * @param height the height of the applet.
142: */
143: public void setHeight(int height) {
144: this .height = height;
145: fireValuesChanged();
146: }
147:
148: /**
149: * Sets the code base.
150: *
151: * @param codeBase the code base (as URL).
152: */
153: public void setCodeBase(String codeBase) {
154: this .codeBase = codeBase;
155: fireValuesChanged();
156: }
157:
158: /**
159: * Sets the document base.
160: *
161: * @param documentBase the document base (as URL).
162: */
163: public void setDocumentBase(String documentBase) {
164: this .documentBase = documentBase;
165: fireValuesChanged();
166: }
167:
168: /**
169: * Returns the code base.
170: *
171: * @return DOCUMENT ME!
172: */
173: public String getCodeBase() {
174: return codeBase;
175: }
176:
177: /**
178: * Returns the document base.
179: *
180: * @return DOCUMENT ME!
181: */
182: public String getDocumentBase() {
183: return documentBase;
184: }
185:
186: /**
187: * Returns the width of the applet.
188: *
189: * @return guess!
190: */
191: public int getWidth() {
192: return width;
193: }
194:
195: /**
196: * Returns the height of the applet.
197: *
198: * @return guess!
199: */
200: public int getHeight() {
201: return height;
202: }
203:
204: /**
205: * Returns the parameters in a hashtable.
206: *
207: * @return DOCUMENT ME!
208: */
209: public Hashtable getParameters() {
210: return parameters;
211: }
212:
213: /**
214: * Sets a parameter.
215: *
216: * @param name DOCUMENT ME!
217: * @param value DOCUMENT ME!
218: */
219: public void setParameter(String name, String value) {
220: parameters.put(name, value);
221: }
222:
223: /**
224: * Removes a parameter.
225: *
226: * @param name DOCUMENT ME!
227: */
228: public void removeParameter(String name) {
229: if (parameters.containsKey(name)) {
230: parameters.remove(name);
231: }
232: }
233:
234: /**
235: * Removes all parameters.
236: */
237: public void removeAllParameters() {
238: parameters.clear();
239: }
240:
241: /**
242: * Starts the applet class.
243: *
244: * @return <code>true</code> if the starting process was successful; otherwise
245: * <code>false</code>
246: */
247: public boolean exec() {
248: boolean result = true;
249:
250: try {
251: Class starterClass = getMainClass();
252:
253: if (starterClass != null) {
254: // Start the applet
255: Initializer initializer = createInitializer();
256:
257: if (initializer != null) {
258: initializer.initBeforeStarting();
259: }
260:
261: Applet applet = (Applet) starterClass.newInstance();
262: JacaretoAppletContext context = new JacaretoAppletContext(
263: env);
264: JacaretoAppletStub stub = new JacaretoAppletStub(env,
265: context, getCodeBase(), getDocumentBase());
266: Enumeration enumeration = getParameters().keys();
267:
268: while (enumeration.hasMoreElements()) {
269: String key = (String) enumeration.nextElement();
270: stub
271: .setParameter(key, (String) parameters
272: .get(key));
273: }
274:
275: applet.setStub(stub);
276:
277: frame = new AppletFrame(getName(), applet, getWidth(),
278: getHeight());
279: frame.setLocation(50, 50);
280: frame.setVisible(true);
281:
282: if (initializer != null) {
283: initializer.initAfterStarting(applet);
284: }
285:
286: getLogger().info(
287: getLanguage().getString(
288: "Starters.AppletStarter.Msg.Started")
289: + " \"" + getName() + "\"");
290: }
291: } catch (IllegalAccessException e) {
292: getLogger()
293: .error(
294: getLanguage()
295: .getString(
296: "Starters.AppletStarter.Error.IllegalAccess")
297: + getClassname());
298: result = false;
299: } catch (InstantiationException e) {
300: getLogger()
301: .error(
302: getLanguage()
303: .getString(
304: "Starters.AppletStarter.Error.Instantiation")
305: + getClassname());
306: result = false;
307: } catch (Exception e) {
308: getLogger().error(
309: getLanguage().getString(
310: "Starters.AppletStarter.Error.Starting")
311: + getClassname());
312: getLogger().error(e);
313: e.printStackTrace();
314: result = false;
315: }
316:
317: return result;
318: }
319:
320: /**
321: * Returns the name of the applet starter as recordable
322: *
323: * @return the name
324: */
325: public String getElementName() {
326: return getLanguage().getString("Starters.AppletStarter.Name");
327: }
328:
329: /**
330: * Returns a description of the recordable.
331: *
332: * @return the description
333: */
334: public String getElementDescription() {
335: return getLanguage().getString(
336: "Starters.AppletStarter.Description");
337: }
338:
339: /**
340: * Returns a String which describes the content of the recordable shortly.
341: *
342: * @return a string with a short description of the recordable
343: */
344: public String toShortString() {
345: return getElementName() + " (" + getName() + ")";
346: }
347:
348: /**
349: * Returns <code>true</code>, because an applet starter is an atomic recordable.
350: *
351: * @return <code>true</code>
352: */
353: public boolean isAtomic() {
354: return true;
355: }
356:
357: /**
358: * Clones the applet starter.
359: *
360: * @return DOCUMENT ME!
361: */
362: public Object clone() {
363: AppletStarter result = new AppletStarter(env, getName(),
364: getClassname(), getInitClassname(),
365: getClassPathExtension(), getDuration(),
366: getDetectDuration(), getWidth(), getHeight(),
367: getCodeBase(), getDocumentBase());
368: Enumeration enumeration = parameters.keys();
369:
370: while (enumeration.hasMoreElements()) {
371: String key = (String) enumeration.nextElement();
372: result.setParameter(key, (String) parameters.get(key));
373: }
374:
375: Enumeration enum2 = getCursorImages().keys();
376:
377: while (enum2.hasMoreElements()) {
378: String key = (String) enum2.nextElement();
379: result.setCursorImage(key, (String) getCursorImages().get(
380: key));
381: }
382:
383: Enumeration enum3 = getSystemProperties().keys();
384:
385: while (enum3.hasMoreElements()) {
386: String key = (String) enum3.nextElement();
387: result.setSystemProperty(key,
388: (String) getSystemProperties().get(key));
389: }
390:
391: return result;
392: }
393:
394: /**
395: * Sets all attributes to the values of the specified applet starter. Afterwards the two applet
396: * starters are equal.
397: *
398: * @param other DOCUMENT ME!
399: */
400: public void setValues(AppletStarter other) {
401: setName(other.getName());
402: setClassname(other.getClassname());
403: setInitClassname(other.getInitClassname());
404: setClassPathExtension(other.getClassPathExtension());
405: setDuration(other.getDuration());
406: setDetectDuration(other.getDetectDuration());
407: setWidth(other.getWidth());
408: setHeight(other.getHeight());
409: setCodeBase(other.getCodeBase());
410: setDocumentBase(other.getDocumentBase());
411: removeAllParameters();
412:
413: Enumeration enumeration = other.getParameters().keys();
414:
415: while (enumeration.hasMoreElements()) {
416: String key = (String) enumeration.nextElement();
417: setParameter(key, (String) other.getParameters().get(key));
418: }
419:
420: Enumeration enum2 = other.getCursorImages().keys();
421:
422: while (enum2.hasMoreElements()) {
423: String key = (String) enum2.nextElement();
424: setCursorImage(key, (String) other.getCursorImages().get(
425: key));
426: }
427:
428: Enumeration enum3 = other.getSystemProperties().keys();
429:
430: while (enum3.hasMoreElements()) {
431: String key = (String) enum3.nextElement();
432: setSystemProperty(key, (String) other.getSystemProperties()
433: .get(key));
434: }
435: }
436: }
|