Skip to content

Commit cb23dd6

Browse files
committed
Divide checks into corresponded support controllers and prepare parameters validation
1 parent 29bcda1 commit cb23dd6

File tree

8 files changed

+115
-40
lines changed

8 files changed

+115
-40
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Explore, contribute, and customize the application according to your preferences
3838
- Allow working with app by using only keyboard (with correct Tabs, Esc and so on work).
3939
- Exclude sub-directories from command running.
4040
- Allow export and import config.
41+
- Release version for Linux and macOS.
4142

4243
## License
4344
GitWave is licensed under the [Apache license](LICENSE.md), providing you with the freedom to use, modify, and distribute the software.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
</configuration>
139139
</execution>
140140
<execution>
141+
<!-- TODO Try to create .exe by C++ -->
141142
<id>create-exe-based-on-launcher</id>
142143
<phase>package</phase>
143144
<goals>

src/main/java/com/github/introfog/gitwave/controller/SupportController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ public abstract class SupportController extends BaseController {
66
public SupportController(FxmlStageHolder fxmlStageHolder) {
77
initialize(fxmlStageHolder);
88
}
9+
10+
public abstract boolean isValid();
911
}

src/main/java/com/github/introfog/gitwave/controller/main/CommandTabController.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,39 @@
2828
import javafx.scene.control.Button;
2929
import javafx.scene.control.ButtonType;
3030
import javafx.scene.control.TextField;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3133

3234
public class CommandTabController extends SupportController {
35+
private static final Logger LOGGER = LoggerFactory.getLogger(CommandTabController.class);
3336
private static final String GREEN_TEXT_CSS_STYLE = "-fx-text-fill: green";
3437
private static final String RED_TEXT_CSS_STYLE = "-fx-text-fill: red";
3538
private static final String BLACK_TEXT_CSS_STYLE = "-fx-text-fill: black";
3639
private final TextField command;
3740
private final TextField description;
3841
private final Button save;
39-
private final PropertiesTabController propertiesTabController;
42+
private final ParametersTabController parametersTabController;
4043
private CommandDto sourceCommand;
4144

42-
public CommandTabController(FxmlStageHolder fxmlStageHolder, TextField command, TextField description, Button save, PropertiesTabController propertiesTabController) {
45+
public CommandTabController(FxmlStageHolder fxmlStageHolder, TextField command, TextField description, Button save, ParametersTabController parametersTabController) {
4346
super(fxmlStageHolder);
4447
this.command = command;
4548
this.description = description;
4649
this.save = save;
47-
this.propertiesTabController = propertiesTabController;
50+
this.parametersTabController = parametersTabController;
4851
setUpSaveIndication();
4952
}
5053

54+
@Override
55+
public boolean isValid() {
56+
if (command.getText().isEmpty()) {
57+
LOGGER.warn("Command '{}' is empty, running git command was skipped.", command.getText());
58+
DialogFactory.createErrorAlert("Invalid command", "Command can't be empty.");
59+
return false;
60+
}
61+
return true;
62+
}
63+
5164
public void clean() {
5265
specifySourceCommand(null);
5366
}
@@ -85,7 +98,7 @@ public void saveCommand() {
8598

8699
private void setUpSaveIndication() {
87100
command.textProperty().addListener((obs, oldText, newText) -> {
88-
propertiesTabController.parseCommandParameters(newText);
101+
parametersTabController.parseCommandParameters(newText);
89102
if (sourceCommand != null) {
90103
updateSaveIndication(newText, command, true);
91104
} else {
@@ -125,7 +138,7 @@ private void specifySourceCommand(CommandDto commandDto) {
125138
description.setStyle(BLACK_TEXT_CSS_STYLE);
126139
} else {
127140
command.setText(commandDto.getCommand());
128-
propertiesTabController.parseCommandParameters(commandDto.getCommand());
141+
parametersTabController.parseCommandParameters(commandDto.getCommand());
129142
command.setStyle(GREEN_TEXT_CSS_STYLE);
130143
description.setText(commandDto.getDescription());
131144
description.setStyle(GREEN_TEXT_CSS_STYLE);

src/main/java/com/github/introfog/gitwave/controller/main/DirectoryTabController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,38 @@
1818

1919
import com.github.introfog.gitwave.controller.SupportController;
2020
import com.github.introfog.gitwave.model.AppConfig;
21+
import com.github.introfog.gitwave.model.DialogFactory;
2122
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
2223

2324
import java.io.File;
2425
import javafx.scene.control.TextField;
2526
import javafx.stage.DirectoryChooser;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2629

2730
public class DirectoryTabController extends SupportController {
31+
private static final Logger LOGGER = LoggerFactory.getLogger(DirectoryTabController.class);
2832
private final TextField directory;
2933

3034
public DirectoryTabController(FxmlStageHolder fxmlStageHolder, TextField directory) {
3135
super(fxmlStageHolder);
3236
this.directory = directory;
3337
}
3438

39+
@Override
40+
public boolean isValid() {
41+
if (directory.getText().isEmpty()) {
42+
LOGGER.warn("Directory '{}' is empty, running git command was skipped.", directory.getText());
43+
DialogFactory.createErrorAlert("Invalid directory", "Directory can't be empty.");
44+
return false;
45+
}
46+
return true;
47+
}
48+
49+
public String getDirectory() {
50+
return directory.getText();
51+
}
52+
3553
protected void browseDirectory() {
3654
DirectoryChooser directoryChooser = new DirectoryChooser();
3755
String lastOpenedFolderPath = AppConfig.getInstance().getLastRunFolder();

src/main/java/com/github/introfog/gitwave/controller/main/MainController.java

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,15 @@
2525
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
2626
import com.github.introfog.gitwave.model.dto.ParameterDto;
2727

28-
import java.io.File;
2928
import javafx.fxml.FXML;
3029
import javafx.scene.control.Button;
3130
import javafx.scene.control.Label;
3231
import javafx.scene.control.TableView;
3332
import javafx.scene.control.TextField;
3433
import javafx.stage.Stage;
35-
import org.slf4j.Logger;
36-
import org.slf4j.LoggerFactory;
3734

3835
public class MainController extends BaseController {
3936
// TODO add opportunity to check if new release is available for GitWave
40-
private static final Logger LOGGER = LoggerFactory.getLogger(MainController.class);
41-
4237
private DirectoryTabController directoryTabController;
4338
@FXML
4439
private TextField directory;
@@ -51,12 +46,14 @@ public class MainController extends BaseController {
5146
@FXML
5247
private Button save;
5348

54-
private PropertiesTabController propertiesTabController;
49+
private ParametersTabController parametersTabController;
5550
@FXML
5651
private Label parametersText;
5752
@FXML
5853
private TableView<ParameterDto> parametersTable;
5954

55+
private SettingsController settingsController;
56+
6057
@Override
6158
public void initialize(FxmlStageHolder fxmlStageHolder) {
6259
super.initialize(fxmlStageHolder);
@@ -66,8 +63,10 @@ public void initialize(FxmlStageHolder fxmlStageHolder) {
6663
DialogFactory.createCloseConfirmationAlert(primaryStage);
6764
});
6865
directoryTabController = new DirectoryTabController(fxmlStageHolder, directory);
69-
propertiesTabController = new PropertiesTabController(fxmlStageHolder, parametersTable, parametersText);
70-
commandTabController = new CommandTabController(fxmlStageHolder, command, description, save, propertiesTabController);
66+
parametersTabController = new ParametersTabController(fxmlStageHolder, parametersTable, parametersText);
67+
commandTabController = new CommandTabController(fxmlStageHolder, command, description, save,
68+
parametersTabController);
69+
settingsController = new SettingsController(fxmlStageHolder);
7170
}
7271

7372
@FXML
@@ -92,36 +91,20 @@ protected void saveCommand() {
9291

9392
@FXML
9493
protected void runCommand() {
95-
final String pathToGitBashExe = AppConfig.getInstance().getPathToGitBashExe();
96-
if (pathToGitBashExe == null || pathToGitBashExe.isEmpty()) {
97-
StageFactory.createModalSettingsWindow().getStage().showAndWait();
98-
} else if (!(new File(pathToGitBashExe)).exists()) {
99-
LOGGER.error("Specified GitBash.exe path '{}' points to not-existent file, running git command was skipped.", pathToGitBashExe);
100-
DialogFactory.createErrorAlert("Invalid path to GitBash.exe", "Specified path \"" + pathToGitBashExe +
101-
"\" points to not-existent file. Specify correct path in settings.");
102-
} else {
103-
if (command.getText().isEmpty()) {
104-
LOGGER.warn("Command '{}' is empty, running git command was skipped.", command.getText());
105-
DialogFactory.createErrorAlert("Invalid command", "Command can't be empty");
106-
return;
107-
}
108-
if (directory.getText().isEmpty()) {
109-
LOGGER.warn("Directory '{}' is empty, running git command was skipped.", directory.getText());
110-
DialogFactory.createErrorAlert("Invalid directory", "Directory can't be empty");
111-
return;
112-
}
113-
114-
AppConfig.getInstance().setLastRunFolder(directory.getText());
94+
if (settingsController.isValid() && directoryTabController.isValid()
95+
&& commandTabController.isValid() && parametersTabController.isValid()) {
96+
97+
AppConfig.getInstance().setLastRunFolder(directoryTabController.getDirectory());
11598

11699
new Thread(() -> {
117-
CommandExecutor.searchGitReposAndExecuteCommand(directory.getText(), command.getText());
100+
CommandExecutor.searchGitReposAndExecuteCommand(directoryTabController.getDirectory(), command.getText());
118101
}).start();
119102
}
120103
}
121104

122105
@FXML
123106
protected void openSettings() {
124-
StageFactory.createModalSettingsWindow().getStage().showAndWait();
107+
settingsController.openSettings();
125108
}
126109

127110
@FXML

src/main/java/com/github/introfog/gitwave/controller/main/PropertiesTabController.java renamed to src/main/java/com/github/introfog/gitwave/controller/main/ParametersTabController.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.github.introfog.gitwave.controller.main;
1818

1919
import com.github.introfog.gitwave.controller.SupportController;
20+
import com.github.introfog.gitwave.model.DialogFactory;
2021
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
2122
import com.github.introfog.gitwave.model.dto.ParameterDto;
2223

@@ -31,22 +32,40 @@
3132
import javafx.scene.control.TableView;
3233
import javafx.scene.control.cell.PropertyValueFactory;
3334
import javafx.scene.control.cell.TextFieldTableCell;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3437

35-
public class PropertiesTabController extends SupportController {
36-
private static final Pattern CURL_BRACKETS_PATTERN = Pattern.compile("\\{(\\S+)\\}");
38+
public class ParametersTabController extends SupportController {
39+
private static final Logger LOGGER = LoggerFactory.getLogger(ParametersTabController.class);
40+
private static final Pattern PARAMETERS_PATTERN = Pattern.compile("\\{(\\S+)\\}");
41+
private static final Pattern ONLY_SPACES_PATTERN = Pattern.compile("^\\s*$");
3742
private final Label parametersText;
38-
3943
private final TableView<ParameterDto> parametersTable;
4044

41-
public PropertiesTabController(FxmlStageHolder fxmlStageHolder, TableView<ParameterDto> parametersTable, Label parametersText) {
45+
public ParametersTabController(FxmlStageHolder fxmlStageHolder, TableView<ParameterDto> parametersTable, Label parametersText) {
4246
super(fxmlStageHolder);
4347
this.parametersTable = parametersTable;
4448
this.parametersText = parametersText;
4549
}
4650

51+
@Override
52+
public boolean isValid() {
53+
for (ParameterDto parameter : parametersTable.getItems()) {
54+
final String value = parameter.getValue();
55+
final String name = parameter.getName();
56+
if (value == null | | ONLY_SPACES_PATTERN.matcher(value).matches()) {
57+
LOGGER.warn("Parameter '{}' hasn't been specified yet, either remove or set value.", name);
58+
DialogFactory.createErrorAlert("Invalid parameter",
59+
"Parameter {" + name + "} hasn't been specified yet, either remove or set value.");
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
4766
public void parseCommandParameters(String command) {
4867
final Set<ParameterDto> parameters = new HashSet<>();
49-
Matcher matcher = CURL_BRACKETS_PATTERN.matcher(command);
68+
Matcher matcher = PARAMETERS_PATTERN.matcher(command);
5069
while (matcher.find()) {
5170
final String name = matcher.group(1);
5271
parameters.add(new ParameterDto(name, null));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.introfog.gitwave.controller.main;
2+
3+
import com.github.introfog.gitwave.controller.SupportController;
4+
import com.github.introfog.gitwave.model.AppConfig;
5+
import com.github.introfog.gitwave.model.DialogFactory;
6+
import com.github.introfog.gitwave.model.StageFactory;
7+
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
8+
9+
import java.io.File;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class SettingsController extends SupportController {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(SettingsController.class);
15+
16+
public SettingsController(FxmlStageHolder fxmlStageHolder) {
17+
super(fxmlStageHolder);
18+
}
19+
20+
@Override
21+
public boolean isValid() {
22+
final String pathToGitBashExe = AppConfig.getInstance().getPathToGitBashExe();
23+
if (pathToGitBashExe == null || pathToGitBashExe.isEmpty()) {
24+
StageFactory.createModalSettingsWindow().getStage().showAndWait();
25+
return false;
26+
} if (!(new File(pathToGitBashExe)).exists()) {
27+
LOGGER.error("Specified GitBash.exe path '{}' points to not-existent file, running git command was skipped.", pathToGitBashExe);
28+
DialogFactory.createErrorAlert("Invalid path to GitBash.exe", "Specified path \"" + pathToGitBashExe +
29+
"\" points to not-existent file. Specify correct path in settings.");
30+
return false;
31+
}
32+
return true;
33+
}
34+
35+
public void openSettings() {
36+
StageFactory.createModalSettingsWindow().getStage().showAndWait();
37+
}
38+
}

0 commit comments

Comments
 (0)