Advertisement
rplantiko

Parametrizable Listbox for command chains

May 22nd, 2013
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. // This command produces a listbox and writes the chosen option to stdout
  2.  
  3. // Best suited for use in command chains (scripts or .bat files)
  4. // See also http://ruediger-plantiko.blogspot.ch/2013/05/eine-konfigurierbare-dropdown-liste.html
  5.  
  6. // The listbox data are read from standard input as JSON object
  7. //
  8. // Example:
  9. //
  10. //  { "options": {
  11. //      "test1":"Testoption 1",
  12. //      "test2":"Testoption 2"
  13. //      },
  14. //     "title":"Test Options",
  15. //     "msg":"Please select"
  16. //     }
  17. //
  18. // The standard input is expected in unicode encoding UTF-8.
  19.  
  20. import javax.swing.JOptionPane;
  21. import javax.script.ScriptEngineManager;
  22. import javax.script.ScriptEngine;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.util.ArrayList;
  26.  
  27. class ListBox {
  28.  
  29.   private static ArrayList<String> list = new ArrayList<String>();
  30.   private static String title = "Selection";       // Default
  31.   private static String msg   = "Please select";   // Default
  32.  
  33.   public static void main(String[] args) throws IOException {
  34.        
  35.   getListData();
  36.   if (list.size() == 0) {
  37.     return;
  38.     }
  39.  
  40.   String selectedValue = (String) JOptionPane.showInputDialog(
  41.         null,
  42.         msg,
  43.         title,
  44.         JOptionPane.INFORMATION_MESSAGE,
  45.         null,
  46.         list.toArray(),
  47.         list.get(0)
  48.         );
  49.  
  50.   if (selectedValue == null) selectedValue = "";      
  51.        
  52.   System.out.println( selectedValue.replaceFirst("\\s*\\-.*$","") );
  53.  
  54.   }
  55.  
  56.  
  57.  
  58. // Den JSON-Array mit dem Directory auswerten  
  59.   private static void getListData( ) throws IOException {
  60.    
  61.     String _title = "",
  62.            _msg   = "",
  63.            json = readEntireInput();
  64.            
  65.     if (json.length() == 0) {
  66.       System.err.println( "No option input" );
  67.       return;
  68.       }      
  69.            
  70.     try {
  71.  
  72.       String dir = "var all = " + json + ";"
  73.                  + "for( var key in all.options ) { "
  74.                  + "  list.add( key + ' - ' + all.options[key] ); "
  75.                  + "  }; "
  76.                  + "if (all.title) title = all.title;"
  77.                  + "if (all.msg)   msg = all.msg;";
  78.                  
  79.       ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
  80.       js.put("list",list);
  81.       js.eval( dir );  
  82.       _title = (String) js.get("title");
  83.       if (_title != null && _title.length() > 0) {
  84.         title = _title;
  85.         }
  86.       _msg = (String) js.get("msg");
  87.       if (_msg != null && _msg.length() > 0) {
  88.         msg = _msg;
  89.         }
  90.      
  91.       }
  92.       catch (javax.script.ScriptException ex) {
  93.         ex.printStackTrace();
  94.         }
  95.    
  96.     }  
  97.    
  98.   private static final int MAX_INPUT = 4096;
  99.    
  100. // Copied from http://rosettacode.org/wiki/Read_entire_file
  101.   private static String readEntireInput() throws IOException {
  102.     StringBuilder contents = new StringBuilder();
  103.     InputStreamReader in = new InputStreamReader(System.in, "UTF-8");      
  104.     char[] buffer = new char[MAX_INPUT];  
  105.     int read = 0;
  106.     do {
  107.       contents.append(buffer, 0, read);
  108.       read = in.read(buffer);
  109.       } while (read >= 0);
  110.     return contents.toString();
  111.     }    
  112.      
  113.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement