36
36
37
37
public class ExecuteController extends BaseController {
38
38
private static final Logger LOGGER = LoggerFactory .getLogger (ExecuteController .class );
39
+ private static final String GREEN_TEXT_CSS_STYLE = "-fx-text-fill: green" ;
40
+ private static final String RED_TEXT_CSS_STYLE = "-fx-text-fill: red" ;
41
+ private static final String BLACK_TEXT_CSS_STYLE = "-fx-text-fill: black" ;
39
42
40
43
@ FXML
41
44
private TextField directory ;
@@ -46,19 +49,10 @@ public class ExecuteController extends BaseController {
46
49
@ FXML
47
50
private TextField description ;
48
51
49
- @ FXML
50
- private Button run ;
51
-
52
- @ FXML
53
- private Button update ;
54
-
55
- @ FXML
56
- private Button clean ;
57
-
58
52
@ FXML
59
53
private Button save ;
60
54
61
- private CommandDto savedCommand ;
55
+ private CommandDto sourceCommand ;
62
56
63
57
@ Override
64
58
public void initialize (FxmlStageHolder fxmlStageHolder ) {
@@ -68,6 +62,35 @@ public void initialize(FxmlStageHolder fxmlStageHolder) {
68
62
event .consume ();
69
63
DialogFactory .createCloseConfirmationAlert (primaryStage );
70
64
});
65
+
66
+ command .textProperty ().addListener ((obs , oldText , newText ) -> {
67
+ if (sourceCommand != null ) {
68
+ updateFields (newText , command , true );
69
+ } else {
70
+ save .setDisable (newText .isEmpty ());
71
+ }
72
+ });
73
+ description .textProperty ().addListener ((obs , oldText , newText ) -> {
74
+ if (sourceCommand != null ) {
75
+ updateFields (newText , description , false );
76
+ }
77
+ });
78
+ }
79
+
80
+ private void updateFields (String currentMainText , TextField field , boolean isCommand ) {
81
+ final String sourceMainText = isCommand ? sourceCommand .getCommand () : sourceCommand .getDescription ();
82
+ final String sourceSecText = isCommand ? sourceCommand .getDescription () : sourceCommand .getCommand ();
83
+ final String currentSecText = isCommand ? description .getText () : command .getText ();
84
+
85
+ if (sourceMainText .equals (currentMainText )){
86
+ field .setStyle (GREEN_TEXT_CSS_STYLE );
87
+ if (sourceSecText .equals (currentSecText )) {
88
+ save .setDisable (true );
89
+ }
90
+ } else {
91
+ save .setDisable (false );
92
+ field .setStyle (RED_TEXT_CSS_STYLE );
93
+ }
71
94
}
72
95
73
96
@ FXML
@@ -88,24 +111,8 @@ protected void browseDirectory() {
88
111
}
89
112
90
113
@ FXML
91
- protected void updateSavedCommand () {
92
- // TODO get rid of special window for updating saved command, just add buttons "update existed" and "save as new" to execute window
93
- // TODO when the saved command hasn't been changed, write it green, when you change it, write red, and show buttons "update existed" and "save as new"
94
- FxmlStageHolder holder = StageFactory .createModalStage ("view/updater.fxml" , "Command updater" );
95
-
96
- UpdateController updateController = holder .getFxmlLoader ().getController ();
97
- updateController .setExecuteController (this );
98
- updateController .setCommand (savedCommand );
99
-
100
- holder .getStage ().showAndWait ();
101
- }
102
-
103
- @ FXML
104
- protected void cleanSavedCommand () {
105
- command .clear ();
106
- description .clear ();
107
- savedCommand = null ;
108
- switchToSavedCommand (false );
114
+ protected void clean () {
115
+ specifySourceCommand (null );
109
116
}
110
117
111
118
@ FXML
@@ -120,12 +127,17 @@ protected void chooseFromSaved() {
120
127
121
128
@ FXML
122
129
protected void saveCommand () {
123
- if (command .getText ().isEmpty ()) {
124
- DialogFactory .createErrorAlert ("Invalid command" , "Command can't be empty" );
125
- } else {
126
- final CommandDto commandDto = new CommandDto (command .getText (), description .getText ());
130
+ final CommandDto commandDto = new CommandDto (command .getText (), description .getText ());
131
+ if (sourceCommand == null ) {
127
132
AppConfig .getInstance ().addCommand (commandDto );
128
- setCommand (commandDto );
133
+ specifySourceCommand (commandDto );
134
+ // TODO MINOR if DTO is already existed, nothing was happened, is it OK?
135
+ } else {
136
+ FxmlStageHolder holder = StageFactory .createModalStage ("view/updater.fxml" , "Command updater" );
137
+ UpdateController updateController = holder .getFxmlLoader ().getController ();
138
+ // TODO add required methods for all stages where necessary
139
+ updateController .setRequiredFields (this , sourceCommand , commandDto );
140
+ holder .getStage ().showAndWait ();
129
141
}
130
142
}
131
143
@@ -173,23 +185,28 @@ protected void foundIssue() {
173
185
AppConfig .getInstance ().getHostServices ().showDocument (AppConstants .LINK_TO_GIT_CONTRIBUTING_FILE );
174
186
}
175
187
176
- void setCommand (CommandDto commandDto ) {
177
- command .setText (commandDto .getCommand ());
178
- description .setText (commandDto .getDescription ());
179
- savedCommand = commandDto ;
180
- switchToSavedCommand (true );
188
+ void specifySourceCommand (CommandDto commandDto ) {
189
+ save .setDisable (true );
190
+ sourceCommand = commandDto ;
191
+ if (commandDto == null ) {
192
+ command .clear ();
193
+ command .setStyle (BLACK_TEXT_CSS_STYLE );
194
+ description .clear ();
195
+ description .setStyle (BLACK_TEXT_CSS_STYLE );
196
+ } else {
197
+ command .setText (commandDto .getCommand ());
198
+ command .setStyle (GREEN_TEXT_CSS_STYLE );
199
+ description .setText (commandDto .getDescription ());
200
+ description .setStyle (GREEN_TEXT_CSS_STYLE );
201
+ }
181
202
}
182
203
183
204
void removeSavedCommand (CommandDto commandDto ) {
184
- if (Objects .equals (commandDto , savedCommand )) {
185
- savedCommand = null ;
186
- switchToSavedCommand (false );
205
+ if (Objects .equals (commandDto , sourceCommand )) {
206
+ sourceCommand = null ;
207
+ command .setStyle (BLACK_TEXT_CSS_STYLE );
208
+ description .setStyle (BLACK_TEXT_CSS_STYLE );
209
+ save .setDisable (false );
187
210
}
188
211
}
189
-
190
- private void switchToSavedCommand (boolean switchToSavedCommand ) {
191
- update .setDisable (!switchToSavedCommand );
192
- clean .setDisable (!switchToSavedCommand );
193
- save .setDisable (switchToSavedCommand );
194
- }
195
212
}
0 commit comments