2929import de .doubleslash .keeptime .model .Model ;
3030import de .doubleslash .keeptime .model .Project ;
3131import de .doubleslash .keeptime .model .Work ;
32- import javafx .beans . binding . Bindings ;
32+ import javafx .application . Platform ;
3333import javafx .beans .property .StringProperty ;
34+ import javafx .beans .value .ChangeListener ;
35+ import javafx .beans .value .ObservableValue ;
3436import javafx .fxml .FXML ;
3537import javafx .scene .control .ComboBox ;
3638import javafx .scene .control .DatePicker ;
4042import javafx .scene .control .SpinnerValueFactory ;
4143import javafx .scene .control .TextArea ;
4244import javafx .scene .layout .GridPane ;
43- import javafx .scene .layout .StackPane ;
4445import javafx .scene .paint .Color ;
4546import javafx .util .Callback ;
4647import javafx .util .StringConverter ;
@@ -73,15 +74,15 @@ public class ManageWorkController {
7374 @ FXML
7475 private ComboBox <Project > projectComboBox ;
7576
77+ private boolean userInteraction ;
78+
7679 public void setModel (final Model model ) {
7780 this .model = model ;
81+ projectComboBox .setItems (model .getAllProjects ());
7882 }
7983
80- public void initializeWith (final Work work ) {
81- LOG .info ("Setting values." );
82- startDatePicker .setValue (work .getStartTime ().toLocalDate ());
83- endDatePicker .setValue (work .getEndTime ().toLocalDate ());
84-
84+ @ FXML
85+ private void initialize () {
8586 startTimeSpinner .getEditor ().textProperty ().addListener ((observable , oldValue , newValue ) -> {
8687 final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter (FormatStyle .MEDIUM );
8788 final StringProperty text = (StringProperty ) observable ;
@@ -167,13 +168,12 @@ public void increment(final int steps) {
167168
168169 });
169170
170- startTimeSpinner .getValueFactory ().setValue (work .getStartTime ().toLocalTime ());
171- endTimeSpinner .getValueFactory ().setValue (work .getEndTime ().toLocalTime ());
171+ setUpComboBox ();
172172
173- noteTextArea .setText (work .getNotes ());
174- projectComboBox .getItems ().addAll (model .getAvailableProjects ());
173+ }
175174
176- // Dropdown Options
175+ private void setUpComboBox () {
176+ // color Dropdown Options
177177 projectComboBox .setCellFactory (new Callback <ListView <Project >, ListCell <Project >>() {
178178
179179 @ Override
@@ -187,12 +187,8 @@ protected void updateItem(final Project item, final boolean empty) {
187187 setGraphic (null );
188188 } else {
189189 final Color color = item .getColor ();
190-
191- final double opacity = 0 ;
192- String style = StyleUtils .changeStyleAttribute (getStyle (), "fx-background-color" ,
190+ final String style = StyleUtils .changeStyleAttribute (getStyle (), "fx-background-color" ,
193191 "rgba(" + ColorHelper .colorToCssRgba (color ) + ")" );
194- style = StyleUtils .changeStyleAttribute (style , "fx-border-color" ,
195- "rgba(" + ColorHelper .colorToCssRgb (color ) + ", " + opacity + ")" );
196192 setStyle (style );
197193 setText (item .getName ());
198194
@@ -202,42 +198,7 @@ protected void updateItem(final Project item, final boolean empty) {
202198 }
203199 });
204200
205- // selected Item
206- projectComboBox .buttonCellProperty ().bind (Bindings .createObjectBinding (() -> {
207-
208- // Get the arrow button of the combo-box
209- final StackPane arrowButton = (StackPane ) projectComboBox .lookup (".arrow-button" );
210-
211- return new ListCell <Project >() {
212-
213- @ Override
214- protected void updateItem (final Project item , final boolean empty ) {
215- super .updateItem (item , empty );
216- if (empty || item == null ) {
217- setGraphic (null );
218- } else {
219- final Color color = item .getColor ();
220-
221- final double opacity = 0 ;
222- String style = StyleUtils .changeStyleAttribute (getStyle (), "fx-background-color" ,
223- "rgba(" + ColorHelper .colorToCssRgba (color ) + ")" );
224- style = StyleUtils .changeStyleAttribute (style , "fx-border-color" ,
225- "rgba(" + ColorHelper .colorToCssRgb (color ) + ", " + opacity + ")" );
226- setStyle (style );
227-
228- setText (item .getName ());
229- }
230-
231- // Set the background of the arrow also
232- if (arrowButton != null ) {
233- arrowButton .setStyle (getStyle ());
234- }
235- }
236-
237- };
238- }, projectComboBox .valueProperty ()));
239-
240- // selected value showed in combo box
201+ // set text of selected value
241202 projectComboBox .setConverter (new StringConverter <Project >() {
242203 @ Override
243204 public String toString (final Project project ) {
@@ -250,20 +211,63 @@ public String toString(final Project project) {
250211
251212 @ Override
252213 public Project fromString (final String string ) {
253- return null ;
214+
215+ return projectComboBox .getValue ();
254216 }
255217 });
256218
219+ // needs to set again because editable is ignored from fxml because of custom preselection
220+ projectComboBox .setEditable (true );
221+
222+ projectComboBox .getEditor ().setOnKeyTyped ((e ) -> {
223+ userInteraction = true ;
224+ });
225+
226+ projectComboBox .getEditor ().textProperty ().addListener (new ChangeListener <String >() {
227+
228+ @ Override
229+ public void changed (final ObservableValue <? extends String > observable , final String oldValue ,
230+ final String newValue ) {
231+
232+ if (userInteraction ) {
233+ userInteraction = false ;
234+
235+ // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
236+ Platform .runLater (() -> {
237+ LOG .debug ("Value:" + newValue );
238+ projectComboBox .hide ();
239+ projectComboBox .setItems (model .getAllProjects ().filtered (
240+ (project ) -> ProjectsListViewController .doesProjectMatchSearchFilter (newValue , project )));
241+ projectComboBox .show ();
242+ });
243+
244+ }
245+
246+ }
247+ });
248+
249+ }
250+
251+ public void initializeWith (final Work work ) {
252+ LOG .info ("Setting values." );
253+ startDatePicker .setValue (work .getStartTime ().toLocalDate ());
254+ endDatePicker .setValue (work .getEndTime ().toLocalDate ());
255+
256+ startTimeSpinner .getValueFactory ().setValue (work .getStartTime ().toLocalTime ());
257+ endTimeSpinner .getValueFactory ().setValue (work .getEndTime ().toLocalTime ());
258+
259+ noteTextArea .setText (work .getNotes ());
260+
257261 projectComboBox .getSelectionModel ().select (work .getProject ());
258- final Color color = work .getProject ().getColor ();
259262
260- final double opacity = 0 ;
261- String style = StyleUtils .changeStyleAttribute (projectComboBox .getStyle (), "fx-background-color" ,
263+ setComboBoxColor (work .getProject ().getColor ());
264+
265+ }
266+
267+ private void setComboBoxColor (final Color color ) {
268+ final String style = StyleUtils .changeStyleAttribute (projectComboBox .getStyle (), "fx-background-color" ,
262269 "rgba(" + ColorHelper .colorToCssRgba (color ) + ")" );
263- style = StyleUtils .changeStyleAttribute (style , "fx-border-color" ,
264- "rgba(" + ColorHelper .colorToCssRgb (color ) + ", " + opacity + ")" );
265270 projectComboBox .setStyle (style );
266-
267271 }
268272
269273 public Work getWorkFromUserInput () {
0 commit comments