001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.swing;
020
021import java.util.ArrayList;
022
023import javax.swing.JApplet;
024import javax.swing.SwingUtilities;
025
026import org.nuxeo.shell.Shell;
027import org.nuxeo.shell.cmds.Interactive;
028import org.nuxeo.shell.cmds.InteractiveShellHandler;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@SuppressWarnings("serial")
034public class ShellApplet extends JApplet implements InteractiveShellHandler {
035
036    protected ConsolePanel panel;
037
038    protected String[] getShellArgs() {
039        String host = getParameter("host");
040        String user = getParameter("user");
041        ArrayList<String> args = new ArrayList<String>();
042        if (user != null) {
043            args.add("-u");
044            args.add(user);
045        }
046        if (host != null) {
047            args.add(host);
048        }
049        return args.toArray(new String[args.size()]);
050    }
051
052    @Override
053    public void init() {
054        try {
055            Shell.get(); // initialize the shell to get default settings
056            SwingUtilities.invokeAndWait(new Runnable() {
057                public void run() {
058                    try {
059                        panel = new ConsolePanel();
060                        add(panel);
061                        Interactive.setConsoleReaderFactory(panel.getConsole());
062                        Interactive.setHandler(ShellApplet.this);
063                    } catch (Exception e) {
064                        throw new RuntimeException("Failed to start applet", e);
065                    }
066                }
067            });
068        } catch (Exception e) {
069            e.printStackTrace();
070        }
071    }
072
073    @Override
074    public void start() {
075        new Thread(new Runnable() {
076            public void run() {
077                try {
078                    final Shell shell = Shell.get();
079                    shell.main(getShellArgs());
080                } catch (Exception e) {
081                    e.printStackTrace();
082                }
083            }
084        }).start();
085    }
086
087    @Override
088    public void stop() {
089        panel.getConsole().exit(1);
090    }
091
092    public void enterInteractiveMode() {
093        Interactive.reset();
094        requestFocus(); // doesn't work :/
095    }
096
097    public boolean exitInteractiveMode(int code) {
098        if (code == 1) {
099            // applet stop
100            Interactive.reset();
101            Shell.reset();
102            panel.setVisible(false);
103            return true;
104        } else {
105            // reset console
106            panel.getConsole().reset();
107            return false;
108        }
109    }
110}