diff --git a/.classpath b/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..fb5011632c0ab8d6649a148c6fb5845a1b34c747 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/.project b/.project new file mode 100644 index 0000000000000000000000000000000000000000..b2f045fcf07aaeb6589902f90c840400695a0619 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>CommandLine</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000000000000000000000000000000000..3a21537071bf4118b9e1ee864cb4bc258aa48211 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/README.md b/README.md index 50664bc82c66b60eae554b9931764ea50e6fe705..5ca6fdca22b58e0fdcd33c774b1c62fb20a0b922 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# CommandLine - +# CommandLine \ No newline at end of file diff --git a/bin/application/Controller$1.class b/bin/application/Controller$1.class new file mode 100644 index 0000000000000000000000000000000000000000..9443f57368164992a75c52bf12b88f549fcef31f Binary files /dev/null and b/bin/application/Controller$1.class differ diff --git a/bin/application/Controller.class b/bin/application/Controller.class new file mode 100644 index 0000000000000000000000000000000000000000..abdd41f6c449844cf366c2927e5ce7fed4ffb582 Binary files /dev/null and b/bin/application/Controller.class differ diff --git a/bin/application/Main.class b/bin/application/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..b5b3eedf81bec76ca1d9917dcdd2460e72266410 Binary files /dev/null and b/bin/application/Main.class differ diff --git a/bin/application/MainGUI.fxml b/bin/application/MainGUI.fxml new file mode 100644 index 0000000000000000000000000000000000000000..3d7ae6ee6f84fb892d25f56871229044c017d5b6 --- /dev/null +++ b/bin/application/MainGUI.fxml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.geometry.Insets?> +<?import javafx.scene.control.TextArea?> +<?import javafx.scene.control.TextField?> +<?import javafx.scene.layout.AnchorPane?> +<?import javafx.scene.layout.VBox?> + +<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"> + <children> + <VBox layoutX="14.0" layoutY="14.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0"> + <children> + <TextArea fx:id="textareaHistory" prefHeight="508.0" prefWidth="572.0" VBox.vgrow="ALWAYS" /> + <TextField fx:id="textfieldInput" prefHeight="46.0" prefWidth="572.0"> + <VBox.margin> + <Insets top="14.0" /> + </VBox.margin> + </TextField> + </children> + </VBox> + </children> +</AnchorPane> diff --git a/bin/application/_de.properties b/bin/application/_de.properties new file mode 100644 index 0000000000000000000000000000000000000000..1ea1c0a41f36ff433bffe51a8f687effe0f4d5fe --- /dev/null +++ b/bin/application/_de.properties @@ -0,0 +1,12 @@ +app.name=CommandLine +version.code=0 +version.name=0.0.1 +version.date=22.07.16 + +help.list=list - lists all possible commands +help.help=help - shows help for given command\nSYNTAX: help [COMMAND] +help.clear=clear - clears the history + +error.unknown.command=Unknown command. Use \"list\" for a list of possible commands. +error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help. +error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands \ No newline at end of file diff --git a/bin/commands/Command.class b/bin/commands/Command.class new file mode 100644 index 0000000000000000000000000000000000000000..40fddff4115933d253769c294cf92c52705ce2d1 Binary files /dev/null and b/bin/commands/Command.class differ diff --git a/bin/commands/CommandClear.class b/bin/commands/CommandClear.class new file mode 100644 index 0000000000000000000000000000000000000000..71d46c5942d1e34952a7e6785b6a07bab70240e5 Binary files /dev/null and b/bin/commands/CommandClear.class differ diff --git a/bin/commands/CommandHelp.class b/bin/commands/CommandHelp.class new file mode 100644 index 0000000000000000000000000000000000000000..0ab23cd743bcbf327044dc67dc0c65fd44037e11 Binary files /dev/null and b/bin/commands/CommandHelp.class differ diff --git a/bin/commands/CommandList$1.class b/bin/commands/CommandList$1.class new file mode 100644 index 0000000000000000000000000000000000000000..e3b2858a80d5ef11794f98dfb5e40c266a2e2159 Binary files /dev/null and b/bin/commands/CommandList$1.class differ diff --git a/bin/commands/CommandList.class b/bin/commands/CommandList.class new file mode 100644 index 0000000000000000000000000000000000000000..0b0782e573ce2e8ead6b801933d467ba4ef11a9d Binary files /dev/null and b/bin/commands/CommandList.class differ diff --git a/bin/commands/HistoryEntry.class b/bin/commands/HistoryEntry.class new file mode 100644 index 0000000000000000000000000000000000000000..555ac2bee0604b0272bb28b80dbf6e2bbba727d7 Binary files /dev/null and b/bin/commands/HistoryEntry.class differ diff --git a/bin/commands/HistoryType.class b/bin/commands/HistoryType.class new file mode 100644 index 0000000000000000000000000000000000000000..8c58db9dbfa891c57a2d7ab322752bb56300d7d2 Binary files /dev/null and b/bin/commands/HistoryType.class differ diff --git a/bin/commands/PossibleCommands.class b/bin/commands/PossibleCommands.class new file mode 100644 index 0000000000000000000000000000000000000000..3757d2e80d89e17e3c8bc2acd1889e00262fb30d Binary files /dev/null and b/bin/commands/PossibleCommands.class differ diff --git a/src/application/Controller.java b/src/application/Controller.java new file mode 100644 index 0000000000000000000000000000000000000000..3066586ea2a3498d97725fcb6f2459cac1204ca2 --- /dev/null +++ b/src/application/Controller.java @@ -0,0 +1,185 @@ +package application; + +import java.util.ArrayList; +import java.util.Locale; +import java.util.ResourceBundle; + +import commands.PossibleCommands; +import commands.Command; +import commands.HistoryEntry; +import commands.HistoryType; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.stage.Stage; + +public class Controller +{ + @FXML private TextArea textareaHistory; + @FXML private TextField textfieldInput; + + public Stage stage; + private final ResourceBundle bundle = ResourceBundle.getBundle("application/", Locale.GERMANY); + + private ArrayList<HistoryEntry> globalHistory = new ArrayList<>(); + private int lastShwonCommand = 1; + private ArrayList<HistoryEntry> history = new ArrayList<>(); + private final String promptText = ">>>"; + + public void setStage(Stage stage) + { + this.stage = stage; + } + + public void init() + { + textareaHistory.setEditable(false); + textareaHistory.setWrapText(true); + + textfieldInput.setOnKeyPressed(new EventHandler<KeyEvent>() + { + @Override + public void handle(KeyEvent event) + { + if(event.getCode().equals(KeyCode.ENTER)) + { + parse(); + } + + if(event.getCode().equals(KeyCode.UP)) + { + showLastCommand(); + } + + if(event.getCode().equals(KeyCode.ESCAPE)) + { + clearConsole(); + } + } + }); + + printPrompt(); + } + + public void printPrompt() + { + setConsoleText(); + clearConsole(); + } + + public void print(String message) + { + history.add(new HistoryEntry(HistoryType.MESSAGE, message)); + setConsoleText(); + printPrompt(); + } + + public void clearHistoryLog() + { + textareaHistory.setText(""); + } + + public void clearHistory() + { + history = new ArrayList<>(); + } + + public void clearConsole() + { + textfieldInput.setText(""); + textfieldInput.requestFocus(); + } + + private void setConsoleText() + { + clearHistoryLog(); + + StringBuilder sb = new StringBuilder(); + boolean printedLastEntry = false; + for(int i = 0; i < history.size(); i++) + { + HistoryEntry currentEntry = history.get(i); + if(currentEntry.getType().equals(HistoryType.COMMAND)) + { + if(printedLastEntry) + { + sb.append("\n"); + } + sb.append(promptText); + sb.append(" "); + sb.append(currentEntry.getText()); + printedLastEntry = true; + } + else + { + if(i != 0) + { + sb.append("\n"); + } + sb.append(currentEntry.getText()); + printedLastEntry = true; + } + } + + textareaHistory.setText(sb.toString()); + textareaHistory.positionCaret(sb.toString().length()); + } + + private boolean executeCommand(String[] command) + { + for(Command cmd : PossibleCommands.possibleCommands) + { + if(cmd.getKeyword().equals(command[0])) + { + cmd.execute(command, this); + return true; + } + } + return false; + } + + private void parse() + { + String input = textfieldInput.getText().replace("\n", ""); + + if(input.equals("")) + { + printPrompt(); + return; + } + + globalHistory.add(new HistoryEntry(HistoryType.COMMAND, input)); + history.add(new HistoryEntry(HistoryType.COMMAND, input)); + lastShwonCommand = -1; + + String[] command = input.split(" "); + if(!executeCommand(command)) + { + print(bundle.getString("error.unknown.command")); + } + else + { + printPrompt(); + } + } + + private void showLastCommand() + { + if(globalHistory.size() > 0) + { + if(lastShwonCommand <= 0) + { + textfieldInput.setText(globalHistory.get(globalHistory.size()-1).getText()); + lastShwonCommand = globalHistory.size()-1; + } + else + { + textfieldInput.setText(globalHistory.get(lastShwonCommand - 1).getText()); + lastShwonCommand--; + } + } + } +} \ No newline at end of file diff --git a/src/application/Main.java b/src/application/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..4adcc79a8b4f3241289ee48c889d6793220461f6 --- /dev/null +++ b/src/application/Main.java @@ -0,0 +1,43 @@ +package application; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application +{ + @Override + public void start(Stage stage) + { + try + { + FXMLLoader loader = new FXMLLoader(getClass().getResource("MainGUI.fxml")); + Parent root = (Parent)loader.load(); + + Scene scene = new Scene(root, 600, 600); + + stage.setResizable(true); + stage.setTitle("CommandLine"); + stage.setScene(scene); + stage.setResizable(true); + stage.setMinHeight(250); + stage.setMinWidth(400); + + Controller controller = (Controller)loader.getController(); + controller.setStage(stage); + controller.init(); + stage.show(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + launch(args); + } +} \ No newline at end of file diff --git a/src/application/MainGUI.fxml b/src/application/MainGUI.fxml new file mode 100644 index 0000000000000000000000000000000000000000..3d7ae6ee6f84fb892d25f56871229044c017d5b6 --- /dev/null +++ b/src/application/MainGUI.fxml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.geometry.Insets?> +<?import javafx.scene.control.TextArea?> +<?import javafx.scene.control.TextField?> +<?import javafx.scene.layout.AnchorPane?> +<?import javafx.scene.layout.VBox?> + +<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"> + <children> + <VBox layoutX="14.0" layoutY="14.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0"> + <children> + <TextArea fx:id="textareaHistory" prefHeight="508.0" prefWidth="572.0" VBox.vgrow="ALWAYS" /> + <TextField fx:id="textfieldInput" prefHeight="46.0" prefWidth="572.0"> + <VBox.margin> + <Insets top="14.0" /> + </VBox.margin> + </TextField> + </children> + </VBox> + </children> +</AnchorPane> diff --git a/src/application/_de.properties b/src/application/_de.properties new file mode 100644 index 0000000000000000000000000000000000000000..34e77aea180208a74b51be584f2ecee45d9e1e28 --- /dev/null +++ b/src/application/_de.properties @@ -0,0 +1,12 @@ +app.name=CommandLine +version.code=1 +version.name=1.0.0 +version.date=22.07.16 + +help.list=list - lists all possible commands +help.help=help - shows help for given command\nSYNTAX: help [COMMAND] +help.clear=clear - clears the history + +error.unknown.command=Unknown command. Use \"list\" for a list of possible commands. +error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help. +error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands \ No newline at end of file diff --git a/src/commands/Command.java b/src/commands/Command.java new file mode 100644 index 0000000000000000000000000000000000000000..2f9d39e3a667b86f8ddef64f1ede4061a5fae9b9 --- /dev/null +++ b/src/commands/Command.java @@ -0,0 +1,43 @@ +package commands; + +import java.util.Locale; +import java.util.ResourceBundle; + +import application.Controller; + +public abstract class Command +{ + public String keyword; + public int numberOfParams; + public String helptText; + public final ResourceBundle bundle = ResourceBundle.getBundle("application/", Locale.GERMANY); + + public String getKeyword() + { + return keyword; + } + + public int getNumberOfParams() + { + return numberOfParams; + } + + public String getHelpText() + { + return keyword; + } + + public boolean isValid(String[] command) + { + if((command.length - 1) < numberOfParams || (command.length -1) > numberOfParams) + { + return false; + } + else + { + return true; + } + } + + public abstract void execute(String[] command, Controller controller); +} \ No newline at end of file diff --git a/src/commands/CommandClear.java b/src/commands/CommandClear.java new file mode 100644 index 0000000000000000000000000000000000000000..1facf0b5cf8f18799430b2ff7dd0632fa9f4c8db --- /dev/null +++ b/src/commands/CommandClear.java @@ -0,0 +1,28 @@ +package commands; + +import application.Controller; + +public class CommandClear extends Command +{ + public CommandClear() + { + super.keyword = "clear"; + super.numberOfParams = 0; + super.helptText = "help.clear"; + } + + @Override + public void execute(String[] command, Controller controller) + { + if(!isValid(command)) + { + controller.print(bundle.getString("error.invalid.arguments")); + return; + } + + controller.clearHistory(); + controller.clearHistoryLog(); + controller.clearConsole(); + controller.printPrompt(); + } +} \ No newline at end of file diff --git a/src/commands/CommandHelp.java b/src/commands/CommandHelp.java new file mode 100644 index 0000000000000000000000000000000000000000..36fb39a1675c3f5bc6d2caa7ca0cf4f169e8feb3 --- /dev/null +++ b/src/commands/CommandHelp.java @@ -0,0 +1,35 @@ +package commands; + +import application.Controller; + +public class CommandHelp extends Command +{ + public CommandHelp() + { + super(); + super.keyword = "help"; + super.numberOfParams = 1; + super.helptText = "help.help"; + } + + @Override + public void execute(String[] command, Controller controller) + { + if(!isValid(command)) + { + controller.print(bundle.getString("error.invalid.arguments")); + return; + } + + for(Command cmd : PossibleCommands.possibleCommands) + { + if(cmd.getKeyword().equals(command[1])) + { + controller.print(bundle.getString("help." + command[1])); + return; + } + } + + controller.print(bundle.getString("error.no.help")); + } +} diff --git a/src/commands/CommandList.java b/src/commands/CommandList.java new file mode 100644 index 0000000000000000000000000000000000000000..2deaa747424f52e04451121b34bae0926c2bc5c0 --- /dev/null +++ b/src/commands/CommandList.java @@ -0,0 +1,50 @@ +package commands; + +import java.util.ArrayList; +import java.util.Comparator; + +import application.Controller; + +public class CommandList extends Command +{ + public CommandList() + { + super(); + super.keyword = "list"; + super.numberOfParams = 0; + super.helptText = "help.list"; + } + + @Override + public void execute(String[] command, Controller controller) + { + if(!isValid(command)) + { + controller.print(bundle.getString("error.invalid.arguments")); + return; + } + + ArrayList<Command> commands = PossibleCommands.possibleCommands; + commands.sort(new Comparator<Command>() + { + @Override + public int compare(Command o1, Command o2) + { + return o1.keyword.compareTo(o2.keyword); + } + }); + + StringBuilder sb = new StringBuilder(); + sb.append("All possible commands:\n"); + for(int i = 0; i < commands.size(); i++) + { + sb.append(commands.get(i).keyword); + if(i != (commands.size()-1)) + { + sb.append("\n"); + } + } + + controller.print(sb.toString()); + } +} \ No newline at end of file diff --git a/src/commands/HistoryEntry.java b/src/commands/HistoryEntry.java new file mode 100644 index 0000000000000000000000000000000000000000..ffaf0a91ea12c03be0448de8fe4656d76a163013 --- /dev/null +++ b/src/commands/HistoryEntry.java @@ -0,0 +1,28 @@ +package commands; + +public class HistoryEntry +{ + private HistoryType type; + private String text; + + public HistoryEntry(HistoryType type, String text) + { + this.type = type; + this.text = text; + } + + public HistoryType getType() + { + return type; + } + + public String getText() + { + return text; + } + + public String toString() + { + return "HistoryEntry [type=" + type + ", text=" + text + "]"; + } +} \ No newline at end of file diff --git a/src/commands/HistoryType.java b/src/commands/HistoryType.java new file mode 100644 index 0000000000000000000000000000000000000000..169f897674b8489d8ed2962ff916d1a8ec1016ff --- /dev/null +++ b/src/commands/HistoryType.java @@ -0,0 +1,6 @@ +package commands; + +public enum HistoryType +{ + COMMAND, MESSAGE +} diff --git a/src/commands/PossibleCommands.java b/src/commands/PossibleCommands.java new file mode 100644 index 0000000000000000000000000000000000000000..8de7de25154eda1fba9b6f892f01e0689734453d --- /dev/null +++ b/src/commands/PossibleCommands.java @@ -0,0 +1,14 @@ +package commands; + +import java.util.ArrayList; +import java.util.Arrays; + +public class PossibleCommands +{ + + public static final ArrayList<Command> possibleCommands = new ArrayList<>(Arrays.asList( + new CommandList(), + new CommandHelp(), + new CommandClear() + )); +} \ No newline at end of file