001package fi.jyu.mit.fxgui; 002 003 004import java.util.function.Consumer; 005 006import javafx.beans.property.BooleanProperty; 007import javafx.beans.property.SimpleBooleanProperty; 008import javafx.beans.property.SimpleStringProperty; 009import javafx.beans.property.StringProperty; 010import javafx.beans.value.ChangeListener; 011import javafx.beans.value.ObservableValue; 012import javafx.collections.ObservableList; 013import javafx.scene.control.ComboBox; 014 015/** 016 * A custom ComboBox control that supports easy addition of items in Scene 017 * Builder. 018 * 019 * @author vesal modified by Tero Paavolainen 020 * @version 13.1.2017 021 * @param <T> Mitä luokkaa tallennetaan 022 * 023 */ 024public class ComboBoxChooser<T> extends ComboBox<StringAndObject<T>> implements Chooser<T> { 025 private StringProperty rivit = new SimpleStringProperty(); 026 /** 027 * Is the first to be selected on each run 028 */ 029 protected BooleanProperty selectFirst = new SimpleBooleanProperty(true); 030 031 /** 032 * Initializes the control. 033 */ 034 public ComboBoxChooser() { 035 super(); 036 if(selectFirst.get()) 037 this.getSelectionModel().selectFirst(); 038 } 039 040 041 private void setRows(String[] strings) { 042 ObservableList<StringAndObject<T>> objects = getItems(); 043 objects.clear(); 044 for (String string : strings) { 045 add(string, null); 046 } 047 if(selectFirst.get()) 048 this.getSelectionModel().selectFirst(); 049 } 050 051 052 /** 053 * Sets the rows. 054 * @param jono A multiline string, each line representing a single row. 055 */ 056 @Override 057 public void setRivit(String jono) { 058 this.rivit.set(jono); 059 String[] strings = jono.split("\n"); // erotinMerkki.get()); 060 setRows(strings); 061 } 062 063 064 @Override 065 public void setRivit(String[] rivit) { 066 StringBuilder sb = new StringBuilder(); 067 String sep = ""; 068 for (String s: rivit) { sb.append(sep).append(s); sep = "\n"; } 069 this.rivit.set(sb.toString()); 070 setRows(rivit); 071 } 072 073 074 /** 075 * @return The contents of the Chooser as a multiline string. 076 */ 077 public String getRivit() { 078 return rivit.get(); 079 } 080 081 082 /** 083 * @return The rows of the Chooser as a StringProperty. 084 */ 085 public StringProperty getRivitProperty() { 086 return rivit; 087 } 088 089 090 /** 091 * Adds the given object with given name 092 * @param name objektin kohdalla näkyvä teksti 093 * @param object tallennettava olio 094 */ 095 @Override 096 public void add(String name, T object){ 097 getItems().add(new StringAndObject<T>(removeMnemonic(name), object)); 098 } 099 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, 184 StringAndObject<T> old, StringAndObject<T> newer) { 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}