Skip to content

Commit 3893eb8

Browse files
authored
Merge pull request #89 from advanced-security/feature/show_hide_token_in_settings
Feature/show hide token in settings
2 parents b00ed4b + c4fc202 commit 3893eb8

File tree

1 file changed

+73
-14
lines changed

1 file changed

+73
-14
lines changed

src/main/kotlin/com/github/adrienpessu/sarifviewer/configurable/SettingComponent.kt

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,66 @@ import com.intellij.ui.components.JBLabel
44
import com.intellij.ui.components.JBPasswordField
55
import com.intellij.ui.components.JBTextField
66
import com.intellij.util.ui.FormBuilder
7+
import java.awt.GridBagConstraints
8+
import java.awt.GridBagLayout
79
import javax.swing.JComponent
810
import javax.swing.JPanel
11+
import javax.swing.JTextField
12+
import javax.swing.JToggleButton
913

1014

1115
class SettingComponent {
1216
private var myMainPanel: JPanel? = null
13-
private val ghTokenText = JBPasswordField()
17+
private var ghTokenTextField: JTextField =JBTextField()
18+
private var ghTokenPasswordField: JTextField = JBPasswordField()
1419
private val ghesHostnameText = JBTextField()
15-
private val ghesTokenText = JBPasswordField()
20+
private var ghesTokenTextField: JTextField = JBTextField()
21+
private var ghesTokenPasswordField: JTextField = JBPasswordField()
22+
private val toggleButton = JToggleButton("Show/Hide PAT")
23+
24+
private var isGhTokenVisible: Boolean = false
1625

1726
init {
27+
28+
ghTokenTextField.isVisible = isGhTokenVisible
29+
ghesTokenTextField.isVisible = isGhTokenVisible
1830
myMainPanel = FormBuilder.createFormBuilder()
19-
.addLabeledComponent(JBLabel("GitHub.com PAT "), ghTokenText, 1, false)
20-
.addSeparator()
21-
.addLabeledComponent(JBLabel("GHES Hostname"), ghesHostnameText, 2, false)
22-
.addLabeledComponent(JBLabel("GHES PAT"), ghesTokenText, 3, false)
23-
.addComponentFillVertically(JPanel(), 0)
24-
.getPanel()
31+
.addComponent(JBLabel("GitHub.com PAT "))
32+
.addComponent(ghTokenTextField)
33+
.addComponent(ghTokenPasswordField)
34+
.addSeparator(48)
35+
.addComponent(JBLabel("GHES Hostname "))
36+
.addComponent(ghesHostnameText)
37+
.addComponent(JBLabel("GHES PAT "))
38+
.addComponent(ghesTokenTextField)
39+
.addComponent(ghesTokenPasswordField)
40+
.addComponentFillVertically(JPanel(), 0)
41+
.addSeparator()
42+
.addLabeledComponent("", toggleButton, 1, false)
43+
.panel
44+
45+
toggleButton.addActionListener {
46+
isGhTokenVisible = !isGhTokenVisible
47+
if (isGhTokenVisible) {
48+
ghTokenTextField.text = ghTokenPasswordField.text
49+
ghTokenTextField.isVisible = true
50+
ghTokenPasswordField.isVisible = false
51+
52+
ghesTokenTextField.text = ghesTokenPasswordField.text
53+
ghesTokenTextField.isVisible = true
54+
ghesTokenPasswordField.isVisible = false
55+
} else {
56+
ghTokenPasswordField.text = ghTokenTextField.text
57+
ghTokenTextField.isVisible = false
58+
ghTokenPasswordField.isVisible = true
59+
60+
ghesTokenPasswordField.text = ghesTokenTextField.text
61+
ghesTokenTextField.isVisible = false
62+
ghesTokenPasswordField.isVisible = true
63+
}
64+
myMainPanel!!.revalidate() // Notify the layout manager
65+
myMainPanel!!.repaint() // Redraw the components
66+
}
2567
}
2668

2769

@@ -30,30 +72,47 @@ class SettingComponent {
3072
}
3173

3274
fun getPreferredFocusedComponent(): JComponent {
33-
return ghTokenText
75+
return if (toggleButton.isSelected) {
76+
ghTokenTextField
77+
} else {
78+
ghTokenPasswordField
79+
}
3480
}
3581

3682
fun getGhTokenText(): String {
37-
return ghTokenText.text
83+
return if (isGhTokenVisible) {
84+
ghTokenTextField.text
85+
} else {
86+
ghTokenPasswordField.text
87+
}
3888
}
3989

4090
fun setGhTokenText(newText: String) {
41-
ghTokenText.text = newText
91+
ghTokenTextField.text = newText
92+
ghTokenPasswordField.text = newText
4293
}
4394

4495
fun getGhesHostnameText(): String {
4596
return ghesHostnameText.text
4697
}
4798

4899
fun getGhesTokenText(): String {
49-
return ghesTokenText.text
100+
return if (isGhTokenVisible) {
101+
ghesTokenTextField.text
102+
} else {
103+
ghesTokenPasswordField.text
104+
}
50105
}
51106

52107
fun setGhesHostnameText(newText: String) {
53108
ghesHostnameText.text = newText
54109
}
55110

56111
fun setGhesTokenText(newText: String) {
57-
ghesTokenText.text = newText
112+
ghesTokenTextField.text = newText
113+
ghesTokenPasswordField.text = newText
58114
}
59-
}
115+
}
116+
117+
118+

0 commit comments

Comments
 (0)