Added basic command executor functionality.
This commit is contained in:
parent
eb0e70a384
commit
a947e08165
|
@ -4,6 +4,8 @@ import nl.andrewlalis.model.database.Database;
|
||||||
import nl.andrewlalis.git_api.GithubManager;
|
import nl.andrewlalis.git_api.GithubManager;
|
||||||
import nl.andrewlalis.model.Student;
|
import nl.andrewlalis.model.Student;
|
||||||
import nl.andrewlalis.model.StudentTeam;
|
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.ui.view.InitializerApp;
|
||||||
import nl.andrewlalis.util.CommandLine;
|
import nl.andrewlalis.util.CommandLine;
|
||||||
import nl.andrewlalis.util.Logging;
|
import nl.andrewlalis.util.Logging;
|
||||||
|
@ -36,8 +38,16 @@ public class Main {
|
||||||
logger.severe("Unable to save log to file.");
|
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.
|
// Initialize User Interface.
|
||||||
InitializerApp app = new InitializerApp();
|
InitializerApp app = new InitializerApp(executor);
|
||||||
|
|
||||||
|
app.begin();
|
||||||
|
|
||||||
logger.info("GithubManager for Github Repositories in Educational Organizations.");
|
logger.info("GithubManager for Github Repositories in Educational Organizations.");
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,10 @@ import nl.andrewlalis.model.Student;
|
||||||
import nl.andrewlalis.model.TeachingAssistant;
|
import nl.andrewlalis.model.TeachingAssistant;
|
||||||
import nl.andrewlalis.model.Team;
|
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.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<String, Executable> 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,11 @@
|
||||||
package nl.andrewlalis.ui.view;
|
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 nl.andrewlalis.ui.control.OutputTextHandler;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyEvent;
|
|
||||||
import java.awt.event.KeyListener;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,14 +23,29 @@ public class InitializerApp extends JFrame {
|
||||||
*/
|
*/
|
||||||
private static final Dimension SIZE = new Dimension(800, 600);
|
private static final Dimension SIZE = new Dimension(800, 600);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The pane on which general purpose program output is written.
|
||||||
|
*/
|
||||||
private OutputTextPane outputTextPane;
|
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();
|
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.setContentPane(mainPanel);
|
||||||
|
|
||||||
this.pack();
|
|
||||||
this.initLoggingHandler();
|
this.initLoggingHandler();
|
||||||
this.setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,7 +89,7 @@ public class InitializerApp extends JFrame {
|
||||||
textEnterPanel.setBorder(BorderFactory.createLoweredBevelBorder());
|
textEnterPanel.setBorder(BorderFactory.createLoweredBevelBorder());
|
||||||
textEnterPanel.add(new JLabel("Command:"), BorderLayout.WEST);
|
textEnterPanel.add(new JLabel("Command:"), BorderLayout.WEST);
|
||||||
JTextField commandField = new JTextField();
|
JTextField commandField = new JTextField();
|
||||||
commandField.addKeyListener(new CommandFieldKeyListener());
|
commandField.addKeyListener(new CommandFieldKeyListener(this.executor));
|
||||||
textEnterPanel.add(commandField, BorderLayout.CENTER);
|
textEnterPanel.add(commandField, BorderLayout.CENTER);
|
||||||
// Top Label
|
// Top Label
|
||||||
JLabel commandPanelLabel = new JLabel("Program output", SwingConstants.CENTER);
|
JLabel commandPanelLabel = new JLabel("Program output", SwingConstants.CENTER);
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class Logging {
|
||||||
public static void setup(boolean verbose) throws IOException {
|
public static void setup(boolean verbose) throws IOException {
|
||||||
Logger logger = Logger.getGlobal();
|
Logger logger = Logger.getGlobal();
|
||||||
|
|
||||||
outputFile = new FileHandler("log/latest.txt");
|
outputFile = new FileHandler("log/latest.log");
|
||||||
formatter = new SimpleFormatter();
|
formatter = new SimpleFormatter();
|
||||||
|
|
||||||
outputFile.setFormatter(formatter);
|
outputFile.setFormatter(formatter);
|
||||||
|
|
Loading…
Reference in New Issue