Replace migration dialog with sticky popup (KT-27163)

#KT-27163 Fixed
This commit is contained in:
Nikolay Krasko
2018-09-27 11:58:02 +03:00
parent ce9028e367
commit 41bd29c3e9
7 changed files with 59 additions and 192 deletions
@@ -5,11 +5,7 @@
package org.jetbrains.kotlin.idea.configuration
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager
import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataImportListener
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.module.Module
@@ -26,11 +22,10 @@ import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
import org.jetbrains.kotlin.idea.configuration.ui.MigrationNotificationDialog
import org.jetbrains.kotlin.idea.configuration.ui.showMigrationNotification
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.idea.framework.GRADLE_SYSTEM_ID
import org.jetbrains.kotlin.idea.framework.MAVEN_SYSTEM_ID
import org.jetbrains.kotlin.idea.migration.CodeMigrationAction
import org.jetbrains.kotlin.idea.migration.CodeMigrationToggleAction
import org.jetbrains.kotlin.idea.migration.applicableMigrationTools
import org.jetbrains.kotlin.idea.project.languageVersionSettings
@@ -111,19 +106,7 @@ class KotlinMigrationProjectComponent(val project: Project) {
}
ApplicationManager.getApplication().invokeLater {
val migrationNotificationDialog = MigrationNotificationDialog(project, migrationInfo)
migrationNotificationDialog.show()
if (migrationNotificationDialog.isOK) {
val action = ActionManager.getInstance().getAction(CodeMigrationAction.ACTION_ID)
val dataContext = getDataContextFromDialog(migrationNotificationDialog)
if (dataContext != null) {
val actionEvent = AnActionEvent.createFromAnAction(action, null, ActionPlaces.ACTION_SEARCH, dataContext)
action.actionPerformed(actionEvent)
}
}
showMigrationNotification(project, migrationInfo)
}
} finally {
notifyFinish(migrationInfo, hasApplicableTools)
@@ -1,13 +0,0 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.configuration
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.ui.DialogWrapper
@Suppress("UNUSED_PARAMETER")
internal fun getDataContextFromDialog(wrapper: DialogWrapper): DataContext? = DataManager.getInstance().dataContextFromFocus.result
@@ -1,14 +0,0 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.configuration
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.ui.DialogWrapper
@Suppress("UNUSED_PARAMETER")
internal fun getDataContextFromDialog(wrapper: DialogWrapper): DataContext? =
DataManager.getInstance().getDataContext(wrapper.window)
@@ -1,46 +0,0 @@
package org.jetbrains.kotlin.idea.configuration.ui
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import org.jetbrains.kotlin.idea.configuration.MigrationInfo
import org.jetbrains.kotlin.idea.migration.CodeMigrationToggleAction
import javax.swing.JComponent
internal class MigrationNotificationDialog(val project: Project, migrationInfo: MigrationInfo) : DialogWrapper(project, false) {
private val form = MigrationNotificationForm()
init {
title = "Kotlin Migration (Experimental)"
setOKButtonText("Migrate")
form.libraryInfo.isVisible = migrationInfo.oldStdlibVersion != migrationInfo.newStdlibVersion
form.libraryInfo.text = "Standard library: ${migrationInfo.oldStdlibVersion} to ${migrationInfo.newStdlibVersion}"
form.languageVersionInfo.isVisible = migrationInfo.oldLanguageVersion != migrationInfo.newLanguageVersion
form.languageVersionInfo.text = "Language version: ${migrationInfo.oldLanguageVersion} to ${migrationInfo.newLanguageVersion}"
form.apiVersionInfo.isVisible = migrationInfo.oldApiVersion != migrationInfo.newApiVersion
form.apiVersionInfo.text = "API version: ${migrationInfo.oldApiVersion} to ${migrationInfo.newApiVersion}"
form.disableMigrationDetectionCheckBox.isSelected = !CodeMigrationToggleAction.isEnabled(project)
init()
}
override fun createCenterPanel(): JComponent = form.mainPanel
private fun saveSettings() {
CodeMigrationToggleAction.setEnabled(project, !form.disableMigrationDetectionCheckBox.isSelected)
}
override fun doCancelAction() {
saveSettings()
super.doCancelAction()
}
override fun doOKAction() {
saveSettings()
super.doOKAction()
}
}
@@ -1,84 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.kotlin.idea.configuration.ui.MigrationNotificationForm">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="593" height="233"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="3feff">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="639c3" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="70410" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Migration of Kotlin compiler settings is detected in the project:"/>
</properties>
</component>
</children>
</grid>
<grid id="73ff" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="11878" class="javax.swing.JLabel" binding="libraryInfo">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="192" height="16"/>
</grid>
</constraints>
<properties>
<text value="Standard library: unchanged"/>
</properties>
</component>
<component id="9b963" class="javax.swing.JLabel" binding="languageVersionInfo">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="192" height="16"/>
</grid>
</constraints>
<properties>
<text value="Language version: unchanged"/>
</properties>
</component>
<component id="2a47d" class="javax.swing.JLabel" binding="apiVersionInfo">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="192" height="16"/>
</grid>
</constraints>
<properties>
<text value="API version: unchanged"/>
</properties>
</component>
</children>
</grid>
<component id="4d637" class="javax.swing.JCheckBox" binding="disableMigrationDetectionCheckBox" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Disable migrations detection"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -1,16 +0,0 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.configuration.ui;
import javax.swing.*;
public class MigrationNotificationForm {
public JCheckBox disableMigrationDetectionCheckBox;
public JPanel mainPanel;
public JLabel libraryInfo;
public JLabel languageVersionInfo;
public JLabel apiVersionInfo;
}
@@ -0,0 +1,57 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.configuration.ui
import com.intellij.notification.*
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.configuration.MigrationInfo
import org.jetbrains.kotlin.idea.migration.CodeMigrationAction
import org.jetbrains.kotlin.idea.migration.CodeMigrationToggleAction
internal fun showMigrationNotification(project: Project, migrationInfo: MigrationInfo) {
val detectedChangeMessage = buildString {
appendBr("Detected migration:")
if (migrationInfo.oldStdlibVersion != migrationInfo.newStdlibVersion) {
appendIndentBr("Standard library: ${migrationInfo.oldStdlibVersion} -> ${migrationInfo.newStdlibVersion}")
}
if (migrationInfo.oldLanguageVersion != migrationInfo.newLanguageVersion) {
appendIndentBr("Language version: ${migrationInfo.oldLanguageVersion} -> ${migrationInfo.newLanguageVersion}")
}
if (migrationInfo.oldApiVersion != migrationInfo.newApiVersion) {
appendIndentBr("API version: ${migrationInfo.oldApiVersion} -> ${migrationInfo.newApiVersion}")
}
}
KOTLIN_MIGRATION_NOTIFICATION_GROUP
.createNotification(
KOTLIN_MIGRATION_NOTIFICATION_ID,
"Migrations for Kotlin code are available<br/><br/>$detectedChangeMessage",
NotificationType.WARNING,
null
)
.also { notification ->
notification.addAction(NotificationAction.createSimple("Run migrations") {
val projectContext = SimpleDataContext.getProjectContext(project)
val action = ActionManager.getInstance().getAction(CodeMigrationAction.ACTION_ID)
Notification.fire(notification, action, projectContext)
notification.expire()
})
}
.notify(project)
}
private fun StringBuilder.appendBr(line: String) = this.append("$line<br/>")
private fun StringBuilder.appendIndentBr(line: String) = appendBr("&nbsp;&nbsp;$line")
private const val KOTLIN_MIGRATION_NOTIFICATION_ID = "Kotlin Migration"
private val KOTLIN_MIGRATION_NOTIFICATION_GROUP =
NotificationGroup(KOTLIN_MIGRATION_NOTIFICATION_ID, NotificationDisplayType.STICKY_BALLOON, true)