1
1
package com.vaadin.plugin.module
2
2
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
8
22
9
- class VaadinPanel {
23
+ class VaadinPanel ( propertyGraph : PropertyGraph , private val wizardContext : WizardContext , builder : Panel ) {
10
24
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)
12
28
13
29
private var quickStarterGroup: CollapsibleRow ? = null
14
30
private var skeletonStarterGroup: CollapsibleRow ? = null
@@ -17,7 +33,25 @@ class VaadinPanel {
17
33
private val skeletonStarterPanel = SkeletonStarterPanel ()
18
34
19
35
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
+
21
55
quickStarterGroup = collapsibleGroup(" Project Settings" ) {
22
56
row {}.cell(quickStarterPanel.root)
23
57
}
@@ -41,19 +75,57 @@ class VaadinPanel {
41
75
row {
42
76
text(" For more configuration options, visit <a href=\" https://start.vaadin.com\" >start.vaadin.com</a>" )
43
77
}
44
- }.withVisualPadding( true )
78
+ }
45
79
46
80
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
+ )
49
107
}
50
108
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)
53
115
}
54
116
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)
57
129
}
58
130
59
131
}
0 commit comments