|
6 | 6 | import javafx.scene.Scene; |
7 | 7 | import javafx.scene.chart.*; |
8 | 8 | import javafx.scene.control.*; |
| 9 | +import javafx.scene.input.KeyCode; |
9 | 10 | import javafx.scene.paint.Color; |
10 | 11 | import javafx.scene.image.Image; |
11 | 12 | import javafx.scene.layout.*; |
12 | 13 | import javafx.scene.paint.ImagePattern; |
| 14 | +import javafx.scene.text.TextAlignment; |
13 | 15 | import javafx.stage.Screen; |
14 | 16 | import javafx.stage.Stage; |
15 | 17 | import jfxtras.styles.jmetro8.JMetro; |
|
20 | 22 | import java.util.*; |
21 | 23 |
|
22 | 24 | //TODO: Add progress bars/loading indication |
23 | | -//TODO: Add listener for pressing enter in "new tracker" scene as shortcut to go button |
| 25 | +//TODO: Add more styling to make GUI look better |
24 | 26 |
|
25 | 27 | public class StockTrackerGUI extends Application { |
26 | 28 | private Stage primaryStage; |
@@ -175,21 +177,57 @@ public void updateItem(LocalDate date, boolean empty) { |
175 | 177 | ExtendableTextField tickerCurrencyTextField = new ExtendableTextField(inputDataBox); |
176 | 178 | tickerCurrencyTextField.setMaxWidth(200); |
177 | 179 | tickerCurrencyTextField.setPromptText("ticker"); |
178 | | - |
| 180 | + tickerCurrencyTextField.amountField.setOnKeyPressed(e -> { |
| 181 | + if (e.getCode().equals(KeyCode.ENTER)) { |
| 182 | + plotNewData(startDate.getValue()); |
| 183 | + } |
| 184 | + }); |
179 | 185 |
|
180 | 186 | inputDataBox.setAlignment(Pos.CENTER); |
| 187 | + Label errorLabel = new Label(); |
| 188 | + errorLabel.setTextFill(Color.RED); |
| 189 | + errorLabel.setTextAlignment(TextAlignment.LEFT); |
| 190 | + errorLabel.setPrefWidth(270); |
181 | 191 |
|
182 | 192 | Button goButton = new Button("Go!"); |
183 | | - goButton.setOnAction(e -> plotNewData(startDate.getValue())); |
| 193 | + goButton.setOnAction(e -> { |
| 194 | + if (checkValidInput() && startDate.getValue() != null) { |
| 195 | + plotNewData(startDate.getValue()); |
| 196 | + } |
| 197 | + else { |
| 198 | + errorLabel.setText(""); |
| 199 | + if (!checkValidInput()) { |
| 200 | + errorLabel.setText("Input a stock ticker and a corresponding amount\n"); |
| 201 | + } |
| 202 | + if (startDate.getValue() == null) { |
| 203 | + errorLabel.setText(errorLabel.getText() + "Pick a date"); |
| 204 | + } |
184 | 205 |
|
185 | | - VBox contentPane = new VBox(startDateBox, inputDataBox, goButton); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + VBox contentPane = new VBox(startDateBox, inputDataBox, errorLabel, goButton); |
186 | 210 | contentPane.setSpacing(30); |
187 | 211 | contentPane.setAlignment(Pos.CENTER); |
188 | 212 | root.getChildren().add(contentPane); |
189 | 213 |
|
190 | 214 | createScene(root); |
191 | 215 | } |
192 | 216 |
|
| 217 | + private boolean checkValidInput() { |
| 218 | + boolean valid = true; |
| 219 | + for (ExtendableTextField field: stocksTracked) { |
| 220 | + if (field.getText().equals("") && !field.amountField.getText().equals("") || |
| 221 | + !field.getText().equals("") && field.amountField.getText().equals("")) { |
| 222 | + valid = false; |
| 223 | + } |
| 224 | + } |
| 225 | + if (stocksTracked.get(0).getText().equals("") || stocksTracked.get(0).amountField.getText().equals("")) { |
| 226 | + valid = false; |
| 227 | + } |
| 228 | + return valid; |
| 229 | + } |
| 230 | + |
193 | 231 | private void plotNewData(LocalDate startDate) { |
194 | 232 | ArrayList<String> dataList = new ArrayList<>(); |
195 | 233 | ArrayList<Number> amounts = new ArrayList<>(); |
|
0 commit comments