view src/name/blackcap/imageprep/HelpDialog.kt @ 7:801cdc780ca8

Work around ImageIO bug.
author David Barts <n5jrn@me.com>
date Fri, 17 Jul 2020 19:56:25 -0700
parents 0bded24f746e
children
line wrap: on
line source

/*
 * The dialog that displays the Exif data in a single file (display only,
 * no changing). We do this after washing.
 */
package name.blackcap.imageprep

import java.awt.Dimension
import java.awt.Font
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.*
import javax.swing.text.html.HTMLEditorKit
import javax.swing.text.html.StyleSheet
import kotlin.text.buildString

class HelpDialog : JDialog(Application.mainFrame) {
    private val BW = 9
    private val BW2 = BW * 2
    private val WIDTH = 640
    private val HEIGHT = 480

    private val dismissButton = JButton("Dismiss").also {
        it.addActionListener(ActionListener { setVisible(false) })
    }

    /*
     * The stock HTMLEditorKit shares all style sheet data between all its
     * instances. How unbelievably braindamaged. Correct that.
     */
    private class MyEditorKit: HTMLEditorKit() {
        private var _styleSheet: StyleSheet = defaultStyleSheet
        override fun getStyleSheet() = _styleSheet
        override fun setStyleSheet(value: StyleSheet) {
            _styleSheet = value
        }

        /**
         * Return the default style sheet that all HTMLEditorKit's come with.
         */
        val defaultStyleSheet: StyleSheet
        get() {
            return super.getStyleSheet()
        }
    }

    private val helpPane = JScrollPane(JTextPane().also {
        it.editorKit = MyEditorKit().also { ek ->
            UIManager.getFont("Panel.font")?.let { pFont ->
                ek.styleSheet = StyleSheet().apply {
                    addRule("body { font-family: \"${pFont.family}\"; font-size: ${pFont.size}; }")
                    addStyleSheet(ek.defaultStyleSheet)
                }
            }
        }
        val rawText = this::class.java.getResourceAsStream("help.html").bufferedReader().use { it.readText() }
        it.text = rawText.replace("%CONFIG_FILE_NAME%", buildString {
            for (ch in PROP_FILE.canonicalPath) {
                append(when(ch) {
                    '<' -> "&lt;"
                    '>' -> "&gt;"
                    '&' -> "&amp;"
                    else -> ch
                })
            }
        })
        it.setEditable(false)
    }).apply {
        alignmentX = JScrollPane.CENTER_ALIGNMENT
        addBorder(BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2))
        verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
        horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
        preferredSize = Dimension(WIDTH, HEIGHT)
        background = Application.mainFrame.background
    }

    override fun setVisible(vis: Boolean) {
        if (vis)
            helpPane.verticalScrollBar.run { value = minimum }
        super.setVisible(vis)
    }

    init {
        setVisible(false)
        title = "Help"
        contentPane.apply {
            layout = BoxLayout(this, BoxLayout.Y_AXIS)
            add(helpPane)
            add(Box(BoxLayout.X_AXIS).apply {
                alignmentX = Box.CENTER_ALIGNMENT
                border = BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2)
                add(Box.createHorizontalGlue())
                add(dismissButton)
            })
        }
        pack()
    }
}