Skip to content

Commit eb0ef78

Browse files
committed
#6 Make save commands managements clearer by changing save button to edit
1 parent 041f53c commit eb0ef78

File tree

7 files changed

+188
-84
lines changed

7 files changed

+188
-84
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.controller;
18+
19+
import com.github.introfog.gitwave.model.DialogFactory;
20+
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
21+
import com.github.introfog.gitwave.model.dto.CommandDto;
22+
23+
import javafx.fxml.FXML;
24+
import javafx.scene.control.Button;
25+
import javafx.scene.control.TextField;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
public class EditController extends BaseController {
30+
private static final Logger LOGGER = LoggerFactory.getLogger(EditController.class);
31+
32+
@FXML
33+
private TextField command;
34+
@FXML
35+
private TextField description;
36+
@FXML
37+
private Button save;
38+
39+
private CommandDto result;
40+
private boolean saveAsNew;
41+
42+
@Override
43+
public void initialize(FxmlStageHolder fxmlStageHolder) {
44+
super.initialize(fxmlStageHolder);
45+
super.setClosingOnEscapePressing(fxmlStageHolder);
46+
save.requestFocus();
47+
}
48+
49+
public void setCommand(CommandDto commandDto) {
50+
command.setText(commandDto.getCommand());
51+
description.setText(commandDto.getDescription());
52+
}
53+
54+
public CommandDto getResult() {
55+
return result;
56+
}
57+
58+
public boolean isSaveAsNew() {
59+
return saveAsNew;
60+
}
61+
62+
@FXML
63+
protected void saveAsNew() {
64+
saveAsNew = true;
65+
if (tryToSaveCommand()) {
66+
closeStage();
67+
}
68+
}
69+
70+
@FXML
71+
protected void updateExisted() {
72+
saveAsNew = false;
73+
if (tryToSaveCommand()) {
74+
closeStage();
75+
}
76+
}
77+
78+
private boolean tryToSaveCommand() {
79+
if (command.getText().isEmpty()) {
80+
LOGGER.warn("Command '{}' is empty, command editing was skipped.", command.getText());
81+
DialogFactory.createErrorAlert("Invalid command", "Command can't be empty.");
82+
return false;
83+
} else {
84+
result = new CommandDto(command.getText(), description.getText());
85+
return true;
86+
}
87+
}
88+
}

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

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.github.introfog.gitwave.controller.main;
1818

19+
import com.github.introfog.gitwave.controller.EditController;
1920
import com.github.introfog.gitwave.controller.ExploreController;
2021
import com.github.introfog.gitwave.controller.SupportController;
2122
import com.github.introfog.gitwave.model.AppConfig;
@@ -26,29 +27,28 @@
2627

2728
import java.util.Objects;
2829
import javafx.scene.control.Button;
29-
import javafx.scene.control.ButtonType;
3030
import javafx.scene.control.TextField;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333

3434
public class CommandTabController extends SupportController {
3535
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";
3936
private final TextField command;
4037
private final TextField description;
4138
private final Button save;
4239
private final ParametersTabController parametersTabController;
43-
private CommandDto sourceCommand;
4440

4541
public CommandTabController(FxmlStageHolder fxmlStageHolder, TextField command, TextField description, Button save, ParametersTabController parametersTabController) {
4642
super(fxmlStageHolder);
4743
this.command = command;
4844
this.description = description;
4945
this.save = save;
5046
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+
});
5252
}
5353

5454
@Override
@@ -65,14 +65,14 @@ public String getCommandWithParameters() {
6565
return parametersTabController.applyParameters(command.getText());
6666
}
6767

68-
public void clean() {
68+
public void reset() {
6969
specifySourceCommand(null);
7070
}
7171

7272
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();
7676
final CommandDto pickedItem = exploreController.getPickedItem();
7777
if (pickedItem != null) {
7878
specifySourceCommand(pickedItem);
@@ -82,80 +82,55 @@ public void chooseFromSaved() {
8282
}
8383
}
8484

85-
public void saveCommand() {
85+
public void saveOrEditCommand() {
8686
final CommandDto commandDto = new CommandDto(command.getText(), description.getText());
87-
if (sourceCommand == null) {
87+
if ("Save".equals(save.getText())) {
8888
AppConfig.getInstance().addCommand(commandDto);
8989
specifySourceCommand(commandDto);
90-
// TODO MINOR if DTO is already existed, nothing was happened, is it OK?
9190
} 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+
}
99104
}
100105
}
101106
}
102107

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-
135108
private void specifySourceCommand(CommandDto commandDto) {
136-
save.setDisable(true);
137-
sourceCommand = commandDto;
138109
if (commandDto == null) {
139110
command.clear();
140-
command.setStyle(BLACK_TEXT_CSS_STYLE);
141111
description.clear();
142-
description.setStyle(BLACK_TEXT_CSS_STYLE);
112+
113+
switchSaveMode(true);
143114
} else {
144115
command.setText(commandDto.getCommand());
145116
parametersTabController.parseCommandParameters(commandDto.getCommand());
146-
command.setStyle(GREEN_TEXT_CSS_STYLE);
147117
description.setText(commandDto.getDescription());
148-
description.setStyle(GREEN_TEXT_CSS_STYLE);
118+
119+
switchSaveMode(false);
149120
}
150121
}
151122

152123
private void removeSourceCommand(CommandDto commandDto) {
153124
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);
159128
}
160129
}
130+
131+
private void switchSaveMode(boolean isSaveMode) {
132+
save.setText(isSaveMode ? "Save" : "Edit");
133+
command.setEditable(isSaveMode);
134+
description.setEditable(isSaveMode);
135+
}
161136
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ protected void browseDirectory() {
9393
}
9494

9595
@FXML
96-
protected void clean() {
97-
commandTabController.clean();
96+
protected void reset() {
97+
commandTabController.reset();
9898
}
9999

100100
@FXML
@@ -103,8 +103,8 @@ protected void chooseFromSaved() {
103103
}
104104

105105
@FXML
106-
protected void saveCommand() {
107-
commandTabController.saveCommand();
106+
protected void saveOrEditCommand() {
107+
commandTabController.saveOrEditCommand();
108108
}
109109

110110
@FXML

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,4 @@ public static ButtonType createCloseConfirmationAlert() {
6363
dialog.showAndWait().ifPresent(response -> pressedButton[0] = response);
6464
return pressedButton[0];
6565
}
66-
67-
public static ButtonType createSaveOrUpdateAlert() {
68-
Alert dialog = new Alert(AlertType.CONFIRMATION);
69-
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
70-
stage.getIcons().add(new Image(StageFactory.class.getResourceAsStream(AppConstants.PATH_TO_LOGO)));
71-
72-
dialog.setTitle("Confirmation");
73-
dialog.setHeaderText("Do you want to save command as a new instance?\nSelect 'No' if update existed.");
74-
dialog.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO, ButtonType.CANCEL);
75-
76-
dialog.getDialogPane().setStyle(COMMON_STYLES);
77-
ButtonType[] pressedButton = {null};
78-
dialog.showAndWait().ifPresent(response -> pressedButton[0] = response);
79-
return pressedButton[0];
80-
}
8166
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.github.introfog.gitwave.GitWaveLauncher;
2020
import com.github.introfog.gitwave.controller.BaseController;
21+
import com.github.introfog.gitwave.controller.EditController;
22+
import com.github.introfog.gitwave.model.dto.CommandDto;
2123

2224
import java.io.IOException;
2325
import javafx.fxml.FXMLLoader;
@@ -42,6 +44,15 @@ public static FxmlStageHolder createModalExploreWindow() {
4244
return holder;
4345
}
4446

47+
public static FxmlStageHolder createModalEditWindow(CommandDto commandDto) {
48+
FxmlStageHolder holder = StageFactory.createModalStage("view/edit.fxml", "Command editor");
49+
holder.getStage().setMinWidth(400);
50+
holder.getStage().setMinHeight(210);
51+
EditController editController = holder.getFxmlLoader().getController();
52+
editController.setCommand(commandDto);
53+
return holder;
54+
}
55+
4556
public static FxmlStageHolder createModalSettingsWindow() {
4657
FxmlStageHolder holder = StageFactory.createModalStage("view/settings.fxml", "Settings");
4758
holder.getStage().setMinWidth(500);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.geometry.Insets?>
4+
<?import javafx.scene.control.Button?>
5+
<?import javafx.scene.control.Label?>
6+
<?import javafx.scene.control.TextField?>
7+
<?import javafx.scene.layout.AnchorPane?>
8+
<?import javafx.scene.layout.ColumnConstraints?>
9+
<?import javafx.scene.layout.GridPane?>
10+
<?import javafx.scene.layout.RowConstraints?>
11+
<AnchorPane maxHeight="180.0" maxWidth="-Infinity" minHeight="180.0" minWidth="-Infinity" prefHeight="180.0" prefWidth="500.0" style="-fx-font-family: verdana; -fx-font-size: 12;" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.github.introfog.gitwave.controller.EditController">
12+
<children>
13+
<GridPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
14+
<columnConstraints>
15+
<ColumnConstraints hgrow="ALWAYS" />
16+
</columnConstraints>
17+
<rowConstraints>
18+
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="NEVER" />
19+
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="NEVER" />
20+
<RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" prefHeight="0.0" vgrow="ALWAYS" />
21+
<RowConstraints maxHeight="30.0" minHeight="30.0" prefHeight="30.0" vgrow="NEVER" />
22+
</rowConstraints>
23+
<children>
24+
<AnchorPane prefHeight="200.0" prefWidth="200.0">
25+
<children>
26+
<Label layoutX="44.0" layoutY="6.0" text="Command" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
27+
<TextField fx:id="command" layoutX="24.0" layoutY="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="20.0" />
28+
</children></AnchorPane>
29+
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
30+
<children>
31+
<Label layoutX="35.0" layoutY="14.0" text="Description" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
32+
<TextField fx:id="description" layoutX="14.0" layoutY="19.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="20.0" />
33+
</children></AnchorPane>
34+
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="3">
35+
<children>
36+
<Button layoutX="319.0" layoutY="2.0" mnemonicParsing="false" onAction="#updateExisted" text="Update existed" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="110.0" />
37+
<Button fx:id="save" layoutX="408.0" layoutY="3.0" mnemonicParsing="false" onAction="#saveAsNew" text="Save as new" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" />
38+
</children></AnchorPane>
39+
</children>
40+
</GridPane>
41+
</children>
42+
<padding>
43+
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
44+
</padding>
45+
</AnchorPane>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@
8686
<font>
8787
<Font name="Verdana" size="12.0" />
8888
</font></TextField>
89-
<Button fx:id="save" disable="true" layoutX="48.0" layoutY="138.0" mnemonicParsing="false" onAction="#saveCommand" text="Save" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="140.0">
89+
<Button fx:id="save" disable="true" layoutX="48.0" layoutY="138.0" mnemonicParsing="false" onAction="#saveOrEditCommand" text="Save" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="140.0">
9090
<font>
9191
<Font name="Verdana" size="12.0" />
9292
</font></Button>
9393
<Button layoutX="130.0" layoutY="138.0" mnemonicParsing="false" onAction="#chooseFromSaved" text="Choose from saved" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="140.0">
9494
<font>
9595
<Font name="Verdana" size="12.0" />
9696
</font></Button>
97-
<Button layoutX="287.0" layoutY="138.0" mnemonicParsing="false" onAction="#clean" text="Clean" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="140.0">
97+
<Button layoutX="287.0" layoutY="138.0" mnemonicParsing="false" onAction="#reset" text="Reset" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="140.0">
9898
<font>
9999
<Font name="Verdana" size="12.0" />
100100
</font></Button>

0 commit comments

Comments
 (0)