-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableTable.java
More file actions
569 lines (480 loc) · 13.8 KB
/
Copy pathEditableTable.java
File metadata and controls
569 lines (480 loc) · 13.8 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package application;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
/**
*
* @author Hasan Kara <hasan.kara@fhnw.ch>
*/
public class EditableTable extends Application
{
private final TableView<Person> table = new TableView<>();
private final ObservableList<Typ> typData = FXCollections.observableArrayList(new Typ("Hund"), new Typ("Fuchs"), new Typ("Esel"));
private final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob", this.typData.get(0), new Date()), new Person("Abcd", null, null), new Person("Urs", this.typData.get(1), new Date()), new Person("Hans", this.typData.get(2), new Date()), new Person("Ueli", this.typData.get(2), new Date()));
final HBox hb = new HBox();
@Override
public void start(final Stage stage)
{
final Scene scene = new Scene(new Group());
stage.setWidth(550);
stage.setHeight(550);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
this.table.setEditable(true);
final Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory = (final TableColumn<Person, String> param) -> new EditingCell();
final Callback<TableColumn<Person, Typ>, TableCell<Person, Typ>> comboBoxCellFactory = (final TableColumn<Person, Typ> param) -> new ComboBoxEditingCell();
final Callback<TableColumn<Person, Date>, TableCell<Person, Date>> dateCellFactory = (final TableColumn<Person, Date> param) -> new DateEditingCell();
final TableColumn<Person, String> firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
firstNameCol.setCellFactory(cellFactory);
firstNameCol.setOnEditCommit((final TableColumn.CellEditEvent<Person, String> t) -> {
t.getTableView().getItems().get(t.getTablePosition().getRow()).setFirstName(t.getNewValue());
});
final TableColumn<Person, Typ> lastNameCol = new TableColumn("Type");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(cellData -> cellData.getValue().typObjProperty());
lastNameCol.setCellFactory(comboBoxCellFactory);
lastNameCol.setOnEditCommit((final TableColumn.CellEditEvent<Person, Typ> t) -> {
t.getTableView().getItems().get(t.getTablePosition().getRow()).setTypObj(t.getNewValue());
});
final TableColumn<Person, Date> emailCol = new TableColumn("Birthday");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(cellData -> cellData.getValue().birthdayProperty());
emailCol.setCellFactory(dateCellFactory);
emailCol.setOnEditCommit((final TableColumn.CellEditEvent<Person, Date> t) -> {
t.getTableView().getItems().get(t.getTablePosition().getRow()).setBirthday(t.getNewValue());
});
this.table.setItems(this.data);
this.table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
final TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
final TextField addLastName = new TextField();
addLastName.setPromptText("Last Name");
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
final TextField addEmail = new TextField();
addEmail.setPromptText("email");
addEmail.setMaxWidth(emailCol.getPrefWidth());
final Button addButton = new Button("Add");
addButton.setOnAction((final ActionEvent e) -> {
this.data.add(new Person(addFirstName.getText(), new Typ("Hund"), new Date()));
addFirstName.clear();
addLastName.clear();
addEmail.clear();
});
final Button showButton = new Button("Show");
showButton.setOnAction((final ActionEvent e) -> {
this.table.getItems().forEach(System.out::println);
});
this.hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, showButton);
this.hb.setSpacing(3);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, this.table, this.hb);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args)
{
launch(args);
}
class EditingCell extends TableCell<Person, String>
{
private TextField textField;
private EditingCell()
{
}
@Override
public void startEdit()
{
if(!this.isEmpty())
{
super.startEdit();
this.createTextField();
this.setText(null);
this.setGraphic(this.textField);
this.textField.selectAll();
}
}
@Override
public void cancelEdit()
{
super.cancelEdit();
this.setText(this.getItem());
this.setGraphic(null);
}
@Override
public void updateItem(final String item, final boolean empty)
{
super.updateItem(item, empty);
if(empty)
{
this.setText(item);
this.setGraphic(null);
}
else
{
if(this.isEditing())
{
if(this.textField != null)
{
this.textField.setText(this.getString());
// setGraphic(null);
}
this.setText(null);
this.setGraphic(this.textField);
}
else
{
this.setText(this.getString());
this.setGraphic(null);
}
}
}
private void createTextField()
{
this.textField = new TextField(this.getString());
this.textField.setMinWidth(this.getWidth() - (this.getGraphicTextGap() * 2));
this.textField.setOnAction((e) -> this.commitEdit(this.textField.getText()));
this.textField.focusedProperty().addListener((final ObservableValue<? extends Boolean> observable, final Boolean oldValue, final Boolean newValue) -> {
if(!newValue)
{
System.out.println("Commiting " + this.textField.getText());
this.commitEdit(this.textField.getText());
}
});
}
private String getString()
{
return this.getItem() == null ? "" : this.getItem();
}
}
class DateEditingCell extends TableCell<Person, Date>
{
private DatePicker datePicker;
private DateEditingCell()
{
}
@Override
public void startEdit()
{
if(!this.isEmpty())
{
super.startEdit();
this.createDatePicker();
this.setText(null);
this.setGraphic(this.datePicker);
}
}
@Override
public void cancelEdit()
{
super.cancelEdit();
this.setText(this.getDate().toString());
this.setGraphic(null);
}
@Override
public void updateItem(final Date item, final boolean empty)
{
super.updateItem(item, empty);
if(empty)
{
this.setText(null);
this.setGraphic(null);
}
else
{
if(this.isEditing())
{
if(this.datePicker != null)
{
this.datePicker.setValue(this.getDate());
}
this.setText(null);
this.setGraphic(this.datePicker);
}
else
{
if(this.getDate() != null)
{
if(this.getDate() != null)
{
this.setText(this.getDate().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)));
}
else
{
this.setText(null);
}
this.setGraphic(null);
}
else
{
this.setText(null);
this.setGraphic(null);
}
}
}
}
private void createDatePicker()
{
this.datePicker = new DatePicker(this.getDate());
this.datePicker.setMinWidth(this.getWidth() - (this.getGraphicTextGap() * 2));
this.datePicker.setOnAction((e) -> {
System.out.println("Committed: " + this.datePicker.getValue().toString());
this.commitEdit(Date.from(this.datePicker.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant()));
});
// datePicker.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
// if (!newValue) {
// commitEdit(Date.from(datePicker.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant()));
// }
// });
}
private LocalDate getDate()
{
//return this.getItem() == null ? LocalDate.now() : this.getItem().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return this.getItem() == null ? null : this.getItem().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
}
class ComboBoxEditingCell extends TableCell<Person, Typ>
{
private ComboBox<Typ> comboBox;
private ComboBoxEditingCell()
{
}
@Override
public void startEdit()
{
if(!this.isEmpty())
{
super.startEdit();
this.createComboBox();
this.setText(null);
this.setGraphic(this.comboBox);
}
}
@Override
public void cancelEdit()
{
super.cancelEdit();
this.setText(this.getTyp().getTyp());
this.setGraphic(null);
}
@Override
public void updateItem(final Typ item, final boolean empty)
{
super.updateItem(item, empty);
if(empty)
{
this.setText(null);
this.setGraphic(null);
}
else
{
if(this.isEditing())
{
if(this.comboBox != null)
{
this.comboBox.setValue(this.getTyp());
}
this.setText(this.getTyp().getTyp());
this.setGraphic(this.comboBox);
}
else
{
this.setText(this.getTyp().getTyp());
this.setGraphic(null);
}
}
}
private void createComboBox()
{
this.comboBox = new ComboBox<>(EditableTable.this.typData);
this.comboBoxConverter(this.comboBox);
this.comboBox.valueProperty().set(this.getTyp());
this.comboBox.setMinWidth(this.getWidth() - (this.getGraphicTextGap() * 2));
this.comboBox.setOnAction((e) -> {
System.out.println("Committed: " + this.comboBox.getSelectionModel().getSelectedItem());
this.commitEdit(this.comboBox.getSelectionModel().getSelectedItem());
});
// comboBox.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
// if (!newValue) {
// commitEdit(comboBox.getSelectionModel().getSelectedItem());
// }
// });
}
private void comboBoxConverter(final ComboBox<Typ> comboBox)
{
// Define rendering of the list of values in ComboBox drop down.
comboBox.setCellFactory((c) -> {
return new ListCell<Typ>(){
@Override
protected void updateItem(final Typ item, final boolean empty)
{
super.updateItem(item, empty);
if((item == null) || empty)
{
this.setText(null);
}
else
{
this.setText(item.getTyp());
}
}
};
});
}
private Typ getTyp()
{
return this.getItem() == null ? new Typ("") : this.getItem();
}
}
public static class Typ
{
private final SimpleStringProperty typ;
public Typ(final String typ)
{
this.typ = new SimpleStringProperty(typ);
}
public String getTyp()
{
return this.typ.get();
}
public StringProperty typProperty()
{
return this.typ;
}
public void setTyp(final String typ)
{
this.typ.set(typ);
}
@Override
public String toString()
{
return this.typ.get();
}
}
public static class Project
{
private final SimpleStringProperty name;
private final SimpleListProperty<Person> persons;
public Project(final String name, final List<Person> persons)
{
this.name = new SimpleStringProperty(name);
this.persons = new SimpleListProperty<>();
this.persons.setAll(persons);
}
public String getName()
{
return this.name.get();
}
public StringProperty nameProperty()
{
return this.name;
}
public void setName(final String name)
{
this.name.set(name);
}
public List<Person> getPersons()
{
return this.persons.get();
}
public SimpleListProperty<Person> personsProperty()
{
return this.persons;
}
public void setPersons(final List<Person> persons)
{
this.persons.setAll(persons);
}
}
public static class Person
{
private final SimpleStringProperty firstName;
private final SimpleObjectProperty<Typ> typ;
private final SimpleObjectProperty<Date> birthday;
public Person(final String firstName, final Typ typ, final Date bithday)
{
this.firstName = new SimpleStringProperty(firstName);
this.typ = new SimpleObjectProperty(typ);
this.birthday = new SimpleObjectProperty(bithday);
}
public String getFirstName()
{
return this.firstName.get();
}
public StringProperty firstNameProperty()
{
return this.firstName;
}
public void setFirstName(final String firstName)
{
this.firstName.set(firstName);
}
public Typ getTypObj()
{
return this.typ.get();
}
public ObjectProperty<Typ> typObjProperty()
{
return this.typ;
}
public void setTypObj(final Typ typ)
{
this.typ.set(typ);
}
public Date getBirthday()
{
return this.birthday.get();
}
public ObjectProperty<Date> birthdayProperty()
{
return this.birthday;
}
public void setBirthday(final Date birthday)
{
this.birthday.set(birthday);
}
@Override
public String toString()
{
return this.getFirstName() + this.getBirthday() + this.getTypObj().getTyp();
}
}
}