Skip to content

Commit

Permalink
* Allow for building non-DialogPane panes
Browse files Browse the repository at this point in the history
* Show lock pane on clicking "Cloud options"
  • Loading branch information
dbarashev committed Feb 17, 2019
1 parent 4ace295 commit 2ef1968
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 47 deletions.
43 changes: 27 additions & 16 deletions ganttproject/src/biz/ganttproject/app/OptionPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import javafx.application.Platform
import javafx.event.ActionEvent
import javafx.scene.Node
import javafx.scene.control.*
import javafx.scene.layout.Pane

interface I18N {
fun formatText(key: String, args: Array<Any> = arrayOf()): String
Expand Down Expand Up @@ -67,30 +68,20 @@ class OptionPaneBuilder<T> {
*/
var elements: List<OptionElementData<T>> = listOf()

fun buildDialogPane(optionHandler: (T) -> Unit): DialogPane {
return DialogPane().also {
this.buildDialogPane(it, optionHandler)
}
}
var toggleGroup: ToggleGroup = ToggleGroup()

fun showDialog(optionHandler: (T) -> Unit) {
Platform.runLater {
Dialog<Unit>().apply {
buildDialogPane(dialogPane, optionHandler)
show()
}
}
fun buildPane(): Pane {
return buildPaneImpl(this.toggleGroup)
}

private fun buildDialogPane(pane: DialogPane, optionHandler: (T) -> Unit) {
private fun buildPaneImpl(lockGroup: ToggleGroup): Pane {
val vbox = VBoxBuilder()
vbox.addTitle(this.titleString.update().value)
vbox.add(Label().apply {
this.textProperty().bind(this@OptionPaneBuilder.titleHelpString.update())
this.styleClass.add("help")
})

val lockGroup = ToggleGroup()
this.elements.forEach {
val btn = RadioButton().also { btn ->
btn.textProperty().bind(i18n.create(it.i18nKey))
Expand All @@ -109,7 +100,27 @@ class OptionPaneBuilder<T> {
}
it.customContent?.let { vbox.add(it) }
}
return vbox.vbox
}

fun buildDialogPane(optionHandler: (T) -> Unit): DialogPane {
return DialogPane().also {
this.buildDialogPane(it, optionHandler)
}
}

fun showDialog(optionHandler: (T) -> Unit) {
Platform.runLater {
Dialog<Unit>().apply {
buildDialogPane(dialogPane, optionHandler)
show()
}
}
}

private fun buildDialogPane(pane: DialogPane, optionHandler: (T) -> Unit) {
val lockGroup = ToggleGroup()
val optionsPane = buildPaneImpl(lockGroup)
val builder = this
pane.apply {
styleClass.add(builder.styleClass)
Expand All @@ -118,11 +129,11 @@ class OptionPaneBuilder<T> {
graphic = it
}

content = vbox.vbox
content = optionsPane
buttonTypes.add(ButtonType.OK)
lookupButton(ButtonType.OK).apply {
styleClass.add("btn-attention")
addEventHandler(ActionEvent.ACTION) { evt ->
addEventHandler(ActionEvent.ACTION) {
val userData = lockGroup.selectedToggle.userData as T
optionHandler(userData)
}
Expand Down
82 changes: 57 additions & 25 deletions ganttproject/src/biz/ganttproject/storage/cloud/DocPropertiesUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@ package biz.ganttproject.storage.cloud

import biz.ganttproject.app.OptionElementData
import biz.ganttproject.app.OptionPaneBuilder
import biz.ganttproject.lib.fx.VBoxBuilder
import biz.ganttproject.storage.LockableDocument
import com.fasterxml.jackson.databind.JsonNode
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView
import javafx.application.Platform
import javafx.event.ActionEvent
import javafx.geometry.Pos
import javafx.scene.control.Button
import javafx.scene.control.ButtonType
import javafx.scene.control.Dialog
import javafx.scene.control.DialogPane
import javafx.scene.control.ToggleGroup
import javafx.scene.layout.Pane
import javafx.scene.layout.Priority
import net.sourceforge.ganttproject.GPLogger
import java.time.Duration
import java.util.function.Consumer

typealias OnLockDone = (JsonNode?) -> Unit
typealias BusyUi = (Boolean) -> Unit

typealias LockDurationHandler = (Duration) -> Unit
/**
* @author dbarashev@bardsoftware.com
*/
class DocPropertiesUi(val errorUi: ErrorUi, val busyUi: BusyUi) {

fun createLockSuggestionPane(document: LockableDocument, onLockDone: OnLockDone): Pane {
return OptionPaneBuilder<Duration>().run {
private fun lockPaneBuilder(): OptionPaneBuilder<Duration> {
return OptionPaneBuilder<Duration>().apply {
i18n.rootKey = "cloud.lockOptionPane"
styleClass = "dlg-lock"
styleSheets.add("/biz/ganttproject/storage/cloud/GPCloudStorage.css")
Expand All @@ -52,34 +57,38 @@ class DocPropertiesUi(val errorUi: ErrorUi, val busyUi: BusyUi) {
OptionElementData("lock2h", Duration.ofHours(2)),
OptionElementData("lock24h", Duration.ofHours(24))
)
}
}

buildDialogPane { duration ->
if (duration.isZero) {
if (document.status.get().locked) {

} else {
onLockDone(null)
}
} else {
toggleProjectLock(
document = document,
done = Consumer { onLockDone(it) },
busyIndicator = busyUi,
lockDuration = duration
)
}
private fun lockDurationHandler(document: LockableDocument, onLockDone: OnLockDone): LockDurationHandler {
return { duration ->
if (!duration.isZero || document.status.get().locked) {
toggleProjectLock(
document = document,
done = onLockDone,
busyIndicator = busyUi,
lockDuration = duration
)
} else {
onLockDone(null)
}
}
}

fun createLockSuggestionPane(document: LockableDocument, onLockDone: OnLockDone): Pane {
return lockPaneBuilder().run {
buildDialogPane(lockDurationHandler(document, onLockDone))
}
}

private fun toggleProjectLock(document: LockableDocument,
done: Consumer<JsonNode>,
done: OnLockDone,
busyIndicator: BusyUi,
lockDuration: Duration = Duration.ofMinutes(10)) {
busyIndicator(true)
document.toggleLocked(lockDuration)
.thenAccept { status ->
done.accept(status.raw!!)
done(status?.raw)
busyIndicator(false)
}
.exceptionally { ex ->
Expand All @@ -92,9 +101,32 @@ class DocPropertiesUi(val errorUi: ErrorUi, val busyUi: BusyUi) {

fun showDialog(document: LockableDocument, onLockDone: OnLockDone) {
Platform.runLater {
Dialog<Unit>().apply {
this.dialogPane = createLockSuggestionPane(document, onLockDone) as DialogPane
show()
val toggleGroup = ToggleGroup()
val lockDurationHandler = lockDurationHandler(document, onLockDone)
val vboxBuilder = VBoxBuilder().apply {
add(node = lockPaneBuilder().let {
it.toggleGroup = toggleGroup
it.buildPane()
}, alignment = Pos.CENTER, growth = Priority.ALWAYS)
add(node = Button("Lock").also {
it.addEventHandler(ActionEvent.ACTION) {
val selectedDuration = toggleGroup.selectedToggle.userData as Duration
lockDurationHandler(selectedDuration)
}
})
}
Dialog<Unit>().also {
it.dialogPane.apply {
content = vboxBuilder.vbox
styleClass.addAll("dlg-lock")
stylesheets.addAll("/biz/ganttproject/storage/cloud/GPCloudStorage.css", "/biz/ganttproject/storage/StorageDialog.css")

buttonTypes.add(ButtonType.OK)
lookupButton(ButtonType.OK).apply {
styleClass.add("btn-attention")
}
}
it.show()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class GPCloudBrowserPane(

fun onAction() {
selectedProject?.let { this@GPCloudBrowserPane.openDocument(it) }
?: this@GPCloudBrowserPane.createDocument(selectedTeam, paneElements!!.filenameInput.text)
?: this@GPCloudBrowserPane.createDocument(selectedTeam, paneElements.filenameInput.text)

}
}
Expand All @@ -186,11 +186,11 @@ class GPCloudBrowserPane(
)
}
},*/
itemActionFactory = Function {
if (it is ProjectJsonAsFolderItem) {
itemActionFactory = Function { folderItem ->
if (folderItem is ProjectJsonAsFolderItem) {
mapOf(
"history" to Consumer { item ->
this@GPCloudBrowserPane.loadHistory(it, builder.resultConsumer, builder.busyIndicatorToggler)
"history" to Consumer {
this@GPCloudBrowserPane.loadHistory(folderItem, builder.resultConsumer, builder.busyIndicatorToggler)
}
)
} else {
Expand Down Expand Up @@ -264,7 +264,7 @@ class GPCloudBrowserPane(
ActionOnLocked.OPEN -> {
openDocumentWithLock(document, document.projectJson.node["lock"])
if (notify.isSelected) {
document.status.addListener { status, oldValue, newValue ->
document.status.addListener { _, _, newValue ->
println("new value=$newValue")
if (!newValue.locked) {
Platform.runLater {
Expand Down

0 comments on commit 2ef1968

Please sign in to comment.