Commit 447e4727 authored by Nico Eckes's avatar Nico Eckes

Renamed package to lowercase equivalent to conform to JavaFX. Added first editor code

parent a4cd48c4
Pipeline #739 canceled with stage
package com.unitedworldminers.permio.editor;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
public abstract class AddRemoveList<T extends Node> extends VBox {
private RemoveList rList = new RemoveList();
private TextField addText = new TextField();
public AddRemoveList(String title) {
Label head = new Label(title);
head.setPadding(new Insets(0,0,0,3));
getChildren().addAll(head, rList, addText);
setVgrow(rList, Priority.ALWAYS);
addText.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
String text = addText.getText().trim();
if (text.isEmpty()) return;
boolean notAdded = true;
for (int i = 0; i < rList.getItems().size() && notAdded; i++) {
if (toString(rList.getItems().get(i).element).compareToIgnoreCase(text) > 0) {
rList.getItems().add(i, new RemoveableEntry(elementAdded(text), rList));
rList.getSelectionModel().select(i);
rList.scrollTo(i);
notAdded = false;
}
}
if (notAdded) {
rList.getItems().add(new RemoveableEntry(elementAdded(text), rList));
rList.getSelectionModel().selectLast();
rList.scrollTo(rList.getItems().size()-1);
}
addText.clear();
}
});
}
public abstract T elementAdded(String element);
public abstract String toString(T element);
public abstract void elementRemoved(T element);
class RemoveList extends ListView<RemoveableEntry> {
public RemoveList() {
setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.DELETE) {
RemoveableEntry sel = getSelectionModel().getSelectedItem();
if (sel != null) {
elementRemoved(sel.element);
getItems().remove(sel);
}
}
});
}
}
class RemoveableEntry extends HBox {
private final T element;
public RemoveableEntry(T element, RemoveList parent) {
this.element = element;
getChildren().add(element);
AnchorPane pane = new AnchorPane();
pane.addEventHandler(EventType.ROOT, element::fireEvent);
setHgrow(pane, Priority.SOMETIMES);
getChildren().add(pane);
ImageView removeBtn = new ImageView(new Image(getClass().getClassLoader().getResourceAsStream("com/unitedworldminers/permio/editor/img/remove.png")));
removeBtn.setOnMouseClicked(event -> {
elementRemoved(element);
parent.getItems().remove(this);
});
getChildren().add(removeBtn);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment