github: extract DvcsTextFieldWithBrowseButton to be able to reuse it

GitOrigin-RevId: f3c3ad8c37561a479ee8101236a86cde6d4843f8
This commit is contained in:
Aleksandr Krasilnikov
2019-07-23 17:44:15 +03:00
committed by intellij-monorepo-bot
parent e18fe52b3a
commit d625488698
3 changed files with 39 additions and 34 deletions
@@ -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);
}
@@ -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
}
}
}
}
}
@@ -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
}
}
}
}