-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewPopupSkin.java
More file actions
160 lines (131 loc) · 4.87 KB
/
Copy pathTableViewPopupSkin.java
File metadata and controls
160 lines (131 loc) · 4.87 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
package skin;
import java.net.URL;
import control.TableViewPopup;
import controller.IGenericReferenceDataController;
import javafx.beans.binding.Bindings;
import javafx.collections.transformation.FilteredList;
import javafx.event.Event;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.Skin;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseButton;
public class TableViewPopupSkin<T> implements Skin<TableViewPopup<T>>
{
private static final String FXML_FILE_EXTENSION = ".fxml";
private final TableViewPopup<T> tableViewPopupControl;
private final TableView<T> innerTableViewControl;
private FilteredList<?> innerTableViewControlDataSource;
private FXMLLoader fxmlLoader = null;
private static final int TABLE_ROW_HEIGHT = 26;
private static final int TABLE_TITLE_ROW_HEIGHT = 26;
private IGenericReferenceDataController referenceDataController;
public TableViewPopupSkin(TableViewPopup<T> tableViewPopupControl)
{
this.tableViewPopupControl = tableViewPopupControl;
/* have to check is there a better and efficient way to do this. */
this.innerTableViewControl = loadFXML();
//innerTableViewControl.setItems(tableViewPopupControl.getTableViewDataSource());
//Bindings.bindContent(innerTableViewControl.getItems(), tableViewPopupControl.getData());
//innerTableViewControl.itemProperty().bind(tableViewPopupControl.list); //Here bind the listview items to your control list
//suggestionList = new ListView<>(control.getSuggestions());
//suggestionList.getStylesheets().add(AutoCompletionBinding.class.getResource("autocompletion.css").toExternalForm());
innerTableViewControl.getStylesheets().add(this.getClass().getResource("/css/tableview-popup.css").toExternalForm());
innerTableViewControl.getStyleClass().add(TableViewPopup.DEFAULT_STYLE_CLASS);
/**
* Here we bind the prefHeightProperty to the minimum height between the max visible rows and the current items list. We also add an arbitrary
* 18 number because when we have only one item we have the vertical scrollBar showing for no reason.
*/
innerTableViewControl.prefHeightProperty().bind(Bindings.min(tableViewPopupControl.visibleRowCountProperty(), Bindings.size(innerTableViewControl.getItems())).multiply(TABLE_ROW_HEIGHT).add(TABLE_TITLE_ROW_HEIGHT));
//suggestionList.setCellFactory(TextFieldListCell.forListView(control.getConverter()));
//Allowing the user to control TableView width.
innerTableViewControl.prefWidthProperty().bind(tableViewPopupControl.prefWidthProperty());
innerTableViewControl.maxWidthProperty().bind(tableViewPopupControl.maxWidthProperty());
innerTableViewControl.minWidthProperty().bind(tableViewPopupControl.minWidthProperty());
registerEventListener();
}
private void registerEventListener()
{
innerTableViewControl.setOnMouseClicked(mouseEvent -> {
if(mouseEvent.getButton() == MouseButton.PRIMARY)
{
onRowSelected(innerTableViewControl.getSelectionModel().getSelectedItem());
}
});
innerTableViewControl.setOnKeyPressed(keyEvent -> {
switch(keyEvent.getCode())
{
case ENTER:
onRowSelected(innerTableViewControl.getSelectionModel().getSelectedItem());
break;
case ESCAPE:
if(tableViewPopupControl.isHideOnEscape())
tableViewPopupControl.hide();
break;
default:
break;
}
});
}
private void onRowSelected(T theSelectedRow)
{
if(theSelectedRow != null)
{
Event.fireEvent(tableViewPopupControl, new TableViewPopup.SuggestionEvent<>(theSelectedRow));
}
}
@Override
public TableViewPopup<T> getSkinnable()
{
return tableViewPopupControl;
}
@Override
public Node getNode()
{
return innerTableViewControl;
}
@Override
public void dispose()
{
//tableViewPopupControl = null;
//innerTableViewControl = null;
}
private TableView<T> loadFXML()
{
TableView<T> innerTableViewControl = null;
if(fxmlLoader == null)
{
try
{
//innerTableViewControl = FXMLLoader.load(this.getClass().getResource("/view/PersonView.fxml"));
fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(this.getResourceURL());
innerTableViewControl = fxmlLoader.load();
//IGenericReferenceDataController referenceDataController = fxmlLoader.getController();
referenceDataController = fxmlLoader.getController();
innerTableViewControlDataSource = referenceDataController.getInnerTableViewControlDataSource();
}
catch(Exception exception)
{
System.err.println(exception);
}
}
return innerTableViewControl;
}
private URL getResourceURL()
{
return this.getClass().getResource(getFXMLResourcePath());
}
private String getFXMLResourcePath()
{
return tableViewPopupControl.getFXMLToLoad() + FXML_FILE_EXTENSION;
}
public FilteredList<?> getInnerTableViewControlDataSource()
{
return innerTableViewControlDataSource;
}
public IGenericReferenceDataController getController()
{
return referenceDataController;
}
}