|
indvdum@0
|
1 |
package org.vaadin.console.sample;
|
|
indvdum@0
|
2 |
|
|
indvdum@0
|
3 |
import java.io.IOException;
|
|
indvdum@0
|
4 |
import java.io.InputStream;
|
|
indvdum@0
|
5 |
import java.io.InputStreamReader;
|
|
indvdum@0
|
6 |
import java.util.ArrayList;
|
|
indvdum@0
|
7 |
import java.util.Arrays;
|
|
indvdum@0
|
8 |
|
|
indvdum@0
|
9 |
import org.vaadin.console.Console;
|
|
indvdum@0
|
10 |
import org.vaadin.console.Console.Command;
|
|
indvdum@0
|
11 |
import org.vaadin.console.ObjectInspector;
|
|
indvdum@0
|
12 |
|
|
indvdum@0
|
13 |
import com.vaadin.Application;
|
|
indvdum@0
|
14 |
import com.vaadin.ui.Button;
|
|
indvdum@0
|
15 |
import com.vaadin.ui.Button.ClickEvent;
|
|
indvdum@0
|
16 |
import com.vaadin.ui.HorizontalLayout;
|
|
indvdum@0
|
17 |
import com.vaadin.ui.Label;
|
|
indvdum@0
|
18 |
import com.vaadin.ui.Panel;
|
|
indvdum@0
|
19 |
import com.vaadin.ui.TextField;
|
|
indvdum@0
|
20 |
import com.vaadin.ui.Window;
|
|
indvdum@0
|
21 |
|
|
indvdum@0
|
22 |
public class ConsoleSampleApplication extends Application {
|
|
indvdum@0
|
23 |
private static final long serialVersionUID = 4009702916419936660L;
|
|
indvdum@0
|
24 |
protected static final String HELP = "Sample Vaadin shell. Following command are available:\n";
|
|
indvdum@0
|
25 |
private ObjectInspector inspector;
|
|
indvdum@0
|
26 |
|
|
indvdum@0
|
27 |
@Override
|
|
indvdum@0
|
28 |
public void init() {
|
|
indvdum@0
|
29 |
Window mainWindow = new Window("Vaadin Console Demo");
|
|
indvdum@0
|
30 |
setMainWindow(mainWindow);
|
|
indvdum@0
|
31 |
|
|
indvdum@0
|
32 |
Panel intro = new Panel("Vaadin Console Demo");
|
|
indvdum@0
|
33 |
intro
|
|
indvdum@0
|
34 |
.addComponent(new Label(
|
|
indvdum@0
|
35 |
"This console implements a test environment for itself.<br> All methods in console class are exposed as commands in the console itself.",
|
|
indvdum@0
|
36 |
Label.CONTENT_RAW));
|
|
indvdum@0
|
37 |
intro
|
|
indvdum@0
|
38 |
.addComponent(new Label(
|
|
indvdum@0
|
39 |
"Type 'help' to list all available commands and 'help <command>' to get parameter help.'"));
|
|
indvdum@0
|
40 |
|
|
indvdum@0
|
41 |
// # 1
|
|
indvdum@0
|
42 |
|
|
indvdum@0
|
43 |
// Create a console
|
|
indvdum@0
|
44 |
final Console console = new Console();
|
|
indvdum@0
|
45 |
mainWindow.addComponent(console);
|
|
indvdum@0
|
46 |
|
|
indvdum@0
|
47 |
// Size and greeting
|
|
indvdum@0
|
48 |
console.setPs("}> ");
|
|
indvdum@0
|
49 |
console.setCols(80);
|
|
indvdum@0
|
50 |
console.setRows(24);
|
|
indvdum@0
|
51 |
console.setMaxBufferSize(24);
|
|
indvdum@0
|
52 |
console.setGreeting("Welcome to Vaadin console demo.");
|
|
indvdum@0
|
53 |
console.reset();
|
|
indvdum@0
|
54 |
console.focus();
|
|
indvdum@0
|
55 |
|
|
indvdum@0
|
56 |
// Publish the methods in the Console class itself for testing purposes.
|
|
indvdum@0
|
57 |
console.addCommandProvider(inspector = new ObjectInspector(console));
|
|
indvdum@0
|
58 |
|
|
indvdum@0
|
59 |
// Add help command
|
|
indvdum@0
|
60 |
Command helpCommand = new Console.Command() {
|
|
indvdum@0
|
61 |
private static final long serialVersionUID = 2838665604270727844L;
|
|
indvdum@0
|
62 |
|
|
indvdum@0
|
63 |
public String getUsage(Console console, String[] argv) {
|
|
indvdum@0
|
64 |
return argv[0] + " <command>";
|
|
indvdum@0
|
65 |
}
|
|
indvdum@0
|
66 |
|
|
indvdum@0
|
67 |
public Object execute(Console console, String[] argv)
|
|
indvdum@0
|
68 |
throws Exception {
|
|
indvdum@0
|
69 |
if (argv.length == 2) {
|
|
indvdum@0
|
70 |
Command hc = console.getCommand(argv[1]);
|
|
indvdum@0
|
71 |
ArrayList<String> cmdArgv = new ArrayList<String>(Arrays
|
|
indvdum@0
|
72 |
.asList(argv));
|
|
indvdum@0
|
73 |
cmdArgv.remove(0);
|
|
indvdum@0
|
74 |
return "Usage: "
|
|
indvdum@0
|
75 |
+ hc.getUsage(console, cmdArgv
|
|
indvdum@0
|
76 |
.toArray(new String[] {}));
|
|
indvdum@0
|
77 |
}
|
|
indvdum@0
|
78 |
return listAvailableCommands();
|
|
indvdum@0
|
79 |
}
|
|
indvdum@0
|
80 |
};
|
|
indvdum@0
|
81 |
|
|
indvdum@0
|
82 |
// Bind the same command with multiple names
|
|
indvdum@0
|
83 |
console.addCommand("help", helpCommand);
|
|
indvdum@0
|
84 |
console.addCommand("info", helpCommand);
|
|
indvdum@0
|
85 |
console.addCommand("man", helpCommand);
|
|
indvdum@0
|
86 |
// #
|
|
indvdum@0
|
87 |
|
|
indvdum@0
|
88 |
// # 2
|
|
indvdum@0
|
89 |
Command systemCommand = new Command() {
|
|
indvdum@0
|
90 |
private static final long serialVersionUID = -5733237166568671987L;
|
|
indvdum@0
|
91 |
|
|
indvdum@0
|
92 |
public Object execute(Console console, String[] argv)
|
|
indvdum@0
|
93 |
throws Exception {
|
|
indvdum@0
|
94 |
Process p = Runtime.getRuntime().exec(argv);
|
|
indvdum@0
|
95 |
InputStream in = p.getInputStream();
|
|
indvdum@0
|
96 |
StringBuilder o = new StringBuilder();
|
|
indvdum@0
|
97 |
InputStreamReader r = new InputStreamReader(in);
|
|
indvdum@0
|
98 |
int c = -1;
|
|
indvdum@0
|
99 |
try {
|
|
indvdum@0
|
100 |
while ((c = r.read()) != -1) {
|
|
indvdum@0
|
101 |
o.append((char) c);
|
|
indvdum@0
|
102 |
}
|
|
indvdum@0
|
103 |
} catch (IOException e) {
|
|
indvdum@0
|
104 |
o.append("[truncated]");
|
|
indvdum@0
|
105 |
} finally {
|
|
indvdum@0
|
106 |
if (r != null) {
|
|
indvdum@0
|
107 |
r.close();
|
|
indvdum@0
|
108 |
}
|
|
indvdum@0
|
109 |
}
|
|
indvdum@0
|
110 |
return o.toString();
|
|
indvdum@0
|
111 |
}
|
|
indvdum@0
|
112 |
|
|
indvdum@0
|
113 |
public String getUsage(Console console, String[] argv) {
|
|
indvdum@0
|
114 |
// TODO Auto-generated method stub
|
|
indvdum@0
|
115 |
return null;
|
|
indvdum@0
|
116 |
}
|
|
indvdum@0
|
117 |
};
|
|
indvdum@0
|
118 |
|
|
indvdum@0
|
119 |
// #
|
|
indvdum@0
|
120 |
console.addCommand("ls", systemCommand);
|
|
indvdum@0
|
121 |
|
|
indvdum@0
|
122 |
// Add sample command
|
|
indvdum@0
|
123 |
DummyCmd dummy = new DummyCmd();
|
|
indvdum@0
|
124 |
console.addCommand("dir", dummy);
|
|
indvdum@0
|
125 |
console.addCommand("cd", dummy);
|
|
indvdum@0
|
126 |
console.addCommand("mkdir", dummy);
|
|
indvdum@0
|
127 |
console.addCommand("rm", dummy);
|
|
indvdum@0
|
128 |
console.addCommand("pwd", dummy);
|
|
indvdum@0
|
129 |
console.addCommand("more", dummy);
|
|
indvdum@0
|
130 |
console.addCommand("less", dummy);
|
|
indvdum@0
|
131 |
console.addCommand("exit", dummy);
|
|
indvdum@0
|
132 |
|
|
indvdum@0
|
133 |
HorizontalLayout pl = new HorizontalLayout();
|
|
indvdum@0
|
134 |
pl.setSpacing(true);
|
|
indvdum@0
|
135 |
mainWindow.addComponent(pl);
|
|
indvdum@0
|
136 |
final TextField input = new TextField(null, "print this");
|
|
indvdum@0
|
137 |
pl.addComponent(input);
|
|
indvdum@0
|
138 |
pl.addComponent(new Button("print", new Button.ClickListener() {
|
|
indvdum@0
|
139 |
private static final long serialVersionUID = 1L;
|
|
indvdum@0
|
140 |
|
|
indvdum@0
|
141 |
public void buttonClick(ClickEvent event) {
|
|
indvdum@0
|
142 |
console.print("" + input.getValue());
|
|
indvdum@0
|
143 |
}
|
|
indvdum@0
|
144 |
}));
|
|
indvdum@0
|
145 |
|
|
indvdum@0
|
146 |
pl.addComponent(new Button("println", new Button.ClickListener() {
|
|
indvdum@0
|
147 |
private static final long serialVersionUID = 1L;
|
|
indvdum@0
|
148 |
|
|
indvdum@0
|
149 |
public void buttonClick(ClickEvent event) {
|
|
indvdum@0
|
150 |
console.println("" + input.getValue());
|
|
indvdum@0
|
151 |
}
|
|
indvdum@0
|
152 |
}));
|
|
indvdum@0
|
153 |
|
|
indvdum@0
|
154 |
}
|
|
indvdum@0
|
155 |
|
|
indvdum@0
|
156 |
protected String readToString(InputStream in) {
|
|
indvdum@0
|
157 |
StringBuilder o = new StringBuilder();
|
|
indvdum@0
|
158 |
InputStreamReader r = new InputStreamReader(in);
|
|
indvdum@0
|
159 |
int c = -1;
|
|
indvdum@0
|
160 |
try {
|
|
indvdum@0
|
161 |
while ((c = r.read()) != -1) {
|
|
indvdum@0
|
162 |
o.append((char) c);
|
|
indvdum@0
|
163 |
}
|
|
indvdum@0
|
164 |
} catch (IOException e) {
|
|
indvdum@0
|
165 |
o.append("[truncated]");
|
|
indvdum@0
|
166 |
}
|
|
indvdum@0
|
167 |
return o.toString();
|
|
indvdum@0
|
168 |
}
|
|
indvdum@0
|
169 |
|
|
indvdum@0
|
170 |
public static class DummyCmd implements Console.Command {
|
|
indvdum@0
|
171 |
private static final long serialVersionUID = -7725047596507450670L;
|
|
indvdum@0
|
172 |
|
|
indvdum@0
|
173 |
public Object execute(Console console, String[] argv) throws Exception {
|
|
indvdum@0
|
174 |
return "Sorry, this is not a real shell and '" + argv[0]
|
|
indvdum@0
|
175 |
+ "' is unsupported. Try 'help' instead.";
|
|
indvdum@0
|
176 |
}
|
|
indvdum@0
|
177 |
|
|
indvdum@0
|
178 |
public String getUsage(Console console, String[] argv) {
|
|
indvdum@0
|
179 |
return "Sorry, this is not a real shell and '" + argv[0]
|
|
indvdum@0
|
180 |
+ "' is unsupported. Try 'help' instead.";
|
|
indvdum@0
|
181 |
}
|
|
indvdum@0
|
182 |
}
|
|
indvdum@0
|
183 |
|
|
indvdum@0
|
184 |
protected String listAvailableCommands() {
|
|
indvdum@0
|
185 |
StringBuilder res = new StringBuilder();
|
|
indvdum@0
|
186 |
for (String cmd : inspector.getAvailableCommands()) {
|
|
indvdum@0
|
187 |
res.append(" ");
|
|
indvdum@0
|
188 |
res.append(cmd);
|
|
indvdum@0
|
189 |
}
|
|
indvdum@0
|
190 |
return res.toString().trim();
|
|
indvdum@0
|
191 |
}
|
|
indvdum@0
|
192 |
|
|
indvdum@0
|
193 |
}
|