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 ;
4448import javafx .scene .input .KeyCode ;
49+ import javafx .scene .input .KeyCodeCombination ;
50+ import javafx .scene .input .KeyEvent ;
4551import javafx .scene .layout .GridPane ;
4652import javafx .scene .paint .Color ;
4753import javafx .util .StringConverter ;
@@ -77,9 +83,12 @@ public class ManageWorkController {
7783 private boolean comboChange ;
7884 private Project selectedProject ;
7985
86+ private FilteredList <Project > filteredList ;
87+
8088 public void setModel (final Model model ) {
8189 this .model = model ;
82- projectComboBox .setItems (model .getSortedAvailableProjects ());
90+ filteredList = new FilteredList <>(model .getSortedAvailableProjects ());
91+ projectComboBox .setItems (filteredList );
8392 }
8493
8594 @ FXML
@@ -91,19 +100,19 @@ private void initialize() {
91100
92101 setProjectUpComboBox ();
93102
103+ Platform .runLater (() -> projectComboBox .requestFocus ());
94104 }
95105
96106 private void setUpTimeSpinner (final Spinner <LocalTime > spinner ) {
97- spinner .getEditor ().textProperty (). addListener ((observable , oldValue , newValue ) -> {
107+ spinner .focusedProperty ().addListener ((e ) -> {
98108 final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter (FormatStyle .MEDIUM );
99- final StringProperty text = ( StringProperty ) observable ;
109+ final StringProperty text = spinner . getEditor (). textProperty () ;
100110 try {
101- stringConverter .fromString (newValue );
102- text .setValue (newValue );
111+ stringConverter .fromString (text .get ());
103112 // needed to log in value from editor to spinner
104113 spinner .increment (0 ); // TODO find better Solution
105- } catch (final DateTimeParseException e ) {
106- text .setValue (oldValue );
114+ } catch (final DateTimeParseException ex ) {
115+ text .setValue (spinner . getValue (). toString () );
107116 }
108117 });
109118
@@ -147,8 +156,11 @@ protected void updateItem(final Project project, final boolean empty) {
147156 if (project == null || empty ) {
148157 setGraphic (null );
149158 } else {
150- setColor (this , project .getColor ());
159+ setColor (this , model .hoverBackgroundColor .get ());
160+
161+ setTextFill (project .getColor ());
151162 setText (project .getName ());
163+
152164 setUnderline (project .isWork ());
153165 }
154166 }
@@ -185,16 +197,17 @@ public Project fromString(final String string) {
185197 comboChange = true ;
186198 // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
187199 Platform .runLater (() -> {
188- projectComboBox .getEditor ().selectAll ();
189- setColor (projectComboBox , newValue .getColor ());
200+ setTextColor (projectComboBox .getEditor (), newValue .getColor ());
190201 });
191202 }
192203
193204 );
194205
206+ enableStrgA_combo ();
207+
195208 projectComboBox .getEditor ().textProperty ().addListener (new ChangeListener <String >() {
196209
197- Boolean isValidProject = true ;
210+ boolean isValidProject = true ;
198211
199212 @ Override
200213 public void changed (final ObservableValue <? extends String > observable , final String oldValue ,
@@ -211,14 +224,20 @@ public void changed(final ObservableValue<? extends String> observable, final St
211224 if (isValidProject ) {
212225 isValidProject = false ;
213226 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+ });
214231 }
215232
216233 // needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
217234 Platform .runLater (() -> {
218235 projectComboBox .hide ();
219- projectComboBox
220- .setItems (model .getSortedAvailableProjects ().filtered (project -> ProjectsListViewController
221- .doesProjectMatchSearchFilter (projectComboBox .getEditor ().getText (), project )));
236+
237+ final String searchText = projectComboBox .getEditor ().getText ();
238+ filteredList .setPredicate (
239+ project -> ProjectsListViewController .doesProjectMatchSearchFilter (searchText , project ));
240+
222241 if (projectComboBox .getEditor ().focusedProperty ().get ()) {
223242 projectComboBox .show ();
224243 }
@@ -267,8 +286,33 @@ public void initializeWith(final Work work) {
267286
268287 projectComboBox .getSelectionModel ().select (work .getProject ());
269288
270- setColor (projectComboBox , work .getProject ().getColor ());
289+ setColor (projectComboBox , model .hoverBackgroundColor .get ());
290+ setColor (projectComboBox .getEditor (), model .hoverBackgroundColor .get ());
271291
292+ setTextColor (projectComboBox .getEditor (), model .hoverFontColor .get ());
293+ }
294+
295+ public Work getWorkFromUserInput () {
296+ return new Work (startDatePicker .getValue (),
297+ LocalDateTime .of (startDatePicker .getValue (), startTimeSpinner .getValue ()),
298+ LocalDateTime .of (endDatePicker .getValue (), endTimeSpinner .getValue ()), selectedProject ,
299+ noteTextArea .getText ());
300+ }
301+
302+ private void enableStrgA_combo () {
303+ // strg+a Behaviour bug hack
304+ // https://stackoverflow.com/questions/51943654/javafx-combobox-make-control-a-select-all-in-text-box-while-dropdown-is-visi
305+ projectComboBox .setOnShown (e -> {
306+ final ComboBoxListViewSkin <?> skin = (ComboBoxListViewSkin <?>) projectComboBox .getSkin ();
307+ final ListView <?> list = (ListView <?>) skin .getPopupContent ();
308+ final KeyCodeCombination ctrlA = new KeyCodeCombination (KeyCode .A , KeyCodeCombination .CONTROL_DOWN );
309+ list .addEventFilter (KeyEvent .KEY_PRESSED , keyEvent -> {
310+ if (ctrlA .match (keyEvent )) {
311+ projectComboBox .getEditor ().selectAll ();
312+ }
313+ });
314+ projectComboBox .setOnShown (null );
315+ });
272316 }
273317
274318 private void setColor (final Node object , final Color color ) {
@@ -277,12 +321,10 @@ private void setColor(final Node object, final Color color) {
277321 object .setStyle (style );
278322 }
279323
280- public Work getWorkFromUserInput () {
281-
282- return new Work (startDatePicker .getValue (),
283- LocalDateTime .of (startDatePicker .getValue (), startTimeSpinner .getValue ()),
284- LocalDateTime .of (endDatePicker .getValue (), endTimeSpinner .getValue ()), selectedProject ,
285- noteTextArea .getText ());
324+ private void setTextColor (final Node object , final Color color ) {
325+ final String style = StyleUtils .changeStyleAttribute (object .getStyle (), "fx-text-fill" ,
326+ "rgba(" + ColorHelper .colorToCssRgba (color ) + ")" );
327+ object .setStyle (style );
286328 }
287329
288330}
0 commit comments