Home | History | Annotate | Download | only in resources
      1 /*
      2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  *   - Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  *
     11  *   - Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  *
     15  *   - Neither the name of Oracle nor the names of its
     16  *     contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This source code is provided to illustrate the usage of a given feature
     34  * or technique and has been deliberately simplified. Additional steps
     35  * required for a production-quality application, such as security checks,
     36  * input validation and proper error handling, might not be present in
     37  * this sample code.
     38  */
     39 
     40 /*
     41  * Few user interface utilities.
     42  */
     43 
     44 if (this.window === undefined) {
     45     this.window = null;
     46 }
     47 
     48 /**
     49  * Swing invokeLater - invokes given function in AWT event thread
     50  */
     51 Function.prototype.invokeLater = function() {
     52     var SwingUtilities = javax.swing.SwingUtilities;
     53     var func = this;
     54     var args = arguments;
     55     SwingUtilities.invokeLater(new java.lang.Runnable() {
     56                        run: function() {
     57                            func.apply(func, args);
     58                        }
     59                   });
     60 };
     61 
     62 /**
     63  * Swing invokeAndWait - invokes given function in AWT event thread
     64  * and waits for it's completion
     65  */
     66 Function.prototype.invokeAndWait = function() {
     67     var SwingUtilities = javax.swing.SwingUtilities;
     68     var func = this;
     69     var args = arguments;
     70     SwingUtilities.invokeAndWait(new java.lang.Runnable() {
     71                        run: function() {
     72                            func.apply(func, args);
     73                        }
     74                   });
     75 };
     76 
     77 /**
     78  * Am I running in AWT event dispatcher thread?
     79  */
     80 function isEventThread() {
     81     var SwingUtilities = javax.swing.SwingUtilities;
     82     return SwingUtilities.isEventDispatchThread();
     83 }
     84 isEventThread.docString = "returns whether the current thread is GUI thread";
     85 
     86 /**
     87  * Opens a file dialog box
     88  *
     89  * @param curDir current directory [optional]
     90  * @param save flag tells whether this is a save dialog or not
     91  * @return selected file or else null
     92  */
     93 function fileDialog(curDir, save) {
     94     var result;
     95     function _fileDialog() {
     96         if (curDir == undefined) {
     97             curDir = new java.io.File(".");
     98         }
     99 
    100         var JFileChooser = javax.swing.JFileChooser;
    101         var dialog = new JFileChooser(curDir);
    102         var res = save ? dialog.showSaveDialog(window):
    103             dialog.showOpenDialog(window);
    104 
    105         if (res == JFileChooser.APPROVE_OPTION) {
    106            result = dialog.getSelectedFile();
    107         } else {
    108            result = null;
    109         }
    110     }
    111 
    112     if (isEventThread()) {
    113         _fileDialog();
    114     } else {
    115         _fileDialog.invokeAndWait();
    116     }
    117 
    118     return result;
    119 }
    120 fileDialog.docString = "show a file dialog box";
    121 
    122 /**
    123  * Opens a color chooser dialog box
    124  *
    125  * @param title of the dialog box [optional]
    126  * @param color default color [optional]
    127  * @return chosen color or default color
    128  */
    129 function colorDialog(title, color) {
    130     var result;
    131 
    132     function _colorDialog() {
    133         if (title == undefined) {
    134             title = "Choose Color";
    135         }
    136 
    137         if (color == undefined) {
    138             color = java.awt.Color.BLACK;
    139         }
    140 
    141         var chooser = new javax.swing.JColorChooser();
    142         var res = chooser.showDialog(window, title, color);
    143         result = res ? res : color;
    144     }
    145 
    146     if (isEventThread()) {
    147         _colorDialog();
    148     } else {
    149         _colorDialog.invokeAndWait();
    150     }
    151 
    152     return result;
    153 }
    154 colorDialog.docString = "shows a color chooser dialog box";
    155 
    156 /**
    157  * Shows a message box
    158  *
    159  * @param msg message to be shown
    160  * @param title title of message box [optional]
    161  * @param msgType type of message box [constants in JOptionPane]
    162  */
    163 function msgBox(msg, title, msgType) {
    164     function _msgBox() {
    165         var JOptionPane = javax.swing.JOptionPane;
    166         if (msg === undefined) msg = "undefined";
    167         if (msg === null) msg = "null";
    168         if (title == undefined) title = msg;
    169         if (msgType == undefined) msgType = JOptionPane.INFORMATION_MESSAGE;
    170         JOptionPane.showMessageDialog(window, msg, title, msgType);
    171     }
    172 
    173     if (isEventThread()) {
    174         _msgBox();
    175     } else {
    176         _msgBox.invokeAndWait();
    177     }
    178 }
    179 msgBox.docString = "shows MessageBox to the user";
    180 
    181 /**
    182  * Shows an information alert box
    183  *
    184  * @param msg message to be shown
    185  * @param title title of message box [optional]
    186  */
    187 function alert(msg, title) {
    188     var JOptionPane = javax.swing.JOptionPane;
    189     msgBox(msg, title, JOptionPane.INFORMATION_MESSAGE);
    190 }
    191 alert.docString = "shows an alert message box to the user";
    192 
    193 /**
    194  * Shows an error alert box
    195  *
    196  * @param msg message to be shown
    197  * @param title title of message box [optional]
    198  */
    199 function error(msg, title) {
    200     var JOptionPane = javax.swing.JOptionPane;
    201     msgBox(msg, title, JOptionPane.ERROR_MESSAGE);
    202 }
    203 error.docString = "shows an error message box to the user";
    204 
    205 /**
    206  * Shows a warning alert box
    207  *
    208  * @param msg message to be shown
    209  * @param title title of message box [optional]
    210  */
    211 function warn(msg, title) {
    212     var JOptionPane = javax.swing.JOptionPane;
    213     msgBox(msg, title, JOptionPane.WARNING_MESSAGE);
    214 }
    215 warn.docString = "shows a warning message box to the user";
    216 
    217 /**
    218  * Shows a prompt dialog box
    219  *
    220  * @param question question to be asked
    221  * @param answer default answer suggested [optional]
    222  * @return answer given by user
    223  */
    224 function prompt(question, answer) {
    225     var result;
    226     function _prompt() {
    227         var JOptionPane = javax.swing.JOptionPane;
    228         if (answer == undefined) answer = "";
    229         result = JOptionPane.showInputDialog(window, question, answer);
    230     }
    231 
    232     if (isEventThread()) {
    233         _prompt();
    234     } else {
    235         _prompt.invokeAndWait();
    236     }
    237 
    238     return result;
    239 }
    240 prompt.docString = "shows a prompt box to the user and returns the answer";
    241 
    242 /**
    243  * Shows a confirmation dialog box
    244  *
    245  * @param msg message to be shown
    246  * @param title title of message box [optional]
    247  * @return boolean (yes->true, no->false)
    248  */
    249 function confirm(msg, title) {
    250     var result;
    251     var JOptionPane = javax.swing.JOptionPane;
    252 
    253     function _confirm() {
    254         if (title == undefined) title = msg;
    255         var optionType = JOptionPane.YES_NO_OPTION;
    256         result = JOptionPane.showConfirmDialog(window, msg, title, optionType);
    257     }
    258 
    259     if (isEventThread()) {
    260         _confirm();
    261     } else {
    262         _confirm.invokeAndWait();
    263     }
    264 
    265     return result == JOptionPane.YES_OPTION;
    266 }
    267 confirm.docString = "shows a confirmation message box to the user";
    268 
    269 /**
    270  * Exit the process after confirmation from user
    271  *
    272  * @param exitCode return code to OS [optional]
    273  */
    274 function exit(exitCode) {
    275     if (exitCode == undefined) exitCode = 0;
    276     if (confirm("Do you really want to exit?")) {
    277         java.lang.System.exit(exitCode);
    278     }
    279 }
    280 exit.docString = "exits jconsole";
    281 
    282 // synonym to exit
    283 var quit = exit;
    284 
    285 // if echo function is not defined, define it as synonym
    286 // for println function
    287 if (this.echo == undefined) {
    288     function echo(str) {
    289         println(str);
    290     }
    291 }
    292 
    293