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