这种功能我们一般把秘钥让用户自己设置。对吧
那么就需要一个设置页面
如果没设置就点不进去
那么如何操作呢
书接上回
清零已有的key
data class TrelloState(
var apiKey:String="",
var token:String="",
var fromListId:String="",
var toListId:String="",
// @OptionTag(converter = MyDataTimeConverter::class) val data: DateTime?=null
)
在同services、或者同actions的目录上面 新建package settings
将配置都拿进来
package com.anguomob.anguo.settings
import com.anguomob.anguo.actions.trello.TrelloState
import com.anguomob.anguo.services.TrelloService
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import com.intellij.ui.layout.rowWithIndent
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.JPasswordField
import javax.swing.JTextField
import com.intellij.ui.layout.panel
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener
class TrelloSettings(private val project: Project) : Configurable, DocumentListener {
private val state: TrelloState by lazy {
TrelloService.getInstance(project).state
}
private val apiKeyField: JPasswordField = JPasswordField()
private val tokenKeyField: JPasswordField = JPasswordField()
private val fromListIdField: JTextField = JTextField()
private val toListIdField: JTextField = JTextField()
private var modified = false
private val panle: JPanel = panel {
row("Api Key") { apiKeyField() }
row("Token") { tokenKeyField() }
row("From List id") { fromListIdField() }
row("To List Id") { toListIdField() }
modified = false
}
override fun createComponent(): JComponent? {
apiKeyField.apply {
text = state.apiKey
document.addDocumentListener(this@TrelloSettings)
}
tokenKeyField.apply {
text = state.token
document.addDocumentListener(this@TrelloSettings)
}
fromListIdField.apply {
text = state.fromListId
document.addDocumentListener(this@TrelloSettings)
}
toListIdField.apply {
text = state.toListId
document.addDocumentListener(this@TrelloSettings)
}
return panle
}
override fun isModified(): Boolean {
return modified
}
override fun apply() {
state.apiKey = String(apiKeyField.password)
state.token = String(tokenKeyField.password)
state.fromListId = fromListIdField.text
state.toListId = toListIdField.text
TrelloService.getInstance(project).loadState(state)
}
override fun getDisplayName(): String {
return "安果Trello设置"
}
override fun insertUpdate(e: DocumentEvent?) {
modified = true
}
override fun removeUpdate(e: DocumentEvent?) {
modified = true
}
override fun changedUpdate(e: DocumentEvent?) {
modified = true
}
}
配置到Plugins当中去
<extensions defaultExtensionNs="com.intellij">
<projectConfigurable instance="com.anguomob.anguo.settings.TrelloSettings"/>
<projectService serviceImplementation="com.anguomob.anguo.services.TrelloService"/>
</extensions>
然后就行了。非常的方便。