Provide filename template configuration
This commit is contained in:
parent
958f019a5e
commit
0ec2b040f8
|
|
@ -1,49 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package de.idetools.filename_template
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.ide.IdeBundle
|
||||
import com.intellij.ide.actions.CreateFileAction
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.ui.Messages
|
||||
import com.intellij.psi.PsiDirectory
|
||||
import com.intellij.psi.PsiElement
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
|
||||
class CreateFileWithFilenameTemplateAction(
|
||||
text: String,
|
||||
description: String,
|
||||
private val template: String
|
||||
) :
|
||||
CreateFileAction(text, description, AllIcons.FileTypes.Unknown) {
|
||||
|
||||
override fun invokeDialog(project: Project?, directory: PsiDirectory?): Array<PsiElement> {
|
||||
val validator: MyInputValidator = MyValidator(project, directory)
|
||||
Messages.showInputDialog(
|
||||
project,
|
||||
IdeBundle.message("prompt.enter.new.file.name"),
|
||||
IdeBundle.message("title.new.file"),
|
||||
null,
|
||||
template,
|
||||
validator
|
||||
)
|
||||
return validator.createdElements
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package de.idetools.filename_template.settings
|
||||
|
||||
import com.intellij.openapi.options.Configurable
|
||||
import com.intellij.openapi.options.ConfigurationException
|
||||
import org.jetbrains.annotations.Nls
|
||||
import javax.swing.JComponent
|
||||
|
||||
|
||||
class FilenameTemplateConfigurable : Configurable {
|
||||
private var component: FilenameTemplateSettingsComponent = FilenameTemplateSettingsComponent()
|
||||
|
||||
@Nls(capitalization = Nls.Capitalization.Title)
|
||||
override fun getDisplayName(): String {
|
||||
return "Filename Template Settings"
|
||||
}
|
||||
|
||||
override fun createComponent(): JComponent {
|
||||
return component.panel
|
||||
}
|
||||
|
||||
override fun isModified(): Boolean {
|
||||
val settings: FilenameTemplateSettingsState = FilenameTemplateSettingsState.instance
|
||||
return component.text != settings.text ||
|
||||
component.description != settings.description ||
|
||||
component.template != settings.template
|
||||
}
|
||||
|
||||
@Throws(ConfigurationException::class)
|
||||
override fun apply() {
|
||||
val settings: FilenameTemplateSettingsState = FilenameTemplateSettingsState.instance
|
||||
settings.text = component.text
|
||||
settings.description = component.description
|
||||
settings.template = component.template
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
val settings: FilenameTemplateSettingsState = FilenameTemplateSettingsState.instance
|
||||
component.text = settings.text
|
||||
component.description = settings.description
|
||||
component.template = settings.template
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package de.idetools.filename_template.settings
|
||||
|
||||
import com.intellij.ui.components.JBLabel
|
||||
import com.intellij.ui.components.JBTextField
|
||||
import com.intellij.util.ui.FormBuilder
|
||||
import javax.swing.JPanel
|
||||
|
||||
class FilenameTemplateSettingsComponent {
|
||||
val panel: JPanel
|
||||
private val textField: JBTextField = JBTextField()
|
||||
private val descriptionField: JBTextField = JBTextField()
|
||||
private val templateField: JBTextField = JBTextField()
|
||||
|
||||
var text: String
|
||||
get() = textField.text
|
||||
set(value) {
|
||||
textField.text = value
|
||||
}
|
||||
|
||||
var description: String
|
||||
get() = descriptionField.text
|
||||
set(value) {
|
||||
descriptionField.text = value
|
||||
}
|
||||
|
||||
var template: String
|
||||
get() = templateField.text
|
||||
set(value) {
|
||||
templateField.text = value
|
||||
}
|
||||
|
||||
init {
|
||||
panel = FormBuilder.createFormBuilder()
|
||||
.addLabeledComponent(JBLabel("Text"), textField, 1, false)
|
||||
.addLabeledComponent(JBLabel("Description"), descriptionField, 1, false)
|
||||
.addLabeledComponent(JBLabel("Template"), templateField, 1, false)
|
||||
.addComponentFillVertically(JPanel(), 0)
|
||||
.panel
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package de.idetools.filename_template.settings
|
||||
|
||||
import com.intellij.openapi.components.PersistentStateComponent
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.components.State
|
||||
import com.intellij.openapi.components.Storage
|
||||
import com.intellij.util.xmlb.XmlSerializerUtil
|
||||
|
||||
@State(
|
||||
name = "de.idetools.filename_template.settings.FilenameTemplateSettingsState",
|
||||
storages = [Storage("FilenameTemplateSettings.xml")]
|
||||
)
|
||||
class FilenameTemplateSettingsState : PersistentStateComponent<FilenameTemplateSettingsState> {
|
||||
var text = ""
|
||||
var description = ""
|
||||
var template = ""
|
||||
|
||||
override fun getState(): FilenameTemplateSettingsState {
|
||||
return this
|
||||
}
|
||||
|
||||
override fun loadState(state: FilenameTemplateSettingsState) {
|
||||
XmlSerializerUtil.copyBean(state, this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val instance: FilenameTemplateSettingsState
|
||||
get() = ServiceManager.getService(FilenameTemplateSettingsState::class.java)
|
||||
}
|
||||
}
|
||||
|
|
@ -12,13 +12,15 @@
|
|||
<depends>com.intellij.modules.platform</depends>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<notificationGroup id="Filename Template Notification Group" displayType="BALLOON"/>
|
||||
<applicationService
|
||||
serviceImplementation="de.idetools.filename_template.settings.FilenameTemplateSettingsState"/>
|
||||
<applicationConfigurable parentId="tools"
|
||||
instance="de.idetools.filename_template.settings.FilenameTemplateConfigurable"
|
||||
id="de.idetools.filename_template.settings.FilenameTemplateConfigurable"
|
||||
displayName="Filename Template Settings"/>
|
||||
</extensions>
|
||||
|
||||
<actions>
|
||||
<!-- Add your actions here -->
|
||||
<action id="FilenameTemplateAction" class="CreateFileWithFilenameTemplateAction" text="Filename Template Action">
|
||||
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewScratchFile"/>
|
||||
</action>
|
||||
</actions>
|
||||
</idea-plugin>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user