Use CreateFileAction as action base
This actually creates the file already. The only task is now to provide the initial filename template.
This commit is contained in:
parent
531a8a1b6f
commit
958f019a5e
49
src/main/kotlin/CreateFileWithFilenameTemplateAction.kt
Normal file
49
src/main/kotlin/CreateFileWithFilenameTemplateAction.kt
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import com.intellij.icons.AllIcons
|
||||||
|
import com.intellij.ide.IdeBundle
|
||||||
|
import com.intellij.ide.actions.CreateFileAction
|
||||||
|
import com.intellij.ide.ui.newItemPopup.NewItemPopupUtil
|
||||||
|
import com.intellij.ide.ui.newItemPopup.NewItemSimplePopupPanel
|
||||||
|
import com.intellij.lang.LangBundle
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.ui.InputValidatorEx
|
||||||
|
import com.intellij.openapi.ui.popup.JBPopup
|
||||||
|
import com.intellij.psi.PsiDirectory
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import java.awt.event.InputEvent
|
||||||
|
import java.util.function.Consumer
|
||||||
|
|
||||||
|
class CreateFileWithFilenameTemplateAction :
|
||||||
|
CreateFileAction("Flyway File", "Create flyway migration file", AllIcons.FileTypes.Unknown) {
|
||||||
|
override fun invokeDialog(
|
||||||
|
project: Project,
|
||||||
|
directory: PsiDirectory,
|
||||||
|
elementsConsumer: Consumer<Array<PsiElement?>?>
|
||||||
|
) {
|
||||||
|
val validator: MyInputValidator = MyValidator(project, directory)
|
||||||
|
createLightWeightPopup(validator, elementsConsumer).showCenteredInCurrentWindow(project)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createLightWeightPopup(
|
||||||
|
validator: MyInputValidator,
|
||||||
|
consumer: Consumer<Array<PsiElement?>?>
|
||||||
|
): JBPopup {
|
||||||
|
val contentPanel = NewItemSimplePopupPanel()
|
||||||
|
val nameField = contentPanel.textField
|
||||||
|
|
||||||
|
val popup = NewItemPopupUtil.createNewItemPopup(IdeBundle.message("title.new.file"), contentPanel, nameField)
|
||||||
|
contentPanel.applyAction = com.intellij.util.Consumer { event: InputEvent? ->
|
||||||
|
val name = nameField.text
|
||||||
|
if (validator.checkInput(name) && validator.canClose(name)) {
|
||||||
|
popup.closeOk(event)
|
||||||
|
consumer.accept(validator.createdElements)
|
||||||
|
} else {
|
||||||
|
val errorMessage =
|
||||||
|
if (validator is InputValidatorEx) (validator as InputValidatorEx).getErrorText(name) else LangBundle.message(
|
||||||
|
"incorrect.name"
|
||||||
|
)
|
||||||
|
contentPanel.setError(errorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return popup
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
import com.intellij.notification.NotificationGroupManager
|
|
||||||
import com.intellij.notification.NotificationType
|
|
||||||
import com.intellij.openapi.actionSystem.AnAction
|
|
||||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
|
||||||
import com.intellij.openapi.ui.popup.JBPopup
|
|
||||||
import com.intellij.openapi.ui.popup.JBPopupFactory
|
|
||||||
import com.intellij.openapi.ui.popup.JBPopupListener
|
|
||||||
import com.intellij.openapi.ui.popup.LightweightWindowEvent
|
|
||||||
import com.intellij.ui.components.JBTextField
|
|
||||||
import java.awt.FlowLayout
|
|
||||||
import java.awt.event.ActionEvent
|
|
||||||
import java.awt.event.ActionListener
|
|
||||||
import javax.swing.JPanel
|
|
||||||
|
|
||||||
class FilenameTemplateAction : AnAction() {
|
|
||||||
private var filenameTemplateField: JBTextField = JBTextField()
|
|
||||||
private var filenameTemplatePopup: JBPopup? = null
|
|
||||||
|
|
||||||
init {
|
|
||||||
filenameTemplateField.columns = 20
|
|
||||||
filenameTemplateField.addActionListener(AddStatusClickListener())
|
|
||||||
}
|
|
||||||
|
|
||||||
private inner class AddStatusClickListener : ActionListener {
|
|
||||||
override fun actionPerformed(e: ActionEvent) {
|
|
||||||
filenameTemplatePopup!!.closeOk(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun actionPerformed(event: AnActionEvent) {
|
|
||||||
|
|
||||||
val statusTextFieldPanel = JPanel(FlowLayout(FlowLayout.CENTER)).also {
|
|
||||||
it.add(filenameTemplateField)
|
|
||||||
}
|
|
||||||
|
|
||||||
filenameTemplatePopup =
|
|
||||||
JBPopupFactory.getInstance().createComponentPopupBuilder(statusTextFieldPanel, filenameTemplateField)
|
|
||||||
.setShowBorder(true)
|
|
||||||
.setShowShadow(true)
|
|
||||||
.setRequestFocus(true)
|
|
||||||
.setCancelOnWindowDeactivation(true)
|
|
||||||
.createPopup()
|
|
||||||
|
|
||||||
val listener = object : JBPopupListener {
|
|
||||||
override fun onClosed(e: LightweightWindowEvent) {
|
|
||||||
super.onClosed(e)
|
|
||||||
if (e.isOk) {
|
|
||||||
NotificationGroupManager
|
|
||||||
.getInstance()
|
|
||||||
.getNotificationGroup("Filename Template Notification Group")
|
|
||||||
.createNotification(filenameTemplateField.text, NotificationType.INFORMATION)
|
|
||||||
.notify(event.project)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
filenameTemplatePopup!!.addListener(listener)
|
|
||||||
filenameTemplatePopup!!.showInFocusCenter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
<actions>
|
<actions>
|
||||||
<!-- Add your actions here -->
|
<!-- Add your actions here -->
|
||||||
<action id="FilenameTemplateAction" class="FilenameTemplateAction" text="Filename Template Action">
|
<action id="FilenameTemplateAction" class="CreateFileWithFilenameTemplateAction" text="Filename Template Action">
|
||||||
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewScratchFile"/>
|
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewScratchFile"/>
|
||||||
</action>
|
</action>
|
||||||
</actions>
|
</actions>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user