Skip to content

Commit 9853772

Browse files
committed
SimpleTask - add more flexibility in handling tasks states [#6]
1 parent 51371ef commit 9853772

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

scalafx-extras/src/main/scala/org/scalafx/extras/BusyWorker.scala

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,47 @@ object BusyWorker {
4646
implicit def apply(nodes: Seq[Node]): Seq[jfxs.Node] = nodes.map(_.delegate)
4747

4848
/**
49-
* A simple wrapper for a task that mas a status message property and a progress property.
49+
* A simple wrapper for a task that has a status message property and a progress property.
5050
* Intended for use with [[org.scalafx.extras.BusyWorker#doTask(java.lang.String, org.scalafx.extras.BusyWorker.SimpleTask) BusyWorker#doTask]] method
5151
*
5252
* @tparam R returned value type.
5353
*/
5454
trait SimpleTask[R] {
5555

56+
57+
/**
58+
* Method called whenever the state of the Task has transitioned to the SCHEDULED state.
59+
* This method is invoked on the FX Application Thread after any listeners of the state property and after the
60+
* Task has been fully transitioned to the new state.
61+
*/
62+
def onScheduled(): Unit = {}
63+
64+
/**
65+
* Method called whenever the state of the Task has transitioned to the RUNNING state.
66+
* This method is invoked on the FX Application Thread after any listeners of the state property and after the
67+
* Task has been fully transitioned to the new state.
68+
*/
69+
def onRunning(): Unit = {}
70+
71+
/**
72+
* called whenever the state of the Task has transitioned to the SUCCEEDED state. This method is invoked on the FX Application Thread after any listeners of the state property and after the Task has been fully transitioned to the new state.
73+
*/
74+
def onSucceeded(): Unit = {}
75+
76+
/**
77+
* Method called whenever the state of the Task has transitioned to the CANCELLED state.
78+
* This method is invoked on the FX Application Thread after any listeners of the state property and after the
79+
* Task has been fully transitioned to the new state.
80+
*/
81+
def onCancelled(): Unit = {}
82+
83+
/**
84+
* Method called whenever the state of the Task has transitioned to the FAILED state.
85+
* This method is invoked on the FX Application Thread after any listeners of the state property and after the
86+
* Task has been fully transitioned to the new state.
87+
*/
88+
def onFailed(): Unit = {}
89+
5690
/**
5791
* Message that can be updated while task is executed.
5892
*/
@@ -199,6 +233,7 @@ class BusyWorker private(val title: String,
199233
this(title, _parentWindow = None, _disabledNodes = disabledNodes)
200234

201235
def disabledNodes: Seq[jfxs.Node] = _disabledNodes
236+
202237
def disabledNodes_=(implicit v: Seq[jfxs.Node]): Unit = _disabledNodes = v
203238

204239
override def parentWindow: Option[Window] = _parentWindow match {
@@ -218,6 +253,7 @@ class BusyWorker private(val title: String,
218253
def parentWindow_=(v: Option[Window]): Unit = {
219254
_parentWindow = v
220255
}
256+
221257
def parentWindow_=(v: Window): Unit = {
222258
_parentWindow = Option(v)
223259
}
@@ -345,12 +381,21 @@ class BusyWorker private(val title: String,
345381
*/
346382
def doTask[R](name: String)(task: SimpleTask[R]): Future[R] = {
347383
val jfxTask = new javafx.concurrent.Task[R] {
348-
override def call(): R = {
349-
task.call()
350-
}
384+
override def call(): R = task.call()
385+
386+
override def scheduled(): Unit = task.onScheduled()
387+
388+
override def running(): Unit = task.onRunning()
389+
390+
override def succeeded(): Unit = task.onSucceeded()
391+
392+
override def cancelled(): Unit = task.onCancelled()
393+
394+
override def failed(): Unit = task.onFailed()
351395

352396
task.message.onChange((_, _, newValue) => updateMessage(newValue))
353397
task.progress.onChange((_, _, newValue) => updateProgress(newValue.doubleValue(), 1.0))
398+
354399
}
355400
_doTask(jfxTask, task.onFinish, name)
356401
}

0 commit comments

Comments
 (0)