Skip to content

Commit 615cd65

Browse files
committed
feat: 文件名输入框口支持预设内容
1 parent 74fca84 commit 615cd65

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/main/kotlin/net/allape/component/FileNameTextFieldDialog.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ import java.util.*
1717
*/
1818
class FileNameTextFieldDialog(private val project: Project) {
1919

20-
fun openDialog (isDirectory: Boolean, consumer: Consumer<String>) {
21-
val validator = FileNameValidator(isDirectory)
20+
fun openDialog (isDirectory: Boolean, value: String? = null, consumer: Consumer<String>) {
21+
val validator = FileNameValidator(isDirectory, value)
2222
if (Experiments.getInstance().isFeatureEnabled("show.create.new.element.in.popup")) {
23-
createLightWeightPopup(validator, consumer).showCenteredInCurrentWindow(project)
23+
createLightWeightPopup(validator, value, consumer).showCenteredInCurrentWindow(project)
2424
} else {
2525
Messages.showInputDialog(
2626
this.project, "Create something new",
27-
"New File", null, null, validator
27+
"New ${if (isDirectory) "Folder" else "File"}", null, value, validator
2828
)
2929
}
3030
}
3131

32-
private fun createLightWeightPopup(validator: FileNameValidator, consumer: Consumer<String>): JBPopup {
32+
private fun createLightWeightPopup(validator: FileNameValidator, value: String? = null, consumer: Consumer<String>): JBPopup {
3333
val contentPanel = NewItemSimplePopupPanel()
3434
val nameField = contentPanel.textField
35+
if (value != null) nameField.text = value
3536
return NewItemPopupUtil.createNewItemPopup("New ${validator.objectName.replaceFirstChar { it.uppercaseChar() }}", contentPanel, nameField).also { popup ->
3637
contentPanel.applyAction = Consumer { event: InputEvent? ->
3738
val name = nameField.text
@@ -47,7 +48,10 @@ class FileNameTextFieldDialog(private val project: Project) {
4748

4849
}
4950

50-
class FileNameValidator(private val isDirectory: Boolean) : InputValidatorEx {
51+
class FileNameValidator(
52+
private val isDirectory: Boolean,
53+
private val value: String? = null,
54+
) : InputValidatorEx {
5155

5256
val objectName: String = if (isDirectory) "folder" else "file"
5357

@@ -63,6 +67,10 @@ class FileNameValidator(private val isDirectory: Boolean) : InputValidatorEx {
6367
errorText = "$objectName name can NOT end withs ${ExplorerBaseWindow.FILE_SEP}"
6468
return false
6569
}
70+
if (value == inputString) {
71+
errorText = "$value already exists"
72+
return false
73+
}
6674
val tokenizer = StringTokenizer(
6775
parsedName,
6876
ExplorerBaseWindow.FILE_SEP,

src/main/kotlin/net/allape/xftp/ExplorerWindow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ class ExplorerWindow(
555555
val file = files[0]
556556
val isDir = file.isDir()
557557

558-
FileNameTextFieldDialog(project).openDialog(isDir) { filename ->
558+
FileNameTextFieldDialog(project).openDialog(isDir, file.name()) { filename ->
559559
executeOnPooledThreadWithRemoteUILocked({
560560
val parsedFileName = joinWithCurrentRemotePath(filename.trim())
561561

@@ -584,7 +584,7 @@ class ExplorerWindow(
584584
val file = files[0]
585585
val isDir = file.isDir()
586586

587-
FileNameTextFieldDialog(project).openDialog(isDir) { filename ->
587+
FileNameTextFieldDialog(project).openDialog(isDir, file.name()) { filename ->
588588
executeOnPooledThreadWithRemoteUILocked({
589589
val parsedFileName = joinWithCurrentRemotePath(filename.trim())
590590

0 commit comments

Comments
 (0)