2424import org .slf4j .Logger ;
2525import org .slf4j .LoggerFactory ;
2626
27+ import com .sun .javafx .scene .control .skin .ComboBoxListViewSkin ;
28+
2729import de .doubleslash .keeptime .common .ColorHelper ;
2830import de .doubleslash .keeptime .common .StyleUtils ;
2931import de .doubleslash .keeptime .model .Model ;
3335import javafx .beans .property .StringProperty ;
3436import javafx .beans .value .ChangeListener ;
3537import javafx .beans .value .ObservableValue ;
38+ import javafx .collections .transformation .FilteredList ;
3639import javafx .fxml .FXML ;
3740import javafx .scene .Node ;
3841import javafx .scene .control .ComboBox ;
3942import javafx .scene .control .DatePicker ;
4043import javafx .scene .control .ListCell ;
44+ import javafx .scene .control .ListView ;
4145import javafx .scene .control .Spinner ;
4246import javafx .scene .control .SpinnerValueFactory ;
4347import javafx .scene .control .TextArea ;
48+ import javafx .scene .input .KeyCode ;
49+ import javafx .scene .input .KeyCodeCombination ;
50+ import javafx .scene .input .KeyEvent ;
4451import javafx .scene .layout .GridPane ;
4552import javafx .scene .paint .Color ;
4653import javafx .util .StringConverter ;
@@ -76,9 +83,12 @@ public class ManageWorkController {
7683 private boolean comboChange ;
7784 private Project selectedProject ;
7885
86+ private FilteredList <Project > filteredList ;
87+
7988 public void setModel (final Model model ) {
8089 this .model = model ;
81- projectComboBox .setItems (model .getSortedAvailableProjects ());
90+ filteredList = new FilteredList <>(model .getSortedAvailableProjects ());
91+ projectComboBox .setItems (filteredList );
8292 }
8393
8494 @ FXML
@@ -90,19 +100,19 @@ private void initialize() {
90100
91101 setProjectUpComboBox ();
92102
103+ Platform .runLater (() -> projectComboBox .requestFocus ());
93104 }
94105
95106 private void setUpTimeSpinner (final Spinner <LocalTime > spinner ) {
96- spinner .getEditor ().textProperty (). addListener ((observable , oldValue , newValue ) -> {
107+ spinner .focusedProperty ().addListener ((e ) -> {
97108 final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter (FormatStyle .MEDIUM );
98- final StringProperty text = ( StringProperty ) observable ;
109+ final StringProperty text = spinner . getEditor (). textProperty () ;
99110 try {
100- stringConverter .fromString (newValue );
101- text .setValue (newValue );
111+ stringConverter .fromString (text .get ());
102112 // needed to log in value from editor to spinner
103113 spinner .increment (0 ); // TODO find better Solution
104- } catch (final DateTimeParseException e ) {
105- text .setValue (oldValue );
114+ } catch (final DateTimeParseException ex ) {
115+ text .setValue (spinner . getValue (). toString () );
106116 }
107117 });
108118
@@ -146,8 +156,11 @@ protected void updateItem(final Project project, final boolean empty) {
146156 if (project == null || empty ) {
147157 setGraphic (null );
148158 } else {
149- setColor (this , project .getColor ());
159+ setColor (this , model .hoverBackgroundColor .get ());
160+
161+ setTextFill (project .getColor ());
150162 setText (project .getName ());
163+
151164 setUnderline (project .isWork ());
152165 }
153166 }
@@ -184,16 +197,17 @@ public Project fromString(final String string) {
184197 comboChange = true ;
185198 // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
186199 Platform .runLater (() -> {
187- projectComboBox .getEditor ().selectAll ();
188- setColor (projectComboBox , newValue .getColor ());
200+ setTextColor (projectComboBox .getEditor (), newValue .getColor ());
189201 });
190202 }
191203
192204 );
193205
206+ enableStrgA_combo ();
207+
194208 projectComboBox .getEditor ().textProperty ().addListener (new ChangeListener <String >() {
195209
196- Boolean isValidProject = true ;
210+ boolean isValidProject = true ;
197211
198212 @ Override
199213 public void changed (final ObservableValue <? extends String > observable , final String oldValue ,
@@ -210,14 +224,20 @@ public void changed(final ObservableValue<? extends String> observable, final St
210224 if (isValidProject ) {
211225 isValidProject = false ;
212226 projectComboBox .getSelectionModel ().clearSelection ();
227+ // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
228+ Platform .runLater (() -> {
229+ setTextColor (projectComboBox .getEditor (), model .hoverFontColor .get ());
230+ });
213231 }
214232
215233 // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
216234 Platform .runLater (() -> {
217235 projectComboBox .hide ();
218- projectComboBox
219- .setItems (model .getSortedAvailableProjects ().filtered (project -> ProjectsListViewController
220- .doesProjectMatchSearchFilter (projectComboBox .getEditor ().getText (), project )));
236+
237+ final String searchText = projectComboBox .getEditor ().getText ();
238+ filteredList .setPredicate (
239+ project -> ProjectsListViewController .doesProjectMatchSearchFilter (searchText , project ));
240+
221241 if (projectComboBox .getEditor ().focusedProperty ().get ()) {
222242 projectComboBox .show ();
223243 }
@@ -240,6 +260,17 @@ public void changed(final ObservableValue<? extends String> observable, final St
240260
241261 });
242262
263+ // on
264+ projectComboBox .setOnKeyReleased (ke -> {
265+ if (ke .getCode () == KeyCode .ENTER && projectComboBox .getSelectionModel ().isEmpty ()) {
266+ if (!projectComboBox .getItems ().isEmpty ()) {
267+ projectComboBox .getSelectionModel ().selectFirst ();
268+ comboChange = true ;
269+ }
270+ }
271+
272+ });
273+
243274 }
244275
245276 public void initializeWith (final Work work ) {
@@ -255,8 +286,26 @@ public void initializeWith(final Work work) {
255286
256287 projectComboBox .getSelectionModel ().select (work .getProject ());
257288
258- setColor (projectComboBox , work .getProject ().getColor ());
289+ setColor (projectComboBox , model .hoverBackgroundColor .get ());
290+ setColor (projectComboBox .getEditor (), model .hoverBackgroundColor .get ());
259291
292+ setTextColor (projectComboBox .getEditor (), model .hoverFontColor .get ());
293+ }
294+
295+ private void enableStrgA_combo () {
296+ // strg+a Behaviour bug hack
297+ // https://stackoverflow.com/questions/51943654/javafx-combobox-make-control-a-select-all-in-text-box-while-dropdown-is-visi
298+ projectComboBox .setOnShown (e -> {
299+ final ComboBoxListViewSkin <?> skin = (ComboBoxListViewSkin <?>) projectComboBox .getSkin ();
300+ final ListView <?> list = (ListView <?>) skin .getPopupContent ();
301+ final KeyCodeCombination ctrlA = new KeyCodeCombination (KeyCode .A , KeyCodeCombination .CONTROL_DOWN );
302+ list .addEventFilter (KeyEvent .KEY_PRESSED , keyEvent -> {
303+ if (ctrlA .match (keyEvent )) {
304+ projectComboBox .getEditor ().selectAll ();
305+ }
306+ });
307+ projectComboBox .setOnShown (null );
308+ });
260309 }
261310
262311 private void setColor (final Node object , final Color color ) {
@@ -266,10 +315,15 @@ private void setColor(final Node object, final Color color) {
266315 }
267316
268317 public Work getWorkFromUserInput () {
269-
270318 return new Work (LocalDateTime .of (startDatePicker .getValue (), startTimeSpinner .getValue ()),
271319 LocalDateTime .of (endDatePicker .getValue (), endTimeSpinner .getValue ()), selectedProject ,
272320 noteTextArea .getText ());
273321 }
274322
323+ private void setTextColor (final Node object , final Color color ) {
324+ final String style = StyleUtils .changeStyleAttribute (object .getStyle (), "fx-text-fill" ,
325+ "rgba(" + ColorHelper .colorToCssRgba (color ) + ")" );
326+ object .setStyle (style );
327+ }
328+
275329}
0 commit comments