Advertisement
Guest User

GUITester v2

a guest
Jul 29th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. package com.fsmv.util;
  2.  
  3. /*
  4. * Copyright (c) 2011 Andrew Kallmeyer
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  7. * software and associated documentation files (the "Software"), to deal in the Software
  8. * without restriction, including without limitation the rights to use, copy, modify, merge,
  9. * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  10. * to whom the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or
  13. * substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  17. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  18. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22.  
  23. import javax.swing.*;
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.util.Map;
  27. import java.util.HashMap;
  28. import java.util.Collection;
  29.  
  30. /**
  31. * This class is a terminal emulator front end for text based applications.
  32. * It is meant to be extended.
  33. * It can be used to run functions with different arguments, without having to recompile a test class or running an application many times through the terminal.
  34. */
  35. public class GUITester implements ActionListener {
  36.  
  37. private static Map<String, String> help = new HashMap<String, String>();
  38. private String[] history = new String[50]; //If you think history needs to be longer than 50 commands change it here and at all other instances of 50 in the readHistory method.
  39. private int numCommands = 0;
  40. private int historyIndex = 0;
  41.  
  42. private JFrame frame;
  43. private JTextArea stdout;
  44. private JTextField stdin;
  45.  
  46. /**
  47. * This method simply calls the constructor for this class.
  48. * This class is runnable because it's meant as a front end for an application that would normally run in a terminal.
  49. * Send it a command line argument or change this method to change the window title.
  50. *
  51. * @args The command line arguments sent to this application
  52. */
  53. public static void main (String[] args) {
  54. if(args.length > 0)
  55. new GUITester(args[0]);
  56. else
  57. new GUITester();
  58. }
  59.  
  60. /**
  61. * This creates a window, the gui, and adds the default commands to help.
  62. * The default window title is "GUITester."
  63. */
  64. public GUITester(){
  65. init("GUITester");
  66. }
  67.  
  68. /**
  69. * This creates a window, the gui, and adds the default commands to help.
  70. * Use this constructor to set the window title.
  71. *
  72. * @param title The window title
  73. */
  74. public GUITester(String title){ //Use this constructor to set the window title
  75. init(title);
  76. }
  77.  
  78. /**
  79. * This method should only be run once and only by the constructor.
  80. * It's in a separate method so both constructors don't need this pasted in it.
  81. * Basically, this method keeps down the filesize and gets rid of redundancy.
  82. *
  83. * @param title The window title
  84. */
  85. private void init(String title) {
  86. frame = new JFrame(title);
  87. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88. Container pane = frame.getContentPane();
  89. pane.setLayout(null);
  90.  
  91. JButton goButton = new JButton("Enter"); //This is the button to send the user's input
  92. goButton.addActionListener(this);
  93. goButton.setBounds(410, 5, 69, 29); //button size and position
  94.  
  95. stdout = new JTextArea(); //This is the standard output (stdout)
  96. stdout.setEditable(false);
  97. stdout.setFont(new Font("monospaced", Font.PLAIN, 13));
  98.  
  99. JScrollPane scrollBars = new JScrollPane(stdout); //So stdout will scroll
  100. scrollBars.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  101. scrollBars.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  102. scrollBars.setBounds(5, 40, 475, 420); //stdout size and position
  103.  
  104. stdin = new JTextField(50); //This is the standard input (stdin)
  105. stdin.setBounds(5, 5, 400, 30); //stdin box size and position
  106. stdin.addKeyListener(new KeyAdapter() {
  107. public void keyReleased(KeyEvent e) {
  108. if(e.getKeyCode() == KeyEvent.VK_ENTER)
  109. parseInput();
  110. else if(e.getKeyCode() == KeyEvent.VK_UP)
  111. readHistory(true);
  112. else if(e.getKeyCode() == KeyEvent.VK_DOWN)
  113. readHistory(false);
  114. }
  115. });
  116.  
  117.  
  118. pane.add(goButton);
  119. pane.add(scrollBars);
  120. pane.add(stdin);
  121.  
  122. frame.setSize(500, 500); //Change the window size here. Keep in mind the GUI components have a static value for their size. If you change this you'll have to also change their sizes above.
  123. frame.setVisible(true);
  124.  
  125. //Use these as a model for what to give to the addToHelp method
  126. addToHelp("help", "help [command]\tDisplays help on [command]");
  127. addToHelp("clear", "clear\tClears the screen.");
  128. addToHelp("echo", "echo [string]\tPrints [string] to the screen.");
  129. }
  130.  
  131. /**
  132. * This method is run when the up arrow or down arrow is pressed.
  133. * It allows the user to move up through the last 50 commands they've typed
  134. * It should only be used by the GUI. So, it's private.
  135. *
  136. * @up The scroll direction, true if up, false if down.
  137. */
  138. private void readHistory(boolean up){
  139. if(up){ //Scroll up
  140. if(historyIndex != 0)
  141. historyIndex--;
  142. if(historyIndex <= numCommands)
  143. stdin.setText(history[historyIndex]);
  144. else
  145. historyIndex = numCommands;
  146. }else{ //Scroll down
  147. if(historyIndex == 50)
  148. stdin.setText("");
  149. else
  150. historyIndex++;
  151.  
  152. if(numCommands > 0 && historyIndex <= numCommands)
  153. stdin.setText(history[historyIndex]);
  154. else{
  155. stdin.setText("");
  156. historyIndex = numCommands;
  157. }
  158. }
  159. }
  160.  
  161. /**
  162. * This method is run when the user hits the enter key or the on screen enter button.
  163. * It splits apart the user's input and sends it it runFunctions()
  164. * It should only be used by the GUI. So, it's private.
  165. */
  166. private void parseInput(){
  167. String[] args = stdin.getText().split(" ");
  168.  
  169. history[numCommands] = stdin.getText();
  170. if(numCommands == 50)
  171. numCommands = 0;
  172. else
  173. numCommands++;
  174. historyIndex = numCommands;
  175.  
  176. stdin.setText("");
  177. runFunctions(args);
  178. }
  179.  
  180. /**
  181. * This method is called by parseInput.
  182. * It should be overridden (and called with super) to add new commands.
  183. * It is private so there are no surprises for the user.
  184. * This way only the user is able to run a function and no other class will unexpectedly call one.
  185. *
  186. * @param args this is an array of the command line arguements that were in stdin. It is similar to the args parameter in public static void main(String[] args).
  187. */
  188. private void runFunctions(String[] args){
  189. if(args.length > 0){
  190. if(args[0].equals("clear")) //Add more if's like this to add commands.
  191. stdout.setText("");
  192. if(args.length == 1){
  193. if(args[0].equals("help")){
  194. stdout("GUITester, licensed under the MIT License <http://www.opensource.org/licenses/mit-license.php>\ncopyright ©2011 Andrew Kallmeyer\n\nCommands:\n");
  195. for (Map.Entry<String, String> entry : help.entrySet()){
  196. stdout("\t");
  197. stdout(entry.getValue().split("\t")[0] + "\n");
  198. }
  199. }
  200. }
  201. if(args.length > 1){ //Add more if's like this for commands that require more arguments.
  202. if(args[0].equals("help")){
  203. if(help.containsKey(args[1])){
  204. stdout(help.get(args[1]) + "\n");
  205. }else
  206. stdout("ERROR: Command \"" + args[1] + "\" not found!\n");
  207. }else if(args[0].equals("echo")){
  208. for(int i = 1; i < args.length; i++)
  209. stdout(args[i] + " ");
  210. stdout("\n");
  211. }
  212. }
  213. }
  214. }
  215.  
  216. /**
  217. * Call this method to add documentation for the functions you've implemented.
  218. * This function adds to the help map. It is essential for the help command to work.
  219. *
  220. * @param name Name of the command to be added. Ex: "help"
  221. * @param helpText Line to display when the user types "help name." Ex: "help [command]\tDisplays help on [command]"
  222. */
  223. public void addToHelp(String name, String helpText){ //Make sure to call this for each command you've implemented.
  224. help.put(name, helpText);
  225. }
  226.  
  227. /**
  228. * Displays text to the user.
  229. *
  230. * @out text to display
  231. */
  232. public void stdout(String out){ //Send the output of each function to this method.
  233. stdout.append(out);
  234. }
  235.  
  236. /**
  237. * This method is called when the on screen enter button is pressed.
  238. * It sends the input from stdin to parseInput.
  239. *
  240. * @param event The event that was dispacted
  241. */
  242. public void actionPerformed(ActionEvent event) {
  243. parseInput();
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement