Skip to content

Commit 2396ff7

Browse files
committed
Miscellaneous extra fixes
1 parent 4e492c0 commit 2396ff7

File tree

7 files changed

+88
-112
lines changed

7 files changed

+88
-112
lines changed

src/main/kotlin/io/github/inductiveautomation/kindling/idb/ImagesTab.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.github.inductiveautomation.kindling.utils.transferTo
2424
import io.github.inductiveautomation.kindling.utils.treeCellRenderer
2525
import kotlinx.coroutines.CoroutineScope
2626
import kotlinx.coroutines.Dispatchers
27+
import kotlinx.coroutines.async
2728
import kotlinx.coroutines.launch
2829
import kotlinx.coroutines.withContext
2930
import net.miginfocom.swing.MigLayout
@@ -133,7 +134,7 @@ class ImagesPanel(connection: IdbConnection) : ToolPanel("ins 0, fill, hidemode
133134
tree.addTreeSelectionListener { event ->
134135
when (val node = event.newLeadSelectionPath?.lastPathComponent) {
135136
is ImageNode -> {
136-
EDT_SCOPE.launch {
137+
EDT_SCOPE.async {
137138
val imageRow = node.userObject
138139

139140
imageDisplay.icon = throbber
@@ -153,6 +154,12 @@ class ImagesPanel(connection: IdbConnection) : ToolPanel("ins 0, fill, hidemode
153154
imageRow.description?.let { append(" ($it)") }
154155
append(" [").append(fileSize).append("]")
155156
}
157+
}.invokeOnCompletion {
158+
if (it != null) {
159+
imageDisplay.icon = null
160+
imageInfo.text = "Error loading image: ${it.message}"
161+
it.printStackTrace(System.err)
162+
}
156163
}
157164
}
158165

src/main/kotlin/io/github/inductiveautomation/kindling/statistics/GatewayBackup.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class GatewayBackup(path: Path) {
2626

2727
val projectsDirectory: Path = root.resolve(PROJECTS)
2828

29+
val configDirectory: Path = root.resolve(CONFIG)
30+
2931
private val tempFile: Path = createTempFile("gwbk-stats", "idb")
3032

3133
// eagerly copy out the IDB, since we're always building the statistics view anyways
@@ -55,5 +57,6 @@ class GatewayBackup(path: Path) {
5557
private const val REDUNDANCY = "redundancy.xml"
5658
private const val IGNITION_CONF = "ignition.conf"
5759
private const val PROJECTS = "projects"
60+
private const val CONFIG = "config"
5861
}
5962
}

src/main/kotlin/io/github/inductiveautomation/kindling/utils/Swing.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import com.github.weisj.jsvg.SVGDocument
55
import com.github.weisj.jsvg.view.ViewBox
66
import kotlinx.coroutines.CoroutineScope
77
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.SupervisorJob
89
import kotlinx.coroutines.launch
10+
import kotlinx.coroutines.plus
911
import kotlinx.coroutines.swing.Swing
1012
import org.jdesktop.swingx.decorator.AbstractHighlighter
1113
import org.jdesktop.swingx.decorator.ColorHighlighter
@@ -44,7 +46,7 @@ import javax.swing.text.Document
4446
/**
4547
* A common CoroutineScope bound to the event dispatch thread (see [Dispatchers.Swing]).
4648
*/
47-
val EDT_SCOPE by lazy { CoroutineScope(Dispatchers.Swing) }
49+
val EDT_SCOPE: CoroutineScope = CoroutineScope(Dispatchers.Swing) + SupervisorJob()
4850

4951
val menuShortcutKeyMaskEx = Toolkit.getDefaultToolkit().menuShortcutKeyMaskEx
5052

src/main/kotlin/io/github/inductiveautomation/kindling/utils/Trees.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ abstract class AbstractTreeNode : TreeNode {
3131
yieldAll(child.depthFirstChildren())
3232
}
3333
}
34+
35+
fun sortWith(comparator: Comparator<in TreeNode>, recursive: Boolean = false) {
36+
children.sortWith(comparator)
37+
if (recursive) {
38+
for (child in children) {
39+
(child as? AbstractTreeNode)?.sortWith(comparator, recursive = true)
40+
}
41+
}
42+
}
3443
}
3544

3645
abstract class TypedTreeNode<T> : AbstractTreeNode() {

src/main/kotlin/io/github/inductiveautomation/kindling/utils/ZipFileTree.kt

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.github.inductiveautomation.kindling.utils
22

33
import com.jidesoft.comparator.AlphanumComparator
4+
import com.jidesoft.swing.TreeSearchable
45
import io.github.inductiveautomation.kindling.core.Tool
56
import java.nio.file.FileSystem
67
import java.nio.file.Path
78
import javax.swing.JTree
89
import javax.swing.tree.DefaultTreeModel
910
import javax.swing.tree.TreeModel
11+
import javax.swing.tree.TreeNode
12+
import javax.swing.tree.TreePath
1013
import kotlin.io.path.ExperimentalPathApi
1114
import kotlin.io.path.PathWalkOption.INCLUDE_DIRECTORIES
1215
import kotlin.io.path.div
@@ -16,18 +19,16 @@ import kotlin.io.path.name
1619
import kotlin.io.path.walk
1720

1821
data class PathNode(override val userObject: Path) : TypedTreeNode<Path>() {
19-
override fun toString(): String {
20-
return userObject.name
22+
override fun isLeaf(): Boolean {
23+
return super.isLeaf() || !userObject.isDirectory()
2124
}
2225
}
2326

2427
@OptIn(ExperimentalPathApi::class)
2528
class RootNode(zipFile: FileSystem) : AbstractTreeNode() {
2629
init {
27-
val pathComparator = compareBy<Path> { it.isDirectory() }.thenBy(AlphanumComparator()) { it.name }
2830
val zipFilePaths = zipFile.rootDirectories.asSequence()
2931
.flatMap { it.walk(INCLUDE_DIRECTORIES) }
30-
.sortedWith(pathComparator)
3132

3233
val seen = mutableMapOf<Path, PathNode>()
3334
for (path in zipFilePaths) {
@@ -43,6 +44,19 @@ class RootNode(zipFile: FileSystem) : AbstractTreeNode() {
4344
lastSeen = next
4445
}
4546
}
47+
48+
sortWith(comparator, recursive = true)
49+
}
50+
51+
companion object {
52+
private val comparator = compareBy<TreeNode> { node ->
53+
node as AbstractTreeNode
54+
val isDir = node.children.isNotEmpty() || (node as? PathNode)?.userObject?.isDirectory() == true
55+
!isDir
56+
}.thenBy(AlphanumComparator(false)) { node ->
57+
val path = (node as? PathNode)?.userObject
58+
path?.name.orEmpty()
59+
}
4660
}
4761
}
4862

@@ -54,11 +68,11 @@ class ZipFileTree(fileSystem: FileSystem) : JTree(ZipFileModel(fileSystem)) {
5468
setShowsRootHandles(true)
5569

5670
setCellRenderer(
57-
treeCellRenderer { _, value, selected, _, _, _, _ ->
71+
treeCellRenderer { _, value, _, _, _, _, _ ->
5872
if (value is PathNode) {
5973
val path = value.userObject
6074
toolTipText = path.toString()
61-
text = path.last().toString()
75+
text = path.name
6276
icon = if (path.isRegularFile()) {
6377
Tool.find(path)?.icon?.derive(ACTION_ICON_SCALE_FACTOR) ?: icon
6478
} else {
@@ -68,6 +82,20 @@ class ZipFileTree(fileSystem: FileSystem) : JTree(ZipFileModel(fileSystem)) {
6882
this
6983
},
7084
)
85+
86+
object : TreeSearchable(this) {
87+
init {
88+
isRecursive = true
89+
isRepeats = true
90+
}
91+
92+
override fun convertElementToString(element: Any?): String {
93+
return when (val node = (element as? TreePath)?.lastPathComponent) {
94+
is PathNode -> node.userObject.name
95+
else -> ""
96+
}
97+
}
98+
}
7199
}
72100

73101
override fun getModel(): ZipFileModel? = super.getModel() as ZipFileModel?

src/main/kotlin/io/github/inductiveautomation/kindling/zip/views/FileView.kt

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@ import io.github.inductiveautomation.kindling.core.Theme.Companion.theme
66
import io.github.inductiveautomation.kindling.utils.FlatActionIcon
77
import io.github.inductiveautomation.kindling.utils.configureCellRenderer
88
import io.github.inductiveautomation.kindling.utils.toHumanReadableBinary
9-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.CSS
10-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.INI
11-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.JSON
12-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.Markdown
13-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.Plaintext
14-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.Properties
15-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.Python
16-
import io.github.inductiveautomation.kindling.zip.views.FileView.SyntaxStyle.XML
9+
import io.github.inductiveautomation.kindling.zip.views.FileView.Companion.SyntaxStyle.JSON
10+
import io.github.inductiveautomation.kindling.zip.views.FileView.Companion.SyntaxStyle.Plaintext
1711
import kotlinx.serialization.ExperimentalSerializationApi
1812
import kotlinx.serialization.json.Json
1913
import kotlinx.serialization.json.JsonElement
@@ -68,28 +62,6 @@ class FileView(override val provider: FileSystemProvider, override val path: Pat
6862
}
6963
}
7064

71-
private enum class SyntaxStyle(val style: String) {
72-
Plaintext(SYNTAX_STYLE_NONE),
73-
CSS(SYNTAX_STYLE_CSS),
74-
CSV(SYNTAX_STYLE_CSV),
75-
Dockerfile(SYNTAX_STYLE_DOCKERFILE),
76-
HTML(SYNTAX_STYLE_HTML),
77-
INI(SYNTAX_STYLE_INI),
78-
Java(SYNTAX_STYLE_JAVA),
79-
Javascript(SYNTAX_STYLE_JAVASCRIPT),
80-
JSON(SYNTAX_STYLE_JSON),
81-
Kotlin(SYNTAX_STYLE_KOTLIN),
82-
Markdown(SYNTAX_STYLE_MARKDOWN),
83-
Properties(SYNTAX_STYLE_PROPERTIES_FILE),
84-
Python(SYNTAX_STYLE_PYTHON),
85-
SQL(SYNTAX_STYLE_SQL),
86-
Typescript(SYNTAX_STYLE_TYPESCRIPT),
87-
Shell(SYNTAX_STYLE_UNIX_SHELL),
88-
Batch(SYNTAX_STYLE_WINDOWS_BATCH),
89-
XML(SYNTAX_STYLE_XML),
90-
YAML(SYNTAX_STYLE_YAML),
91-
}
92-
9365
private val syntaxCombo = JComboBox(SyntaxStyle.entries.toTypedArray()).apply {
9466
selectedItem = guessSyntaxScheme()
9567

@@ -167,20 +139,35 @@ class FileView(override val provider: FileSystemProvider, override val path: Pat
167139
prettyPrintIndent = " "
168140
}
169141

170-
private val KNOWN_EXTENSIONS: Map<String, SyntaxStyle> = mapOf(
171-
"conf" to Properties,
172-
"properties" to Properties,
173-
"py" to Python,
174-
"json" to JSON,
175-
"svg" to XML,
176-
"xml" to XML,
177-
"css" to CSS,
178-
"txt" to Plaintext,
179-
"md" to Markdown,
180-
"p7b" to Plaintext,
181-
"log" to Plaintext,
182-
"ini" to INI,
183-
)
142+
private enum class SyntaxStyle(val style: String, vararg val extensions: String) {
143+
Plaintext(SYNTAX_STYLE_NONE, "txt", "p7b", "log", "pem"),
144+
CSS(SYNTAX_STYLE_CSS, "css"),
145+
CSV(SYNTAX_STYLE_CSV, "csv"),
146+
Dockerfile(SYNTAX_STYLE_DOCKERFILE),
147+
HTML(SYNTAX_STYLE_HTML, "html", "htm", "shtml"),
148+
INI(SYNTAX_STYLE_INI, "ini"),
149+
Java(SYNTAX_STYLE_JAVA, "java"),
150+
Javascript(SYNTAX_STYLE_JAVASCRIPT, "js"),
151+
JSON(SYNTAX_STYLE_JSON, "json"),
152+
Kotlin(SYNTAX_STYLE_KOTLIN, "kt", "kts"),
153+
Markdown(SYNTAX_STYLE_MARKDOWN, "md"),
154+
Properties(SYNTAX_STYLE_PROPERTIES_FILE, "properties", "conf"),
155+
Python(SYNTAX_STYLE_PYTHON, "py"),
156+
SQL(SYNTAX_STYLE_SQL, "sql"),
157+
Typescript(SYNTAX_STYLE_TYPESCRIPT, "ts"),
158+
Shell(SYNTAX_STYLE_UNIX_SHELL, "sh", "bash"),
159+
Batch(SYNTAX_STYLE_WINDOWS_BATCH, "bat"),
160+
XML(SYNTAX_STYLE_XML, "xml", "svg"),
161+
YAML(SYNTAX_STYLE_YAML, "yaml"),
162+
}
163+
164+
private val KNOWN_EXTENSIONS: Map<String, SyntaxStyle> = buildMap {
165+
for (style in SyntaxStyle.entries) {
166+
for (string in style.extensions) {
167+
put(string, style)
168+
}
169+
}
170+
}
184171

185172
private val KNOWN_FILENAMES = setOf(
186173
"readme",

src/main/kotlin/io/github/inductiveautomation/kindling/zip/views/GenericFileView.kt

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)