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