/plugins/Console/tags/release-3-7/console/commando/CommandoDialog.java

# · Java · 445 lines · 319 code · 74 blank · 52 comment · 24 complexity · 79f5a9df8096714aa4dc715e72b31e09 MD5 · raw file

  1. /*
  2. * CommandoDialog.java - Commando dialog box
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package console.commando;
  23. //{{{ Imports
  24. import bsh.*;
  25. import com.microstar.xml.*;
  26. import console.*;
  27. import javax.swing.border.*;
  28. import javax.swing.event.*;
  29. import javax.swing.*;
  30. import java.awt.event.*;
  31. import java.awt.BorderLayout;
  32. import java.awt.Component;
  33. import java.awt.GridBagConstraints;
  34. import java.awt.GridBagLayout;
  35. import java.io.*;
  36. import java.util.*;
  37. import org.gjt.sp.jedit.gui.*;
  38. import org.gjt.sp.jedit.*;
  39. import org.gjt.sp.util.Log;
  40. //}}}
  41. public class CommandoDialog extends EnhancedDialog
  42. {
  43. //{{{ CommandoDialog constructor
  44. public CommandoDialog(View view, String command)
  45. {
  46. super(view,jEdit.getProperty("commando.title"),false);
  47. this.view = view;
  48. JPanel content = new JPanel(new BorderLayout(0,12));
  49. content.setBorder(new EmptyBorder(12,12,12,12));
  50. setContentPane(content);
  51. JPanel top = new JPanel(new BorderLayout());
  52. JLabel label = new JLabel(jEdit.getProperty("commando.caption"));
  53. label.setBorder(new EmptyBorder(0,0,0,12));
  54. top.add(BorderLayout.WEST,label);
  55. content.add(BorderLayout.NORTH,top);
  56. EditAction[] commands = ConsolePlugin.getCommandoCommands();
  57. ActionHandler actionListener = new ActionHandler();
  58. commandCombo = new JComboBox(commands);
  59. commandCombo.setRenderer(new Renderer());
  60. commandCombo.addActionListener(actionListener);
  61. top.add(BorderLayout.CENTER,commandCombo);
  62. tabs = new JTabbedPane();
  63. tabs.addTab(jEdit.getProperty("commando.settings"),
  64. settings = new SettingsPane());
  65. tabs.addTab(jEdit.getProperty("commando.commands"),
  66. commandLine = new TextAreaPane());
  67. tabs.addChangeListener(new ChangeHandler());
  68. if(command == null)
  69. command = jEdit.getProperty("commando.last-command");
  70. for(int i = 0; i < commands.length; i++)
  71. {
  72. if(commands[i].getName().equals(command))
  73. {
  74. commandCombo.setSelectedIndex(i);
  75. break;
  76. }
  77. }
  78. load((CommandoCommand)commandCombo.getSelectedItem());
  79. content.add(BorderLayout.CENTER,tabs);
  80. Box buttons = new Box(BoxLayout.X_AXIS);
  81. buttons.add(Box.createGlue());
  82. ok = new JButton(jEdit.getProperty("common.ok"));
  83. ok.addActionListener(actionListener);
  84. getRootPane().setDefaultButton(ok);
  85. buttons.add(ok);
  86. buttons.add(Box.createHorizontalStrut(6));
  87. cancel = new JButton(jEdit.getProperty("common.cancel"));
  88. cancel.addActionListener(actionListener);
  89. buttons.add(cancel);
  90. buttons.add(Box.createGlue());
  91. content.add(BorderLayout.SOUTH,buttons);
  92. pack();
  93. setLocationRelativeTo(view);
  94. show();
  95. } //}}}
  96. //{{{ ok() method
  97. public void ok()
  98. {
  99. updateNameSpace();
  100. jEdit.setProperty("commando.last-command",command.getName());
  101. Vector commands = new Vector();
  102. for(int i = 0; i < scripts.size(); i++)
  103. {
  104. CommandoHandler.Script script
  105. = (CommandoHandler.Script)
  106. scripts.get(i);
  107. CommandoHandler.Command command = script.getCommand();
  108. if(command == null)
  109. {
  110. // user has already seen the BeanShell error,
  111. // so just exit
  112. return;
  113. }
  114. commands.addElement(command);
  115. }
  116. // open a console
  117. DockableWindowManager wm = view.getDockableWindowManager();
  118. wm.addDockableWindow("console");
  119. CommandoThread thread = new CommandoThread(
  120. (Console)wm.getDockable("console"),
  121. commands);
  122. thread.start();
  123. dispose();
  124. } //}}}
  125. //{{{ cancel() method
  126. public void cancel()
  127. {
  128. jEdit.setProperty("commando.last-command",command.getName());
  129. dispose();
  130. } //}}}
  131. //{{{ Private members
  132. //{{{ Instance variables
  133. private View view;
  134. private JComboBox commandCombo;
  135. private JTabbedPane tabs;
  136. private SettingsPane settings;
  137. private TextAreaPane commandLine;
  138. private JButton ok;
  139. private JButton cancel;
  140. private CommandoCommand command;
  141. private NameSpace nameSpace;
  142. private List scripts;
  143. private List components;
  144. private boolean init;
  145. //}}}
  146. //{{{ load() method
  147. private void load(CommandoCommand command)
  148. {
  149. init = true;
  150. this.command = command;
  151. settings.removeAll();
  152. components = new ArrayList();
  153. commandLine.setText(null);
  154. nameSpace = new NameSpace(BeanShell.getNameSpace(),
  155. "commando");
  156. scripts = new ArrayList();
  157. XmlParser parser = new XmlParser();
  158. CommandoHandler handler = new CommandoHandler(view,command,
  159. settings,nameSpace,components,scripts);
  160. parser.setHandler(handler);
  161. Reader in = null;
  162. try
  163. {
  164. in = command.openStream();
  165. parser.parse(null, null, in);
  166. }
  167. catch(XmlException xe)
  168. {
  169. Log.log(Log.ERROR,this,xe);
  170. int line = xe.getLine();
  171. String message = xe.getMessage();
  172. Object[] pp = { command.getLabel() + ".xml", new Integer(line),
  173. message };
  174. GUIUtilities.error(null,"commando.xml-error",pp);
  175. }
  176. catch(IOException io)
  177. {
  178. Log.log(Log.ERROR,this,io);
  179. Object[] pp = { command.getLabel() + ".xml", io.toString() };
  180. GUIUtilities.error(null,"read-error",pp);
  181. }
  182. catch(Exception e)
  183. {
  184. Log.log(Log.ERROR,this,e);
  185. }
  186. finally
  187. {
  188. try
  189. {
  190. if(in != null)
  191. in.close();
  192. }
  193. catch(IOException io)
  194. {
  195. Log.log(Log.ERROR,this,io);
  196. }
  197. }
  198. getRootPane().revalidate();
  199. pack();
  200. init = false;
  201. tabs.setSelectedIndex(0);
  202. } //}}}
  203. //{{{ updateNameSpace() method
  204. private void updateNameSpace()
  205. {
  206. for(int i = 0; i < components.size(); i++)
  207. {
  208. This t = (This)components.get(i);
  209. try
  210. {
  211. t.invokeMethod("valueChanged",new Object[0]);
  212. }
  213. catch(EvalError e)
  214. {
  215. Log.log(Log.ERROR,this,e);
  216. }
  217. }
  218. } //}}}
  219. //{{{ updateTextArea() method
  220. private void updateTextArea()
  221. {
  222. if(init)
  223. return;
  224. StringBuffer buf = new StringBuffer();
  225. for(int i = 0; i < scripts.size(); i++)
  226. {
  227. CommandoHandler.Script script
  228. = (CommandoHandler.Script)
  229. scripts.get(i);
  230. CommandoHandler.Command command = script.getCommand();
  231. if(command == null)
  232. {
  233. // user has already seen the BeanShell error,
  234. // so just exit
  235. return;
  236. }
  237. buf.append(command.shell);
  238. buf.append(": ");
  239. buf.append(command.command);
  240. buf.append('\n');
  241. }
  242. commandLine.setText(buf.toString());
  243. } //}}}
  244. //}}}
  245. //{{{ Inner classes
  246. //{{{ Renderer class
  247. class Renderer extends DefaultListCellRenderer
  248. {
  249. public Component getListCellRendererComponent(
  250. JList list, Object value, int index,
  251. boolean isSelected, boolean cellHasFocus)
  252. {
  253. super.getListCellRendererComponent(list,value,index,
  254. isSelected,cellHasFocus);
  255. EditAction action = (EditAction)value;
  256. setText(action.getLabel());
  257. return this;
  258. }
  259. } //}}}
  260. //{{{ ActionHandler class
  261. class ActionHandler implements ActionListener
  262. {
  263. public void actionPerformed(ActionEvent evt)
  264. {
  265. if(evt.getSource() == commandCombo)
  266. {
  267. CommandoCommand command = (CommandoCommand)commandCombo
  268. .getSelectedItem();
  269. load(command);
  270. }
  271. else if(evt.getSource() == ok)
  272. ok();
  273. else if(evt.getSource() == cancel)
  274. cancel();
  275. }
  276. } //}}}
  277. //{{{ ChangeHandler class
  278. class ChangeHandler implements ChangeListener
  279. {
  280. public void stateChanged(ChangeEvent evt)
  281. {
  282. if(tabs.getSelectedIndex() == 1)
  283. {
  284. updateNameSpace();
  285. updateTextArea();
  286. }
  287. }
  288. } //}}}
  289. //{{{ SettingsPane class
  290. public static class SettingsPane extends JPanel
  291. {
  292. //{{{ SettingsPane constructor
  293. SettingsPane()
  294. {
  295. setLayout(gridBag = new GridBagLayout());
  296. } //}}}
  297. //{{{ addComponent() method
  298. public void addComponent(String left, Component right)
  299. {
  300. JLabel label = new JLabel(left + ":");
  301. label.setBorder(new EmptyBorder(0,0,0,12));
  302. addComponent(label,right);
  303. } //}}}
  304. //{{{ addComponent() method
  305. public void addComponent(Component left, Component right)
  306. {
  307. GridBagConstraints cons = new GridBagConstraints();
  308. cons.gridy = y++;
  309. cons.gridheight = 1;
  310. cons.gridwidth = 1;
  311. cons.weightx = 0.0f;
  312. cons.fill = GridBagConstraints.BOTH;
  313. gridBag.setConstraints(left,cons);
  314. add(left);
  315. cons.gridx = 1;
  316. cons.weightx = 1.0f;
  317. gridBag.setConstraints(right,cons);
  318. add(right);
  319. } //}}}
  320. //{{{ addComponent() method
  321. public void addComponent(Component comp)
  322. {
  323. GridBagConstraints cons = new GridBagConstraints();
  324. cons.gridy = y++;
  325. cons.gridheight = 1;
  326. cons.gridwidth = cons.REMAINDER;
  327. cons.fill = GridBagConstraints.BOTH;
  328. cons.anchor = GridBagConstraints.WEST;
  329. cons.weightx = 1.0f;
  330. gridBag.setConstraints(comp,cons);
  331. add(comp);
  332. } //}}}
  333. private GridBagLayout gridBag;
  334. private int y;
  335. } //}}}
  336. //{{{ TextAreaPane class
  337. static class TextAreaPane extends JPanel
  338. {
  339. //{{{ TextAreaPane constructor
  340. TextAreaPane()
  341. {
  342. super(new BorderLayout());
  343. JPanel panel = new JPanel(new BorderLayout());
  344. panel.setBorder(new EmptyBorder(6,0,6,0));
  345. panel.add(BorderLayout.WEST,copy = new JButton(
  346. jEdit.getProperty("commando.copy")));
  347. copy.addActionListener(new ActionHandler());
  348. add(BorderLayout.NORTH,panel);
  349. add(BorderLayout.CENTER,new JScrollPane(
  350. textArea = new JTextArea(4,30)));
  351. textArea.setEditable(false);
  352. textArea.setLineWrap(true);
  353. } //}}}
  354. //{{{ setText() method
  355. void setText(String text)
  356. {
  357. textArea.setText(text);
  358. } //}}}
  359. private JButton copy;
  360. private JTextArea textArea;
  361. //{{{ ActionHandler class
  362. class ActionHandler implements ActionListener
  363. {
  364. public void actionPerformed(ActionEvent evt)
  365. {
  366. textArea.copy();
  367. }
  368. } //}}}
  369. } //}}}
  370. //}}}
  371. }