src/main/java/org/vaadin/console/sample/ConsoleSampleApplication.java
changeset 0 c46bb04134cb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/org/vaadin/console/sample/ConsoleSampleApplication.java	Sun May 15 17:46:06 2011 +0400
     1.3 @@ -0,0 +1,193 @@
     1.4 +package org.vaadin.console.sample;
     1.5 +
     1.6 +import java.io.IOException;
     1.7 +import java.io.InputStream;
     1.8 +import java.io.InputStreamReader;
     1.9 +import java.util.ArrayList;
    1.10 +import java.util.Arrays;
    1.11 +
    1.12 +import org.vaadin.console.Console;
    1.13 +import org.vaadin.console.Console.Command;
    1.14 +import org.vaadin.console.ObjectInspector;
    1.15 +
    1.16 +import com.vaadin.Application;
    1.17 +import com.vaadin.ui.Button;
    1.18 +import com.vaadin.ui.Button.ClickEvent;
    1.19 +import com.vaadin.ui.HorizontalLayout;
    1.20 +import com.vaadin.ui.Label;
    1.21 +import com.vaadin.ui.Panel;
    1.22 +import com.vaadin.ui.TextField;
    1.23 +import com.vaadin.ui.Window;
    1.24 +
    1.25 +public class ConsoleSampleApplication extends Application {
    1.26 +    private static final long serialVersionUID = 4009702916419936660L;
    1.27 +    protected static final String HELP = "Sample Vaadin shell. Following command are available:\n";
    1.28 +    private ObjectInspector inspector;
    1.29 +
    1.30 +    @Override
    1.31 +    public void init() {
    1.32 +        Window mainWindow = new Window("Vaadin Console Demo");
    1.33 +        setMainWindow(mainWindow);
    1.34 +
    1.35 +        Panel intro = new Panel("Vaadin Console Demo");
    1.36 +        intro
    1.37 +                .addComponent(new Label(
    1.38 +                        "This console implements a test environment for itself.<br> All methods in console class are exposed as commands in the console itself.",
    1.39 +                        Label.CONTENT_RAW));
    1.40 +        intro
    1.41 +                .addComponent(new Label(
    1.42 +                        "Type 'help' to list all available commands and 'help <command>' to get parameter help.'"));
    1.43 +
    1.44 +        // # 1
    1.45 +
    1.46 +        // Create a console
    1.47 +        final Console console = new Console();
    1.48 +        mainWindow.addComponent(console);
    1.49 +
    1.50 +        // Size and greeting
    1.51 +        console.setPs("}> ");
    1.52 +        console.setCols(80);
    1.53 +        console.setRows(24);
    1.54 +        console.setMaxBufferSize(24);
    1.55 +        console.setGreeting("Welcome to Vaadin console demo.");
    1.56 +        console.reset();
    1.57 +        console.focus();
    1.58 +
    1.59 +        // Publish the methods in the Console class itself for testing purposes.
    1.60 +        console.addCommandProvider(inspector = new ObjectInspector(console));
    1.61 +
    1.62 +        // Add help command
    1.63 +        Command helpCommand = new Console.Command() {
    1.64 +            private static final long serialVersionUID = 2838665604270727844L;
    1.65 +
    1.66 +            public String getUsage(Console console, String[] argv) {
    1.67 +                return argv[0] + " <command>";
    1.68 +            }
    1.69 +
    1.70 +            public Object execute(Console console, String[] argv)
    1.71 +                    throws Exception {
    1.72 +                if (argv.length == 2) {
    1.73 +                    Command hc = console.getCommand(argv[1]);
    1.74 +                    ArrayList<String> cmdArgv = new ArrayList<String>(Arrays
    1.75 +                            .asList(argv));
    1.76 +                    cmdArgv.remove(0);
    1.77 +                    return "Usage: "
    1.78 +                            + hc.getUsage(console, cmdArgv
    1.79 +                                    .toArray(new String[] {}));
    1.80 +                }
    1.81 +                return listAvailableCommands();
    1.82 +            }
    1.83 +        };
    1.84 +
    1.85 +        // Bind the same command with multiple names
    1.86 +        console.addCommand("help", helpCommand);
    1.87 +        console.addCommand("info", helpCommand);
    1.88 +        console.addCommand("man", helpCommand);
    1.89 +        // #
    1.90 +
    1.91 +        // # 2
    1.92 +        Command systemCommand = new Command() {
    1.93 +            private static final long serialVersionUID = -5733237166568671987L;
    1.94 +
    1.95 +            public Object execute(Console console, String[] argv)
    1.96 +                    throws Exception {
    1.97 +                Process p = Runtime.getRuntime().exec(argv);
    1.98 +                InputStream in = p.getInputStream();
    1.99 +                StringBuilder o = new StringBuilder();
   1.100 +                InputStreamReader r = new InputStreamReader(in);
   1.101 +                int c = -1;
   1.102 +                try {
   1.103 +                    while ((c = r.read()) != -1) {
   1.104 +                        o.append((char) c);
   1.105 +                    }
   1.106 +                } catch (IOException e) {
   1.107 +                    o.append("[truncated]");
   1.108 +                } finally {
   1.109 +                    if (r != null) {
   1.110 +                        r.close();
   1.111 +                    }
   1.112 +                }
   1.113 +                return o.toString();
   1.114 +            }
   1.115 +
   1.116 +            public String getUsage(Console console, String[] argv) {
   1.117 +                // TODO Auto-generated method stub
   1.118 +                return null;
   1.119 +            }
   1.120 +        };
   1.121 +
   1.122 +        // #
   1.123 +        console.addCommand("ls", systemCommand);
   1.124 +
   1.125 +        // Add sample command
   1.126 +        DummyCmd dummy = new DummyCmd();
   1.127 +        console.addCommand("dir", dummy);
   1.128 +        console.addCommand("cd", dummy);
   1.129 +        console.addCommand("mkdir", dummy);
   1.130 +        console.addCommand("rm", dummy);
   1.131 +        console.addCommand("pwd", dummy);
   1.132 +        console.addCommand("more", dummy);
   1.133 +        console.addCommand("less", dummy);
   1.134 +        console.addCommand("exit", dummy);
   1.135 +
   1.136 +        HorizontalLayout pl = new HorizontalLayout();
   1.137 +        pl.setSpacing(true);
   1.138 +        mainWindow.addComponent(pl);
   1.139 +        final TextField input = new TextField(null, "print this");
   1.140 +        pl.addComponent(input);
   1.141 +        pl.addComponent(new Button("print", new Button.ClickListener() {
   1.142 +            private static final long serialVersionUID = 1L;
   1.143 +
   1.144 +            public void buttonClick(ClickEvent event) {
   1.145 +                console.print("" + input.getValue());
   1.146 +            }
   1.147 +        }));
   1.148 +
   1.149 +        pl.addComponent(new Button("println", new Button.ClickListener() {
   1.150 +            private static final long serialVersionUID = 1L;
   1.151 +
   1.152 +            public void buttonClick(ClickEvent event) {
   1.153 +                console.println("" + input.getValue());
   1.154 +            }
   1.155 +        }));
   1.156 +
   1.157 +    }
   1.158 +
   1.159 +    protected String readToString(InputStream in) {
   1.160 +        StringBuilder o = new StringBuilder();
   1.161 +        InputStreamReader r = new InputStreamReader(in);
   1.162 +        int c = -1;
   1.163 +        try {
   1.164 +            while ((c = r.read()) != -1) {
   1.165 +                o.append((char) c);
   1.166 +            }
   1.167 +        } catch (IOException e) {
   1.168 +            o.append("[truncated]");
   1.169 +        }
   1.170 +        return o.toString();
   1.171 +    }
   1.172 +
   1.173 +    public static class DummyCmd implements Console.Command {
   1.174 +        private static final long serialVersionUID = -7725047596507450670L;
   1.175 +
   1.176 +        public Object execute(Console console, String[] argv) throws Exception {
   1.177 +            return "Sorry, this is not a real shell and '" + argv[0]
   1.178 +                    + "' is unsupported. Try 'help' instead.";
   1.179 +        }
   1.180 +
   1.181 +        public String getUsage(Console console, String[] argv) {
   1.182 +            return "Sorry, this is not a real shell and '" + argv[0]
   1.183 +                    + "' is unsupported. Try 'help' instead.";
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +    protected String listAvailableCommands() {
   1.188 +        StringBuilder res = new StringBuilder();
   1.189 +        for (String cmd : inspector.getAvailableCommands()) {
   1.190 +            res.append(" ");
   1.191 +            res.append(cmd);
   1.192 +        }
   1.193 +        return res.toString().trim();
   1.194 +    }
   1.195 +
   1.196 +}