Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fsmv.util;
- /*
- * Copyright (c) 2011 Andrew Kallmeyer
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify, merge,
- * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
- * to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Collection;
- /**
- * This class is a terminal emulator front end for text based applications.
- * It is meant to be extended.
- * 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.
- */
- public class GUITester implements ActionListener {
- private static Map<String, String> help = new HashMap<String, String>();
- 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.
- private int numCommands = 0;
- private int historyIndex = 0;
- private JFrame frame;
- private JTextArea stdout;
- private JTextField stdin;
- /**
- * This method simply calls the constructor for this class.
- * This class is runnable because it's meant as a front end for an application that would normally run in a terminal.
- * Send it a command line argument or change this method to change the window title.
- *
- * @args The command line arguments sent to this application
- */
- public static void main (String[] args) {
- if(args.length > 0)
- new GUITester(args[0]);
- else
- new GUITester();
- }
- /**
- * This creates a window, the gui, and adds the default commands to help.
- * The default window title is "GUITester."
- */
- public GUITester(){
- init("GUITester");
- }
- /**
- * This creates a window, the gui, and adds the default commands to help.
- * Use this constructor to set the window title.
- *
- * @param title The window title
- */
- public GUITester(String title){ //Use this constructor to set the window title
- init(title);
- }
- /**
- * This method should only be run once and only by the constructor.
- * It's in a separate method so both constructors don't need this pasted in it.
- * Basically, this method keeps down the filesize and gets rid of redundancy.
- *
- * @param title The window title
- */
- private void init(String title) {
- frame = new JFrame(title);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container pane = frame.getContentPane();
- pane.setLayout(null);
- JButton goButton = new JButton("Enter"); //This is the button to send the user's input
- goButton.addActionListener(this);
- goButton.setBounds(410, 5, 69, 29); //button size and position
- stdout = new JTextArea(); //This is the standard output (stdout)
- stdout.setEditable(false);
- stdout.setFont(new Font("monospaced", Font.PLAIN, 13));
- JScrollPane scrollBars = new JScrollPane(stdout); //So stdout will scroll
- scrollBars.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
- scrollBars.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollBars.setBounds(5, 40, 475, 420); //stdout size and position
- stdin = new JTextField(50); //This is the standard input (stdin)
- stdin.setBounds(5, 5, 400, 30); //stdin box size and position
- stdin.addKeyListener(new KeyAdapter() {
- public void keyReleased(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_ENTER)
- parseInput();
- else if(e.getKeyCode() == KeyEvent.VK_UP)
- readHistory(true);
- else if(e.getKeyCode() == KeyEvent.VK_DOWN)
- readHistory(false);
- }
- });
- pane.add(goButton);
- pane.add(scrollBars);
- pane.add(stdin);
- 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.
- frame.setVisible(true);
- //Use these as a model for what to give to the addToHelp method
- addToHelp("help", "help [command]\tDisplays help on [command]");
- addToHelp("clear", "clear\tClears the screen.");
- addToHelp("echo", "echo [string]\tPrints [string] to the screen.");
- }
- /**
- * This method is run when the up arrow or down arrow is pressed.
- * It allows the user to move up through the last 50 commands they've typed
- * It should only be used by the GUI. So, it's private.
- *
- * @up The scroll direction, true if up, false if down.
- */
- private void readHistory(boolean up){
- if(up){ //Scroll up
- if(historyIndex != 0)
- historyIndex--;
- if(historyIndex <= numCommands)
- stdin.setText(history[historyIndex]);
- else
- historyIndex = numCommands;
- }else{ //Scroll down
- if(historyIndex == 50)
- stdin.setText("");
- else
- historyIndex++;
- if(numCommands > 0 && historyIndex <= numCommands)
- stdin.setText(history[historyIndex]);
- else{
- stdin.setText("");
- historyIndex = numCommands;
- }
- }
- }
- /**
- * This method is run when the user hits the enter key or the on screen enter button.
- * It splits apart the user's input and sends it it runFunctions()
- * It should only be used by the GUI. So, it's private.
- */
- private void parseInput(){
- String[] args = stdin.getText().split(" ");
- history[numCommands] = stdin.getText();
- if(numCommands == 50)
- numCommands = 0;
- else
- numCommands++;
- historyIndex = numCommands;
- stdin.setText("");
- runFunctions(args);
- }
- /**
- * This method is called by parseInput.
- * It should be overridden (and called with super) to add new commands.
- * It is private so there are no surprises for the user.
- * This way only the user is able to run a function and no other class will unexpectedly call one.
- *
- * @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).
- */
- private void runFunctions(String[] args){
- if(args.length > 0){
- if(args[0].equals("clear")) //Add more if's like this to add commands.
- stdout.setText("");
- if(args.length == 1){
- if(args[0].equals("help")){
- stdout("GUITester, licensed under the MIT License <http://www.opensource.org/licenses/mit-license.php>\ncopyright ©2011 Andrew Kallmeyer\n\nCommands:\n");
- for (Map.Entry<String, String> entry : help.entrySet()){
- stdout("\t");
- stdout(entry.getValue().split("\t")[0] + "\n");
- }
- }
- }
- if(args.length > 1){ //Add more if's like this for commands that require more arguments.
- if(args[0].equals("help")){
- if(help.containsKey(args[1])){
- stdout(help.get(args[1]) + "\n");
- }else
- stdout("ERROR: Command \"" + args[1] + "\" not found!\n");
- }else if(args[0].equals("echo")){
- for(int i = 1; i < args.length; i++)
- stdout(args[i] + " ");
- stdout("\n");
- }
- }
- }
- }
- /**
- * Call this method to add documentation for the functions you've implemented.
- * This function adds to the help map. It is essential for the help command to work.
- *
- * @param name Name of the command to be added. Ex: "help"
- * @param helpText Line to display when the user types "help name." Ex: "help [command]\tDisplays help on [command]"
- */
- public void addToHelp(String name, String helpText){ //Make sure to call this for each command you've implemented.
- help.put(name, helpText);
- }
- /**
- * Displays text to the user.
- *
- * @out text to display
- */
- public void stdout(String out){ //Send the output of each function to this method.
- stdout.append(out);
- }
- /**
- * This method is called when the on screen enter button is pressed.
- * It sends the input from stdin to parseInput.
- *
- * @param event The event that was dispacted
- */
- public void actionPerformed(ActionEvent event) {
- parseInput();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement