2727import de .doubleslash .keeptime .model .Model ;
2828import de .doubleslash .keeptime .model .Project ;
2929import de .doubleslash .keeptime .model .Work ;
30+ import javafx .beans .binding .Bindings ;
3031import javafx .beans .property .StringProperty ;
3132import javafx .fxml .FXML ;
3233import javafx .scene .control .ComboBox ;
3738import javafx .scene .control .SpinnerValueFactory ;
3839import javafx .scene .control .TextArea ;
3940import javafx .scene .layout .GridPane ;
41+ import javafx .scene .layout .StackPane ;
42+ import javafx .scene .paint .Color ;
4043import javafx .util .Callback ;
4144import javafx .util .StringConverter ;
4245import javafx .util .converter .LocalTimeStringConverter ;
@@ -166,6 +169,7 @@ public void increment(final int steps) {
166169 noteTextArea .setText (work .getNotes ());
167170 projectComboBox .getItems ().addAll (model .getAvailableProjects ());
168171
172+ // Dropdown Options
169173 projectComboBox .setCellFactory (new Callback <ListView <Project >, ListCell <Project >>() {
170174
171175 @ Override
@@ -178,15 +182,50 @@ protected void updateItem(final Project item, final boolean empty) {
178182 if (item == null || empty ) {
179183 setGraphic (null );
180184 } else {
185+ final Color color = item .getColor ();
186+ final String hexColor = String .format ("#%02X%02X%02X" , (int ) (color .getRed () * 255 ),
187+ (int ) (color .getGreen () * 255 ), (int ) (color .getBlue () * 255 ));
188+ setStyle (" -fx-background-color: " + hexColor );
181189 setText (item .getName ());
190+
182191 }
183192 }
184193 };
185194 }
186195 });
187196
188- projectComboBox .setConverter (new StringConverter <Project >() {
197+ // selected Item
198+ projectComboBox .buttonCellProperty ().bind (Bindings .createObjectBinding (() -> {
199+
200+ // Get the arrow button of the combo-box
201+ final StackPane arrowButton = (StackPane ) projectComboBox .lookup (".arrow-button" );
202+
203+ return new ListCell <Project >() {
204+
205+ @ Override
206+ protected void updateItem (final Project item , final boolean empty ) {
207+ super .updateItem (item , empty );
208+ if (empty || item == null ) {
209+ setGraphic (null );
210+ } else {
211+ final Color color = item .getColor ();
212+ final String hexColor = String .format ("#%02X%02X%02X" , (int ) (color .getRed () * 255 ),
213+ (int ) (color .getGreen () * 255 ), (int ) (color .getBlue () * 255 ));
214+ setStyle (" -fx-background-color: " + hexColor );
215+ setText (item .getName ());
216+ }
217+
218+ // Set the background of the arrow also
219+ if (arrowButton != null ) {
220+ arrowButton .setBackground (getBackground ());
221+ }
222+ }
189223
224+ };
225+ }, projectComboBox .valueProperty ()));
226+
227+ // selected value showed in combo box
228+ projectComboBox .setConverter (new StringConverter <Project >() {
190229 @ Override
191230 public String toString (final Project project ) {
192231 if (project == null ) {
@@ -203,6 +242,10 @@ public Project fromString(final String string) {
203242 });
204243
205244 projectComboBox .getSelectionModel ().select (work .getProject ());
245+ final Color color = work .getProject ().getColor ();
246+ final String hexColor = String .format ("#%02X%02X%02X" , (int ) (color .getRed () * 255 ),
247+ (int ) (color .getGreen () * 255 ), (int ) (color .getBlue () * 255 ));
248+ projectComboBox .setStyle (" -fx-background-color: " + hexColor );
206249
207250 }
208251
0 commit comments