mirror of
https://github.com/TermoraDev/termora.git
synced 2026-01-15 18:02:58 +08:00
fix: editor plugin popup
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
project.version = "0.0.5"
|
project.version = "0.0.6"
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.awt.event.WindowAdapter
|
|||||||
import java.awt.event.WindowEvent
|
import java.awt.event.WindowEvent
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import javax.swing.JComponent
|
import javax.swing.JComponent
|
||||||
import javax.swing.JOptionPane
|
import javax.swing.JOptionPane
|
||||||
import javax.swing.UIManager
|
import javax.swing.UIManager
|
||||||
@@ -22,6 +23,7 @@ class EditorDialog(file: Path, owner: Window, private val myDisposable: Disposab
|
|||||||
private val filename = file.name
|
private val filename = file.name
|
||||||
private val filepath = File(file.absolutePathString())
|
private val filepath = File(file.absolutePathString())
|
||||||
private val editorPanel = EditorPanel(this, filepath)
|
private val editorPanel = EditorPanel(this, filepath)
|
||||||
|
private val disposed = AtomicBoolean()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
size = Dimension(UIManager.getInt("Dialog.width"), UIManager.getInt("Dialog.height"))
|
size = Dimension(UIManager.getInt("Dialog.width"), UIManager.getInt("Dialog.height"))
|
||||||
@@ -42,21 +44,28 @@ class EditorDialog(file: Path, owner: Window, private val myDisposable: Disposab
|
|||||||
|
|
||||||
|
|
||||||
private fun initEvents() {
|
private fun initEvents() {
|
||||||
|
|
||||||
addWindowListener(object : WindowAdapter() {
|
addWindowListener(object : WindowAdapter() {
|
||||||
override fun windowClosing(e: WindowEvent?) {
|
override fun windowClosing(e: WindowEvent?) {
|
||||||
doCancelAction()
|
if (disposed.compareAndSet(false, true)) {
|
||||||
|
doCancelAction()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
Disposer.register(myDisposable, object : Disposable {
|
Disposer.register(myDisposable, object : Disposable {
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
doCancelAction()
|
if (disposed.compareAndSet(false, true)) {
|
||||||
|
doCancelAction()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
Disposer.register(disposable, object : Disposable {
|
Disposer.register(disposable, object : Disposable {
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
Disposer.dispose(myDisposable)
|
if (disposed.compareAndSet(false, true)) {
|
||||||
|
Disposer.dispose(myDisposable)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import javax.swing.*
|
|||||||
import javax.swing.SwingConstants.VERTICAL
|
import javax.swing.SwingConstants.VERTICAL
|
||||||
import javax.swing.event.DocumentEvent
|
import javax.swing.event.DocumentEvent
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
class EditorPanel(private val window: JDialog, private val file: File) : JPanel(BorderLayout()) {
|
class EditorPanel(private val window: JDialog, private val file: File) : JPanel(BorderLayout()) {
|
||||||
|
|
||||||
@@ -212,28 +213,7 @@ class EditorPanel(private val window: JDialog, private val file: File) : JPanel(
|
|||||||
|
|
||||||
textArea.actionMap.put("Format", object : AbstractAction() {
|
textArea.actionMap.put("Format", object : AbstractAction() {
|
||||||
override fun actionPerformed(e: ActionEvent) {
|
override fun actionPerformed(e: ActionEvent) {
|
||||||
if (textArea.syntaxEditingStyle == SyntaxConstants.SYNTAX_STYLE_JSON) {
|
format()
|
||||||
runCatching {
|
|
||||||
val json = prettyJson.parseToJsonElement(textArea.text)
|
|
||||||
textArea.text = prettyJson.encodeToString(json)
|
|
||||||
}.onFailure {
|
|
||||||
if (log.isErrorEnabled) {
|
|
||||||
log.error(it.message, it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (textArea.syntaxEditingStyle == SyntaxConstants.SYNTAX_STYLE_XML) {
|
|
||||||
runCatching {
|
|
||||||
val document = SAXReader().read(StringReader(textArea.text))
|
|
||||||
val sw = StringWriter()
|
|
||||||
val writer = XMLWriter(sw, OutputFormat.createPrettyPrint())
|
|
||||||
writer.write(document)
|
|
||||||
textArea.text = sw.toString()
|
|
||||||
}.onFailure {
|
|
||||||
if (log.isErrorEnabled) {
|
|
||||||
log.error(it.message, it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -265,12 +245,59 @@ class EditorPanel(private val window: JDialog, private val file: File) : JPanel(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
prettyBtn.addActionListener(searchTextField.actionMap.get("Format"))
|
prettyBtn.addActionListener(textArea.actionMap.get("Format"))
|
||||||
|
|
||||||
prevBtn.addActionListener { search(false) }
|
prevBtn.addActionListener { search(false) }
|
||||||
nextBtn.addActionListener { search(true) }
|
nextBtn.addActionListener { search(true) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun format() {
|
||||||
|
val vertical = scrollPane.verticalScrollBar.value
|
||||||
|
val horizontal = scrollPane.horizontalScrollBar.value
|
||||||
|
val caretPosition = textArea.caretPosition
|
||||||
|
|
||||||
|
val c = if (textArea.syntaxEditingStyle == SyntaxConstants.SYNTAX_STYLE_JSON) {
|
||||||
|
runCatching {
|
||||||
|
val json = prettyJson.parseToJsonElement(textArea.text)
|
||||||
|
textArea.text = prettyJson.encodeToString(json)
|
||||||
|
}.onFailure {
|
||||||
|
if (log.isErrorEnabled) {
|
||||||
|
log.error(it.message, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (textArea.syntaxEditingStyle == SyntaxConstants.SYNTAX_STYLE_XML) {
|
||||||
|
runCatching {
|
||||||
|
val document = SAXReader().read(StringReader(textArea.text))
|
||||||
|
val sw = StringWriter()
|
||||||
|
val writer = XMLWriter(sw, OutputFormat.createPrettyPrint())
|
||||||
|
writer.write(document)
|
||||||
|
textArea.text = sw.toString()
|
||||||
|
}.onFailure {
|
||||||
|
if (log.isErrorEnabled) {
|
||||||
|
log.error(it.message, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
} ?: return
|
||||||
|
|
||||||
|
c.onSuccess {
|
||||||
|
SwingUtilities.invokeLater {
|
||||||
|
scrollPane.verticalScrollBar.value = min(
|
||||||
|
vertical,
|
||||||
|
scrollPane.verticalScrollBar.maximum
|
||||||
|
)
|
||||||
|
scrollPane.horizontalScrollBar.value = min(
|
||||||
|
horizontal,
|
||||||
|
scrollPane.horizontalScrollBar.maximum
|
||||||
|
)
|
||||||
|
if (caretPosition >= 0 && caretPosition < textArea.document.length) {
|
||||||
|
textArea.caretPosition = caretPosition
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun search(searchForward: Boolean = true) {
|
private fun search(searchForward: Boolean = true) {
|
||||||
textArea.clearMarkAllHighlights()
|
textArea.clearMarkAllHighlights()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user