From 3bf458406f10459aaff0cb5a81df0f5eb0721691 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 20 Mar 2018 15:06:31 +0100 Subject: [PATCH] Components.dialog.ok param should return List, so, client can validate dialog on ok --- .../diagnostic/JetBrainsAccountDialog.kt | 6 +-- .../com/intellij/ui/components/components.kt | 13 ++++- .../src/RepositoryService.kt | 50 ++++--------------- .../src/readOnlySourcesEditor.kt | 23 ++------- .../settings-repository/src/upstreamEditor.kt | 16 ++---- 5 files changed, 33 insertions(+), 75 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/diagnostic/JetBrainsAccountDialog.kt b/platform/platform-impl/src/com/intellij/diagnostic/JetBrainsAccountDialog.kt index a4d0c0498424..80f446eb0883 100644 --- a/platform/platform-impl/src/com/intellij/diagnostic/JetBrainsAccountDialog.kt +++ b/platform/platform-impl/src/com/intellij/diagnostic/JetBrainsAccountDialog.kt @@ -1,6 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2018 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.diagnostic import com.intellij.CommonBundle @@ -50,6 +48,6 @@ fun showJetBrainsAccountDialog(parent: Component, project: Project? = null): Dia if (!userName.isNullOrBlank()) { PasswordSafe.getInstance().set(CredentialAttributes(ErrorReportConfigurable.SERVICE_NAME, userName), Credentials(userName, if (rememberCheckBox.isSelected) passwordField.password else null)) } - return@dialog true + return@dialog null } } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/ui/components/components.kt b/platform/platform-impl/src/com/intellij/ui/components/components.kt index 8a267f66e1a1..5a57d60c4a8e 100644 --- a/platform/platform-impl/src/com/intellij/ui/components/components.kt +++ b/platform/platform-impl/src/com/intellij/ui/components/components.kt @@ -12,6 +12,7 @@ import com.intellij.openapi.ui.ComponentWithBrowseButton.BrowseFolderActionListe import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.DialogWrapper.IdeModalityType import com.intellij.openapi.ui.TextComponentAccessor +import com.intellij.openapi.ui.ValidationInfo import com.intellij.openapi.ui.ex.MultiLineLabel import com.intellij.openapi.vcs.changes.issueLinks.LinkMouseListenerBase import com.intellij.openapi.vfs.VirtualFile @@ -129,7 +130,7 @@ fun dialog(title: String, parent: Component? = null, errorText: String? = null, modality: IdeModalityType = IdeModalityType.IDE, - ok: (() -> Boolean)? = null): DialogWrapper { + ok: (() -> List?)? = null): DialogWrapper { return object: DialogWrapper(project, parent, true, modality) { init { setTitle(title) @@ -149,9 +150,17 @@ fun dialog(title: String, override fun getPreferredFocusedComponent() = focusedComponent override fun doOKAction() { - if (okAction.isEnabled && (ok == null || ok())) { + if (!okAction.isEnabled) { + return + } + + val validationInfoList = ok?.invoke() + if (validationInfoList == null || validationInfoList.isEmpty()) { super.doOKAction() } + else { + setErrorInfoAll(validationInfoList) + } } } } diff --git a/plugins/settings-repository/src/RepositoryService.kt b/plugins/settings-repository/src/RepositoryService.kt index 43340bdc63dd..6190c9e575ba 100644 --- a/plugins/settings-repository/src/RepositoryService.kt +++ b/plugins/settings-repository/src/RepositoryService.kt @@ -1,23 +1,8 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 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 org.jetbrains.settingsRepository import com.intellij.openapi.project.Project import com.intellij.openapi.ui.MessageDialogBuilder -import com.intellij.openapi.ui.Messages import com.intellij.util.io.URLUtil import com.intellij.util.io.exists import com.intellij.util.io.isDirectory @@ -29,37 +14,25 @@ import java.nio.file.Path import java.nio.file.Paths interface RepositoryService { - fun checkUrl(uriString: String, project: Project? = null): Boolean { + fun checkUrl(uriString: String, project: Project? = null): String? { val uri = URIish(uriString) - val isFile: Boolean - if (uri.scheme == URLUtil.FILE_PROTOCOL) { - isFile = true - } - else { - isFile = uri.scheme == null && uri.host == null - } - - if (isFile && !checkFileRepo(uriString, project)) { - return false - } - return true + val isFile = uri.scheme == URLUtil.FILE_PROTOCOL || (uri.scheme == null && uri.host == null) + return if (isFile) checkFileRepo(uriString, project) else null } - private fun checkFileRepo(url: String, project: Project?): Boolean { + private fun checkFileRepo(url: String, project: Project?): String? { val suffix = "/${Constants.DOT_GIT}" val file = Paths.get(if (url.endsWith(suffix)) url.substring(0, url.length - suffix.length) else url) if (file.exists()) { if (!file.isDirectory()) { - Messages.showErrorDialog(project, "Path is not a directory", "") - return false + return "Path is not a directory" } else if (isValidRepository(file)) { - return true + return null } } else if (!file.isAbsolute) { - Messages.showErrorDialog(project, icsMessage("specify.absolute.path.dialog.message"), "") - return false + return icsMessage("specify.absolute.path.dialog.message") } if (MessageDialogBuilder @@ -69,15 +42,14 @@ interface RepositoryService { .isYes) { return try { createBareRepository(file) - true + null } catch (e: IOException) { - Messages.showErrorDialog(project, icsMessage("init.failed.message", e.message), icsMessage("init.failed.title")) - false + icsMessage("init.failed.message", e.message) } } else { - return false + return "" } } diff --git a/plugins/settings-repository/src/readOnlySourcesEditor.kt b/plugins/settings-repository/src/readOnlySourcesEditor.kt index f2d611670529..1336dd73de92 100644 --- a/plugins/settings-repository/src/readOnlySourcesEditor.kt +++ b/plugins/settings-repository/src/readOnlySourcesEditor.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 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 org.jetbrains.settingsRepository import com.intellij.openapi.application.ApplicationManager @@ -22,6 +8,7 @@ import com.intellij.openapi.options.ConfigurableUi import com.intellij.openapi.progress.runModalTask import com.intellij.openapi.ui.TextBrowseFolderListener import com.intellij.openapi.ui.TextFieldWithBrowseButton +import com.intellij.openapi.ui.ValidationInfo import com.intellij.ui.components.dialog import com.intellij.ui.layout.* import com.intellij.util.Function @@ -71,12 +58,12 @@ internal fun createReadOnlySourcesEditor(): ConfigurableUi { dialog(title = "Add read-only source", panel = panel, focusedComponent = urlField) { val url = urlField.text.nullize(true) - if (!validateUrl(url, null)) { - return@dialog false + validateUrl(url, null)?.let { + return@dialog listOf(ValidationInfo(it)) } mutator.`fun`(item).url = url - return@dialog true + return@dialog null } .show() } diff --git a/plugins/settings-repository/src/upstreamEditor.kt b/plugins/settings-repository/src/upstreamEditor.kt index 63e723229319..034a7b9d073d 100644 --- a/plugins/settings-repository/src/upstreamEditor.kt +++ b/plugins/settings-repository/src/upstreamEditor.kt @@ -16,17 +16,8 @@ import java.awt.event.ActionEvent import javax.swing.AbstractAction import javax.swing.Action -fun validateUrl(url: String?, project: Project?): Boolean { - if (url == null) { - Messages.showErrorDialog(project, "URL is empty", "") - return false - } - - if (!icsManager.repositoryService.checkUrl(url, project)) { - return false - } - - return true +fun validateUrl(url: String?, project: Project?): String? { + return if (url == null) "URL is empty" else icsManager.repositoryService.checkUrl(url, project) } fun createMergeActions(project: Project?, urlTextField: TextFieldWithBrowseButton, dialogParent: Container, okAction: (() -> Unit)): Array { @@ -42,7 +33,8 @@ fun createMergeActions(project: Project?, urlTextField: TextFieldWithBrowseButto object : AbstractAction(icsMessage("action.${if (syncType == SyncType.MERGE) "Merge" else (if (syncType == SyncType.OVERWRITE_LOCAL) "ResetToTheirs" else "ResetToMy")}Settings.text")) { private fun saveRemoteRepositoryUrl(): Boolean { val url = urlTextField.text.nullize(true) - if (!validateUrl(url, project)) { + validateUrl(url, project)?.let { + Messages.showErrorDialog(project, it, "") return false }