在基础篇,我们接触到了AnAction这个类,现在我们深入了解一些其他的相关类。包括:
ModelWizardStep
WizardModel
ModelWizard
Validator
ValidatorPanel
TextFieldWithBrowseButton
StringValueProperty
BoolValueProperty
Messages
Notifications
我们知道Wizard是向导的意思。首先我们来看一下ModelWizard,源码是这样解释的。
/**
* A wizard that owns a series of {@link ModelWizardStep}s. When finished, it iterates through its
* steps, queries the {@link WizardModel} they're associated with, and calls their
* {@link WizardModel#handleFinished()} method.
* <p/>
* In this way, users of this framework can design steps which handle the UI logic while putting
* all non-UI business logic in a data model class.
*
* To avoid memory leaks, you must dispose a wizard when you're done with it (although this may be
* done for you by a wrapping class, such as a model wizard dialog).
*/
一个向导拥有一个ModelWizardStep序列,当Finish的时候,通过它的步骤(steps)迭代,查询和它们交互的WizardModel,并且调它们的handleFinished方法。因此,这个框架的用户能够设计处理这些UI逻辑的步骤,在放所有非UI业务逻辑在一个data model类中的时候。 为了避免内存泄漏,当你完成的时候,你必须清除掉一个wizard(虽然这被一个包裹类完成了,诸如ModelWizardDialog)。大概意思就是说,这是一个控制整体流程的控制器,而我们具体的向导步骤是通过继承ModelWizardStep来实现的。 我们再来看一下ModelWizardStep这个关键的类,它里面有一些生命周期方法。比如onWizardStarting、onWizardFinished、onProceeding、onEntering,以及canGoForward、canGoBack,能不能点上一步,下一步。WizardModel用来承载我们业务的数据,为了防止内存泄漏,我们用它推荐的StringValueProperty和BoolValueProperty等包裹一下数据。我们再来看一下Validator。
class MyPathValidator : Validator<String> {
override fun validate(value: String): Validator.Result {
if (value.isEmpty()) {
return Validator.Result(Validator.Severity.ERROR, "路径是必须的")
}
return Validator.Result.OK
}
}
myValidatorPanel.registerValidator(model.myPathProperty, MyPathValidator())
校验器用来让用户的输入规范化,如果不符合规范,则会显示错误提示。 如果你需要选择一个文件路径,你可以用TextFieldWithBrowseButton
这通常是非常有用的。当然你也可以用Swing的其他组件,如JLabel、JTextField、JButton、JComboBox、JCheckBox等。
如果你要弹一个消息弹窗,你可以使用Messages.showMessageDialog。如果你要显示通知,你可以使用NotificationGroup.create创建一个通知组,再通过group.createNotification创建通知,最后通过Notifications.Bus.notify()显示这个通知。