-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawDataPane.java
More file actions
executable file
·313 lines (291 loc) · 9.31 KB
/
RawDataPane.java
File metadata and controls
executable file
·313 lines (291 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* RawDataPane.java created by wenxiyang on MacBook Pro in a2
*
* Author: Wenxi Yang(wyang235@wisc.edu)
* Date: 4/21/2020
*
* Course: CS400
* Semester: Spring 2020
* Lecture: 001
*
* IDE: Eclipse IDE for Java Developers
* Version: 2019-12 (4.14.0)
* Build id: 20191212-1212
*
* Device: Wenxi's MacBook Pro
* OS: MacOS Mojave
* Version: 10.14.4
* OS Build: 18E226
*
* List Collaboratons:
*
* Other Credits:
*
* Known bugs:
*/
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.util.Callback;
import javafx.util.converter.IntegerStringConverter;
/**
* The Class RawDataPane.
*/
public class RawDataPane {
/** The pane. */
private static RawDataPane pane;
/** The table view. */
private TableView<MilkItem> tableView;
/** My pane. */
private BorderPane myPane;
/** The milk data. */
private MilkData milkData;
/** The month combo box. */
private ComboBox<String> monthComboBox;
/**
* Creates the raw data pane.
*
* @param milkData the milk data
* @return the raw data pane
*/
public static RawDataPane createRawDataPane(MilkData milkData) {
if (pane == null) {
Insets insets = new Insets(10);
pane = new RawDataPane();
pane.milkData = milkData;
pane.createMilkRawTableV();
Pane btPane = pane.createAddPane();
Pane topPane = pane.createChoosePane();
pane.myPane = new BorderPane();
pane.myPane.setPadding(new Insets(10, 10, 10, 10));
pane.myPane.setPrefWidth(840);
pane.myPane.setPrefHeight(600);
pane.myPane.setTop(topPane);
pane.myPane.setCenter(pane.tableView);
pane.myPane.setBottom(btPane);
BorderPane.setMargin(topPane, insets);
BorderPane.setMargin(pane.tableView, insets);
BorderPane.setMargin(btPane, insets);
}
return pane;
}
/**
* Gets my pane.
*
* @return my pane
*/
public BorderPane getMyPane() {
return myPane;
}
/**
* Creates the choose pane.
*
* @return the pane
*/
private Pane createChoosePane() {
ToggleGroup radioGroup = new ToggleGroup();
GridPane pane = new GridPane();
// Insets top, right, bottom, left
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setVgap(8);
pane.setHgap(8);
RadioButton rbtAll = new RadioButton("All");
rbtAll.setToggleGroup(radioGroup);
pane.add(rbtAll, 0, 0);
RadioButton rbtMonth = new RadioButton("Month");
rbtMonth.setToggleGroup(radioGroup);
pane.add(rbtMonth, 0, 1);
monthComboBox = new ComboBox<>();
monthComboBox.setPrefWidth(160);
pane.add(monthComboBox, 1, 1);
RadioButton rbtDay = new RadioButton("Day");
rbtDay.setToggleGroup(radioGroup);
pane.add(rbtDay, 0, 2);
DatePicker datePicker = new DatePicker();
pane.add(datePicker, 1, 2);
rbtDay.setSelected(true);
Button bt_search = new Button("Search");
bt_search.setPrefWidth(80);
pane.add(bt_search, 2, 2);
bt_search.setOnAction(action -> {
RadioButton selectedRadioButton = (RadioButton) radioGroup.getSelectedToggle();
if (selectedRadioButton.equals(rbtAll)) {
List<MilkItem> list = milkData.getAllData();
addMilkItems(list);
} else if (selectedRadioButton.equals(rbtMonth)) {
List<MilkItem> list = milkData.getDataByMonth(monthComboBox.getValue());
addMilkItems(list);
} else {
LocalDate day = datePicker.getValue();
List<MilkItem> list = milkData.getDataByDay(CommonMilkTool.formatDate(day));
addMilkItems(list);
}
});
return pane;
}
/**
* Creates the add pane.
*
* @return the grid pane
*/
private GridPane createAddPane() {
TextField date = new TextField();
date.setPrefWidth(200);
TextField farm = new TextField();
farm.setPrefWidth(200);
TextField weight = new TextField();
weight.setPrefWidth(200);
Button bt_add = new Button("Add");
bt_add.setPrefWidth(100);
bt_add.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String dateStr = date.getText();
if (!CommonMilkTool.verifyDate(dateStr)) {
new Alert(Alert.AlertType.NONE, "Date format error", new ButtonType[] { ButtonType.CLOSE }).show();
return;
}
String farmStr = farm.getText().trim();
if (farmStr.isEmpty()) {
new Alert(Alert.AlertType.NONE, "Farm ID error", new ButtonType[] { ButtonType.CLOSE }).show();
return;
}
int weightInt = 0;
try {
weightInt = Integer.valueOf(weight.getText().trim());
} catch (NumberFormatException e) {
new Alert(Alert.AlertType.NONE, "weight error", new ButtonType[] { ButtonType.CLOSE }).show();
return;
}
if (!milkData.checkData(dateStr, farmStr)) {
milkData.addData(dateStr, farmStr, weightInt);
tableView.getItems().add(new MilkItem(dateStr, farmStr, weightInt));
EventLog.getInstance().log("Add Data: " + farmStr + ", " + dateStr);
} else {
new Alert(Alert.AlertType.NONE, "Date and Farm ID exist", new ButtonType[] { ButtonType.CLOSE })
.show();
return;
}
}
});
Button bt_delete = new Button("Delete");
bt_delete.setTooltip(new Tooltip("Delete the selected row"));
bt_delete.setPrefWidth(100);
bt_delete.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
MilkItem item = tableView.getItems().remove(tableView.getSelectionModel().getSelectedIndex());
milkData.removeData(item.getDate(), item.getFarmID());
EventLog.getInstance().log("Delete Data: " + item.getFarmID() + ", " + item.getDate());
}
});
Button bt_save = new Button("Save");
bt_save.setTooltip(new Tooltip("Save the data change to csv files"));
bt_save.setPrefWidth(100);
bt_save.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
milkData.writeMilkData();
EventLog.getInstance().log("Save Data");
} catch (IOException e) {
e.printStackTrace();
}
}
});
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setVgap(8);
gridPane.setHgap(8);
gridPane.add(new Text("Date:"), 0, 0);
gridPane.add(date, 0, 1);
gridPane.add(new Text("Farm ID:"), 1, 0);
gridPane.add(farm, 1, 1);
gridPane.add(new Text("Weight:"), 2, 0);
gridPane.add(weight, 2, 1);
gridPane.add(bt_add, 3, 1);
gridPane.add(bt_delete, 4, 1);
gridPane.add(bt_save, 5, 1);
return gridPane;
}
/**
* Creates the milk raw table View.
*/
private void createMilkRawTableV() {
tableView = new TableView<MilkItem>();
tableView.setEditable(true);
tableView.setPrefWidth(500);
tableView.setPrefWidth(400);
TableColumn<MilkItem, String> colDate = new TableColumn<>("date(year-month-day)");
colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
colDate.setEditable(false);
colDate.setSortable(true);
colDate.setPrefWidth(200);
colDate.setCellFactory(TextFieldTableCell.forTableColumn());
colDate.setComparator(new MilkComparator());
// MilkItem refers to table data type, String refers to the according instance
// variable type of MilkItem
TableColumn<MilkItem, String> colFarmID = new TableColumn<>("Farm ID");
// "farmID" refers to the according instance variable name of MilkItem
colFarmID.setCellValueFactory(new PropertyValueFactory<>("farmID"));
colFarmID.setPrefWidth(200);
colFarmID.setEditable(false);
colFarmID.setCellFactory(TextFieldTableCell.forTableColumn());
colFarmID.setComparator(new MilkComparator());
TableColumn<MilkItem, Integer> colWeight = new TableColumn<>("Weight");
colWeight.setCellValueFactory(new PropertyValueFactory<>("weight"));
colWeight.setPrefWidth(200);
colWeight.setEditable(true);
colWeight.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
colWeight.setSortable(false);
colWeight.setOnEditCommit((TableColumn.CellEditEvent<MilkItem, Integer> t) -> {
MilkItem item = t.getTableView().getItems().get(t.getTablePosition().getRow());
item.setWeight(t.getNewValue());
milkData.changeData(item.getDate(), item.getFarmID(), item.getWeight());
});
tableView.getColumns().add(colDate);
tableView.getColumns().add(colFarmID);
tableView.getColumns().add(colWeight);
}
/**
* Fill in the month combo box.
*/
public void fillMonthComboBox() {
monthComboBox.getItems().clear();
monthComboBox.getItems().addAll(milkData.getMonths());
// Select the first month item
monthComboBox.getSelectionModel().selectFirst();
}
/**
* Adds the milk items.
*
* @param data the data
*/
private void addMilkItems(List<MilkItem> data) {
ObservableList<MilkItem> list = tableView.getItems();
list.clear();
data.forEach((item) -> list.add(item));
}
}