Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This command produces a listbox and writes the chosen option to stdout
- // Best suited for use in command chains (scripts or .bat files)
- // See also http://ruediger-plantiko.blogspot.ch/2013/05/eine-konfigurierbare-dropdown-liste.html
- // The listbox data are read from standard input as JSON object
- //
- // Example:
- //
- // { "options": {
- // "test1":"Testoption 1",
- // "test2":"Testoption 2"
- // },
- // "title":"Test Options",
- // "msg":"Please select"
- // }
- //
- // The standard input is expected in unicode encoding UTF-8.
- import javax.swing.JOptionPane;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptEngine;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- class ListBox {
- private static ArrayList<String> list = new ArrayList<String>();
- private static String title = "Selection"; // Default
- private static String msg = "Please select"; // Default
- public static void main(String[] args) throws IOException {
- getListData();
- if (list.size() == 0) {
- return;
- }
- String selectedValue = (String) JOptionPane.showInputDialog(
- null,
- msg,
- title,
- JOptionPane.INFORMATION_MESSAGE,
- null,
- list.toArray(),
- list.get(0)
- );
- if (selectedValue == null) selectedValue = "";
- System.out.println( selectedValue.replaceFirst("\\s*\\-.*$","") );
- }
- // Den JSON-Array mit dem Directory auswerten
- private static void getListData( ) throws IOException {
- String _title = "",
- _msg = "",
- json = readEntireInput();
- if (json.length() == 0) {
- System.err.println( "No option input" );
- return;
- }
- try {
- String dir = "var all = " + json + ";"
- + "for( var key in all.options ) { "
- + " list.add( key + ' - ' + all.options[key] ); "
- + " }; "
- + "if (all.title) title = all.title;"
- + "if (all.msg) msg = all.msg;";
- ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
- js.put("list",list);
- js.eval( dir );
- _title = (String) js.get("title");
- if (_title != null && _title.length() > 0) {
- title = _title;
- }
- _msg = (String) js.get("msg");
- if (_msg != null && _msg.length() > 0) {
- msg = _msg;
- }
- }
- catch (javax.script.ScriptException ex) {
- ex.printStackTrace();
- }
- }
- private static final int MAX_INPUT = 4096;
- // Copied from http://rosettacode.org/wiki/Read_entire_file
- private static String readEntireInput() throws IOException {
- StringBuilder contents = new StringBuilder();
- InputStreamReader in = new InputStreamReader(System.in, "UTF-8");
- char[] buffer = new char[MAX_INPUT];
- int read = 0;
- do {
- contents.append(buffer, 0, read);
- read = in.read(buffer);
- } while (read >= 0);
- return contents.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement