All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.simiacryptus.skyenet.TabbedDisplay.kt Maven / Gradle / Ivy

There is a newer version: 1.2.21
Show newest version
package com.simiacryptus.skyenet

import com.simiacryptus.skyenet.webui.session.SessionTask
import java.util.*

open class TabbedDisplay(
    val task: SessionTask,
    val tabs: MutableList> = mutableListOf(),
) {
    var selectedTab: Int = 0

    companion object {
        val log = org.slf4j.LoggerFactory.getLogger(TabbedDisplay::class.java)
    }

    val size: Int get() = tabs.size
    open fun render() = if(tabs.isEmpty()) "" else """
    
${renderTabButtons()} ${ tabs.toTypedArray().withIndex().joinToString("\n") { (idx, t) -> renderContentTab(t, idx) } }
""".trimIndent() val container: StringBuilder by lazy { log.debug("Initializing container with rendered content") task.add(render())!! } open fun renderTabButtons() = """
${ tabs.toTypedArray().withIndex().joinToString("\n") { (idx, pair) -> if (idx == selectedTab) { """""" } else { """""" } } }
""".trimIndent() open fun renderContentTab(t: Pair, idx: Int) = """
"" } }" data-tab="$idx">${t.second}
""".trimIndent() operator fun get(i: String) = tabs.toMap()[i] operator fun set(name: String, content: String) = when (val index = find(name)) { null -> { log.debug("Adding new tab: $name") val stringBuilder = StringBuilder(content) tabs.add(name to stringBuilder) update() stringBuilder } else -> { log.debug("Updating existing tab: $name") val stringBuilder = tabs[index].second stringBuilder.clear() stringBuilder.append(content) update() stringBuilder } } fun find(name: String) = tabs.withIndex().firstOrNull { it.value.first == name }?.index open fun label(i: Int): String { return "${tabs.size + 1}" } open fun clear() { log.debug("Clearing all tabs") tabs.clear() update() } open fun update() { log.debug("Updating container content") synchronized(container) { if (tabs.isNotEmpty() && (selectedTab < 0 || selectedTab >= tabs.size)) { selectedTab = 0 } container.clear() container.append(render()) } task.complete() } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy