1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.starters.local.wizard
import com.intellij.icons.AllIcons
import com.intellij.ide.starters.JavaStartersBundle
import com.intellij.ide.starters.local.*
import com.intellij.ide.starters.shared.*
import com.intellij.ide.starters.shared.ui.LibraryDescriptionPanel
import com.intellij.ide.starters.shared.ui.SelectedLibrariesPanel
import com.intellij.ide.util.projectWizard.ModuleWizardStep
import com.intellij.ide.wizard.withVisualPadding
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.util.NlsSafe
import com.intellij.ui.*
import com.intellij.ui.components.JBLabel
import com.intellij.ui.layout.*
import com.intellij.util.containers.Convertor
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import com.intellij.util.ui.components.BorderLayoutPanel
import com.intellij.util.ui.tree.TreeUtil
import java.awt.Dimension
import java.awt.GridBagLayout
import java.awt.event.ItemEvent
import javax.swing.DefaultComboBoxModel
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.JTree
import javax.swing.event.TreeSelectionListener
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.DefaultTreeModel
import javax.swing.tree.TreePath
import javax.swing.tree.TreeSelectionModel
open class StarterLibrariesStep(contextProvider: StarterContextProvider) : ModuleWizardStep() {
protected val starterContext = contextProvider.starterContext
protected val starterSettings: StarterWizardSettings = contextProvider.settings
protected val moduleBuilder: StarterModuleBuilder = contextProvider.moduleBuilder
private val topLevelPanel: BorderLayoutPanel = BorderLayoutPanel()
private val contentPanel: DialogPanel by lazy { createComponent() }
private val libraryDescriptionPanel: LibraryDescriptionPanel by lazy { LibraryDescriptionPanel() }
private val selectedLibrariesPanel: SelectedLibrariesPanel by lazy { createSelectedLibrariesPanel() }
private val dependencyConfig: Map<String, DependencyConfig> by lazy { moduleBuilder.loadDependencyConfigInternal() }
private val selectedLibraryIds: MutableSet<String> = mutableSetOf()
private var selectedStarterId: String? = null
private val startersComboBox: ComboBox<Starter> by lazy { ComboBox<Starter>() }
private val librariesList: CheckboxTreeBase by lazy { createLibrariesList() }
override fun updateDataModel() {
starterContext.starter = startersComboBox.selectedItem as? Starter
starterContext.libraryIds.clear()
starterContext.libraryIds.addAll(selectedLibraryIds)
starterContext.starterDependencyConfig = dependencyConfig[starterContext.starter?.id]
}
override fun onStepLeaving() {
super.onStepLeaving()
updateDataModel()
}
override fun getComponent(): JComponent {
return topLevelPanel
}
@NlsSafe
private fun getLibraryVersion(library: Library): String? {
if (library.group == null || library.artifact == null) return null
val selectedStarter = startersComboBox.selectedItem as? Starter ?: return null
return dependencyConfig[selectedStarter.id]?.getVersion(library.group, library.artifact)
}
override fun getPreferredFocusedComponent(): JComponent {
return librariesList
}
private fun createLibrariesList(): CheckboxTreeBase {
val list = CheckboxTreeBase(object : CheckboxTree.CheckboxTreeCellRenderer() {
override fun customizeRenderer(
tree: JTree?,
value: Any?,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
hasFocus: Boolean
) {
if (value !is DefaultMutableTreeNode) return
this.border = JBUI.Borders.empty(2)
val renderer = textRenderer
when (val item = value.userObject) {
is LibraryCategory -> {
renderer.icon = item.icon ?: AllIcons.Nodes.PpLibFolder
renderer.append(item.title, SimpleTextAttributes.REGULAR_ATTRIBUTES)
}
is Library -> {
renderer.icon = item.icon ?: AllIcons.Nodes.PpLib
renderer.append(item.title, SimpleTextAttributes.REGULAR_ATTRIBUTES)
val version = getLibraryVersion(item)
if (version != null) {
renderer.append(" ($version)", SimpleTextAttributes.GRAYED_ATTRIBUTES)
}
}
}
}
}, null)
enableEnterKeyHandling(list)
list.rowHeight = 0
list.isRootVisible = false
list.selectionModel.selectionMode = TreeSelectionModel.SINGLE_TREE_SELECTION
list.addCheckboxTreeListener(object : CheckboxTreeListener {
override fun nodeStateChanged(node: CheckedTreeNode) {
val library = node.userObject as? Library ?: return
val libraryId = library.id
if (node.isChecked) {
selectedLibraryIds.add(libraryId)
selectedLibraryIds.removeAll(library.includesLibraries)
}
else {
selectedLibraryIds.remove(libraryId)
}
updateIncludedLibraries(library, node)
updateSelectedLibraries()
list.repaint()
}
})
return list
}
private fun createComponent(): DialogPanel {
startersComboBox.setMinimumAndPreferredWidth(200)
startersComboBox.renderer = SimpleListCellRenderer.create("", Starter::title)
startersComboBox.addItemListener { e ->
if (e.stateChange == ItemEvent.SELECTED) {
val newValue = e.item as? Starter
if (newValue != null) {
selectedStarterId = newValue.id
updateLibrariesList(newValue, false)
}
}
}
TreeSpeedSearch(librariesList, Convertor { treePath: TreePath ->
when (val dataObject = (treePath.lastPathComponent as DefaultMutableTreeNode).userObject) {
is LibraryCategory -> dataObject.title
is Library -> dataObject.title
else -> ""
}
})
librariesList.selectionModel.addTreeSelectionListener(TreeSelectionListener { e ->
val path = e.path
if (path != null && e.isAddedPath) {
when (val item = (path.lastPathComponent as? DefaultMutableTreeNode)?.userObject) {
is LibraryCategory -> libraryDescriptionPanel.update(item.title, item.description)
is Library -> libraryDescriptionPanel.update(item, null)
}
}
else {
libraryDescriptionPanel.reset()
}
})
val messages = starterSettings.customizedMessages
return panel {
if (starterContext.starterPack.starters.size > 1) {
row {
cell(isFullWidth = true) {
label(messages?.frameworkVersionLabel ?: JavaStartersBundle.message("title.project.version.label"))
component(startersComboBox)
}
}.largeGapAfter()
}
row {
label(messages?.dependenciesLabel ?: JavaStartersBundle.message("title.project.dependencies.label"))
}
row {
component(JPanel(GridBagLayout()).apply {
add(ScrollPaneFactory.createScrollPane(librariesList).apply {
preferredSize = Dimension(0, 0)
}, gridConstraint(0, 0))
add(JPanel(GridBagLayout()).apply {
border = JBUI.Borders.emptyLeft(UIUtil.DEFAULT_HGAP * 2)
preferredSize = Dimension(0, 0)
add(libraryDescriptionPanel.apply {
preferredSize = Dimension(0, 0)
}, gridConstraint(0, 0))
add(BorderLayoutPanel().apply {
preferredSize = Dimension(0, 0)
addToTop(JBLabel(messages?.selectedDependenciesLabel
?: JavaStartersBundle.message("title.project.dependencies.selected.label")).apply {
border = JBUI.Borders.empty(0, 0, UIUtil.DEFAULT_VGAP * 2, 0)
})
addToCenter(selectedLibrariesPanel)
}, gridConstraint(0, 1))
}, gridConstraint(1, 0))
}).constraints(push, grow)
}
}.withVisualPadding()
}
private fun createSelectedLibrariesPanel(): SelectedLibrariesPanel {
val panel = SelectedLibrariesPanel()
val messages = starterSettings.customizedMessages
panel.emptyText.text = messages?.noDependenciesSelectedLabel ?: JavaStartersBundle.message("hint.dependencies.not.selected")
panel.libraryRemoveListener = { libraryInfo ->
// do not remove from selectedLibraryIds directly, library can be included
walkCheckedTree(getLibrariesRoot()) {
if (it.userObject == libraryInfo && it.isEnabled) {
librariesList.setNodeState(it, false)
}
}
updateSelectedLibraries()
}
return panel
}
private fun updateSelectedLibraries() {
val selected = mutableListOf<LibraryInfo>()
walkCheckedTree(getLibrariesRoot()) {
val library = (it.userObject as? Library)
if (library != null && it.isChecked) {
selected.add(library)
}
}
selectedLibrariesPanel.update(selected)
}
private fun getLibrariesRoot(): CheckedTreeNode? {
return librariesList.model.root as? CheckedTreeNode
}
override fun _init() {
super._init()
if (topLevelPanel.componentCount == 0) {
// create UI only on first show
topLevelPanel.addToCenter(contentPanel)
}
// step became visible
val starterPack = starterContext.starterPack
startersComboBox.model = DefaultComboBoxModel(starterPack.starters.toTypedArray())
val initial = selectedStarterId == null
val selectedStarter = when (val previouslySelectedStarter = starterContext.starter?.id) {
null -> starterPack.starters.firstOrNull()
else -> starterPack.starters.find { it.id == previouslySelectedStarter }
}
if (selectedStarter != null) {
startersComboBox.selectedItem = selectedStarter
selectedStarterId = selectedStarter.id
selectedLibraryIds.clear()
for (libraryId in starterContext.libraryIds) {
val lib = selectedStarter.libraries.find { it.id == libraryId }
if (lib != null) {
selectedLibraryIds.add(libraryId)
selectedLibraryIds.removeAll(lib.includesLibraries)
}
}
updateLibrariesList(selectedStarter, initial)
}
}
private fun updateLibrariesList(starter: Starter, init: Boolean) {
val previouslyExpandedGroups = getExpandedCategories()
val librariesRoot = CheckedTreeNode()
val categoryNodes = mutableMapOf<LibraryCategory, DefaultMutableTreeNode>()
val selectedLibraries = mutableListOf<Pair<Library, CheckedTreeNode>>()
val libraryNodes = mutableListOf<CheckedTreeNode>()
for (library in starter.libraries) {
if (!moduleBuilder.isDependencyAvailableInternal(starter, library)) continue
val libraryNode = CheckedTreeNode(library)
if (library.isRequired || library.isDefault && init) {
selectedLibraryIds.add(library.id)
}
libraryNode.isChecked = selectedLibraryIds.contains(library.id)
libraryNode.isEnabled = !library.isRequired
if (library.category == null) {
librariesRoot.add(libraryNode)
}
else {
val categoryNode = categoryNodes.getOrPut(library.category) {
val newCategoryNode = DefaultMutableTreeNode(library.category, true)
librariesRoot.add(newCategoryNode)
newCategoryNode
}
categoryNode.add(libraryNode)
}
libraryNodes.add(libraryNode)
if (libraryNode.isChecked) {
selectedLibraries.add(library to libraryNode)
}
}
val starterLibraryIds = starter.libraries.map { it.id }.toSet()
selectedLibraryIds.removeIf { !starterLibraryIds.contains(it) }
librariesList.model = DefaultTreeModel(librariesRoot)
expandCategories(categoryNodes, librariesRoot, previouslyExpandedGroups, init)
if (libraryNodes.isNotEmpty()) {
val toExpand = libraryNodes.find { librariesList.isExpanded(TreeUtil.getPath(librariesRoot, it.parent)) }
if (toExpand != null) {
librariesList.selectionModel.addSelectionPath(TreeUtil.getPath(librariesRoot, toExpand))
}
}
for ((library, node) in selectedLibraries) {
updateIncludedLibraries(library, node)
}
updateSelectedLibraries()
}
private fun expandCategories(categoryNodes: MutableMap<LibraryCategory, DefaultMutableTreeNode>,
librariesRoot: CheckedTreeNode,
expandedCategories: List<LibraryCategory>,
isInitial: Boolean) {
if (isInitial) {
val collapsedCategories = moduleBuilder.getCollapsedDependencyCategoriesInternal()
for ((_, node) in categoryNodes) {
val categoryId = (node.userObject as? LibraryCategory)?.id
val path = TreeUtil.getPath(librariesRoot, node)
if (!collapsedCategories.contains(categoryId)) {
librariesList.expandPath(path)
}
else {
librariesList.collapsePath(path)
}
}
}
else {
for (group in expandedCategories) {
val newGroup = categoryNodes.entries.find { it.key == group }
if (newGroup != null) {
val path = TreeUtil.getPath(librariesRoot, newGroup.value)
librariesList.expandPath(path)
}
}
}
}
private fun getExpandedCategories(): List<LibraryCategory> {
val librariesRoot = getLibrariesRoot() ?: return emptyList()
return librariesRoot.children().toList()
.filter { librariesList.isExpanded(TreeUtil.getPath(librariesRoot, it)) }
.mapNotNull { (it as? DefaultMutableTreeNode)?.userObject as? LibraryCategory }
}
private fun updateIncludedLibraries(library: Library, node: CheckedTreeNode) {
if (library.includesLibraries.isNotEmpty()) {
val rootNode = librariesList.model.root as? CheckedTreeNode
if (rootNode != null) {
for (child in rootNode.children()) {
if (child is CheckedTreeNode) {
updateNodeIncluded(child, library, node)
}
else if (child is DefaultMutableTreeNode) {
for (groupChild in child.children()) {
updateNodeIncluded(groupChild, library, node)
}
}
}
}
}
}
private fun updateNodeIncluded(child: Any?, library: Library, node: CheckedTreeNode) {
val checkedNode = child as? CheckedTreeNode
val nodeLibrary = checkedNode?.userObject as? Library ?: return
if (library.includesLibraries.contains(nodeLibrary.id)) {
checkedNode.isChecked = node.isChecked
checkedNode.isEnabled = !node.isChecked
}
}
}
|