intellij 插件教程_IntelliJ插件开发教程Pt。 2 –处理用户输入

intellij 插件教程

在我们的intellij插件开发教程的上一部分中 ,我们了解了如何在intellij中创建插件。 我们创建了一个简单的通知,并了解了如何创建操作,如何向用户组添加操作以及如何在用户单击操作时执行任务。

在下面的教程中,我们将了解如何处理用户输入。 我们将创建一个简单的插件,以接收用户输入的firstName和lastName。 然后显示一个气球通知。

如果您没有看过上一篇有关intellij插件开发入门的文章,我建议您快速阅读。 尽管不是强制性的,但它是使您熟悉操作的很好的读物。

首先,创建一个新的I ntellij插件项目。 该过程与上一篇文章中介绍的过程类似

我们将使用gradle作为构建工具,并包括Java和Kotlin的依赖项。

接下来,在kotlin目录中创建一个Kotlin类。 我已将其命名为DisplayInputDialog.kt ,您可以根据需要命名。

AnAction类扩展该类,并重写onActionPerformed方法。 此方法将包含我们的代码以向用户显示输入对话框。

 class DisplayInputBox : AnAction() { 
     override fun actionPerformed(e: AnActionEvent) { 
     }  } 

最后,我们需要在plugin.xml中注册此动作类。 最后,将其添加到MainMenu中。

 <actions> 
     <group id= "TopMenu" text= "InputBox" description= "Handling user input demo" > 
         <add-to-group group-id= "MainMenu" anchor= "last" /> 
         <action id= "DisplayInputBox" class = "DisplayInputBox" text= "InputBox Demo" /> 
     </group>  </actions> 

现在我们需要为输入框创建一个对话框 。 我们将创建一个自定义DialogWrapper 。 DialogWrapper是intellij.openapi.ui软件包中的抽象类。

创建一个名为MyCustomDialog.kt的类,并从DialogWrapper类进行扩展。

覆盖createPanel函数,我们将从该函数返回面板(对话框)。 对于intellij插件开发教程的这一部分,我们需要:2个文本标签和2个输入字段。

 class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     override fun createCenterPanel(): JComponent? { 
     }  } 

初始化一个容器(JPanel)和两个文本输入字段(JTextFields)。 另外,我们将使用JBLabel创建标签。

 class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     private val firstName: JTextField = JTextField() 
     private val lastName: JTextField = JTextField() 
     val centerPanel: JPanel = JPanel(GridBagLayout()) 
     override fun createCenterPanel(): JComponent? { 
     } 
     private fun getLabel(text: String): JComponent { 
         val label = JBLabel(text) 
         label.componentStyle = UIUtil.ComponentStyle.SMALL 
         label.fontColor = UIUtil.FontColor.BRIGHTER 
         label.border = JBUI.Borders.empty( 0 , 5 , 2 , 0 ) 
         return label 
     }  } 

我们将使用GridBagLayout作为布局管理器。 它用于水平,垂直或沿基线布置组件。 它等效于Android开发中的LinearLayout或Web开发中的<div>。

接下来,创建一个GridBag。 这将用于在布局时为我们的组件提供约束。 要添加文本字段和标签,请看下面的代码。

 class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     private val firstName: JTextField = JTextField() 
     private val lastName: JTextField = JTextField() 
     val centerPanel: JPanel = JPanel(GridBagLayout()) 
     override fun createCenterPanel(): JComponent? { 
         val gridbag = GridBag() 
             .setDefaultWeightX( 1.0 ) 
             .setDefaultFill(GridBagConstraints.HORIZONTAL) 
             .setDefaultInsets(Insets( 0 , 0 , AbstractLayout.DEFAULT_VGAP, AbstractLayout.DEFAULT_HGAP)) 
         return centerPanel 
     } 
     private fun getLabel(text: String): JComponent { 
         val label = JBLabel(text) 
         label.componentStyle = UIUtil.ComponentStyle.SMALL 
         label.fontColor = UIUtil.FontColor.BRIGHTER 
         label.border = JBUI.Borders.empty( 0 , 5 , 2 , 0 ) 
         return label 
     }  } 

水平总权重为1。请注意,我们已经为标签分配了0.2权重,为输入字段分配了0.8权重。 这意味着标签将占用20%的可用空间,文本字段将占用80%的可用空间。 这使其可以灵活地用于小屏幕。 这是添加标签和文本输入字段的代码。

 class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     private val firstName: JTextField = JTextField() 
     private val lastName: JTextField = JTextField() 
     val centerPanel: JPanel = JPanel(GridBagLayout()) 
     override fun createCenterPanel(): JComponent? { 
         val gridbag = GridBag() 
             .setDefaultWeightX( 1.0 ) 
             .setDefaultFill(GridBagConstraints.HORIZONTAL) 
             .setDefaultInsets(Insets( 0 , 0 , AbstractLayout.DEFAULT_VGAP, AbstractLayout.DEFAULT_HGAP)) 
         centerPanel.preferredSize = Dimension( 400 , 50 ) 
         centerPanel.add(getLabel( "First Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(firstName, gridbag.next().weightx( 0.8 )) 
         centerPanel.add(getLabel( "Last Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(lastName, gridbag.next().weightx( 0.8 )) 
         return centerPanel 
     } 
     private fun getLabel(text: String): JComponent { 
         val label = JBLabel(text) 
         label.componentStyle = UIUtil.ComponentStyle.SMALL 
         label.fontColor = UIUtil.FontColor.BRIGHTER 
         label.border = JBUI.Borders.empty( 0 , 5 , 2 , 0 ) 
         return label 
     }  } 

现在我们已经设置好对话框,我们要做的就是在用户单击OK时显示通知。

要处理此问题,请覆盖doOkAction函数。 每当用户单击“确定”按钮时,就会调用此方法。 这是我们向用户触发通知的地方。

 class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     private val firstName: JTextField = JTextField() 
     private val lastName: JTextField = JTextField() 
     val centerPanel: JPanel = JPanel(GridBagLayout()) 
     override fun createCenterPanel(): JComponent? { 
         val gridbag = GridBag() 
             .setDefaultWeightX( 1.0 ) 
             .setDefaultFill(GridBagConstraints.HORIZONTAL) 
             .setDefaultInsets(Insets( 0 , 0 , AbstractLayout.DEFAULT_VGAP, AbstractLayout.DEFAULT_HGAP)) 
         centerPanel.preferredSize = Dimension( 400 , 50 ) 
         centerPanel.add(getLabel( "First Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(firstName, gridbag.next().weightx( 0.8 )) 
         centerPanel.add(getLabel( "Last Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(lastName, gridbag.next().weightx( 0.8 )) 
         return centerPanel 
     } 
     private fun getLabel(text: String): JComponent { 
         val label = JBLabel(text) 
         label.componentStyle = UIUtil.ComponentStyle.SMALL 
         label.fontColor = UIUtil.FontColor.BRIGHTER 
         label.border = JBUI.Borders.empty( 0 , 5 , 2 , 0 ) 
         return label 
     } 
     override fun doOKAction() { 
         NotificationManager.createAndShowNotification(project, firstName.text, lastName.text) 
         super .doOKAction() 
     }  } 

注意,我们在super.doOkAction()调用之前添加了代码。 这样做是为了确保在执行默认操作之前可以验证用户输入! 但是我们将专注于仅显示我们的通知。

在kotlin目录中创建一个名为NotificationManager的类,并创建一个名为createAndShowNotification的方法。 它包含3个参数:项目,firstName和lastName。 最后添加代码以显示通知。

 class NotificationManager { 
     companion object { 
         @JvmStatic 
         fun createAndShowNotification(project: Project, firstName: String, lastName: String) { 
             val notification = NotificationGroup( "AndroidVille" , NotificationDisplayType.BALLOON, true ) 
             notification.createNotification( 
                 "Hola!" , 
                 "Welcome to AndroidVille $firstName $lastName" , 
                 NotificationType.INFORMATION, 
                 null 
             ).notify(project) 
         } 
     }  } 

提醒:请查看上一篇文章 ,其中详细介绍了操作,组以及如何在intellij插件中创建通知。

我们快完成了! 进入MyCustomDialog类,并在init {…}块中调用init()方法并为对话框设置标题。 最终结果如下所示。

 import com.intellij.openapi.project.Project  import com.intellij.openapi.ui.DialogWrapper  import com.intellij.ui.components.JBLabel  import com.intellij.uiDesigner.core.AbstractLayout  import com.intellij.util.ui.GridBag  import com.intellij.util.ui.JBUI  import com.intellij.util.ui.UIUtil  import java.awt.Dimension  import java.awt.GridBagConstraints  import java.awt.GridBagLayout  import java.awt.Insets  import javax.swing.JComponent  import javax.swing.JPanel  import javax.swing.JTextField  class MyCustomDialog(val project: Project) : DialogWrapper( true ) { 
     private val firstName: JTextField = JTextField() 
     private val lastName: JTextField = JTextField() 
     val centerPanel: JPanel = JPanel(GridBagLayout()) 
     init { 
         init() 
         title = "AndroidVille Dialog" 
     } 
     override fun createCenterPanel(): JComponent? { 
         val gridbag = GridBag() 
             .setDefaultWeightX( 1.0 ) 
             .setDefaultFill(GridBagConstraints.HORIZONTAL) 
             .setDefaultInsets(Insets( 0 , 0 , AbstractLayout.DEFAULT_VGAP, AbstractLayout.DEFAULT_HGAP)) 
         centerPanel.preferredSize = Dimension( 400 , 50 ) 
         centerPanel.add(getLabel( "First Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(firstName, gridbag.next().weightx( 0.8 )) 
         centerPanel.add(getLabel( "Last Name: " ), gridbag.nextLine().next().weightx( 0.2 )) 
         centerPanel.add(lastName, gridbag.next().weightx( 0.8 )) 
         return centerPanel 
     } 
     private fun getLabel(text: String): JComponent { 
         val label = JBLabel(text) 
         label.componentStyle = UIUtil.ComponentStyle.SMALL 
         label.fontColor = UIUtil.FontColor.BRIGHTER 
         label.border = JBUI.Borders.empty( 0 , 5 , 2 , 0 ) 
         return label 
     } 
     override fun doOKAction() { 
         NotificationManager.createAndShowNotification(project, firstName.text, lastName.text) 
         super .doOKAction() 
     }  } 

现在我们需要做的就是显示输入对话框。 我们在actionPerformed方法内的DisplayInputBox类内创建MyCustomDialog的新实例。

最后,在实例上调用showAndGet方法。 如果单击“确定”,则此方法返回true,否则返回false。

 import com.intellij.openapi.actionSystem.AnAction  import com.intellij.openapi.actionSystem.AnActionEvent  class DisplayInputBox : AnAction() { 
     override fun actionPerformed(e: AnActionEvent) { 
         if (e.project == null ) return 
         val wrapper = MyCustomDialog(e.project!!) 
         if (wrapper.showAndGet()) { 
             println( "Successfully handled user input" ) 
         } 
     }  } 

我们完成了intellij插件开发教程。 我们需要做的就是测试我们的插件。 因此,单击播放按钮。 将打开一个新的IDE实例,您会在MainMenu的最右边看到您的插件!

我有一个简单的任务可以帮助您更好地理解这些概念。 我希望您处理此项目中的验证。 实现以下功能:

  1. 如果用户尝试输入空白字段,请向他显示错误对话框/通知。
  2. 如果用户按下“取消”按钮,请向他显示通知/对话框。

这些都是快速,容易实现的,并且会为您自己的插件创建UI,对话框,通知而使您不知所措🙂完成注释部分中的任务后,请告诉我。

如果您遇到任何阻碍,请在评论部分让我知道,我们将很乐意答复并亲自为您提供帮助。

欢迎使用AndroidVille

AndroidVille是一个移动开发人员社区,我们在其中共享与Android开发,Flutter开发,React Native教程,Java,Kotlin等相关的知识。

我们有一个SLACK工作区 ,我们在其中共享与新工作机会相关的更新,有关移动开发的文章/行业更新。 我们还提供渠道来帮助您解决任何问题,例如您需要的开发帮助。 只需提出一个问题,人们就会随时准备帮助您。

翻译自: https://www.javacodegeeks.com/2020/04/intellij-plugin-development-tutorial-pt-2-handling-user-input.html

intellij 插件教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值