001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.swing;
018
019import java.util.ArrayList;
020
021import javax.swing.JApplet;
022import javax.swing.SwingUtilities;
023
024import org.nuxeo.shell.Shell;
025import org.nuxeo.shell.cmds.Interactive;
026import org.nuxeo.shell.cmds.InteractiveShellHandler;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031@SuppressWarnings("serial")
032public class ShellApplet extends JApplet implements InteractiveShellHandler {
033
034    protected ConsolePanel panel;
035
036    protected String[] getShellArgs() {
037        String host = getParameter("host");
038        String user = getParameter("user");
039        ArrayList<String> args = new ArrayList<String>();
040        if (user != null) {
041            args.add("-u");
042            args.add(user);
043        }
044        if (host != null) {
045            args.add(host);
046        }
047        return args.toArray(new String[args.size()]);
048    }
049
050    @Override
051    public void init() {
052        try {
053            Shell.get(); // initialize the shell to get default settings
054            SwingUtilities.invokeAndWait(new Runnable() {
055                public void run() {
056                    try {
057                        panel = new ConsolePanel();
058                        add(panel);
059                        Interactive.setConsoleReaderFactory(panel.getConsole());
060                        Interactive.setHandler(ShellApplet.this);
061                    } catch (Exception e) {
062                        throw new RuntimeException("Failed to start applet", e);
063                    }
064                }
065            });
066        } catch (Exception e) {
067            e.printStackTrace();
068        }
069    }
070
071    @Override
072    public void start() {
073        new Thread(new Runnable() {
074            public void run() {
075                try {
076                    final Shell shell = Shell.get();
077                    shell.main(getShellArgs());
078                } catch (Exception e) {
079                    e.printStackTrace();
080                }
081            }
082        }).start();
083    }
084
085    @Override
086    public void stop() {
087        panel.getConsole().exit(1);
088    }
089
090    public void enterInteractiveMode() {
091        Interactive.reset();
092        requestFocus(); // doesn't work :/
093    }
094
095    public boolean exitInteractiveMode(int code) {
096        if (code == 1) {
097            // applet stop
098            Interactive.reset();
099            Shell.reset();
100            panel.setVisible(false);
101            return true;
102        } else {
103            // reset console
104            panel.getConsole().reset();
105            return false;
106        }
107    }
108}