From d625488698220232f6e13a705acb3798a7c0bd31 Mon Sep 17 00:00:00 2001 From: Aleksandr Krasilnikov Date: Wed, 3 Jul 2019 14:27:01 +0300 Subject: [PATCH] github: extract DvcsTextFieldWithBrowseButton to be able to reuse it GitOrigin-RevId: f3c3ad8c37561a479ee8101236a86cde6d4843f8 --- .../dvcs/ui/CloneDvcsValidationUtils.java | 6 ++-- .../dvcs/ui/DvcsCloneDialogComponent.kt | 32 +---------------- .../SelectChildTextFieldWithBrowseButton.kt | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 34 deletions(-) create mode 100644 platform/dvcs-impl/src/com/intellij/dvcs/ui/SelectChildTextFieldWithBrowseButton.kt diff --git a/platform/dvcs-impl/src/com/intellij/dvcs/ui/CloneDvcsValidationUtils.java b/platform/dvcs-impl/src/com/intellij/dvcs/ui/CloneDvcsValidationUtils.java index 4ca6ef35079d..6787c8fb2eaa 100644 --- a/platform/dvcs-impl/src/com/intellij/dvcs/ui/CloneDvcsValidationUtils.java +++ b/platform/dvcs-impl/src/com/intellij/dvcs/ui/CloneDvcsValidationUtils.java @@ -30,7 +30,7 @@ public class CloneDvcsValidationUtils { @Nullable - static ValidationInfo createDestination(@NotNull String path) { + public static ValidationInfo createDestination(@NotNull String path) { try { Path directoryPath = Paths.get(path); if (!directoryPath.toFile().exists()) { @@ -55,7 +55,7 @@ public class CloneDvcsValidationUtils { * @return null if destination directory is OK. */ @Nullable - static ValidationInfo checkDirectory(String directoryPath, JTextField component) { + public static ValidationInfo checkDirectory(String directoryPath, JTextField component) { if (directoryPath.length() == 0) { return new ValidationInfo(""); } @@ -89,7 +89,7 @@ public class CloneDvcsValidationUtils { * @return null if repository URL is OK. */ @Nullable - static ValidationInfo checkRepositoryURL(JComponent component, String repository) { + public static ValidationInfo checkRepositoryURL(JComponent component, String repository) { if (repository.length() == 0) { return new ValidationInfo(DvcsBundle.getString("clone.repository.url.error.empty"), component); } diff --git a/platform/dvcs-impl/src/com/intellij/dvcs/ui/DvcsCloneDialogComponent.kt b/platform/dvcs-impl/src/com/intellij/dvcs/ui/DvcsCloneDialogComponent.kt index a3dfb7369d5b..71690d9915f4 100644 --- a/platform/dvcs-impl/src/com/intellij/dvcs/ui/DvcsCloneDialogComponent.kt +++ b/platform/dvcs-impl/src/com/intellij/dvcs/ui/DvcsCloneDialogComponent.kt @@ -5,7 +5,6 @@ import com.intellij.dvcs.DvcsRememberedInputs import com.intellij.dvcs.repo.ClonePathProvider import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory import com.intellij.openapi.project.Project -import com.intellij.openapi.ui.TextFieldWithBrowseButton import com.intellij.openapi.ui.ValidationInfo import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vcs.CheckoutProvider @@ -14,8 +13,6 @@ import com.intellij.ui.DocumentAdapter import com.intellij.ui.components.JBTextField import com.intellij.ui.layout.* import com.intellij.util.containers.ContainerUtil -import java.nio.file.InvalidPathException -import java.nio.file.Paths import javax.swing.JPanel import javax.swing.event.DocumentEvent @@ -24,7 +21,7 @@ abstract class DvcsCloneDialogComponent(var project: Project, rememberedInputs: DvcsRememberedInputs) : VcsCloneComponent { private val mainPanel: JPanel private val urlEditor = JBTextField() - private val directoryField = MyTextFieldWithBrowseButton(ClonePathProvider.defaultParentDirectoryPath(project, rememberedInputs)) + private val directoryField = SelectChildTextFieldWithBrowseButton(ClonePathProvider.defaultParentDirectoryPath(project, rememberedInputs)) init { val fcd = FileChooserDescriptorFactory.createSingleFolderDescriptor() @@ -68,31 +65,4 @@ abstract class DvcsCloneDialogComponent(var project: Project, fun getDirectory() = directoryField.text fun getUrl() = urlEditor.text - - class MyTextFieldWithBrowseButton constructor(defaultParentPath: String) : TextFieldWithBrowseButton() { - private val myDefaultParentPath = Paths.get(defaultParentPath).toAbsolutePath() - private var myModifiedByUser = false - - init { - text = myDefaultParentPath.toString() - textField.document.addDocumentListener(object : DocumentAdapter() { - override fun textChanged(e: DocumentEvent) { - myModifiedByUser = true - } - }) - } - - fun trySetChildPath(child: String) { - if (!myModifiedByUser) { - try { - text = myDefaultParentPath.resolve(child).toString() - } - catch (ignored: InvalidPathException) { - } - finally { - myModifiedByUser = false - } - } - } - } } \ No newline at end of file diff --git a/platform/dvcs-impl/src/com/intellij/dvcs/ui/SelectChildTextFieldWithBrowseButton.kt b/platform/dvcs-impl/src/com/intellij/dvcs/ui/SelectChildTextFieldWithBrowseButton.kt new file mode 100644 index 000000000000..5261a1a4c4ad --- /dev/null +++ b/platform/dvcs-impl/src/com/intellij/dvcs/ui/SelectChildTextFieldWithBrowseButton.kt @@ -0,0 +1,35 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.dvcs.ui + +import com.intellij.openapi.ui.TextFieldWithBrowseButton +import com.intellij.ui.DocumentAdapter +import java.nio.file.InvalidPathException +import java.nio.file.Paths +import javax.swing.event.DocumentEvent + +class SelectChildTextFieldWithBrowseButton constructor(defaultParentPath: String) : TextFieldWithBrowseButton() { + private val myDefaultParentPath = Paths.get(defaultParentPath).toAbsolutePath() + private var myModifiedByUser = false + + init { + text = myDefaultParentPath.toString() + textField.document.addDocumentListener(object : DocumentAdapter() { + override fun textChanged(e: DocumentEvent) { + myModifiedByUser = true + } + }) + } + + fun trySetChildPath(child: String) { + if (!myModifiedByUser) { + try { + text = myDefaultParentPath.resolve(child).toString() + } + catch (ignored: InvalidPathException) { + } + finally { + myModifiedByUser = false + } + } + } +} \ No newline at end of file