All Classes Namespaces Files Functions Variables
ListChooser.java
Go to the documentation of this file.
1 package fi.jyu.mit.fxgui;
2 
3 import java.util.function.Consumer;
4 
5 import javafx.beans.property.BooleanProperty;
6 import javafx.beans.property.SimpleBooleanProperty;
7 import javafx.beans.property.SimpleStringProperty;
8 import javafx.beans.property.StringProperty;
9 import javafx.beans.value.ChangeListener;
10 import javafx.beans.value.ObservableValue;
11 import javafx.collections.ObservableList;
12 import javafx.scene.control.ListView;
13 
14 /**
15  * A custom ListView control that supports easy addition of items in Scene
16  * Builder.
17  *
18  * @author Tero Paavolainen extended from ListChooser
19  * @version 13.1.2017
20  * @param <T> mitä luokkaa tallennetaan
21  *
22  */
23 public class ListChooser<T> extends ListView<StringAndObject<T>> implements Chooser<T> {
24 
25  private StringProperty rivit = new SimpleStringProperty("\n");
26  private BooleanProperty nollataanko = new SimpleBooleanProperty(false);
27 
28 
29  /**
30  * Initializes the control.
31  */
32  public ListChooser() {
33  super();
34 
35  }
36 
37 
38  /**
39  * Adds the given object with given name
40  * @param name objektin kohdalla näkyvä teksti
41  * @param object tallennettava olio
42  */
43  @Override
44  public void add(String name, T object){
45  getItems().add(new StringAndObject<T>(removeMnemonic(name), object));
46  }
47 
48 
49  /**
50  * Addes the given object to the list
51  * @param object tallennettava olio
52  */
53  @Override
54  public void add(T object){
55  add(null, object);
56  }
57 
58 
59  /**
60  * Adds example text to the list
61  * @param text teksti joka näytetään
62  */
63  @Override
64  public void addExample(String text){
65  add(text, null);
66  }
67 
68 
69  /**
70  * Clears the ObservableList that holds the objects
71  */
72  @Override
73  public void clear(){
74  getItems().clear();
75  }
76 
77 
78  private void setRows(String[] strings) {
79  ObservableList<StringAndObject<T>> objects = getItems();
80  objects.clear();
81  for (String string : strings) {
82  add(string, null);
83  }
84  this.getSelectionModel().selectFirst();
85  }
86 
87 
88  /**
89  * Sets the rows.
90  *
91  * @param jono
92  * A multiline string, each line representing a single row.
93  */
94  @Override
95  public void setRivit(String jono) {
96  this.rivit.set(jono);
97  String[] strings = jono.split("\n"); // erotinMerkki.get());
98  setRows(strings);
99  //rivit.setAll();
100  }
101 
102 
103  @Override
104  public void setRivit(String[] rivit) {
105  StringBuilder sb = new StringBuilder();
106  String sep = "";
107  for (String s: rivit) { sb.append(sep).append(s); sep = "\n"; }
108  this.rivit.set(sb.toString());
109  setRows(rivit);
110  }
111 
112  /**
113  * Palauttaa valitun olion tai null
114  * @return valittu olio tai null
115  */
116  @Override
117  public T getSelectedObject() {
118  StringAndObject<T> item = getSelectionModel().getSelectedItem();
119  if ( item == null ) return null;
120  return item.getObject();
121  }
122 
123 
124  /**
125  * Returns the contents of the ListChooser as a multiline string.
126  *
127  * @return The contents of the ListChooser as a multiline string.
128  */
129  public String getRivit() {
130  return rivit.get();
131  }
132 
133 
134  /**
135  * Sets the value if the component should be reset after the original example data
136  * @param reset should it be reset
137  */
138  public void setNollataanko(boolean reset){
139  nollataanko.set(reset);
140  }
141 
142 
143  /**
144  * Returns if the component was selected to be cleared
145  * @return should it be cleared
146  */
147  public boolean getNollataanko(){
148  return nollataanko.get();
149  }
150 
151 
152  @Override
153  public String getSelectedText() {
154  StringAndObject<T> item = getSelectionModel().getSelectedItem();
155  if ( item == null ) return null;
156  return item.getName();
157  }
158 
159  @Override
160  public int getSelectedIndex() {
161  return getSelectionModel().getSelectedIndex();
162  }
163 
164 
165  @Override
166  public int setSelectedIndex(int index) {
167  int oldIndex = getSelectedIndex();
168  getSelectionModel().select(index);
169  return oldIndex;
170  }
171 
172 
173  @Override
174  public void addSelectionListener(Consumer<T> event) {
175  this.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<StringAndObject<T>>() {
176  @Override
177  public void changed(ObservableValue<? extends StringAndObject<T>> observable,
178  StringAndObject<T> oldValue, StringAndObject<T> newValue) {
179  event.accept(newValue.getObject());
180  }
181  } );
182 
183  }
184 
185 
186  /**
187  * Poistetaan alleviiva
188  * @param item mistä poistetaan
189  * @return ilman 1. alleviivaa
190  */
191  @Override
192  public String removeMnemonic(String item) {
193  return item.replaceFirst("_", "");
194  }
195 
196 
197 }