src/main/java/org/vaadin/console/sample/ConsoleSampleApplication.java
changeset 0 c46bb04134cb
equal deleted inserted replaced
-1:000000000000 0:c46bb04134cb
       
     1 package org.vaadin.console.sample;
       
     2 
       
     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;
       
     8 
       
     9 import org.vaadin.console.Console;
       
    10 import org.vaadin.console.Console.Command;
       
    11 import org.vaadin.console.ObjectInspector;
       
    12 
       
    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;
       
    21 
       
    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;
       
    26 
       
    27     @Override
       
    28     public void init() {
       
    29         Window mainWindow = new Window("Vaadin Console Demo");
       
    30         setMainWindow(mainWindow);
       
    31 
       
    32         Panel intro = new Panel("Vaadin Console Demo");
       
    33         intro
       
    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.",
       
    36                         Label.CONTENT_RAW));
       
    37         intro
       
    38                 .addComponent(new Label(
       
    39                         "Type 'help' to list all available commands and 'help <command>' to get parameter help.'"));
       
    40 
       
    41         // # 1
       
    42 
       
    43         // Create a console
       
    44         final Console console = new Console();
       
    45         mainWindow.addComponent(console);
       
    46 
       
    47         // Size and greeting
       
    48         console.setPs("}> ");
       
    49         console.setCols(80);
       
    50         console.setRows(24);
       
    51         console.setMaxBufferSize(24);
       
    52         console.setGreeting("Welcome to Vaadin console demo.");
       
    53         console.reset();
       
    54         console.focus();
       
    55 
       
    56         // Publish the methods in the Console class itself for testing purposes.
       
    57         console.addCommandProvider(inspector = new ObjectInspector(console));
       
    58 
       
    59         // Add help command
       
    60         Command helpCommand = new Console.Command() {
       
    61             private static final long serialVersionUID = 2838665604270727844L;
       
    62 
       
    63             public String getUsage(Console console, String[] argv) {
       
    64                 return argv[0] + " <command>";
       
    65             }
       
    66 
       
    67             public Object execute(Console console, String[] argv)
       
    68                     throws Exception {
       
    69                 if (argv.length == 2) {
       
    70                     Command hc = console.getCommand(argv[1]);
       
    71                     ArrayList<String> cmdArgv = new ArrayList<String>(Arrays
       
    72                             .asList(argv));
       
    73                     cmdArgv.remove(0);
       
    74                     return "Usage: "
       
    75                             + hc.getUsage(console, cmdArgv
       
    76                                     .toArray(new String[] {}));
       
    77                 }
       
    78                 return listAvailableCommands();
       
    79             }
       
    80         };
       
    81 
       
    82         // Bind the same command with multiple names
       
    83         console.addCommand("help", helpCommand);
       
    84         console.addCommand("info", helpCommand);
       
    85         console.addCommand("man", helpCommand);
       
    86         // #
       
    87 
       
    88         // # 2
       
    89         Command systemCommand = new Command() {
       
    90             private static final long serialVersionUID = -5733237166568671987L;
       
    91 
       
    92             public Object execute(Console console, String[] argv)
       
    93                     throws Exception {
       
    94                 Process p = Runtime.getRuntime().exec(argv);
       
    95                 InputStream in = p.getInputStream();
       
    96                 StringBuilder o = new StringBuilder();
       
    97                 InputStreamReader r = new InputStreamReader(in);
       
    98                 int c = -1;
       
    99                 try {
       
   100                     while ((c = r.read()) != -1) {
       
   101                         o.append((char) c);
       
   102                     }
       
   103                 } catch (IOException e) {
       
   104                     o.append("[truncated]");
       
   105                 } finally {
       
   106                     if (r != null) {
       
   107                         r.close();
       
   108                     }
       
   109                 }
       
   110                 return o.toString();
       
   111             }
       
   112 
       
   113             public String getUsage(Console console, String[] argv) {
       
   114                 // TODO Auto-generated method stub
       
   115                 return null;
       
   116             }
       
   117         };
       
   118 
       
   119         // #
       
   120         console.addCommand("ls", systemCommand);
       
   121 
       
   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);
       
   132 
       
   133         HorizontalLayout pl = new HorizontalLayout();
       
   134         pl.setSpacing(true);
       
   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;
       
   140 
       
   141             public void buttonClick(ClickEvent event) {
       
   142                 console.print("" + input.getValue());
       
   143             }
       
   144         }));
       
   145 
       
   146         pl.addComponent(new Button("println", new Button.ClickListener() {
       
   147             private static final long serialVersionUID = 1L;
       
   148 
       
   149             public void buttonClick(ClickEvent event) {
       
   150                 console.println("" + input.getValue());
       
   151             }
       
   152         }));
       
   153 
       
   154     }
       
   155 
       
   156     protected String readToString(InputStream in) {
       
   157         StringBuilder o = new StringBuilder();
       
   158         InputStreamReader r = new InputStreamReader(in);
       
   159         int c = -1;
       
   160         try {
       
   161             while ((c = r.read()) != -1) {
       
   162                 o.append((char) c);
       
   163             }
       
   164         } catch (IOException e) {
       
   165             o.append("[truncated]");
       
   166         }
       
   167         return o.toString();
       
   168     }
       
   169 
       
   170     public static class DummyCmd implements Console.Command {
       
   171         private static final long serialVersionUID = -7725047596507450670L;
       
   172 
       
   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.";
       
   176         }
       
   177 
       
   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.";
       
   181         }
       
   182     }
       
   183 
       
   184     protected String listAvailableCommands() {
       
   185         StringBuilder res = new StringBuilder();
       
   186         for (String cmd : inspector.getAvailableCommands()) {
       
   187             res.append(" ");
       
   188             res.append(cmd);
       
   189         }
       
   190         return res.toString().trim();
       
   191     }
       
   192 
       
   193 }