16
16
17
17
package com .github .introfog .gitwave .controller .main ;
18
18
19
+ import com .github .introfog .gitwave .controller .EditController ;
19
20
import com .github .introfog .gitwave .controller .ExploreController ;
20
21
import com .github .introfog .gitwave .controller .SupportController ;
21
22
import com .github .introfog .gitwave .model .AppConfig ;
26
27
27
28
import java .util .Objects ;
28
29
import javafx .scene .control .Button ;
29
- import javafx .scene .control .ButtonType ;
30
30
import javafx .scene .control .TextField ;
31
31
import org .slf4j .Logger ;
32
32
import org .slf4j .LoggerFactory ;
33
33
34
34
public class CommandTabController extends SupportController {
35
35
private static final Logger LOGGER = LoggerFactory .getLogger (CommandTabController .class );
36
- private static final String GREEN_TEXT_CSS_STYLE = "-fx-text-fill: green" ;
37
- private static final String RED_TEXT_CSS_STYLE = "-fx-text-fill: red" ;
38
- private static final String BLACK_TEXT_CSS_STYLE = "-fx-text-fill: black" ;
39
36
private final TextField command ;
40
37
private final TextField description ;
41
38
private final Button save ;
42
39
private final ParametersTabController parametersTabController ;
43
- private CommandDto sourceCommand ;
44
40
45
41
public CommandTabController (FxmlStageHolder fxmlStageHolder , TextField command , TextField description , Button save , ParametersTabController parametersTabController ) {
46
42
super (fxmlStageHolder );
47
43
this .command = command ;
48
44
this .description = description ;
49
45
this .save = save ;
50
46
this .parametersTabController = parametersTabController ;
51
- setUpSaveIndication ();
47
+ // if command field is editable, it means that user works with not saved command
48
+ this .command .textProperty ().addListener ((obs , oldText , newText ) -> {
49
+ this .parametersTabController .parseCommandParameters (newText );
50
+ this .save .setDisable (newText .isEmpty ());
51
+ });
52
52
}
53
53
54
54
@ Override
@@ -65,14 +65,14 @@ public String getCommandWithParameters() {
65
65
return parametersTabController .applyParameters (command .getText ());
66
66
}
67
67
68
- public void clean () {
68
+ public void reset () {
69
69
specifySourceCommand (null );
70
70
}
71
71
72
72
public void chooseFromSaved () {
73
- FxmlStageHolder holder = StageFactory .createModalExploreWindow ();
74
- holder .getStage ().showAndWait ();
75
- ExploreController exploreController = holder .getFxmlLoader ().getController ();
73
+ FxmlStageHolder exploreHolder = StageFactory .createModalExploreWindow ();
74
+ exploreHolder .getStage ().showAndWait ();
75
+ ExploreController exploreController = exploreHolder .getFxmlLoader ().getController ();
76
76
final CommandDto pickedItem = exploreController .getPickedItem ();
77
77
if (pickedItem != null ) {
78
78
specifySourceCommand (pickedItem );
@@ -82,80 +82,55 @@ public void chooseFromSaved() {
82
82
}
83
83
}
84
84
85
- public void saveCommand () {
85
+ public void saveOrEditCommand () {
86
86
final CommandDto commandDto = new CommandDto (command .getText (), description .getText ());
87
- if (sourceCommand == null ) {
87
+ if ("Save" . equals ( save . getText ()) ) {
88
88
AppConfig .getInstance ().addCommand (commandDto );
89
89
specifySourceCommand (commandDto );
90
- // TODO MINOR if DTO is already existed, nothing was happened, is it OK?
91
90
} else {
92
- ButtonType result = DialogFactory .createSaveOrUpdateAlert ();
93
- if (ButtonType .YES == result ) {
94
- AppConfig .getInstance ().addCommand (commandDto );
95
- specifySourceCommand (commandDto );
96
- } else if (ButtonType .NO == result ) {
97
- AppConfig .getInstance ().updateExistedCommand (sourceCommand , commandDto );
98
- specifySourceCommand (commandDto );
91
+ FxmlStageHolder editHolder = StageFactory .createModalEditWindow (commandDto );
92
+ editHolder .getStage ().showAndWait ();
93
+ EditController editController = editHolder .getFxmlLoader ().getController ();
94
+ CommandDto result = editController .getResult ();
95
+ boolean isSaveAsNew = editController .isSaveAsNew ();
96
+ if (result != null ) {
97
+ if (isSaveAsNew ) {
98
+ AppConfig .getInstance ().addCommand (result );
99
+ specifySourceCommand (result );
100
+ } else {
101
+ AppConfig .getInstance ().updateExistedCommand (commandDto , result );
102
+ specifySourceCommand (result );
103
+ }
99
104
}
100
105
}
101
106
}
102
107
103
- private void setUpSaveIndication () {
104
- command .textProperty ().addListener ((obs , oldText , newText ) -> {
105
- parametersTabController .parseCommandParameters (newText );
106
- if (sourceCommand != null ) {
107
- updateSaveIndication (newText , command , true );
108
- } else {
109
- save .setDisable (newText .isEmpty ());
110
- }
111
- });
112
- description .textProperty ().addListener ((obs , oldText , newText ) -> {
113
- if (sourceCommand != null ) {
114
- updateSaveIndication (newText , description , false );
115
- }
116
- });
117
- }
118
-
119
- private void updateSaveIndication (String currentMainText , TextField field , boolean isCommand ) {
120
- final String sourceMainText = isCommand ? sourceCommand .getCommand () : sourceCommand .getDescription ();
121
- final String sourceSecText = isCommand ? sourceCommand .getDescription () : sourceCommand .getCommand ();
122
- final String currentSecText = isCommand ? description .getText () : command .getText ();
123
-
124
- if (sourceMainText .equals (currentMainText )){
125
- field .setStyle (GREEN_TEXT_CSS_STYLE );
126
- if (sourceSecText .equals (currentSecText )) {
127
- save .setDisable (true );
128
- }
129
- } else {
130
- save .setDisable (false );
131
- field .setStyle (RED_TEXT_CSS_STYLE );
132
- }
133
- }
134
-
135
108
private void specifySourceCommand (CommandDto commandDto ) {
136
- save .setDisable (true );
137
- sourceCommand = commandDto ;
138
109
if (commandDto == null ) {
139
110
command .clear ();
140
- command .setStyle (BLACK_TEXT_CSS_STYLE );
141
111
description .clear ();
142
- description .setStyle (BLACK_TEXT_CSS_STYLE );
112
+
113
+ switchSaveMode (true );
143
114
} else {
144
115
command .setText (commandDto .getCommand ());
145
116
parametersTabController .parseCommandParameters (commandDto .getCommand ());
146
- command .setStyle (GREEN_TEXT_CSS_STYLE );
147
117
description .setText (commandDto .getDescription ());
148
- description .setStyle (GREEN_TEXT_CSS_STYLE );
118
+
119
+ switchSaveMode (false );
149
120
}
150
121
}
151
122
152
123
private void removeSourceCommand (CommandDto commandDto ) {
153
124
AppConfig .getInstance ().removeCommand (commandDto );
154
- if (Objects .equals (commandDto , sourceCommand )) {
155
- sourceCommand = null ;
156
- command .setStyle (BLACK_TEXT_CSS_STYLE );
157
- description .setStyle (BLACK_TEXT_CSS_STYLE );
158
- save .setDisable (false );
125
+ final CommandDto currentCommand = new CommandDto (command .getText (), description .getText ());
126
+ if (Objects .equals (commandDto , currentCommand )) {
127
+ switchSaveMode (true );
159
128
}
160
129
}
130
+
131
+ private void switchSaveMode (boolean isSaveMode ) {
132
+ save .setText (isSaveMode ? "Save" : "Edit" );
133
+ command .setEditable (isSaveMode );
134
+ description .setEditable (isSaveMode );
135
+ }
161
136
}
0 commit comments