|
| 1 | +/* |
| 2 | + * Copyrightest (c) 2024 Joseph Roskopf |
| 3 | + */ |
| 4 | + |
| 5 | +package com.joetr.modulemaker |
| 6 | + |
| 7 | +import androidx.compose.foundation.layout.height |
| 8 | +import androidx.compose.foundation.layout.width |
| 9 | +import androidx.compose.material.Surface |
| 10 | +import androidx.compose.runtime.Composable |
| 11 | +import androidx.compose.runtime.mutableStateOf |
| 12 | +import androidx.compose.runtime.remember |
| 13 | +import androidx.compose.ui.Modifier |
| 14 | +import androidx.compose.ui.awt.ComposePanel |
| 15 | +import androidx.compose.ui.unit.dp |
| 16 | +import com.intellij.openapi.ui.DialogWrapper |
| 17 | +import com.intellij.openapi.util.io.FileUtilRt |
| 18 | +import com.joetr.modulemaker.data.toProjectFile |
| 19 | +import com.joetr.modulemaker.ui.file.FileTree |
| 20 | +import com.joetr.modulemaker.ui.file.FileTreeView |
| 21 | +import com.joetr.modulemaker.ui.theme.WidgetTheme |
| 22 | +import java.io.File |
| 23 | +import javax.swing.Action |
| 24 | +import javax.swing.JComponent |
| 25 | + |
| 26 | +private const val WINDOW_WIDTH = 400 |
| 27 | +private const val WINDOW_HEIGHT = 600 |
| 28 | + |
| 29 | +class PreviewDialogWrapper(val filesToBeCreated: List<File>, val root: String) : DialogWrapper(true) { |
| 30 | + |
| 31 | + private var tempRoot: File |
| 32 | + |
| 33 | + init { |
| 34 | + title = "Preview" |
| 35 | + init() |
| 36 | + |
| 37 | + tempRoot = FileUtilRt.createTempDirectory(root, null, true) |
| 38 | + |
| 39 | + createFileStructure( |
| 40 | + tempRoot, |
| 41 | + filesToBeCreated.map { |
| 42 | + val pathToRoot = tempRoot.absolutePath |
| 43 | + val split = it.absolutePath.split(root) |
| 44 | + |
| 45 | + // splice together the files to have a root of our temp folder |
| 46 | + File(pathToRoot, split.drop(1).joinToString(separator = "")) |
| 47 | + } |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + override fun dispose() { |
| 52 | + super.dispose() |
| 53 | + tempRoot.parentFile.deleteRecursively() |
| 54 | + } |
| 55 | + |
| 56 | + override fun createCenterPanel(): JComponent { |
| 57 | + return ComposePanel().apply { |
| 58 | + setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) |
| 59 | + setContent { |
| 60 | + WidgetTheme { |
| 61 | + Surface { |
| 62 | + FileTreeJPanel( |
| 63 | + modifier = Modifier.height(WINDOW_HEIGHT.dp).width(WINDOW_WIDTH.dp) |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Composable |
| 72 | + private fun FileTreeJPanel( |
| 73 | + modifier: Modifier = Modifier |
| 74 | + ) { |
| 75 | + val height = remember { mutableStateOf(WINDOW_HEIGHT) } |
| 76 | + |
| 77 | + FileTreeView( |
| 78 | + modifier = modifier, |
| 79 | + model = FileTree(root = tempRoot.toProjectFile()), |
| 80 | + height = height.value.dp, |
| 81 | + onClick = { } |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + private fun List<File>.root(): File { |
| 86 | + return this.minBy { file -> |
| 87 | + file.absolutePath.count { it.toString() == File.separator } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private fun createFileStructure(root: File, structure: List<File>) { |
| 92 | + root.mkdirs() |
| 93 | + structure.forEach { |
| 94 | + it.mkdirs() |
| 95 | + if (it.isDirectory.not() && it.extension.isEmpty().not()) { |
| 96 | + it.writeText("") |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + override fun createActions(): Array<Action> { |
| 102 | + return arrayOf( |
| 103 | + DialogWrapperExitAction( |
| 104 | + "Okay", |
| 105 | + 2 |
| 106 | + ) |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
0 commit comments