88import de .doubleslash .keeptime .common .DateFormatter ;
99import de .doubleslash .keeptime .model .Model ;
1010import de .doubleslash .keeptime .model .Project ;
11+ import javafx .application .Platform ;
1112import javafx .beans .binding .Bindings ;
1213import javafx .beans .property .LongProperty ;
1314import javafx .geometry .Insets ;
@@ -24,16 +25,25 @@ public class ChangeWithTimeDialog {
2425
2526 private static final Logger LOG = LoggerFactory .getLogger (ChangeWithTimeDialog .class );
2627 private static final String TIME_ZERO = "00:00:00" ;
27- private static final int BIG_SLIDER_STEP = 5 ;
28+ private static final int FAST_MINUTE_STEPS = 5 ;
29+
30+ private final Model model ;
31+ private final LongProperty activeWorkSecondsProperty ;
32+ private final Project projectToChangeTo ;
2833
29- private LongProperty activeWorkSecondsProperty ;
30- private Model model ;
3134 private Dialog <Integer > dialog ;
3235 private boolean ctrlIsPressed = false ;
3336
34- public void setUpDialog (final Project p ) {
37+ public ChangeWithTimeDialog (final Model model , final LongProperty activeWorkSecondsProperty ,
38+ final Project projectToChangeTo ) {
39+ this .model = model ;
40+ this .activeWorkSecondsProperty = activeWorkSecondsProperty ;
41+ this .projectToChangeTo = projectToChangeTo ;
42+
43+ setUpDialog ();
44+ }
3545
36- LOG . info ( "Change with time" );
46+ private void setUpDialog () {
3747 dialog = new Dialog <>();
3848 dialog .setTitle ("Change project with time transfer" );
3949 dialog .setHeaderText ("Choose the time to transfer" );
@@ -55,7 +65,7 @@ public void setUpDialog(final Project p) {
5565 grid .setPadding (new Insets (20 , 150 , 10 , 10 ));
5666 int gridRow = 0 ;
5767 grid .add (new Label ("Minutes to transfer" ), 0 , gridRow );
58- final Slider slider = setUpSliderChangeWithTimeMenuItem ( activeWorkSecondsProperty );
68+ final Slider slider = setupSlider ( );
5969
6070 grid .add (slider , 1 , gridRow );
6171 final Label minutesToTransferLabel = new Label ("999 minute(s)" );
@@ -74,7 +84,7 @@ public void setUpDialog(final Project p) {
7484 grid .add (newEndTimeLabel , 1 , gridRow );
7585 gridRow ++;
7686
77- grid .add (new Label ("New project duration: " + p .getName ()), 0 , gridRow );
87+ grid .add (new Label ("New project duration: " + projectToChangeTo .getName ()), 0 , gridRow );
7888 final Label newProjectTimeLabel = new Label (TIME_ZERO );
7989 grid .add (newProjectTimeLabel , 1 , gridRow );
8090
@@ -94,11 +104,10 @@ public void setUpDialog(final Project p) {
94104 slider .valueProperty ().addListener ((obs , oldValue , newValue ) -> updateLabelsRunnable .run ());
95105 vBox .getChildren ().add (grid );
96106
97- // hack to get focus on slider (doesn't work with dialog.setOnShowing(...) or dialog.setOnShown(...))
98- okButton .setOnKeyPressed (keyEvent -> {
99- if ((keyEvent .getCode () == KeyCode .LEFT || keyEvent .getCode () == KeyCode .RIGHT ) && !slider .isFocused ()) {
100- slider .requestFocus ();
101- }
107+ dialog .setOnShown (de -> {
108+ // workaround to set focus to slider when showing the dialog
109+ // onShown is actually called before the dialog is shown
110+ Platform .runLater (slider ::requestFocus );
102111 });
103112
104113 dialog .getDialogPane ().setContent (vBox );
@@ -112,21 +121,17 @@ public void setUpDialog(final Project p) {
112121
113122 }
114123
115- public Optional <Integer > show () {
116-
124+ /**
125+ * Shows the dialog to the user.
126+ *
127+ * @return optional with the amount of seconds to transfer to the new project or null if the user does not confirm
128+ */
129+ public Optional <Integer > showAndWait () {
130+ LOG .info ("Showing dialog" );
117131 return dialog .showAndWait ();
118-
119- }
120-
121- public void setModel (final Model model ) {
122- this .model = model ;
123- }
124-
125- public void setActiveWorkSecondsProperty (final LongProperty activeWorkSecondsProperty ) {
126- this .activeWorkSecondsProperty = activeWorkSecondsProperty ;
127132 }
128133
129- private Slider setUpSliderChangeWithTimeMenuItem ( final LongProperty activeWorkSecondsProperty ) {
134+ private Slider setupSlider ( ) {
130135 final Slider slider = new Slider ();
131136
132137 slider .setMin (0 );
@@ -139,6 +144,7 @@ private Slider setUpSliderChangeWithTimeMenuItem(final LongProperty activeWorkSe
139144 slider .setDisable (true );
140145 return 1l ;
141146 }, activeWorkSecondsProperty ));
147+
142148 slider .setValue (0 );
143149 slider .setShowTickLabels (true );
144150 slider .setShowTickMarks (true );
@@ -150,22 +156,17 @@ private Slider setUpSliderChangeWithTimeMenuItem(final LongProperty activeWorkSe
150156
151157 // allows you to make bigger steps with arrow keys on slider if ctrl is pressed
152158 slider .setOnKeyPressed (ke -> {
153- if (!slider .isFocused ()) {
154- slider .requestFocus ();
155- }
156-
157159 if (ke .getCode () == KeyCode .CONTROL ) {
158160 ctrlIsPressed = true ;
159161 }
160162
161163 if (ke .getCode () == KeyCode .LEFT && ctrlIsPressed ) {
162- slider .adjustValue (slider .getValue () - BIG_SLIDER_STEP );
164+ slider .adjustValue (slider .getValue () - FAST_MINUTE_STEPS );
163165 }
164166
165167 if (ke .getCode () == KeyCode .RIGHT && ctrlIsPressed ) {
166- slider .adjustValue (slider .getValue () + BIG_SLIDER_STEP );
168+ slider .adjustValue (slider .getValue () + FAST_MINUTE_STEPS );
167169 }
168-
169170 });
170171
171172 slider .setOnKeyReleased (ke -> {
0 commit comments