Components.dialog.ok param should return List<ValidationInfo>, so, client can validate dialog on ok

This commit is contained in:
Vladimir Krivosheev
2018-03-20 15:10:27 +01:00
parent 5f92c38ef8
commit 3bf458406f
5 changed files with 33 additions and 75 deletions
@@ -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
}
}
@@ -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<ValidationInfo>?)? = 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)
}
}
}
}
@@ -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 ""
}
}
@@ -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<IcsSettings> {
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()
}
@@ -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<Action> {
@@ -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
}