From f073bcc86d3ab22fca9dc3fd5dabadf9f69508cb Mon Sep 17 00:00:00 2001 From: Vera Petrenkova Date: Wed, 20 Nov 2024 13:08:25 +0100 Subject: [PATCH] IJPL-171716 Improve project path handling in untrusted project dialogs Enhance null safety for project path names in dialog titles and UI elements. Adjust logic to handle cases where parent folders or file names are missing, ensuring better user experience and preventing potential null pointer exceptions. GitOrigin-RevId: 9c37e2d41b113e42ee4f11efb4097f20cb1faf85 --- .../ExternalSystemTrustedProjectDialog.kt | 2 +- .../TrustedProjectStartupDialog.kt | 53 ++++++++++--------- .../project/impl/ProjectManagerImpl.kt | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/trusted/ExternalSystemTrustedProjectDialog.kt b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/trusted/ExternalSystemTrustedProjectDialog.kt index 10b0d4bcb2d4..9fe3637d41a0 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/trusted/ExternalSystemTrustedProjectDialog.kt +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/trusted/ExternalSystemTrustedProjectDialog.kt @@ -18,7 +18,7 @@ object ExternalSystemTrustedProjectDialog { return TrustedProjectsDialog.confirmOpeningOrLinkingUntrustedProject( projectRoot, project, - title = IdeBundle.message("untrusted.project.link.dialog.title", systemId.readableName, projectRoot.fileName), + title = IdeBundle.message("untrusted.project.link.dialog.title", systemId.readableName, projectRoot.fileName ?: projectRoot.toString()), cancelButtonText = IdeBundle.message("untrusted.project.link.dialog.cancel.button")) } diff --git a/platform/platform-impl/src/com/intellij/ide/trustedProjects/TrustedProjectStartupDialog.kt b/platform/platform-impl/src/com/intellij/ide/trustedProjects/TrustedProjectStartupDialog.kt index 98f8dde6de42..dffae7c5c7c1 100644 --- a/platform/platform-impl/src/com/intellij/ide/trustedProjects/TrustedProjectStartupDialog.kt +++ b/platform/platform-impl/src/com/intellij/ide/trustedProjects/TrustedProjectStartupDialog.kt @@ -135,33 +135,38 @@ internal class TrustedProjectStartupDialog( row { text(message) } - row { - val trimmedFolderName = StringUtil.shortenTextWithEllipsis(projectPath.parent.name, 40, 0, true) - checkBox(IdeBundle.message("untrusted.project.warning.trust.location.checkbox", trimmedFolderName)) - .bindSelected(trustAll) - .apply { - component.toolTipText = null - component.addMouseMotionListener(TooltipMouseAdapter { listOf(getParentFolder().pathString) }) - } - .onChanged { - if (it.isSelected) { - windowsDefender.set(false) + if (getParentFolder() != null) { + row { + val trimmedFolderName = StringUtil.shortenTextWithEllipsis(getParentFolder()!!.name, 40, 0, true) + checkBox(IdeBundle.message("untrusted.project.warning.trust.location.checkbox", trimmedFolderName)) + .bindSelected(trustAll) + .apply { + component.toolTipText = null + component.addMouseMotionListener(TooltipMouseAdapter { listOf(getParentFolder()!!.pathString) }) } + .onChanged { + if (it.isSelected) { + windowsDefender.set(false) + } - if (trustAction != null) { - val trustButton = getButton(trustAction!!) - val text = if (it.isSelected) { - val truncatedParentFolderName = StringUtil.shortenTextWithEllipsis(getTrustFolder(it.isSelected).name, 18, 0, true) - IdeBundle.message("untrusted.project.dialog.trust.folder.button", truncatedParentFolderName) - } else trustButtonText - trustButton?.text = text + if (trustAction != null) { + val trustButton = getButton(trustAction!!) + val text = if (it.isSelected) { + val truncatedParentFolderName = StringUtil.shortenTextWithEllipsis(getTrustFolder(it.isSelected).name, 18, 0, true) + IdeBundle.message("untrusted.project.dialog.trust.folder.button", truncatedParentFolderName) + } + else trustButtonText + trustButton?.text = text + } + val trimmedFolderName = StringUtil.shortenTextWithEllipsis(getTrustFolder(it.isSelected).name, 18, 0, true) + windowsDefenderCheckBox?.component?.text = IdeBundle.message("untrusted.project.windows.defender.trust.location.checkbox", trimmedFolderName) } - val trimmedFolderName = StringUtil.shortenTextWithEllipsis(getTrustFolder(it.isSelected).name, 18, 0, true) - windowsDefenderCheckBox?.component?.text = IdeBundle.message("untrusted.project.windows.defender.trust.location.checkbox", trimmedFolderName) - } + } } row { - val trimmedFolderName = StringUtil.shortenTextWithEllipsis(projectPath.name, 18, 0, true) + val trimmedFolderName = StringUtil.shortenTextWithEllipsis(projectPath.name.ifEmpty { projectPath.toString() } + + , 18, 0, true) val idePaths = pathsToExclude.asSequence().filter { it != projectPath }.joinToString(separator = "
") windowsDefenderCheckBox = checkBox(IdeBundle.message("untrusted.project.windows.defender.trust.location.checkbox", trimmedFolderName)) .bindSelected(windowsDefender) @@ -212,9 +217,9 @@ internal class TrustedProjectStartupDialog( } } - private fun getTrustFolder(isTrustAll: Boolean): Path = if (isTrustAll) getParentFolder() else projectPath + private fun getTrustFolder(isTrustAll: Boolean): Path = if (isTrustAll) getParentFolder() ?: projectPath else projectPath - private fun getParentFolder(): Path = projectPath.parent + private fun getParentFolder(): Path? = projectPath.parent override fun createActions(): Array { val actions: MutableList = mutableListOf() diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.kt b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.kt index f35d05f4fa34..8355c5f4655b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.kt +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.kt @@ -1487,7 +1487,7 @@ private suspend fun checkTrustedState(projectStoreBaseDir: Path): Boolean { return confirmOpeningOrLinkingUntrustedProject( projectRoot = projectStoreBaseDir, project = null, - title = IdeBundle.message("untrusted.project.open.dialog.title", projectStoreBaseDir.fileName) + title = IdeBundle.message("untrusted.project.open.dialog.title", projectStoreBaseDir.fileName ?: projectStoreBaseDir.toString()) ) }