Android Studio 插件开发4自定义配置文件

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 插件开发4自定义配置文件相关的知识,希望对你有一定的参考价值。

 这种功能我们一般把秘钥让用户自己设置。对吧

那么就需要一个设置页面

如果没设置就点不进去

那么如何操作呢

 书接上回

清零已有的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>

 然后就行了。非常的方便。

以上是关于Android Studio 插件开发4自定义配置文件的主要内容,如果未能解决你的问题,请参考以下文章

4.0Android Studio配置你的构建

4.0Android Studio配置你的构建

Android Gradle 插件将自定义 Gradle 插件上传到自建 Maven 仓库 ④ ( 默认生成的 pom 文件 | Maven 中的 pom 配置 | 自定义 pom 文件节点 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ④ ( 为自定义 Gradle 插件的扩展配置扩展 | 在自定义插件中获取扩展属性 )

Android Gradle 插件将自定义 Gradle 插件上传到自建 Maven 仓库 ⑥ ( 配置 Sonatype Nexus 搭建的 Maven 私服 | 配置端口号 | 配置JVM )

Android Gradle 插件自定义 Gradle 任务 ① ( Gradle 面板显示任务列表 | 自定义任务生成与显示分组 )