-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewPopup.java
More file actions
220 lines (184 loc) · 5.56 KB
/
Copy pathTableViewPopup.java
File metadata and controls
220 lines (184 loc) · 5.56 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
package control;
import com.sun.javafx.event.EventHandlerManager;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventDispatchChain;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.control.PopupControl;
import javafx.scene.control.Skin;
import javafx.stage.Window;
import javafx.util.StringConverter;
import skin.TableViewPopupSkin;
/**
* The TableViewPopup provides an list of available suggestions in order to complete current user input.
*/
@SuppressWarnings("restriction")
public class TableViewPopup<T> extends PopupControl
{
public static final String DEFAULT_STYLE_CLASS = "tableview-popup";
private static final int SOURCE_NODE_HEIGHT = 28; //!HARD CODED!. ASSUMING THAT THE SOURCE NODE WILL BE A TEXTFIELD WHOSE DEFAULT HEIGHT IS 25
/* need to check whether this is the proper way to do it */
//private TableViewPopupSkin<T> tableViewPopupSkin;
private final ObservableList<T> suggestions = FXCollections.observableArrayList();
private final ObservableList<T> data = FXCollections.observableArrayList();
/**
* Creates a new TableViewPopup
*/
public TableViewPopup()
{
getStyleClass().add(DEFAULT_STYLE_CLASS);
this.setAutoFix(true);
this.setAutoHide(true);
this.setHideOnEscape(true);
}
/**
* The maximum number of rows to be visible in the popup when it is showing. By default this value is 5, but this can be changed to increase
* or decrease the height of the popup.
*/
private IntegerProperty visibleRowCount = new SimpleIntegerProperty(this, "visibleRowCount", 10);
public final void setVisibleRowCount(int value)
{
visibleRowCount.set(value);
}
public final int getVisibleRowCount()
{
return visibleRowCount.get();
}
public final IntegerProperty visibleRowCountProperty()
{
return visibleRowCount;
}
/**
* Get the suggestions presented by this TableViewPopup
*/
public ObservableList<T> getSuggestions()
{
return suggestions;
}
/**
* Get the data set on the TableView
*/
public ObservableList<T> getData()
{
return data;
}
/**
* Show this popup right below the given Node
*/
public void show(Node node)
{
if(node.getScene() == null || node.getScene().getWindow() == null)
throw new IllegalStateException("Can not show popup. The node must be attached to a scene/window.");
if(isShowing())
return;
Window parent = node.getScene().getWindow();
this.show(parent, parent.getX() + node.localToScene(0, 0).getX() + node.getScene().getX(), parent.getY() + node.localToScene(0, 0).getY() + node.getScene().getY() + SOURCE_NODE_HEIGHT);
}
/**
* Set the string converter used to turn a generic suggestion into a string
*/
private StringConverter<T> converter;
public void setConverter(StringConverter<T> converter)
{
this.converter = converter;
}
/**
* Get the string converter used to turn a generic suggestion into a string
*/
public StringConverter<T> getConverter()
{
return converter;
}
private final EventHandlerManager eventHandlerManager = new EventHandlerManager(this);
private ObjectProperty<EventHandler<SuggestionEvent<T>>> onSuggestion = new ObjectPropertyBase<EventHandler<SuggestionEvent<T>>>(){
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
protected void invalidated()
{
eventHandlerManager.setEventHandler(SuggestionEvent.SUGGESTION, (EventHandler<SuggestionEvent>) (Object) get());
}
@Override
public Object getBean()
{
return TableViewPopup.this;
}
@Override
public String getName()
{
return "onSuggestion";
}
};
public final ObjectProperty<EventHandler<SuggestionEvent<T>>> onSuggestionProperty()
{
return onSuggestion;
}
public final void setOnSuggestion(EventHandler<SuggestionEvent<T>> value)
{
onSuggestionProperty().set(value);
}
public final EventHandler<SuggestionEvent<T>> getOnSuggestion()
{
return onSuggestionProperty().get();
}
@Override
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
{
return super.buildEventDispatchChain(tail).append(eventHandlerManager);
}
@Override
protected Skin<?> createDefaultSkin()
{
return new TableViewPopupSkin<T>(this);
//tableViewPopupSkin = new TableViewPopupSkin<T>(this);
//return tableViewPopupSkin;
}
private String fxmlToLoad;
public void setFXMLToLoad(String fxmlToLoad)
{
this.fxmlToLoad = fxmlToLoad;
}
public String getFXMLToLoad()
{
return this.fxmlToLoad;
}
/***************************************************************************
* *
* Inner classes *
* *
**************************************************************************/
/**
* Represents an Event which is fired when the user has selected a suggestion/row from the auto-complete popup
*/
@SuppressWarnings("serial")
public static class SuggestionEvent<TE> extends Event
{
@SuppressWarnings("rawtypes")
public static final EventType<SuggestionEvent> SUGGESTION = new EventType<>("SUGGESTION");
private final TE suggestion;
public SuggestionEvent(TE suggestion)
{
super(SUGGESTION);
this.suggestion = suggestion;
}
/**
* Returns the suggestion which was chosen by the user
*/
public TE getSuggestion()
{
return suggestion;
}
}
/* need to double check this */
public TableViewPopupSkin<T> getTableViewPopupSkin()
{
//return tableViewPopupSkin;
return (TableViewPopupSkin<T>) getSkin();
}
}