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

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