PageRenderTime 70ms CodeModel.GetById 62ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/XInsert/src/ShowDialogCommand.java

#
Java | 94 lines | 46 code | 12 blank | 36 comment | 14 complexity | 8b3ba7a82f8affdfa15486edff363e88 MD5 | raw file
 1/*
 2
 3 *
 4
 5 * ShowDialogCommand.java
 6 * Copyright (C) 2001 Dominic Stolerman
 7 * dstolerman@jedit.org
 8
 9 *
10
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or any later version.
15
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21
22 *
23
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 */
28
29import org.gjt.sp.jedit.View;
30
31/**
32 *
33 * @author  Dominic Stolerman
34 */
35
36public class ShowDialogCommand extends Object implements Command {
37
38  /** Creates a new ShowDialogCommand */
39
40  public ShowDialogCommand(String command)  {
41    this.command = command;
42    }
43
44  public void run(ScriptContext sc) {
45    View parent = sc.getView();
46    XTreeNode node = sc.getNode();
47    String _command = command;
48    // System.out.println("_command=" + _command);
49    boolean giveOpt = (_command.charAt(_command.length() -1) == '?');
50    String key = _command.substring(1, XScripter.findWordEnd(_command, 2, null));
51    // System.out.println("key=" + key);
52    _command = _command.substring(key.length() + 1, giveOpt ? _command.length() - 1 : _command.length()).trim();
53    // System.out.println("_command=" + _command);
54    if(_command.startsWith("%set")) {
55      _command = _command.substring(4).trim();
56      if(_command.startsWith("$"))
57        node.addVariable(key, XScripter.getSubstituteFor(parent,_command, node));
58      else {
59        _command = Utilities.replace(_command, "\\$", "$");
60        node.addVariable(key, _command);
61        }
62      return;
63
64    }
65
66    String[] opts = Utilities.findStrings(_command);
67    String message;
68    String[] _opts;
69
70    if(opts != null && opts.length > 0) {
71      message = opts[0];
72      _opts = new String[opts.length - 1];
73      System.arraycopy(opts, 1, _opts, 0, _opts.length);
74      opts = null;
75      }
76    else {
77      message = "Please enter a value for " + key + ":";
78      _opts = new String[0];
79      }
80    if(_opts.length == 0)
81      node.addVariable(key, XScripter.showInputDialog(parent, message, key, XScripter.getSubstituteFor(parent, key, node)));
82    else if(_opts.length == 1)
83      node.addVariable(key, XScripter.showInputDialog(parent, message, key, _opts[0]));
84    else {
85      String val = XScripter.showComboDialog(parent, message, key, _opts, XScripter.getSubstituteFor(parent, key, node), giveOpt);
86      // System.out.println("key=" + key + " value=" + val);
87      node.addVariable(key, val);
88      }
89    }
90
91  private final String command; 
92
93}
94