Skip to content

Commit bf10ba7

Browse files
committed
First part of parameters implementation: introduce parameters table and parsing
1 parent 935ba78 commit bf10ba7

File tree

7 files changed

+157
-26
lines changed

7 files changed

+157
-26
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ 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-
- Implement dynamic fields in commands, e.g. command `git checkout -f {branch}` and you can quickly specify which branch checkout in each command run.
4241

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

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

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,33 @@
2323
import com.github.introfog.gitwave.model.StageFactory;
2424
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
2525
import com.github.introfog.gitwave.model.dto.CommandDto;
26+
import com.github.introfog.gitwave.model.dto.ParameterDto;
2627

2728
import java.io.File;
29+
import java.util.HashSet;
2830
import java.util.Objects;
31+
import java.util.Set;
32+
import java.util.regex.Matcher;
33+
import java.util.regex.Pattern;
34+
import javafx.collections.FXCollections;
35+
import javafx.collections.ObservableList;
2936
import javafx.fxml.FXML;
3037
import javafx.scene.control.Button;
3138
import javafx.scene.control.ButtonType;
39+
import javafx.scene.control.Label;
40+
import javafx.scene.control.TableColumn;
41+
import javafx.scene.control.TableView;
3242
import javafx.scene.control.TextField;
43+
import javafx.scene.control.cell.PropertyValueFactory;
44+
import javafx.scene.control.cell.TextFieldTableCell;
3345
import javafx.stage.DirectoryChooser;
3446
import javafx.stage.Stage;
3547
import org.slf4j.Logger;
3648
import org.slf4j.LoggerFactory;
3749

3850
public class ExecuteController extends BaseController {
51+
// TODO add opportunity to check if new release is available for GitWave
52+
private static final Pattern CURL_BRACKETS_PATTERN = Pattern.compile("\\{(\\S+)\\}");
3953
private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteController.class);
4054
private static final String GREEN_TEXT_CSS_STYLE = "-fx-text-fill: green";
4155
private static final String RED_TEXT_CSS_STYLE = "-fx-text-fill: red";
@@ -53,6 +67,12 @@ public class ExecuteController extends BaseController {
5367
@FXML
5468
private Button save;
5569

70+
@FXML
71+
private Label parametersText;
72+
73+
@FXML
74+
private TableView<ParameterDto> parametersTable;
75+
5676
private CommandDto sourceCommand;
5777

5878
@Override
@@ -64,18 +84,7 @@ public void initialize(FxmlStageHolder fxmlStageHolder) {
6484
DialogFactory.createCloseConfirmationAlert(primaryStage);
6585
});
6686

67-
command.textProperty().addListener((obs, oldText, newText) -> {
68-
if (sourceCommand != null) {
69-
updateFields(newText, command, true);
70-
} else {
71-
save.setDisable(newText.isEmpty());
72-
}
73-
});
74-
description.textProperty().addListener((obs, oldText, newText) -> {
75-
if (sourceCommand != null) {
76-
updateFields(newText, description, false);
77-
}
78-
});
87+
setUpSaveIndication();
7988
}
8089

8190
@FXML
@@ -177,7 +186,23 @@ protected void foundIssue() {
177186
AppConfig.getInstance().getHostServices().showDocument(AppConstants.LINK_TO_GIT_CONTRIBUTING_FILE);
178187
}
179188

180-
private void updateFields(String currentMainText, TextField field, boolean isCommand) {
189+
private void setUpSaveIndication() {
190+
command.textProperty().addListener((obs, oldText, newText) -> {
191+
parseCommandParameters();
192+
if (sourceCommand != null) {
193+
updateSaveIndication(newText, command, true);
194+
} else {
195+
save.setDisable(newText.isEmpty());
196+
}
197+
});
198+
description.textProperty().addListener((obs, oldText, newText) -> {
199+
if (sourceCommand != null) {
200+
updateSaveIndication(newText, description, false);
201+
}
202+
});
203+
}
204+
205+
private void updateSaveIndication(String currentMainText, TextField field, boolean isCommand) {
181206
final String sourceMainText = isCommand ? sourceCommand.getCommand() : sourceCommand.getDescription();
182207
final String sourceSecText = isCommand ? sourceCommand.getDescription() : sourceCommand.getCommand();
183208
final String currentSecText = isCommand ? description.getText() : command.getText();
@@ -193,6 +218,39 @@ private void updateFields(String currentMainText, TextField field, boolean isCom
193218
}
194219
}
195220

221+
private void parseCommandParameters() {
222+
final Set<ParameterDto> parameters = new HashSet<>();
223+
Matcher matcher = CURL_BRACKETS_PATTERN.matcher(command.getText());
224+
while (matcher.find()) {
225+
final String name = matcher.group(1);
226+
parameters.add(new ParameterDto(name, ""));
227+
}
228+
if (parameters.isEmpty()) {
229+
parametersText.setVisible(true);
230+
231+
parametersTable.setDisable(true);
232+
parametersTable.setVisible(false);
233+
parametersTable.getItems().clear();
234+
} else {
235+
parametersText.setVisible(false);
236+
237+
parametersTable.setDisable(false);
238+
parametersTable.setVisible(true);
239+
240+
ObservableList<ParameterDto> itemList = FXCollections.observableArrayList(parameters);
241+
parametersTable.getItems().clear();
242+
parametersTable.setItems(itemList);
243+
244+
final TableColumn<ParameterDto, String> nameTableColumn = (TableColumn<ParameterDto, String>) parametersTable.getColumns().get(0);
245+
nameTableColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
246+
nameTableColumn.setCellFactory(TextFieldTableCell.forTableColumn());
247+
248+
final TableColumn<ParameterDto, String> valueTableColumn = (TableColumn<ParameterDto, String>) parametersTable.getColumns().get(1);
249+
valueTableColumn.setCellValueFactory(new PropertyValueFactory<>("value"));
250+
valueTableColumn.setCellFactory(TextFieldTableCell.forTableColumn());
251+
}
252+
}
253+
196254
private void specifySourceCommand(CommandDto commandDto) {
197255
save.setDisable(true);
198256
sourceCommand = commandDto;
@@ -203,6 +261,7 @@ private void specifySourceCommand(CommandDto commandDto) {
203261
description.setStyle(BLACK_TEXT_CSS_STYLE);
204262
} else {
205263
command.setText(commandDto.getCommand());
264+
parseCommandParameters();
206265
command.setStyle(GREEN_TEXT_CSS_STYLE);
207266
description.setText(commandDto.getDescription());
208267
description.setStyle(GREEN_TEXT_CSS_STYLE);

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@
3333
import javafx.scene.control.cell.TextFieldTableCell;
3434
import javafx.scene.input.KeyCode;
3535
import javafx.scene.input.MouseEvent;
36-
import javafx.scene.layout.AnchorPane;
3736

3837
public class ExploreController extends BaseController {
3938
@FXML
4039
private TableView<CommandDto> commandsTable;
4140

42-
@FXML
43-
private AnchorPane anchor;
4441
private CommandDto pickedItem;
4542
private final List<CommandDto> removedItems = new ArrayList<>();
4643

@@ -91,14 +88,6 @@ private void setUpAndFillTable() {
9188
TableColumn<CommandDto, String> removeTableColumn = (TableColumn<CommandDto, String>) commandsTable.getColumns().get(2);
9289
removeTableColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
9390
removeTableColumn.setCellFactory(column -> new RemoveTableCell(this));
94-
95-
96-
anchor.widthProperty().addListener((obs, oldVal, newVal) -> {
97-
double width = (newVal.doubleValue() - removeTableColumn.getWidth() - 4) * 0.5;
98-
// TODO MINOR there is a bug that when user change column width manually, and after that resize window, it automatically reset user changes
99-
commandTableColumn.setPrefWidth(width);
100-
descriptionTableColumn.setPrefWidth(width);
101-
});
10291
}
10392

10493
private static class RemoveTableCell extends TableCell<CommandDto, String> {

src/main/java/com/github/introfog/gitwave/model/AppConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public final class AppConstants {
2121
public static final String LINK_TO_GIT_CONTRIBUTING_FILE = "https://github.com/introfog/GitWave/blob/master/CONTRIBUTING.md";
2222
public static final String LINK_TO_GIT_REPO = "https://github.com/introfog/GitWave";
2323
public static final String PATH_TO_LOGO = "/logo.png";
24+
// TODO make pretty logo
2425

2526
private AppConstants() {
2627
// empty
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2023-2024 Dmitry Chubrick
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.introfog.gitwave.model.dto;
18+
19+
import com.fasterxml.jackson.annotation.JsonProperty;
20+
import java.util.Objects;
21+
22+
public class ParameterDto {
23+
@JsonProperty("name")
24+
private String name;
25+
@JsonProperty("value")
26+
private String value;
27+
28+
public ParameterDto(@JsonProperty("name") String name,
29+
@JsonProperty("value") String value) {
30+
this.name = name;
31+
this.value = value;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getValue() {
43+
return value;
44+
}
45+
46+
public void setValue(String value) {
47+
this.value = value;
48+
}
49+
50+
@Override
51+
public boolean equals(Object o) {
52+
if (this == o) {
53+
return true;
54+
}
55+
if (o == null || getClass() != o.getClass()) {
56+
return false;
57+
}
58+
ParameterDto that = (ParameterDto) o;
59+
return Objects.equals(name, that.name) && Objects.equals(value, that.value);
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(name, value);
65+
}
66+
}

src/main/resources/com/github/introfog/gitwave/view/executor.fxml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<?import javafx.scene.control.Separator?>
1010
<?import javafx.scene.control.SeparatorMenuItem?>
1111
<?import javafx.scene.control.Tab?>
12+
<?import javafx.scene.control.TableColumn?>
13+
<?import javafx.scene.control.TableView?>
1214
<?import javafx.scene.control.TabPane?>
1315
<?import javafx.scene.control.TextField?>
1416
<?import javafx.scene.layout.AnchorPane?>
@@ -99,7 +101,19 @@
99101
</Tab>
100102
<Tab text="3. Parameters">
101103
<content>
102-
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
104+
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
105+
<children>
106+
<TableView fx:id="parametersTable" disable="true" editable="true" layoutX="91.0" layoutY="-18.0" prefHeight="200.0" prefWidth="200.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
107+
<columns>
108+
<TableColumn editable="false" minWidth="100.0" prefWidth="75.0" text="Name" />
109+
<TableColumn minWidth="100.0" prefWidth="75.0" text="Value" />
110+
</columns>
111+
<columnResizePolicy>
112+
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
113+
</columnResizePolicy>
114+
</TableView>
115+
<Label fx:id="parametersText" layoutX="28.0" layoutY="14.0" prefHeight="50.0" prefWidth="418.0" text="Current command doesn't define any parameters. Enter your parameter in the format {parameterName}. Ensure that parameterName is not empty or contains only spaces." wrapText="true" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
116+
</children></AnchorPane>
103117
</content>
104118
</Tab>
105119
</tabs>

src/main/resources/com/github/introfog/gitwave/view/explorer.fxml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<TableColumn minWidth="100.0" prefWidth="-1.0" text="Description" />
1212
<TableColumn editable="false" maxWidth="30.0" minWidth="30.0" prefWidth="30.0" resizable="false" sortable="false" />
1313
</columns>
14+
<columnResizePolicy>
15+
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
16+
</columnResizePolicy>
1417
</TableView>
1518
</children>
1619
</AnchorPane>

0 commit comments

Comments
 (0)