/
FilterableListView.java
162 lines (136 loc) · 5.68 KB
/
FilterableListView.java
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
package at.esque.kafka.controls;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Collection;
public class FilterableListView extends VBox {
@FXML
private TextField filtertextField;
@FXML
private ListView<String> listView;
@FXML
private HBox toolbarBox;
@FXML
private Button addButton;
@FXML
private Button refreshButton;
private ObservableList<String> baseList;
private FilteredList<String> filteredList;
private SortedList<String> sortedList;
public BooleanProperty addButtonVisible = new SimpleBooleanProperty(true);
public BooleanProperty refreshButtonVisible = new SimpleBooleanProperty(true);
public FilterableListView() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"/fxml/controls/filterableListView.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
setup();
}
private void setup() {
baseList = FXCollections.observableArrayList();
filteredList = new FilteredList<>(baseList);
sortedList = new SortedList<>(filteredList);
listView.setItems(sortedList);
filtertextField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredList.setPredicate(item -> StringUtils.isEmpty(newValue)
|| StringUtils.containsIgnoreCase(item, newValue));
});
sortedList.setComparator(String::compareTo);
bindButtonProperties();
}
private void bindButtonProperties() {
addButton.onActionProperty().bind(onAddActionProperty());
addButton.visibleProperty().bind(addButtonVisibleProperty());
refreshButton.visibleProperty().bind(refreshButtonVisibleProperty());
addButton.maxWidthProperty().bind(Bindings.when(addButtonVisibleProperty()).then(Region.USE_COMPUTED_SIZE).otherwise(0));
addButton.minWidthProperty().bind(Bindings.when(addButtonVisibleProperty()).then(Region.USE_COMPUTED_SIZE).otherwise(0));
refreshButton.maxWidthProperty().bind(Bindings.when(refreshButtonVisibleProperty()).then(Region.USE_COMPUTED_SIZE).otherwise(0));
refreshButton.minWidthProperty().bind(Bindings.when(refreshButtonVisibleProperty()).then(Region.USE_COMPUTED_SIZE).otherwise(0));
}
public ObservableList<String> getBaseList() {
return baseList;
}
public void setItems(Collection<String> items) {
baseList.clear();
baseList.addAll(items);
}
public void addItems(Collection<String> items) {
baseList.addAll(items);
}
public ListView<String> getListView() {
return listView;
}
public boolean isAddButtonVisible() {
return addButtonVisible.get();
}
public BooleanProperty addButtonVisibleProperty() {
return addButtonVisible;
}
public void setAddButtonVisible(boolean addButtonVisible) {
this.addButtonVisible.set(addButtonVisible);
}
public boolean isRefreshButtonVisible() {
return refreshButtonVisible.get();
}
public BooleanProperty refreshButtonVisibleProperty() {
return refreshButtonVisible;
}
public void setRefreshButtonVisible(boolean refreshButtonVisible) {
this.refreshButtonVisible.set(refreshButtonVisible);
}
public final ObjectProperty<EventHandler<ActionEvent>> onAddActionProperty() { return onAddAction; }
public final void setOnAddAction(EventHandler<ActionEvent> value) { onAddActionProperty().set(value); }
public final EventHandler<ActionEvent> getOnAddAction() { return onAddActionProperty().get(); }
private ObjectProperty<EventHandler<ActionEvent>> onAddAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
@Override protected void invalidated() {
setEventHandler(ActionEvent.ACTION, get());
}
@Override
public Object getBean() {
return FilterableListView.this;
}
@Override
public String getName() {
return "onAddAction";
}
};
public final ObjectProperty<EventHandler<ActionEvent>> onRefreshActionProperty() { return onRefreshAction; }
public final void setOnRefreshAction(EventHandler<ActionEvent> value) { onRefreshActionProperty().set(value); }
public final EventHandler<ActionEvent> getOnRefreshAction() { return onRefreshActionProperty().get(); }
private ObjectProperty<EventHandler<ActionEvent>> onRefreshAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
@Override protected void invalidated() {
setEventHandler(ActionEvent.ACTION, get());
}
@Override
public Object getBean() {
return FilterableListView.this;
}
@Override
public String getName() {
return "onRefreshAction";
}
};
}