1 package fi.jyu.mit.fxgui;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import javafx.beans.property.BooleanProperty;
8 import javafx.beans.property.SimpleBooleanProperty;
9 import javafx.beans.property.SimpleStringProperty;
10 import javafx.beans.property.StringProperty;
11 import javafx.beans.value.ChangeListener;
12 import javafx.beans.value.ObservableValue;
13 import javafx.collections.FXCollections;
14 import javafx.collections.ListChangeListener;
15 import javafx.collections.ObservableList;
16 import javafx.geometry.Insets;
17 import javafx.scene.Node;
18 import javafx.scene.layout.VBox;
27 public abstract class BaseBoxChooser<TO, TC
extends Node> extends VBox implements MultipleChooser<TO> {
31 protected StringProperty rivit =
new SimpleStringProperty(
"\n");
35 protected BooleanProperty nollataanko =
new SimpleBooleanProperty(
false);
39 protected ArrayList<TC> boxes =
new ArrayList<TC>();
43 protected ObservableList<StringAndObject<TO>> items = FXCollections.observableArrayList();
47 protected ArrayList<Consumer<TO>> lambdaListeners =
new ArrayList<Consumer<TO>>();
51 protected ArrayList<ChangeListener<Boolean>> changeListenersForRemoval =
new ArrayList<>();
59 items.addListener(
new ListChangeListener<StringAndObject<TO>>() {
62 public void onChanged(Change<? extends StringAndObject<TO>> c) {
65 if (c.wasPermutated()) {
70 }
else if (c.wasUpdated()) {
85 this.setSpacing(10.0);
86 this.setPadding(
new Insets(10, 0, 10, 20));
97 this.lambdaListeners.add(event);
107 public void add(String name, TO
object){
108 add(
new StringAndObject<TO>(name,
object));
116 public void add(StringAndObject<TO> so) {
120 final TC box = addCorrectComponent(so.getName(), so.getObject());
122 ChangeListener<Boolean> listener =
new ChangeListener<Boolean>() {
124 public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
129 changeListenersForRemoval.add(listener);
131 addChangeListener(box, listener);
132 this.getChildren().add(box);
144 for (Consumer<TO> checkBoxListener : lambdaListeners) {
145 TO selected = getSelectedObject();
146 checkBoxListener.accept(selected);
156 protected abstract void addChangeListener(TC box, ChangeListener<Boolean> listener);
166 protected abstract TC addCorrectComponent(String name, TO
object);
173 public ObservableList<StringAndObject<TO>>
getItems() {
184 for (StringAndObject<TO> stringAndObject : list) {
185 add(stringAndObject);
195 public void add(TO
object){
216 ObservableList<Node> children = this.getChildren();
217 for (TC node : boxes) {
218 children.remove(node);
220 removeListener(node, changeListenersForRemoval);
225 changeListenersForRemoval.clear();
235 protected abstract void removeListener(TC node, ArrayList<ChangeListener<Boolean>> list);
239 ObservableList<StringAndObject<TO>> objects = getItems();
241 for (String
string : strings) {
255 this.rivit.set(jono);
256 String[] strings = jono.split(
"\n");
269 StringBuilder sb =
new StringBuilder();
271 for (String
s: rivit) { sb.append(sep).append(
s); sep =
"\n"; }
272 this.rivit.set(sb.toString());
283 for (
int i = 0;
i < boxes.size();
i++) {
284 if(isComponentSelected(boxes.get(
i)))
286 return items.get(
i).getObject();
298 for (
int i = 0;
i < boxes.size();
i++) {
299 if(isComponentSelected(boxes.get(
i)))
301 return removeMnemonic(items.get(
i).getName());
313 for (
int i = 0;
i < boxes.size();
i++) {
314 if(isComponentSelected(boxes.get(
i)))
325 int oldIndex = getSelectedIndex();
326 if ( index < 0 || boxes.size() <= index )
328 setComponentSelected(boxes.get(index),
true);
338 protected abstract boolean isComponentSelected(TC component);
346 protected abstract boolean setComponentSelected(TC component,
boolean value);
351 List<TO> objects =
new ArrayList<TO>();
352 for (
int i = 0;
i < boxes.size();
i++) {
353 if(isComponentSelected(boxes.get(
i))){
354 objects.add(items.get(
i).getObject());
364 List<String> objects =
new ArrayList<String>();
365 for (
int i = 0;
i < boxes.size();
i++) {
366 if(isComponentSelected(boxes.get(
i))){
367 objects.add(removeMnemonic(items.get(
i).getName()));
377 List<Integer> objects =
new ArrayList<Integer>();
378 for (
int i = 0;
i < boxes.size();
i++) {
379 if(isComponentSelected(boxes.get(
i))){
403 nollataanko.set(reset);
412 return nollataanko.get();
423 return item.replaceFirst(
"_",
"");