4
4
package software.aws.toolkits.jetbrains.settings
5
5
6
6
import com.intellij.openapi.application.ApplicationManager
7
+ import com.intellij.openapi.application.ModalityState
8
+ import com.intellij.openapi.application.runInEdt
7
9
import com.intellij.openapi.options.BoundConfigurable
8
10
import com.intellij.openapi.project.ProjectManager
9
11
import com.intellij.ui.CheckBoxList
10
12
import com.intellij.ui.FilterComponent
11
13
import com.intellij.ui.ListSpeedSearch
12
14
import com.intellij.ui.layout.panel
15
+ import software.aws.toolkits.core.utils.replace
13
16
import software.aws.toolkits.jetbrains.core.explorer.ExplorerToolWindow
14
17
import software.aws.toolkits.jetbrains.services.dynamic.DynamicResourceSupportedTypes
15
18
import software.aws.toolkits.jetbrains.services.dynamic.explorer.OtherResourcesNode
16
19
import software.aws.toolkits.resources.message
17
- import javax.swing.DefaultListModel
18
- import javax.swing.JCheckBox
19
20
import javax.swing.ListSelectionModel
20
21
21
22
class DynamicResourcesConfigurable : BoundConfigurable (message("aws.settings.dynamic_resources_configurable.title")) {
22
23
23
- private val checklistModel = DefaultListModel <JCheckBox >()
24
- private val checklist = CheckBoxList <String >(checklistModel)
25
- private val changeSet = mutableSetOf<Int >()
26
- private val checkboxListener = { idx: Int ->
27
- if (idx in changeSet) {
28
- changeSet.remove(idx)
29
- } else {
30
- changeSet.add(idx)
24
+ private val checklist = CheckBoxList <String >()
25
+ private val allResources = mutableSetOf<String >()
26
+ private val selected = mutableSetOf<String >()
27
+ private val filter = object : FilterComponent (" filter" , 5 ) {
28
+ override fun filter () {
29
+ updateCheckboxList()
31
30
}
32
31
}
33
32
34
33
init {
35
34
checklist.selectionMode = ListSelectionModel .MULTIPLE_INTERVAL_SELECTION
36
- checklist.setCheckBoxListListener { idx, _ ->
37
- checkboxListener(idx)
38
- }
35
+ checklist.setCheckBoxListListener(::checkboxStateHandler)
39
36
40
37
ListSpeedSearch (checklist) {
41
38
it.text.substringAfter(" ::" )
@@ -45,52 +42,27 @@ class DynamicResourcesConfigurable : BoundConfigurable(message("aws.settings.dyn
45
42
override fun getPreferredFocusedComponent () = checklist
46
43
47
44
override fun createPanel () = panel {
48
- val allCheckboxes = mutableListOf<JCheckBox >()
49
- val selected = DynamicResourcesSettings .getInstance().selected
50
-
45
+ selected.replace(DynamicResourcesSettings .getInstance().selected)
51
46
ApplicationManager .getApplication().executeOnPooledThread {
52
- DynamicResourceSupportedTypes .getInstance().getSupportedTypes().forEach {
53
- checklist.addItem(it, it, it in selected)
47
+ allResources.addAll(DynamicResourceSupportedTypes .getInstance().getSupportedTypes())
48
+ runInEdt(ModalityState .any()) {
49
+ updateCheckboxList()
54
50
}
55
- allCheckboxes.addAll(checklist.map { _, checkbox -> checkbox })
56
51
}
57
52
53
+ row { filter(growX) }
58
54
row {
59
- // filter
60
- val field = object : FilterComponent (" filter" , 5 ) {
61
- override fun filter () {
62
- checklistModel.clear()
63
- checklistModel.addAll(allCheckboxes.filter { it.text.contains(filter, ignoreCase = true ) })
64
- }
65
- }
66
- field(growX)
67
- }
68
-
69
- row {
70
- // scrollpane
71
55
scrollPane(checklist)
72
56
.constraints(growX, pushX)
73
- .onIsModified {
74
- // returns true if there is a change
75
- changeSet.size != 0
76
- }
57
+ .onIsModified { selected != DynamicResourcesSettings .getInstance().selected }
77
58
.onApply {
78
- changeSet.clear()
79
-
80
- DynamicResourcesSettings .getInstance().selected = allCheckboxes.filter { it.isSelected }.map { it.text }.toSet()
81
- ProjectManager .getInstance().openProjects.forEach { project ->
82
- if (! project.isDisposed) {
83
- val toolWindow = ExplorerToolWindow .getInstance(project)
84
- toolWindow.findNode(OtherResourcesNode ::class ).then { node ->
85
- node?.let {
86
- toolWindow.invalidateTree(it)
87
- }
88
- }
89
- }
90
- }
59
+ DynamicResourcesSettings .getInstance().selected = selected
60
+ refreshAwsExplorer()
61
+ }
62
+ .onReset {
63
+ selected.replace(DynamicResourcesSettings .getInstance().selected)
64
+ updateCheckboxList()
91
65
}
92
-
93
- // select/clearall
94
66
right {
95
67
cell(isVerticalFlow = true ) {
96
68
val sizeGroup = " buttons"
@@ -107,33 +79,37 @@ class DynamicResourcesConfigurable : BoundConfigurable(message("aws.settings.dyn
107
79
}
108
80
109
81
private fun CheckBoxList <* >.toggleAll (state : Boolean ) {
110
- this .forEachIndexed { index, checkbox ->
111
- checkbox.isSelected = state
112
- checkboxListener(index)
82
+ (0 until model.size).forEach { idx ->
83
+ checkboxStateHandler(idx, state)
113
84
}
114
- this .repaint ()
85
+ updateCheckboxList ()
115
86
}
116
87
117
- private companion object {
118
- fun <T > CheckBoxList <* >.map (fn : (Int , JCheckBox ) -> T ): List <T > {
119
- val size = this .model.size - 1
88
+ private fun updateCheckboxList () {
89
+ checklist.clear()
90
+ allResources.filter { it.contains(filter.filter, ignoreCase = true ) }.sorted().forEach { checklist.addItem(it, it, it in selected) }
91
+ }
120
92
121
- return (0 .. size).map {
122
- fn(it, this .model.getElementAt(it))
93
+ private fun checkboxStateHandler (idx : Int , state : Boolean ) {
94
+ checklist.getItemAt(idx)?.let { value ->
95
+ if (state) {
96
+ selected.add(value)
97
+ } else {
98
+ selected.remove(value)
123
99
}
124
100
}
101
+ }
125
102
126
- fun CheckBoxList <* >.filter (fn : (JCheckBox ) -> Boolean ): List <JCheckBox > =
127
- this .map { _, it ->
128
- if (fn(it)) {
129
- it
130
- } else {
131
- null
103
+ private fun refreshAwsExplorer () {
104
+ ProjectManager .getInstance().openProjects.forEach { project ->
105
+ if (! project.isDisposed) {
106
+ val toolWindow = ExplorerToolWindow .getInstance(project)
107
+ toolWindow.findNode(OtherResourcesNode ::class ).then { node ->
108
+ node.let {
109
+ toolWindow.invalidateTree(it)
110
+ }
132
111
}
133
- }.filterNotNull()
134
-
135
- fun CheckBoxList <* >.forEachIndexed (fn : (Int , JCheckBox ) -> Unit ) {
136
- this .map(fn)
112
+ }
137
113
}
138
114
}
139
115
}
0 commit comments