Skip to content
Snippets Groups Projects
Commit ee2d2bb6 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

initial commit

parent 80549e0d
No related branches found
No related tags found
No related merge requests found
Showing
with 319 additions and 2 deletions
<?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>
.project 0 → 100644
<?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>
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
# CommandLine
\ No newline at end of file
File added
File added
File added
<?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>
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
File added
File added
File added
File added
File added
File added
File added
File added
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
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
<?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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment