1 package org.vaadin.console.sample;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7 import java.util.Arrays;
9 import org.vaadin.console.Console;
10 import org.vaadin.console.Console.Command;
11 import org.vaadin.console.ObjectInspector;
13 import com.vaadin.Application;
14 import com.vaadin.ui.Button;
15 import com.vaadin.ui.Button.ClickEvent;
16 import com.vaadin.ui.HorizontalLayout;
17 import com.vaadin.ui.Label;
18 import com.vaadin.ui.Panel;
19 import com.vaadin.ui.TextField;
20 import com.vaadin.ui.Window;
22 public class ConsoleSampleApplication extends Application {
23 private static final long serialVersionUID = 4009702916419936660L;
24 protected static final String HELP = "Sample Vaadin shell. Following command are available:\n";
25 private ObjectInspector inspector;
29 Window mainWindow = new Window("Vaadin Console Demo");
30 setMainWindow(mainWindow);
32 Panel intro = new Panel("Vaadin Console Demo");
34 .addComponent(new Label(
35 "This console implements a test environment for itself.<br> All methods in console class are exposed as commands in the console itself.",
38 .addComponent(new Label(
39 "Type 'help' to list all available commands and 'help <command>' to get parameter help.'"));
44 final Console console = new Console();
45 mainWindow.addComponent(console);
51 console.setMaxBufferSize(24);
52 console.setGreeting("Welcome to Vaadin console demo.");
56 // Publish the methods in the Console class itself for testing purposes.
57 console.addCommandProvider(inspector = new ObjectInspector(console));
60 Command helpCommand = new Console.Command() {
61 private static final long serialVersionUID = 2838665604270727844L;
63 public String getUsage(Console console, String[] argv) {
64 return argv[0] + " <command>";
67 public Object execute(Console console, String[] argv)
69 if (argv.length == 2) {
70 Command hc = console.getCommand(argv[1]);
71 ArrayList<String> cmdArgv = new ArrayList<String>(Arrays
75 + hc.getUsage(console, cmdArgv
76 .toArray(new String[] {}));
78 return listAvailableCommands();
82 // Bind the same command with multiple names
83 console.addCommand("help", helpCommand);
84 console.addCommand("info", helpCommand);
85 console.addCommand("man", helpCommand);
89 Command systemCommand = new Command() {
90 private static final long serialVersionUID = -5733237166568671987L;
92 public Object execute(Console console, String[] argv)
94 Process p = Runtime.getRuntime().exec(argv);
95 InputStream in = p.getInputStream();
96 StringBuilder o = new StringBuilder();
97 InputStreamReader r = new InputStreamReader(in);
100 while ((c = r.read()) != -1) {
103 } catch (IOException e) {
104 o.append("[truncated]");
113 public String getUsage(Console console, String[] argv) {
114 // TODO Auto-generated method stub
120 console.addCommand("ls", systemCommand);
122 // Add sample command
123 DummyCmd dummy = new DummyCmd();
124 console.addCommand("dir", dummy);
125 console.addCommand("cd", dummy);
126 console.addCommand("mkdir", dummy);
127 console.addCommand("rm", dummy);
128 console.addCommand("pwd", dummy);
129 console.addCommand("more", dummy);
130 console.addCommand("less", dummy);
131 console.addCommand("exit", dummy);
133 HorizontalLayout pl = new HorizontalLayout();
135 mainWindow.addComponent(pl);
136 final TextField input = new TextField(null, "print this");
137 pl.addComponent(input);
138 pl.addComponent(new Button("print", new Button.ClickListener() {
139 private static final long serialVersionUID = 1L;
141 public void buttonClick(ClickEvent event) {
142 console.print("" + input.getValue());
146 pl.addComponent(new Button("println", new Button.ClickListener() {
147 private static final long serialVersionUID = 1L;
149 public void buttonClick(ClickEvent event) {
150 console.println("" + input.getValue());
156 protected String readToString(InputStream in) {
157 StringBuilder o = new StringBuilder();
158 InputStreamReader r = new InputStreamReader(in);
161 while ((c = r.read()) != -1) {
164 } catch (IOException e) {
165 o.append("[truncated]");
170 public static class DummyCmd implements Console.Command {
171 private static final long serialVersionUID = -7725047596507450670L;
173 public Object execute(Console console, String[] argv) throws Exception {
174 return "Sorry, this is not a real shell and '" + argv[0]
175 + "' is unsupported. Try 'help' instead.";
178 public String getUsage(Console console, String[] argv) {
179 return "Sorry, this is not a real shell and '" + argv[0]
180 + "' is unsupported. Try 'help' instead.";
184 protected String listAvailableCommands() {
185 StringBuilder res = new StringBuilder();
186 for (String cmd : inspector.getAvailableCommands()) {
190 return res.toString().trim();