PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java

https://bitbucket.org/jwalton/antlr3
Java | 633 lines | 452 code | 132 blank | 49 comment | 68 complexity | 3688d83f3fd2bfde88df111275360a5d MD5 | raw file
  1. /*
  2. [The "BSD licence"]
  3. Copyright (c) 2009 Shaoting Cai
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package org.antlr.gunit.swingui;
  27. import org.antlr.gunit.swingui.model.*;
  28. import org.antlr.gunit.swingui.ImageFactory;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import java.util.HashMap;
  32. import javax.swing.*;
  33. import javax.swing.event.*;
  34. /**
  35. *
  36. * @author scai
  37. */
  38. public class TestCaseEditController implements IController {
  39. private JPanel view = new JPanel();
  40. private JScrollPane scroll;
  41. private JPanel paneDetail;
  42. private AbstractEditorPane paneDetailInput, paneDetailOutput;
  43. private JToolBar toolbar;
  44. private JList listCases;
  45. private ListModel listModel ;
  46. public ActionListener onTestCaseNumberChange;
  47. /* EDITORS */
  48. private InputFileEditor editInputFile;
  49. private InputStringEditor editInputString;
  50. private InputMultiEditor editInputMulti;
  51. private OutputResultEditor editOutputResult;
  52. private OutputAstEditor editOutputAST;
  53. private OutputStdEditor editOutputStd;
  54. private OutputReturnEditor editOutputReturn;
  55. private JComboBox comboInputType, comboOutputType;
  56. /* TYPE NAME */
  57. private static final String IN_TYPE_STRING = "Single-line Text";
  58. private static final String IN_TYPE_MULTI = "Multi-line Text";
  59. private static final String IN_TYPE_FILE = "Disk File";
  60. private static final String OUT_TYPE_BOOL = "OK or Fail";
  61. private static final String OUT_TYPE_AST = "AST";
  62. private static final String OUT_TYPE_STD = "Standard Output";
  63. private static final String OUT_TYPE_RET = "Return Value";
  64. private static final String DEFAULT_IN_SCRIPT = "";
  65. private static final String DEFAULT_OUT_SCRIPT = "";
  66. private static final Object[] INPUT_TYPE = {
  67. IN_TYPE_STRING, IN_TYPE_MULTI, IN_TYPE_FILE
  68. };
  69. private static final Object[] OUTPUT_TYPE = {
  70. OUT_TYPE_BOOL, OUT_TYPE_AST, OUT_TYPE_STD, OUT_TYPE_RET
  71. };
  72. /* SIZE */
  73. private static final int TEST_CASE_DETAIL_WIDTH = 300;
  74. private static final int TEST_EDITOR_WIDTH = 280;
  75. private static final int TEST_CASE_DETAIL_HEIGHT = 250;
  76. private static final int TEST_EDITOR_HEIGHT = 120;
  77. /* MODEL */
  78. private Rule currentRule = null;
  79. private TestCase currentTestCase = null;
  80. /* END OF MODEL*/
  81. private static final HashMap<Class, String> TypeNameTable;
  82. static {
  83. TypeNameTable = new HashMap<Class, String> ();
  84. TypeNameTable.put(TestCaseInputString.class, IN_TYPE_STRING);
  85. TypeNameTable.put(TestCaseInputMultiString.class, IN_TYPE_MULTI);
  86. TypeNameTable.put(TestCaseInputFile.class, IN_TYPE_FILE);
  87. TypeNameTable.put(TestCaseOutputResult.class, OUT_TYPE_BOOL);
  88. TypeNameTable.put(TestCaseOutputAST.class, OUT_TYPE_AST);
  89. TypeNameTable.put(TestCaseOutputStdOut.class, OUT_TYPE_STD);
  90. TypeNameTable.put(TestCaseOutputReturn.class, OUT_TYPE_RET);
  91. }
  92. //private WorkSpaceView owner;
  93. public TestCaseEditController(WorkSpaceView workspace) {
  94. //this.owner = workspace;
  95. initComponents();
  96. }
  97. public TestCaseEditController() {
  98. initComponents();
  99. }
  100. public void OnLoadRule(Rule rule) {
  101. if(rule == null) throw new IllegalArgumentException("Null");
  102. this.currentRule = rule;
  103. this.currentTestCase = null;
  104. this.listModel = rule;
  105. this.listCases.setModel(this.listModel);
  106. }
  107. public void setCurrentTestCase(TestCase testCase) {
  108. if(testCase == null) throw new IllegalArgumentException("Null");
  109. this.listCases.setSelectedValue(testCase, true);
  110. this.currentTestCase = testCase;
  111. }
  112. public Rule getCurrentRule() {
  113. return this.currentRule;
  114. }
  115. private void initComponents() {
  116. /* CASE LIST */
  117. listCases = new JList();
  118. listCases.addListSelectionListener(new TestCaseListSelectionListener());
  119. listCases.setCellRenderer(listRenderer);
  120. listCases.setOpaque(false);
  121. scroll = new JScrollPane(listCases);
  122. scroll.setBorder(BorderFactory.createTitledBorder(
  123. BorderFactory.createEmptyBorder(), "Test Cases"));
  124. scroll.setOpaque(false);
  125. scroll.setViewportBorder(BorderFactory.createEtchedBorder());
  126. /* CASE DETAIL */
  127. editInputString = new InputStringEditor();
  128. editInputMulti = new InputMultiEditor();
  129. editInputFile = new InputFileEditor();
  130. editOutputResult = new OutputResultEditor();
  131. editOutputAST = new OutputAstEditor();
  132. editOutputStd = new OutputStdEditor();
  133. editOutputReturn = new OutputReturnEditor();
  134. paneDetail = new JPanel();
  135. paneDetail.setBorder(BorderFactory.createEmptyBorder());
  136. paneDetail.setOpaque(false);
  137. comboInputType = new JComboBox(INPUT_TYPE);
  138. comboInputType.addActionListener(new ActionListener() {
  139. public void actionPerformed(ActionEvent event) {
  140. OnInputTestCaseTypeChanged(comboInputType.getSelectedItem());
  141. }
  142. });
  143. comboOutputType = new JComboBox(OUTPUT_TYPE);
  144. comboOutputType.addActionListener(new ActionListener() {
  145. public void actionPerformed(ActionEvent event) {
  146. OnOutputTestCaseTypeChanged(comboOutputType.getSelectedItem());
  147. }
  148. });
  149. paneDetailInput = new InputEditorPane(comboInputType);
  150. paneDetailOutput = new OutputEditorPane(comboOutputType);
  151. BoxLayout layout = new BoxLayout(paneDetail, BoxLayout.PAGE_AXIS);
  152. paneDetail.setLayout(layout);
  153. paneDetail.add(this.paneDetailInput);
  154. paneDetail.add(this.paneDetailOutput);
  155. /* TOOLBAR */
  156. toolbar = new JToolBar("Edit TestCases", JToolBar.VERTICAL);
  157. toolbar.setFloatable(false);
  158. toolbar.add(new AddTestCaseAction());
  159. toolbar.add(new RemoveTestCaseAction());
  160. /* COMPOSITE */
  161. view.setLayout(new BorderLayout());
  162. view.setBorder(BorderFactory.createEmptyBorder());
  163. view.setOpaque(false);
  164. view.add(toolbar, BorderLayout.WEST);
  165. view.add(scroll, BorderLayout.CENTER);
  166. view.add(paneDetail, BorderLayout.EAST);
  167. }
  168. private void updateInputEditor() {
  169. JComponent editor = null;
  170. if(currentTestCase != null ) {
  171. ITestCaseInput input = this.currentTestCase.getInput();
  172. if(input instanceof TestCaseInputString) {
  173. this.editInputString.setText(input.getScript());
  174. editor = this.editInputString;
  175. comboInputType.setSelectedItem(IN_TYPE_STRING);
  176. } else if(input instanceof TestCaseInputMultiString) {
  177. this.editInputMulti.setText(input.getScript());
  178. editor = this.editInputMulti.getView();
  179. comboInputType.setSelectedItem(IN_TYPE_MULTI);
  180. } else if(input instanceof TestCaseInputFile) {
  181. this.editInputFile.setText(input.getScript());
  182. editor = this.editInputFile;
  183. comboInputType.setSelectedItem(IN_TYPE_FILE);
  184. } else {
  185. throw new Error("Wrong type");
  186. }
  187. }
  188. paneDetailInput.setEditor(editor);
  189. }
  190. private void updateOutputEditor() {
  191. JComponent editor = null;
  192. if(currentTestCase != null) {
  193. ITestCaseOutput output = this.currentTestCase.getOutput();
  194. if(output instanceof TestCaseOutputAST) {
  195. this.editOutputAST.setText(output.getScript());
  196. editor = this.editOutputAST.getView();
  197. comboOutputType.setSelectedItem(OUT_TYPE_AST);
  198. } else if(output instanceof TestCaseOutputResult) {
  199. this.editOutputResult.setValue(output.getScript());
  200. editor = this.editOutputResult;
  201. comboOutputType.setSelectedItem(OUT_TYPE_BOOL);
  202. } else if(output instanceof TestCaseOutputStdOut) {
  203. this.editOutputStd.setText(output.getScript());
  204. editor = this.editOutputStd.getView();
  205. comboOutputType.setSelectedItem(OUT_TYPE_STD);
  206. } else if(output instanceof TestCaseOutputReturn) {
  207. this.editOutputReturn.setText(output.getScript());
  208. editor = this.editOutputReturn.getView();
  209. comboOutputType.setSelectedItem(OUT_TYPE_RET);
  210. } else {
  211. throw new Error("Wrong type");
  212. }
  213. }
  214. this.paneDetailOutput.setEditor(editor);
  215. }
  216. private void OnInputTestCaseTypeChanged(Object inputTypeStr) {
  217. if(this.currentTestCase != null) {
  218. ITestCaseInput input ;
  219. if(inputTypeStr == IN_TYPE_STRING) {
  220. input = new TestCaseInputString(DEFAULT_IN_SCRIPT);
  221. } else if(inputTypeStr == IN_TYPE_MULTI) {
  222. input = new TestCaseInputMultiString(DEFAULT_IN_SCRIPT);
  223. } else if(inputTypeStr == IN_TYPE_FILE) {
  224. input = new TestCaseInputFile(DEFAULT_IN_SCRIPT);
  225. } else {
  226. throw new Error("Wrong Type");
  227. }
  228. if(input.getClass().equals(this.currentTestCase.getInput().getClass()))
  229. return ;
  230. this.currentTestCase.setInput(input);
  231. }
  232. this.updateInputEditor();
  233. }
  234. private void OnOutputTestCaseTypeChanged(Object outputTypeStr) {
  235. if(this.currentTestCase != null) {
  236. ITestCaseOutput output ;
  237. if(outputTypeStr == OUT_TYPE_AST) {
  238. output = new TestCaseOutputAST(DEFAULT_OUT_SCRIPT);
  239. } else if(outputTypeStr == OUT_TYPE_BOOL) {
  240. output = new TestCaseOutputResult(false);
  241. } else if(outputTypeStr == OUT_TYPE_STD) {
  242. output = new TestCaseOutputStdOut(DEFAULT_OUT_SCRIPT);
  243. } else if(outputTypeStr == OUT_TYPE_RET) {
  244. output = new TestCaseOutputReturn(DEFAULT_OUT_SCRIPT);
  245. } else {
  246. throw new Error("Wrong Type");
  247. }
  248. if(output.getClass().equals(this.currentTestCase.getOutput().getClass()))
  249. return ;
  250. this.currentTestCase.setOutput(output);
  251. }
  252. this.updateOutputEditor();
  253. }
  254. private void OnTestCaseSelected(TestCase testCase) {
  255. //if(testCase == null) throw new RuntimeException("Null TestCase");
  256. this.currentTestCase = testCase;
  257. updateInputEditor();
  258. updateOutputEditor();
  259. }
  260. private void OnAddTestCase() {
  261. if(currentRule == null) return;
  262. final TestCase newCase = new TestCase(
  263. new TestCaseInputString(""),
  264. new TestCaseOutputResult(true));
  265. this.currentRule.addTestCase(newCase);
  266. setCurrentTestCase(newCase);
  267. this.listCases.setSelectedValue(newCase, true);
  268. this.listCases.updateUI();
  269. this.OnTestCaseSelected(newCase);
  270. this.onTestCaseNumberChange.actionPerformed(null);
  271. }
  272. private void OnRemoveTestCase() {
  273. if(currentTestCase == null) return;
  274. currentRule.removeElement(currentTestCase);
  275. listCases.updateUI();
  276. final TestCase nextActiveCase = listCases.isSelectionEmpty() ?
  277. null : (TestCase) listCases.getSelectedValue() ;
  278. OnTestCaseSelected(nextActiveCase);
  279. this.onTestCaseNumberChange.actionPerformed(null);
  280. }
  281. public Object getModel() {
  282. return currentRule;
  283. }
  284. public Component getView() {
  285. return view;
  286. }
  287. /* EDITOR CONTAINER */
  288. abstract public class AbstractEditorPane extends JPanel {
  289. private JComboBox combo;
  290. private JComponent editor;
  291. private String title;
  292. private JLabel placeHolder = new JLabel();
  293. public AbstractEditorPane(JComboBox comboBox, String title) {
  294. this.combo = comboBox;
  295. this.editor = placeHolder;
  296. this.title = title;
  297. this.initComponents();
  298. }
  299. private void initComponents() {
  300. placeHolder.setPreferredSize(new Dimension(
  301. TEST_CASE_DETAIL_WIDTH, TEST_CASE_DETAIL_HEIGHT));
  302. this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  303. this.add(combo, BorderLayout.NORTH);
  304. this.add(editor, BorderLayout.CENTER);
  305. this.setOpaque(false);
  306. this.setBorder(BorderFactory.createTitledBorder(title));
  307. this.setPreferredSize(new Dimension(
  308. TEST_CASE_DETAIL_WIDTH, TEST_CASE_DETAIL_HEIGHT));
  309. }
  310. public void setEditor(JComponent newEditor) {
  311. if(newEditor == null) newEditor = placeHolder;
  312. this.remove(editor);
  313. this.add(newEditor);
  314. this.editor = newEditor;
  315. this.updateUI();
  316. }
  317. }
  318. public class InputEditorPane extends AbstractEditorPane {
  319. public InputEditorPane(JComboBox comboBox) {
  320. super(comboBox, "Input");
  321. }
  322. }
  323. public class OutputEditorPane extends AbstractEditorPane {
  324. public OutputEditorPane(JComboBox comboBox) {
  325. super(comboBox, "Output");
  326. }
  327. }
  328. /* INPUT EDITORS */
  329. public class InputStringEditor extends JTextField implements CaretListener {
  330. public InputStringEditor() {
  331. super();
  332. this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
  333. this.addCaretListener(this);
  334. }
  335. public void caretUpdate(CaretEvent arg0) {
  336. currentTestCase.getInput().setScript(getText());
  337. listCases.updateUI();
  338. }
  339. }
  340. public class InputMultiEditor implements CaretListener {
  341. private JTextArea textArea = new JTextArea(20, 30);
  342. private JScrollPane scroll = new JScrollPane(textArea,
  343. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  344. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  345. public InputMultiEditor() {
  346. super();
  347. scroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
  348. textArea.addCaretListener(this);
  349. }
  350. public void caretUpdate(CaretEvent arg0) {
  351. currentTestCase.getInput().setScript(getText());
  352. listCases.updateUI();
  353. }
  354. public String getText() {
  355. return textArea.getText();
  356. }
  357. public void setText(String text) {
  358. textArea.setText(text);
  359. }
  360. public JComponent getView() {
  361. return scroll;
  362. }
  363. }
  364. public class InputFileEditor extends InputStringEditor {};
  365. public class OutputResultEditor extends JPanel implements ActionListener {
  366. private JToggleButton tbFail, tbOk;
  367. public OutputResultEditor() {
  368. super();
  369. tbFail = new JToggleButton("Fail");
  370. tbOk = new JToggleButton("OK");
  371. ButtonGroup group = new ButtonGroup();
  372. group.add(tbFail);
  373. group.add(tbOk);
  374. this.add(tbFail);
  375. this.add(tbOk);
  376. this.tbFail.addActionListener(this);
  377. this.tbOk.addActionListener(this);
  378. this.setPreferredSize(
  379. new Dimension(TEST_EDITOR_WIDTH, 100));
  380. }
  381. public void actionPerformed(ActionEvent e) {
  382. TestCaseOutputResult output =
  383. (TestCaseOutputResult) currentTestCase.getOutput();
  384. if(e.getSource() == tbFail) {
  385. output.setScript(false);
  386. } else {
  387. output.setScript(true);
  388. }
  389. listCases.updateUI();
  390. }
  391. public void setValue(String value) {
  392. if(TestCaseOutputResult.OK.equals(value)) {
  393. this.tbOk.setSelected(true);
  394. } else {
  395. this.tbFail.setSelected(true);
  396. }
  397. }
  398. }
  399. public class OutputAstEditor implements CaretListener {
  400. private JTextArea textArea = new JTextArea(20, 30);
  401. private JScrollPane scroll = new JScrollPane(textArea,
  402. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  403. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  404. public OutputAstEditor() {
  405. super();
  406. scroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
  407. textArea.addCaretListener(this);
  408. }
  409. public void caretUpdate(CaretEvent arg0) {
  410. currentTestCase.getOutput().setScript(getText());
  411. listCases.updateUI();
  412. }
  413. public void setText(String text) {
  414. this.textArea.setText(text);
  415. }
  416. public String getText() {
  417. return this.textArea.getText();
  418. }
  419. public JScrollPane getView() {
  420. return this.scroll;
  421. }
  422. }
  423. public class OutputStdEditor extends OutputAstEditor {}
  424. public class OutputReturnEditor extends OutputAstEditor {}
  425. /* EVENT HANDLERS */
  426. private class TestCaseListSelectionListener implements ListSelectionListener {
  427. public void valueChanged(ListSelectionEvent e) {
  428. if(e.getValueIsAdjusting()) return;
  429. final JList list = (JList) e.getSource();
  430. final TestCase value = (TestCase) list.getSelectedValue();
  431. if(value != null) OnTestCaseSelected(value);
  432. }
  433. }
  434. /* ACTIONS */
  435. private class AddTestCaseAction extends AbstractAction {
  436. public AddTestCaseAction() {
  437. super("Add", ImageFactory.getSingleton().ADD);
  438. putValue(SHORT_DESCRIPTION, "Add a gUnit test case.");
  439. }
  440. public void actionPerformed(ActionEvent e) {
  441. OnAddTestCase();
  442. }
  443. }
  444. private class RemoveTestCaseAction extends AbstractAction {
  445. public RemoveTestCaseAction() {
  446. super("Remove", ImageFactory.getSingleton().DELETE);
  447. putValue(SHORT_DESCRIPTION, "Remove a gUnit test case.");
  448. }
  449. public void actionPerformed(ActionEvent e) {
  450. OnRemoveTestCase();
  451. }
  452. }
  453. /* CELL RENDERERS */
  454. private static TestCaseListRenderer listRenderer
  455. = new TestCaseListRenderer();
  456. private static class TestCaseListRenderer implements ListCellRenderer {
  457. private static Font IN_FONT = new Font("mono", Font.PLAIN, 12);
  458. private static Font OUT_FONT = new Font("default", Font.BOLD, 12);
  459. public static String clamp(String text, int len) {
  460. if(text.length() > len) {
  461. return text.substring(0, len - 3).concat("...");
  462. } else {
  463. return text;
  464. }
  465. }
  466. public static String clampAtNewLine(String text) {
  467. int pos = text.indexOf('\n');
  468. if(pos >= 0) {
  469. return text.substring(0, pos).concat("...");
  470. } else {
  471. return text;
  472. }
  473. }
  474. public Component getListCellRendererComponent(
  475. JList list, Object value, int index,
  476. boolean isSelected, boolean hasFocus) {
  477. final JPanel pane = new JPanel();
  478. if (value instanceof TestCase) {
  479. final TestCase item = (TestCase) value;
  480. // create components
  481. final JLabel labIn = new JLabel(
  482. clamp(clampAtNewLine(item.getInput().getScript()), 18));
  483. final JLabel labOut = new JLabel(
  484. clamp(clampAtNewLine(item.getOutput().getScript()), 18));
  485. labOut.setFont(OUT_FONT);
  486. labIn.setFont(IN_FONT);
  487. labIn.setIcon(item.getInput() instanceof TestCaseInputFile ?
  488. ImageFactory.getSingleton().FILE16 :
  489. ImageFactory.getSingleton().EDIT16);
  490. pane.setBorder(BorderFactory.createEtchedBorder());
  491. pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
  492. pane.add(labIn);
  493. pane.add(labOut);
  494. pane.setBackground(isSelected ? Color.LIGHT_GRAY : Color.WHITE);
  495. }
  496. return pane;
  497. }
  498. }
  499. }