Skip to content

Commit 2f5e8ad

Browse files
authored
Merge pull request #354 from alyssaruth/more-dialog-fun
More dialog fun
2 parents 8879f1d + 6b115c1 commit 2f5e8ad

11 files changed

Lines changed: 201 additions & 192 deletions

File tree

‎src/main/kotlin/dartzee/core/util/DialogUtil.kt‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dartzee.screen.ScreenCache
1111
import dartzee.utils.InjectedThings
1212
import java.awt.Component
1313
import java.io.File
14+
import javax.swing.JFileChooser
1415
import javax.swing.JOptionPane
1516
import javax.swing.SwingUtilities
1617

@@ -87,9 +88,23 @@ object DialogUtil {
8788

8889
fun showOption(title: String, message: String, options: List<String>): String? {
8990
logDialogShown("Option", title, message)
90-
val selectionStr = dialogFactory.showOption(title, message, options)
91-
logDialogClosed("Option", selectionStr)
92-
return selectionStr
91+
92+
val typedArray = (options + "Cancel").toTypedArray()
93+
val selection =
94+
JOptionPane.showOptionDialog(
95+
null,
96+
message,
97+
title,
98+
JOptionPane.DEFAULT_OPTION,
99+
JOptionPane.QUESTION_MESSAGE,
100+
null,
101+
typedArray,
102+
options.first(),
103+
)
104+
105+
val result = if (selection > -1) typedArray[selection] else null
106+
logDialogClosed("Option", result)
107+
return if (result == "Cancel") null else result
93108
}
94109

95110
fun <K> showInput(
@@ -106,7 +121,16 @@ object DialogUtil {
106121

107122
fun chooseDirectory(parent: Component?): File? {
108123
logDialogShown("File selector", "", "")
109-
val file = dialogFactory.chooseDirectory(parent)
124+
125+
val fc = JFileChooser()
126+
fc.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
127+
val option = fc.showDialog(parent, "Select")
128+
if (option != JFileChooser.APPROVE_OPTION) {
129+
logDialogClosed("File selector", null)
130+
return null
131+
}
132+
133+
val file = fc.selectedFile
110134
logDialogClosed("File selector", file?.absolutePath)
111135
return file
112136
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package dartzee.core.util
22

3-
import java.awt.Component
4-
import java.io.File
5-
63
interface IMessageDialogFactory {
7-
fun showOption(title: String, message: String, options: List<String>): String?
8-
94
fun <K> showInput(title: String, message: String, options: Array<K>?, defaultOption: K?): K?
10-
11-
fun chooseDirectory(parent: Component?): File?
125
}
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package dartzee.core.util
22

3-
import java.awt.Component
4-
import java.io.File
5-
import javax.swing.JFileChooser
63
import javax.swing.JOptionPane
74

85
class MessageDialogFactory : IMessageDialogFactory {
@@ -22,31 +19,4 @@ class MessageDialogFactory : IMessageDialogFactory {
2219
options,
2320
defaultOption,
2421
) as K?
25-
26-
override fun showOption(title: String, message: String, options: List<String>): String? {
27-
val typedArray = options.toTypedArray()
28-
val selection =
29-
JOptionPane.showOptionDialog(
30-
null,
31-
message,
32-
title,
33-
JOptionPane.DEFAULT_OPTION,
34-
JOptionPane.QUESTION_MESSAGE,
35-
null,
36-
typedArray,
37-
options.first(),
38-
)
39-
return if (selection > -1) typedArray[selection] else null
40-
}
41-
42-
override fun chooseDirectory(parent: Component?): File? {
43-
val fc = JFileChooser()
44-
fc.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
45-
val option = fc.showDialog(parent, "Select")
46-
if (option != JFileChooser.APPROVE_OPTION) {
47-
return null
48-
}
49-
50-
return fc.selectedFile
51-
}
5222
}

‎src/main/kotlin/dartzee/sync/SyncConfigurer.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class SyncConfigurer(private val dbStore: IRemoteDatabaseStore) {
1414
}
1515

1616
if (dbStore.databaseExists(remoteName)) {
17-
val options = listOf("Overwrite local data", "Sync with local data", "Cancel")
17+
val options = listOf("Overwrite local data", "Sync with local data")
1818
val response =
1919
DialogUtil.showOption(
2020
"Database found",
2121
"Shared database '$remoteName' already exists. How would you like to proceed?",
2222
options,
2323
)
2424

25-
if (response == null || response == "Cancel") {
25+
if (response == null) {
2626
return null
2727
}
2828

@@ -31,15 +31,15 @@ class SyncConfigurer(private val dbStore: IRemoteDatabaseStore) {
3131
else SyncMode.NORMAL_SYNC
3232
return SyncConfig(choice, remoteName)
3333
} else {
34-
val options = listOf("Create '$remoteName'", "Cancel")
34+
val options = listOf("Create '$remoteName'")
3535
val response =
3636
DialogUtil.showOption(
3737
"Database not found",
3838
"No shared database found called '$remoteName'. Would you like to create it?",
3939
options,
4040
)
4141

42-
if (response == null || response == "Cancel") {
42+
if (response == null) {
4343
return null
4444
}
4545

‎src/test/kotlin/dartzee/TestUtils.kt‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dartzee
22

3+
import com.github.alyssaburlton.swingtest.clickCancel
34
import com.github.alyssaburlton.swingtest.clickChild
45
import com.github.alyssaburlton.swingtest.clickOk
56
import com.github.alyssaburlton.swingtest.clickYes
@@ -68,6 +69,7 @@ import javax.swing.JTextField
6869
import javax.swing.SwingUtilities
6970
import javax.swing.table.DefaultTableModel
7071
import javax.swing.text.JTextComponent
72+
import kotlin.jvm.javaClass
7173

7274
val bullseye = DartboardSegment(SegmentType.DOUBLE, 25)
7375
val outerBull = DartboardSegment(SegmentType.OUTER_SINGLE, 25)
@@ -213,13 +215,20 @@ fun GameplayDartboard.throwDartByClick(
213215

214216
fun GameplayDartboard.segmentStatuses() = getChild<PresentationDartboard>().segmentStatuses
215217

216-
fun getFileChooser() = getWindow<JDialog> { it.title == "Open" }
218+
fun getFileChooser(title: String) = getWindow<JDialog> { it.title == title }
217219

218220
fun <T> List<T>.only(): T {
219221
size shouldBe 1
220222
return first()
221223
}
222224

225+
fun selectFile(path: String, title: String) {
226+
val chooserDialog = getFileChooser(title)
227+
chooserDialog.getChild<JTextComponent>().typeText(path)
228+
chooserDialog.clickChild<JButton>(text = title)
229+
flushEdt()
230+
}
231+
223232
fun PlayerImageDialog.selectImage(playerImageId: String) {
224233
getChild<JTabbedPane>().selectTab<JPanel>("uploadTab")
225234

@@ -232,11 +241,8 @@ fun PlayerImageDialog.selectImage(playerImageId: String) {
232241
fun FileUploader.uploadFileFromResource(resourceName: String) {
233242
clickChild<JButton>(text = "...", async = true)
234243

235-
val chooserDialog = getFileChooser()
236244
val rsrcPath = javaClass.getResource(resourceName)!!.path
237-
chooserDialog.getChild<JTextComponent>().typeText(rsrcPath)
238-
chooserDialog.clickChild<JButton>(text = "Open")
239-
flushEdt()
245+
selectFile(rsrcPath, "Open")
240246

241247
getChild<JTextField>().text shouldBe File(rsrcPath).path
242248
clickChild<JButton>(text = "Upload", async = true)
@@ -271,20 +277,40 @@ fun findLoadingDialog(text: String) = findWindow<LoadingDialog> { it.message ==
271277

272278
private fun getInfoDialog() = findInfoDialog()!!
273279

274-
private fun findInfoDialog() = findOptionPaneDialog("Information")
280+
fun findInfoDialog(predicate: (window: JDialog) -> Boolean = { true }) =
281+
findOptionPaneDialog("Information", predicate)
275282

276-
fun getQuestionDialog() = findQuestionDialog()!!
283+
fun getQuestionDialog() = getOptionPaneDialog("Question")
277284

278285
fun findQuestionDialog() = findOptionPaneDialog("Question")
279286

280287
fun getErrorDialog(predicate: (window: JDialog) -> Boolean = { true }) =
281-
findErrorDialog(predicate)!!
288+
getOptionPaneDialog("Error", predicate)
282289

283290
fun findErrorDialog(predicate: (window: JDialog) -> Boolean = { true }) =
284291
findOptionPaneDialog("Error", predicate)
285292

286293
fun waitForQuestionDialog(): JDialog = waitForWindow<JDialog> { it.title == "Question" }
287294

295+
fun selectFromOptionDialog(title: String, selection: String) {
296+
val dialog = getOptionPaneDialog(title) { it.isVisible }
297+
dialog.clickButton(text = selection, async = true)
298+
}
299+
300+
fun cancelOptionDialog(title: String) {
301+
val dialog = getOptionPaneDialog(title) { it.isVisible }
302+
dialog.clickCancel(async = true)
303+
}
304+
305+
fun dismissOptionDialog(title: String) {
306+
val dialog = getOptionPaneDialog(title) { it.isVisible }
307+
dialog.dispose()
308+
flushEdt()
309+
}
310+
311+
private fun getOptionPaneDialog(title: String, predicate: (window: JDialog) -> Boolean = { true }) =
312+
getWindow<JDialog> { it.title == title && predicate(it) }
313+
288314
private fun findOptionPaneDialog(
289315
title: String,
290316
predicate: (window: JDialog) -> Boolean = { true },

‎src/test/kotlin/dartzee/bean/FileUploaderTest.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class FileUploaderTest : AbstractTest() {
4848

4949
uploader.clickChild<JButton>(text = "...", async = true)
5050

51-
val chooserDialog = getFileChooser()
51+
val chooserDialog = getFileChooser("Open")
5252
chooserDialog.clickCancel()
5353

5454
InjectedThings.preferenceService.find(Preferences.imageUploadDirectory) shouldBe null
@@ -93,7 +93,7 @@ class FileUploaderTest : AbstractTest() {
9393

9494
uploader.clickChild<JButton>(text = "...", async = true)
9595

96-
val chooserDialog = getFileChooser()
96+
val chooserDialog = getFileChooser("Open")
9797
val combo =
9898
chooserDialog.getChild<JComboBox<FileNameExtensionFilter>> {
9999
it.selectedItem is FileNameExtensionFilter
@@ -121,7 +121,7 @@ class FileUploaderTest : AbstractTest() {
121121
uploaderTwo.getChild<JTextField>().text shouldBe rsrcDirectory
122122

123123
uploaderTwo.clickChild<JButton>(text = "...", async = true)
124-
val chooser = getFileChooser()
124+
val chooser = getFileChooser("Open")
125125
val combo = chooser.getChild<JComboBox<File>> { it.selectedItem is File }
126126
combo.selectedItemTyped() shouldBe File(rsrcDirectory)
127127
}
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
package dartzee.core.helper
22

33
import dartzee.core.util.IMessageDialogFactory
4-
import java.awt.Component
5-
import java.io.File
64

75
class TestMessageDialogFactory : IMessageDialogFactory {
8-
// Directory
9-
var directoryToSelect: File? = null
10-
116
// Inputs
127
var inputSelection: Any? = null
138
var inputOptionsPresented: Array<*>? = null
149
val inputsShown = mutableListOf<String>()
1510

16-
val optionSequence = mutableListOf<String?>()
17-
val optionsShown = mutableListOf<String>()
18-
1911
override fun <K> showInput(
2012
title: String,
2113
message: String,
@@ -38,19 +30,8 @@ class TestMessageDialogFactory : IMessageDialogFactory {
3830
)
3931
}
4032

41-
override fun showOption(title: String, message: String, options: List<String>): String? {
42-
optionsShown.add(message)
43-
val selection = optionSequence.removeAt(0)
44-
return selection
45-
}
46-
47-
override fun chooseDirectory(parent: Component?) = directoryToSelect
48-
4933
fun reset() {
5034
inputsShown.clear()
5135
inputOptionsPresented = arrayOf<Any>()
52-
optionSequence.clear()
53-
optionsShown.clear()
54-
directoryToSelect = null
5536
}
5637
}

0 commit comments

Comments
 (0)