Skip to content

Commit c562547

Browse files
authored
ref: project generation (#38)
* project generation * project generation * project generation * cleanup * fix for Project Setting Module add * NLs * update model on expand / collapse
1 parent 3ef09ef commit c562547

10 files changed

+197
-148
lines changed

src/main/kotlin/com/vaadin/plugin/module/PostModuleCreatedActivity.kt

-25
This file was deleted.

src/main/kotlin/com/vaadin/plugin/module/VaadinCustomOptionsStep.kt

-18
This file was deleted.

src/main/kotlin/com/vaadin/plugin/module/VaadinModuleBuilder.kt

-50
This file was deleted.

src/main/kotlin/com/vaadin/plugin/module/VaadinModuleType.kt

-26
This file was deleted.

src/main/kotlin/com/vaadin/plugin/module/VaadinPanel.kt

+87-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package com.vaadin.plugin.module
22

3-
import com.intellij.ide.wizard.withVisualPadding
4-
import com.intellij.openapi.ui.DialogPanel
5-
import com.intellij.ui.dsl.builder.CollapsibleRow
6-
import com.intellij.ui.dsl.builder.panel
7-
import com.vaadin.plugin.starter.DownloadableModel
3+
import com.intellij.ide.IdeBundle
4+
import com.intellij.ide.util.projectWizard.WizardContext
5+
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
6+
import com.intellij.openapi.observable.properties.GraphProperty
7+
import com.intellij.openapi.observable.properties.PropertyGraph
8+
import com.intellij.openapi.observable.util.joinCanonicalPath
9+
import com.intellij.openapi.observable.util.transform
10+
import com.intellij.openapi.ui.BrowseFolderDescriptor.Companion.withPathToTextConvertor
11+
import com.intellij.openapi.ui.BrowseFolderDescriptor.Companion.withTextToPathConvertor
12+
import com.intellij.openapi.ui.TextFieldWithBrowseButton
13+
import com.intellij.openapi.ui.getCanonicalPath
14+
import com.intellij.openapi.ui.getPresentablePath
15+
import com.intellij.openapi.util.io.FileUtil
16+
import com.intellij.openapi.util.text.StringUtil
17+
import com.intellij.ui.UIBundle
18+
import com.intellij.ui.dsl.builder.*
19+
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.PROJECT_MODEL_PROP_KEY
20+
import org.jetbrains.annotations.Nls
21+
import java.io.File
822

9-
class VaadinPanel {
23+
class VaadinPanel(propertyGraph: PropertyGraph, private val wizardContext: WizardContext, builder: Panel) {
1024

11-
private var dialogPanel: DialogPanel? = null
25+
private val entityNameProperty = propertyGraph.lazyProperty(::suggestName)
26+
private val locationProperty = propertyGraph.lazyProperty(::suggestLocationByName)
27+
private val canonicalPathProperty = locationProperty.joinCanonicalPath(entityNameProperty)
1228

1329
private var quickStarterGroup: CollapsibleRow? = null
1430
private var skeletonStarterGroup: CollapsibleRow? = null
@@ -17,7 +33,25 @@ class VaadinPanel {
1733
private val skeletonStarterPanel = SkeletonStarterPanel()
1834

1935
init {
20-
dialogPanel = panel {
36+
builder.panel {
37+
row("Name:") {
38+
textField().bindText(entityNameProperty)
39+
}
40+
row("Location:") {
41+
val commentLabel = projectLocationField(locationProperty, wizardContext)
42+
.align(AlignX.FILL)
43+
.comment(getLocationComment(), 100).comment!!
44+
entityNameProperty.afterChange {
45+
commentLabel.text = getLocationComment()
46+
updateModel()
47+
}
48+
locationProperty.afterChange {
49+
commentLabel.text = getLocationComment()
50+
entityNameProperty.set(suggestName(entityNameProperty.get()))
51+
updateModel()
52+
}
53+
}
54+
2155
quickStarterGroup = collapsibleGroup("Project Settings") {
2256
row {}.cell(quickStarterPanel.root)
2357
}
@@ -41,19 +75,57 @@ class VaadinPanel {
4175
row {
4276
text("For more configuration options, visit <a href=\"https://start.vaadin.com\">start.vaadin.com</a>")
4377
}
44-
}.withVisualPadding(true)
78+
}
4579

4680
quickStarterGroup!!.expanded = true
47-
quickStarterGroup!!.addExpandedListener { if (it) skeletonStarterGroup!!.expanded = false }
48-
skeletonStarterGroup!!.addExpandedListener { if (it) quickStarterGroup!!.expanded = false }
81+
quickStarterGroup!!.addExpandedListener { if (it) skeletonStarterGroup!!.expanded = false; updateModel() }
82+
skeletonStarterGroup!!.addExpandedListener { if (it) quickStarterGroup!!.expanded = false; updateModel() }
83+
84+
updateModel()
85+
}
86+
87+
private fun suggestName(): String {
88+
return suggestName("untitled")
89+
}
90+
91+
private fun suggestName(prefix: String): String {
92+
val projectFileDirectory = File(wizardContext.projectFileDirectory)
93+
return FileUtil.createSequentFileName(projectFileDirectory, prefix, "")
94+
}
95+
96+
private fun suggestLocationByName(): String {
97+
return wizardContext.projectFileDirectory
98+
}
99+
100+
private fun getLocationComment(): @Nls String {
101+
val shortPath = StringUtil.shortenPathWithEllipsis(getPresentablePath(canonicalPathProperty.get()), 60)
102+
return UIBundle.message(
103+
"label.project.wizard.new.project.path.description",
104+
wizardContext.isCreatingNewProjectInt,
105+
shortPath
106+
)
49107
}
50108

51-
fun getComponent(): DialogPanel {
52-
return dialogPanel!!
109+
private fun updateModel() {
110+
wizardContext.setProjectFileDirectory(canonicalPathProperty.get())
111+
wizardContext.projectName = entityNameProperty.get()
112+
wizardContext.defaultModuleName = entityNameProperty.get()
113+
val projectModel = if (quickStarterGroup!!.expanded) quickStarterPanel.model else skeletonStarterPanel.model
114+
wizardContext.getUserData(PROJECT_MODEL_PROP_KEY)?.set(projectModel)
53115
}
54116

55-
fun getModel(): DownloadableModel {
56-
return if (quickStarterGroup!!.expanded) quickStarterPanel.model else skeletonStarterPanel.model
117+
private fun Row.projectLocationField(
118+
locationProperty: GraphProperty<String>,
119+
wizardContext: WizardContext
120+
): Cell<TextFieldWithBrowseButton> {
121+
val fileChooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor()
122+
.withFileFilter { it.isDirectory }
123+
.withPathToTextConvertor(::getPresentablePath)
124+
.withTextToPathConvertor(::getCanonicalPath)
125+
val title = IdeBundle.message("title.select.project.file.directory", wizardContext.presentationName)
126+
val property = locationProperty.transform(::getPresentablePath, ::getCanonicalPath)
127+
return textFieldWithBrowseButton(title, wizardContext.project, fileChooserDescriptor)
128+
.bindText(property)
57129
}
58130

59131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.vaadin.plugin.module
2+
3+
import com.intellij.ide.util.projectWizard.WizardContext
4+
import com.intellij.ide.wizard.GeneratorNewProjectWizardBuilderAdapter
5+
import com.intellij.ide.wizard.NewProjectWizardStep
6+
import com.intellij.openapi.actionSystem.impl.ActionManagerImpl
7+
import com.intellij.openapi.fileEditor.FileEditorManager
8+
import com.intellij.openapi.fileEditor.OpenFileDescriptor
9+
import com.intellij.openapi.observable.properties.PropertyGraph
10+
import com.intellij.openapi.project.Project
11+
import com.intellij.openapi.vfs.VfsUtil
12+
import com.vaadin.plugin.utils.VaadinProjectUtil
13+
import java.io.File
14+
15+
16+
class VaadinProjectBuilderAdapter(private val vaadinWizard: VaadinProjectWizard = VaadinProjectWizard()) :
17+
GeneratorNewProjectWizardBuilderAdapter(vaadinWizard) {
18+
19+
private val propertyGraph = PropertyGraph()
20+
21+
private val projectDownloadedProperty = propertyGraph.property(false)
22+
23+
override fun createStep(context: WizardContext): NewProjectWizardStep {
24+
return vaadinWizard.createStep(context)
25+
}
26+
27+
override fun createProject(name: String?, path: String?): Project? {
28+
return super.createProject(name, path)?.let { project ->
29+
project.putUserData(VaadinProjectUtil.PROJECT_DOWNLOADED_PROP_KEY, projectDownloadedProperty)
30+
projectDownloadedProperty.afterChange { afterProjectCreated(project) }
31+
VaadinProjectUtil.downloadAndExtract(project, vaadinWizard.projectModel!!.getDownloadLink(project))
32+
project
33+
}
34+
}
35+
36+
override fun isAvailable(): Boolean {
37+
val lastPerformedActionId = (ActionManagerImpl.getInstance() as ActionManagerImpl).lastPreformedActionId
38+
lastPerformedActionId ?: return false
39+
return lastPerformedActionId.contains("NewProject", true)
40+
}
41+
42+
private fun afterProjectCreated(project: Project) {
43+
VfsUtil.findFileByIoFile(File(project.basePath, "README.md"), true)?.let {
44+
val descriptor = OpenFileDescriptor(project, it)
45+
descriptor.setUsePreviewTab(true)
46+
FileEditorManager.getInstance(project).openEditor(descriptor, true)
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.vaadin.plugin.module
2+
3+
import com.intellij.ide.util.projectWizard.WizardContext
4+
import com.intellij.ide.wizard.GeneratorNewProjectWizard
5+
import com.intellij.ide.wizard.NewProjectWizardStep
6+
import com.intellij.openapi.observable.properties.PropertyGraph
7+
import com.intellij.openapi.util.IconLoader
8+
import com.vaadin.plugin.starter.DownloadableModel
9+
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.PROJECT_MODEL_PROP_KEY
10+
import javax.swing.Icon
11+
12+
class VaadinProjectWizard : GeneratorNewProjectWizard {
13+
14+
override val icon: Icon
15+
get() = IconLoader.getIcon("/META-INF/pluginIcon.svg", javaClass.classLoader)
16+
17+
override val id: String
18+
get() = "Vaadin"
19+
20+
override val name: String
21+
get() = "Vaadin"
22+
23+
private val propertyGraph: PropertyGraph
24+
get() = PropertyGraph("Vaadin project")
25+
26+
private val projectModelProperty = propertyGraph.property<DownloadableModel?>(null)
27+
28+
val projectModel: DownloadableModel? by projectModelProperty
29+
30+
override fun createStep(context: WizardContext): NewProjectWizardStep {
31+
context.putUserData(PROJECT_MODEL_PROP_KEY, projectModelProperty)
32+
return VaadinProjectWizardStep(context, propertyGraph)
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vaadin.plugin.module
2+
3+
import com.intellij.ide.util.projectWizard.WizardContext
4+
import com.intellij.ide.wizard.NewProjectWizardStep
5+
import com.intellij.openapi.observable.properties.PropertyGraph
6+
import com.intellij.openapi.util.UserDataHolder
7+
import com.intellij.openapi.util.UserDataHolderBase
8+
import com.intellij.ui.dsl.builder.Panel
9+
10+
class VaadinProjectWizardStep(override val context: WizardContext, override val propertyGraph: PropertyGraph) :
11+
NewProjectWizardStep {
12+
13+
override val data: UserDataHolder
14+
get() = UserDataHolderBase()
15+
16+
override val keywords: NewProjectWizardStep.Keywords
17+
get() = NewProjectWizardStep.Keywords()
18+
19+
override fun setupUI(builder: Panel) {
20+
VaadinPanel(propertyGraph, context, builder)
21+
}
22+
}

0 commit comments

Comments
 (0)