/
TreeItem.scala
287 lines (242 loc) · 9.99 KB
/
TreeItem.scala
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
/*
* Copyright (c) 2011-2015, ScalaFX Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the ScalaFX Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package scalafx.scene.control
import javafx.scene.{control => jfxsc}
import javafx.{collections => jfxc, event => jfxe, scene => jfxs}
import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.language.implicitConversions
import scalafx.Includes._
import scalafx.beans.property.{BooleanProperty, ObjectProperty, ReadOnlyBooleanProperty, ReadOnlyObjectProperty}
import scalafx.collections.ObservableBuffer
import scalafx.delegate.SFXDelegate
import scalafx.event.{Event, EventHandlerDelegate, EventType}
import scalafx.scene.Node
object TreeItem {
implicit def sfxTreeItemToJfx[T](v: TreeItem[T]): jfxsc.TreeItem[T] = if (v != null) v.delegate else null
object TreeModificationEvent {
implicit def sfxTreeModificationEvent2jfx[T](v: TreeModificationEvent[T]): jfxsc.TreeItem.TreeModificationEvent[T] = if (v != null) v.delegate else null
}
class TreeModificationEvent[T](override val delegate: jfxsc.TreeItem.TreeModificationEvent[T])
extends Event(delegate)
with SFXDelegate[jfxsc.TreeItem.TreeModificationEvent[T]] {
/**
* Constructs a basic TreeModificationEvent - this is useful in situations
* where the tree item has not received a new value, has not changed
* between expanded/collapsed states, and whose children has not changed.
*/
def this(eventType: EventType[_ <: jfxe.Event], treeItem: TreeItem[T]) =
this(new jfxsc.TreeItem.TreeModificationEvent[T](eventType, treeItem))
/**
* Constructs a TreeModificationEvent for when the TreeItem has had its
* `TreeItem.expandedProperty()` changed.
*/
def this(eventType: EventType[_ <: jfxe.Event], treeItem: TreeItem[T], expanded: Boolean) =
this(new jfxsc.TreeItem.TreeModificationEvent[T](eventType, treeItem, expanded))
/**
* Constructs a TreeModificationEvent for when the TreeItem has had its
* children list changed.
*/
def this(eventType: jfxe.EventType[_ <: jfxe.Event],
treeItem: jfxsc.TreeItem[T],
added: mutable.Buffer[_ <: jfxsc.TreeItem[T]],
removed: mutable.Buffer[_ <: jfxsc.TreeItem[T]]) =
this(new jfxsc.TreeItem.TreeModificationEvent[T](eventType, treeItem, added, removed))
/**
* Constructs a TreeModificationEvent for when the TreeItem has had its
* `TreeItem.valueProperty()` changed.
*/
def this(eventType: EventType[_ <: jfxe.Event], treeItem: TreeItem[T], newValue: T) =
this(new jfxsc.TreeItem.TreeModificationEvent[T](eventType, treeItem, newValue))
/**
* Returns the children added to the TreeItem in this event, or an empty
* list if no children were added.
*/
def addedChildren: mutable.Buffer[_ <: jfxsc.TreeItem[T]] = delegate.getAddedChildren
/**
* Returns the number of children items that were added in this event, or
* zero if no children were added.
*/
def addedSize: Int = delegate.getAddedSize
/**
* If the value of the TreeItem changed, this method will return the new
* value.
*/
def newValue: T = delegate.getNewValue
/**
* Returns the children removed from the TreeItem in this event, or an
* empty list if no children were added.
*/
def removedChildren: mutable.Buffer[_ <: jfxsc.TreeItem[T]] = delegate.getRemovedChildren
/**
* Returns the number of children items that were removed in this event,
* or zero if no children were removed.
*/
def removedSize: Int = delegate.getRemovedSize
/**
* Returns the TreeItem upon which this event occurred.
*/
override def source: TreeItem[_] = delegate.getSource
/**
* Returns the TreeItem that this event occurred upon.
*/
def treeItem: TreeItem[T] = delegate.getTreeItem
/**
* Returns true if this event represents a TreeItem event where children
* TreeItems were added.
*/
def wasAdded: Boolean = delegate.wasAdded
/**
* Returns true if this event represents a TreeItem collapse event, and
* false if the TreeItem was not collapsed.
*/
def wasCollapsed: Boolean = delegate.wasCollapsed
/**
* Returns true if this event represents a TreeItem expansion event, and
* false if the TreeItem was not expanded.
*/
def wasExpanded: Boolean = delegate.wasExpanded
/**
* Returns true if this event represents a TreeItem event where children
* TreeItems were removed.
*/
def wasRemoved: Boolean = delegate.wasRemoved
}
/**
* An EventType used when the TreeItem receives a modification to its
* expanded property, such that the TreeItem is now in the collapsed state.
*/
def branchCollapsedEvent = jfxsc.TreeItem.branchCollapsedEvent
/**
* An EventType used when the TreeItem receives a modification to its
* expanded property, such that the TreeItem is now in the expanded state.
*/
def branchExpandedEvent = jfxsc.TreeItem.branchExpandedEvent
/**
* An EventType used when the TreeItem receives a direct modification to its
* children list.
*/
def childrenModificationEvent = jfxsc.TreeItem.childrenModificationEvent
/** The general EventType used when the TreeItem receives a modification
* that results in the number of children being visible changes.
*
* @since 8.0
*/
def expandedItemCountChangeEvent = jfxsc.TreeItem.expandedItemCountChangeEvent
/**
* An EventType used when the TreeItem receives a modification to its
* graphic property.
*/
def graphicChangedEvent = jfxsc.TreeItem.graphicChangedEvent
/**
* The general EventType used when the TreeItem receives a modification that
* results in the number of children being visible changes.
*/
// NOTE JFX8: Comment method that disappeared in JavaFX 8. Did it moved somewhere?
// def treeItemCountChangeEvent = jfxsc.TreeItem.treeItemCountChangeEvent
/**
* The base EventType used to indicate that an event has occurred within a
* TreeItem.
*/
def treeNotificationEvent = jfxsc.TreeItem.treeNotificationEvent
/**
* An EventType used when the TreeItem receives a modification to its value
* property.
*/
def valueChangedEvent = jfxsc.TreeItem.valueChangedEvent
}
/**
* Wraps [[http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeItem.html javafx.scene.control.TreeItem]] class.
*/
class TreeItem[T](override val delegate: jfxsc.TreeItem[T] = new jfxsc.TreeItem[T])
extends EventHandlerDelegate
with SFXDelegate[jfxsc.TreeItem[T]] {
/**
* Creates a TreeItem with the value property set to the provided object.
*/
def this(value: T) = this(new jfxsc.TreeItem[T](value))
/**
* Creates a TreeItem with the value property set to the provided object,
* and the graphic set to the provided Node.
*/
def this(value: T, graphic: Node) = this(new jfxsc.TreeItem[T](value, graphic))
/**
* The expanded state of this TreeItem.
*/
def expanded: BooleanProperty = delegate.expandedProperty
def expanded_=(v: Boolean) {
expanded() = v
}
/**
* The node that is generally shown to the left of the value property.
*/
def graphic: ObjectProperty[jfxs.Node] = delegate.graphicProperty
def graphic_=(v: Node) {
graphic() = v
}
/**
* Represents the TreeItem leaf property, which is true if the TreeItem has no children.
*/
def leaf: ReadOnlyBooleanProperty = delegate.leafProperty
/**
* A property that represents the parent of this TreeItem.
*/
def parent: ReadOnlyObjectProperty[jfxsc.TreeItem[T]] = delegate.parentProperty
/**
* A property representing the application-specific data contained within
* this TreeItem.
*/
def value: ObjectProperty[T] = delegate.valueProperty
def value_=(v: T) {
value.set(v)
}
/**
* The children of this TreeItem.
*/
def children: ObservableBuffer[jfxsc.TreeItem[T]] = delegate.getChildren
def children_=(items: Seq[TreeItem[T]]) {
children.clear()
items.foreach(children += _)
}
/**
* Returns the next sibling of the TreeItem.
*/
def nextSibling = delegate.nextSibling
/**
* Returns the next sibling after the given node.
*/
def nextSibling(afterNode: TreeItem[T]) = delegate.nextSibling(afterNode)
/**
* Returns the previous sibling of the TreeItem.
*/
def previousSibling = delegate.previousSibling
/**
* Returns the previous sibling previous the given node.
*/
def previousSibling(afterNode: TreeItem[T]) = delegate.previousSibling(afterNode)
override protected def eventHandlerDelegate = delegate.asInstanceOf[EventHandled]
}