001: /*
002: * @(#)PnutsConsoleApplet.java 1.1 05/06/14
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import pnuts.tools.*;
012: import pnuts.lang.*;
013: import pnuts.lang.Package;
014: import pnuts.lang.Runtime;
015: import pnuts.compiler.*;
016: import java.awt.*;
017: import java.security.*;
018: import java.io.*;
019: import java.util.*;
020: import java.applet.*;
021: import javax.swing.*;
022: import javax.swing.text.*;
023: import javax.swing.border.EmptyBorder;
024:
025: /**
026: * Pnuts Console Applet
027: *
028: * Usage: <pre>
029: * <applet code="pnuts.tools.PnutsConsoleApplet" archive="pnuts.jar,pnuts-modules.jar" codebase="/" width="100%" height="100%">
030: * <param name="modules" value="pnuts.tools"/>
031: * </applet>
032: * </pre>
033: */
034: public class PnutsConsoleApplet extends JApplet {
035:
036: private final static String PNUTS_APPLET = "pnuts.applet".intern();
037:
038: final static String[] runtimePermissions = { "createClassLoader",
039: "getProtectionDomain" };
040:
041: static {
042: try {
043: UIManager.setLookAndFeel(UIManager
044: .getSystemLookAndFeelClassName());
045: } catch (Exception e) { /* skip */
046: }
047: }
048:
049: private Context context;
050: private Console console;
051:
052: public PnutsConsoleApplet() {
053: }
054:
055: public void init() {
056: boolean canCreateClassLoader = false;
057: try {
058: for (int i = 0; i < runtimePermissions.length; i++) {
059: RuntimePermission perm = new RuntimePermission(
060: runtimePermissions[i], null);
061: AccessController.checkPermission(perm);
062: }
063: canCreateClassLoader = true;
064: } catch (SecurityException e) {
065: }
066: Package pkg = new Package("", null);
067: this .context = new Context(pkg);
068: if (canCreateClassLoader) {
069: context.setImplementation(new CompilerPnutsImpl());
070: } else {
071: context.setImplementation(new PnutsImpl());
072: }
073: context.set(PNUTS_APPLET, this );
074:
075: initializeModuleProperty(Runtime
076: .getProperty("pnuts.tools.modules"), context);
077: initializeModuleProperty(getParameter("modules"), context);
078:
079: getContentPane().setLayout(new BorderLayout());
080: this .console = new Console();
081: PnutsConsoleUI ui = new PnutsConsoleUI(console);
082: JTextComponent component = ui.getJTextComponent();
083: JScrollPane sp = new JScrollPane(component);
084: console.setConsoleUI(ui);
085: getContentPane().add("Center", sp);
086: sp.setBorder(new EmptyBorder(0, 0, 0, 0));
087:
088: Writer w = console.getWriter();
089: context.setWriter(w);
090: context.setTerminalWriter(w);
091: context.setErrorWriter(w);
092: }
093:
094: public void start() {
095: Thread th = new Thread() {
096: public void run() {
097: Pnuts.load(console.getReader(), true, context);
098: PrintWriter tw = context.getTerminalWriter();
099: tw.println("Terminated.");
100: tw.flush();
101: }
102: };
103: th.setDaemon(true);
104: th.start();
105: }
106:
107: static void initializeModuleProperty(String property,
108: Context context) {
109: if (property != null) {
110: StringTokenizer stoken = new StringTokenizer(property, ",");
111: while (stoken.hasMoreTokens()) {
112: context.usePackage(stoken.nextToken());
113: }
114: }
115: }
116: }
|