-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a Swing Realm and render image
- Loading branch information
Simon Scholz
committed
Oct 31, 2023
1 parent
b066b8f
commit a851c61
Showing
9 changed files
with
166 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,66 @@ | ||
package io.github.simonscholz | ||
|
||
import io.github.simonscholz.model.QrCodeConfigViewModel | ||
import io.github.simonscholz.observables.SwingRealm | ||
import io.github.simonscholz.qrcode.QrCodeConfig | ||
import io.github.simonscholz.qrcode.QrCodeFactory | ||
import io.github.simonscholz.ui.ImageUI | ||
import io.github.simonscholz.ui.MainUI | ||
import io.github.simonscholz.ui.PropertiesUI | ||
import java.awt.event.WindowAdapter | ||
import java.awt.event.WindowEvent | ||
import java.awt.image.BufferedImage | ||
import org.eclipse.core.databinding.DataBindingContext | ||
import javax.swing.JFrame | ||
import javax.swing.SwingUtilities | ||
|
||
fun main() { | ||
val frame = JFrame("QR Code AWT/Swing UI") | ||
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE | ||
val dataBindingContext = DataBindingContext() | ||
frame.addWindowListener(object : java.awt.event.WindowAdapter() { | ||
override fun windowClosing(windowEvent: java.awt.event.WindowEvent?) { | ||
dataBindingContext.dispose() | ||
SwingUtilities.invokeLater { | ||
val frame = JFrame("QR Code AWT/Swing UI") | ||
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE | ||
val dataBindingContext = DataBindingContext(SwingRealm()) | ||
frame.addWindowListener( | ||
object : WindowAdapter() { | ||
override fun windowClosing(windowEvent: WindowEvent) { | ||
dataBindingContext.dispose() | ||
} | ||
}, | ||
) | ||
|
||
val qrCodeConfigViewModel = QrCodeConfigViewModel() | ||
|
||
val (imagePanel, setImage) = ImageUI.createImagePanel() | ||
val (propertiesPanel, applyOnChange) = PropertiesUI.createPropertiesUI(qrCodeConfigViewModel, dataBindingContext) { | ||
// TODO render new qr code image | ||
if(qrCodeConfigViewModel.qrCodeContent.value.isNotBlank()) { | ||
val qrCodeImage = renderImage(qrCodeConfigViewModel) | ||
setImage(qrCodeImage) | ||
} | ||
} | ||
},) | ||
|
||
val qrCodeConfigViewModel = QrCodeConfigViewModel() | ||
val mainPanel = MainUI.createMainPanel(imagePanel, propertiesPanel) | ||
frame.add(mainPanel) | ||
|
||
val imagePanel = ImageUI.createImagePanel() | ||
val propertiesPanel = PropertiesUI.createPropertiesUI(qrCodeConfigViewModel, dataBindingContext) | ||
frame.pack() | ||
frame.isVisible = true | ||
|
||
val mainPanel = MainUI.createMainPanel(imagePanel, propertiesPanel) | ||
frame.add(mainPanel) | ||
dataBindingContext.bindings.forEach { | ||
it.model.addChangeListener { | ||
if(applyOnChange()) { | ||
// TODO render new qr code image | ||
if(qrCodeConfigViewModel.qrCodeContent.value.isNotBlank()) { | ||
val qrCodeImage = renderImage(qrCodeConfigViewModel) | ||
setImage(qrCodeImage) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
frame.pack() | ||
frame.isVisible = true | ||
private fun renderImage(qrCodeConfigViewModel: QrCodeConfigViewModel): BufferedImage { | ||
// TODO render new qr code image | ||
println(qrCodeConfigViewModel) | ||
val qrCodeConfig = QrCodeConfig.Builder(qrCodeConfigViewModel.qrCodeContent.value).qrCodeSize(qrCodeConfigViewModel.size.value).build() | ||
return QrCodeFactory.createQrCodeApi().createQrCodeImage(qrCodeConfig) | ||
} |
4 changes: 4 additions & 0 deletions
4
kotlin-app/src/main/kotlin/io/github/simonscholz/extension/BindingExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package io.github.simonscholz.extension | ||
|
||
import io.github.simonscholz.observables.DocumentObservable | ||
import io.github.simonscholz.observables.JSpinnerIntObservable | ||
import org.eclipse.core.databinding.observable.value.IObservableValue | ||
import javax.swing.JSpinner | ||
import javax.swing.text.JTextComponent | ||
|
||
fun JTextComponent.toObservable(): IObservableValue<String> = | ||
DocumentObservable(this) | ||
|
||
fun JSpinner.toIntObservable(): IObservableValue<Int> = JSpinnerIntObservable(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 30 additions & 21 deletions
51
kotlin-app/src/main/kotlin/io/github/simonscholz/observables/DocumentObservable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
package io.github.simonscholz.observables | ||
|
||
import org.eclipse.core.databinding.observable.Diffs | ||
import org.eclipse.core.databinding.observable.value.AbstractObservableValue | ||
import javax.swing.event.DocumentEvent | ||
import javax.swing.event.DocumentListener | ||
import javax.swing.text.Document | ||
import javax.swing.text.JTextComponent | ||
import org.eclipse.core.databinding.observable.Diffs | ||
import org.eclipse.core.databinding.observable.value.AbstractObservableValue | ||
|
||
class DocumentObservable(private val document: Document) : AbstractObservableValue<String>() { | ||
private var documentValue = document.getText(0, document.length) | ||
|
||
constructor(jTextComponent: JTextComponent) : this(jTextComponent.document) | ||
|
||
init { | ||
document.addDocumentListener(object : DocumentListener { | ||
override fun changedUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
|
||
override fun insertUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
|
||
override fun removeUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
},) | ||
document.addDocumentListener( | ||
object : DocumentListener { | ||
override fun changedUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
|
||
override fun insertUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
|
||
override fun removeUpdate(e: DocumentEvent) { | ||
val oldValue = documentValue | ||
documentValue = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, documentValue)) | ||
} | ||
}, | ||
) | ||
} | ||
override fun getValueType(): Any = String::class.java | ||
|
||
override fun doGetValue(): String = | ||
document.getText(0, document.length) | ||
|
||
override fun doSetValue(value: String?) { | ||
if (documentValue !== value) { | ||
document.remove(0, document.length) | ||
document.insertString(0, value, null) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
kotlin-app/src/main/kotlin/io/github/simonscholz/observables/JSpinnerObservable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.simonscholz.observables | ||
|
||
import org.eclipse.core.databinding.observable.Diffs | ||
import org.eclipse.core.databinding.observable.value.AbstractObservableValue | ||
import javax.swing.JSpinner | ||
|
||
class JSpinnerIntObservable( | ||
private val jSpinner: JSpinner, | ||
) : AbstractObservableValue<Int>() { | ||
|
||
private var value: Int = jSpinner.value as Int | ||
|
||
init { | ||
jSpinner.addChangeListener { | ||
val oldValue = value | ||
value = doGetValue() | ||
fireValueChange(Diffs.createValueDiff(oldValue, value)) | ||
} | ||
} | ||
|
||
override fun doGetValue(): Int = | ||
jSpinner.value as Int | ||
|
||
override fun getValueType(): Any = Int::class.java | ||
|
||
override fun doSetValue(value: Int?) { | ||
jSpinner.value = value | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
kotlin-app/src/main/kotlin/io/github/simonscholz/observables/SwingRealm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.simonscholz.observables | ||
|
||
import javax.swing.SwingUtilities | ||
import org.eclipse.core.databinding.observable.Realm | ||
|
||
class SwingRealm: Realm() { | ||
|
||
init { | ||
setDefault(this) | ||
} | ||
override fun isCurrent(): Boolean = | ||
SwingUtilities.isEventDispatchThread() | ||
|
||
override fun syncExec(runnable: Runnable) = | ||
SwingUtilities.invokeAndWait(runnable) | ||
|
||
override fun asyncExec(runnable: Runnable) = | ||
SwingUtilities.invokeLater(runnable) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters