From a947e081658a27a923853286de63cac2b975b92c Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 24 Aug 2018 17:08:05 +0200 Subject: [PATCH] Added basic command executor functionality. --- src/main/java/nl/andrewlalis/Main.java | 12 +++- .../andrewlalis/model/database/Database.java | 5 +- .../ui/control/CommandExecutor.java | 10 --- .../ui/control/CommandFieldKeyListener.java | 24 -------- .../ui/control/command/CommandExecutor.java | 61 +++++++++++++++++++ .../ui/control/command/Executable.java | 17 ++++++ .../listeners/CommandFieldKeyListener.java | 42 +++++++++++++ .../andrewlalis/ui/view/InitializerApp.java | 30 ++++++--- .../java/nl/andrewlalis/util/Logging.java | 2 +- 9 files changed, 157 insertions(+), 46 deletions(-) delete mode 100644 src/main/java/nl/andrewlalis/ui/control/CommandExecutor.java delete mode 100644 src/main/java/nl/andrewlalis/ui/control/CommandFieldKeyListener.java create mode 100644 src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java create mode 100644 src/main/java/nl/andrewlalis/ui/control/command/Executable.java create mode 100644 src/main/java/nl/andrewlalis/ui/control/listeners/CommandFieldKeyListener.java diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index 3545fe7..e2aee76 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -4,6 +4,8 @@ import nl.andrewlalis.model.database.Database; import nl.andrewlalis.git_api.GithubManager; import nl.andrewlalis.model.Student; import nl.andrewlalis.model.StudentTeam; +import nl.andrewlalis.ui.control.command.CommandExecutor; +import nl.andrewlalis.ui.control.command.Executable; import nl.andrewlalis.ui.view.InitializerApp; import nl.andrewlalis.util.CommandLine; import nl.andrewlalis.util.Logging; @@ -36,8 +38,16 @@ public class Main { logger.severe("Unable to save log to file."); } + CommandExecutor executor = new CommandExecutor(); + executor.registerCommand("test", args1 -> { + System.out.println("TESTING"); + return true; + }); + // Initialize User Interface. - InitializerApp app = new InitializerApp(); + InitializerApp app = new InitializerApp(executor); + + app.begin(); logger.info("GithubManager for Github Repositories in Educational Organizations."); diff --git a/src/main/java/nl/andrewlalis/model/database/Database.java b/src/main/java/nl/andrewlalis/model/database/Database.java index 33fb282..311dde3 100644 --- a/src/main/java/nl/andrewlalis/model/database/Database.java +++ b/src/main/java/nl/andrewlalis/model/database/Database.java @@ -5,7 +5,10 @@ import nl.andrewlalis.model.Student; import nl.andrewlalis.model.TeachingAssistant; import nl.andrewlalis.model.Team; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.List; import java.util.logging.Logger; diff --git a/src/main/java/nl/andrewlalis/ui/control/CommandExecutor.java b/src/main/java/nl/andrewlalis/ui/control/CommandExecutor.java deleted file mode 100644 index 2d52f50..0000000 --- a/src/main/java/nl/andrewlalis/ui/control/CommandExecutor.java +++ /dev/null @@ -1,10 +0,0 @@ -package nl.andrewlalis.ui.control; - -/** - * Manages parsing an entered string and executing a task based upon information in the command. - */ -public class CommandExecutor { - - - -} diff --git a/src/main/java/nl/andrewlalis/ui/control/CommandFieldKeyListener.java b/src/main/java/nl/andrewlalis/ui/control/CommandFieldKeyListener.java deleted file mode 100644 index f0b170f..0000000 --- a/src/main/java/nl/andrewlalis/ui/control/CommandFieldKeyListener.java +++ /dev/null @@ -1,24 +0,0 @@ -package nl.andrewlalis.ui.control; - -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -public class CommandFieldKeyListener implements KeyListener { - - @Override - public void keyTyped(KeyEvent keyEvent) { - - } - - @Override - public void keyPressed(KeyEvent keyEvent) { - - } - - @Override - public void keyReleased(KeyEvent keyEvent) { - if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) { - System.out.println("Enter pressed."); - } - } -} diff --git a/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java b/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java new file mode 100644 index 0000000..222bfb3 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java @@ -0,0 +1,61 @@ +package nl.andrewlalis.ui.control.command; + +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +/** + * Manages parsing an entered string and executing a task based upon information in the command. + */ +public class CommandExecutor { + + /** + * The logger for outputting debug info. + */ + private static final Logger logger = Logger.getLogger(CommandExecutor.class.getName()); + static { + logger.setParent(Logger.getGlobal()); + } + + /** + * A list of named commands which can be executed. + */ + private Map commands; + + public CommandExecutor() { + this.commands = new HashMap<>(); + } + + /** + * Adds a new command to the list of commands which this executor can handle. + * @param commandName The name that the command will be found by. + * @param executable The executable command that is bound to the given name. + */ + public void registerCommand(String commandName, Executable executable) { + this.commands.put(commandName, executable); + } + + /** + * Attempts to execute a command string, or show an error message if an invalid command or argument was entered. + * @param commandString The String command and any arguments that go with it. + */ + public void executeString(String commandString) { + String[] words = commandString.trim().toLowerCase().split(" "); + if (words.length < 1) { + logger.warning("No command supplied."); + return; + } + String commandName = words[0]; + String[] args = new String[words.length - 1]; + if (words.length > 1) { + System.arraycopy(words, 1, args, 0, words.length - 1); + } + if (this.commands.containsKey(commandName)) { + this.commands.get(commandName).execute(args); + logger.info(commandString); + } else { + logger.warning(commandName + " is not a valid command."); + } + } + +} diff --git a/src/main/java/nl/andrewlalis/ui/control/command/Executable.java b/src/main/java/nl/andrewlalis/ui/control/command/Executable.java new file mode 100644 index 0000000..77ed4bd --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/command/Executable.java @@ -0,0 +1,17 @@ +package nl.andrewlalis.ui.control.command; + +/** + * Classes which implement this interface tell that they may be 'executed', either via command-line, or through the use + * of user interface actions. + */ +public interface Executable { + + /** + * Runs this Executable's main functionality. + * + * @param args The list of arguments supplied to the executable. + * @return True if successful, false if an error occurred. + */ + boolean execute(String[] args); + +} diff --git a/src/main/java/nl/andrewlalis/ui/control/listeners/CommandFieldKeyListener.java b/src/main/java/nl/andrewlalis/ui/control/listeners/CommandFieldKeyListener.java new file mode 100644 index 0000000..a90f909 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/listeners/CommandFieldKeyListener.java @@ -0,0 +1,42 @@ +package nl.andrewlalis.ui.control.listeners; + +import nl.andrewlalis.ui.control.command.CommandExecutor; + +import javax.swing.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +/** + * This Key Listener listens for when the ENTER key is pressed in the command-line text field, and executes the command + * when that is the case. + */ +public class CommandFieldKeyListener implements KeyListener { + + /** + * This is responsible for parsing and running entered commands. + */ + CommandExecutor executor; + + public CommandFieldKeyListener(CommandExecutor executor) { + this.executor = executor; + } + + @Override + public void keyTyped(KeyEvent keyEvent) { + + } + + @Override + public void keyPressed(KeyEvent keyEvent) { + + } + + @Override + public void keyReleased(KeyEvent keyEvent) { + if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) { + JTextField inputField = (JTextField) keyEvent.getComponent(); + this.executor.executeString(inputField.getText()); + inputField.setText(null); + } + } +} diff --git a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java index 3eef11f..7a3b614 100644 --- a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java +++ b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java @@ -1,12 +1,11 @@ package nl.andrewlalis.ui.view; -import nl.andrewlalis.ui.control.CommandFieldKeyListener; +import nl.andrewlalis.ui.control.command.CommandExecutor; +import nl.andrewlalis.ui.control.listeners.CommandFieldKeyListener; import nl.andrewlalis.ui.control.OutputTextHandler; import javax.swing.*; import java.awt.*; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; import java.util.logging.Logger; /** @@ -24,14 +23,29 @@ public class InitializerApp extends JFrame { */ private static final Dimension SIZE = new Dimension(800, 600); + /** + * The pane on which general purpose program output is written. + */ private OutputTextPane outputTextPane; - public InitializerApp() { + /** + * The executor responsible for performing meaningful actions. + */ + private CommandExecutor executor; + + public InitializerApp(CommandExecutor executor) { + this.executor = executor; + + // UI initialization. this.initFrame(); } - public void printMessage() { - + /** + * Begins showing the application + */ + public void begin() { + this.pack(); + this.setVisible(true); } /** @@ -57,9 +71,7 @@ public class InitializerApp extends JFrame { this.setContentPane(mainPanel); - this.pack(); this.initLoggingHandler(); - this.setVisible(true); } /** @@ -77,7 +89,7 @@ public class InitializerApp extends JFrame { textEnterPanel.setBorder(BorderFactory.createLoweredBevelBorder()); textEnterPanel.add(new JLabel("Command:"), BorderLayout.WEST); JTextField commandField = new JTextField(); - commandField.addKeyListener(new CommandFieldKeyListener()); + commandField.addKeyListener(new CommandFieldKeyListener(this.executor)); textEnterPanel.add(commandField, BorderLayout.CENTER); // Top Label JLabel commandPanelLabel = new JLabel("Program output", SwingConstants.CENTER); diff --git a/src/main/java/nl/andrewlalis/util/Logging.java b/src/main/java/nl/andrewlalis/util/Logging.java index b9b7113..9161693 100644 --- a/src/main/java/nl/andrewlalis/util/Logging.java +++ b/src/main/java/nl/andrewlalis/util/Logging.java @@ -14,7 +14,7 @@ public class Logging { public static void setup(boolean verbose) throws IOException { Logger logger = Logger.getGlobal(); - outputFile = new FileHandler("log/latest.txt"); + outputFile = new FileHandler("log/latest.log"); formatter = new SimpleFormatter(); outputFile.setFormatter(formatter);