Skip to content

Commit 103a98c

Browse files
authored
fix: add Enter Enterprise License Key action (#7159)
* fix: add Enter Enterprise License Key action Solves CON-3473 Notes: * I don't have a license key, so I haven't fully tested this, but looking at the TypeScript: true in the response means it's valid, and false means it's not * Extra: I added Try Again because I noticed that searching for the action every time the key is invalid is inconvenient * I wasn't sure if restarting the IDE is safe without more testing, so I ended with Restart IDE action (it was much faster to implement) * fix: review * fix: review
1 parent 187d445 commit 103a98c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.github.continuedev.continueintellijextension.license
2+
3+
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
4+
import com.github.continuedev.continueintellijextension.utils.castNestedOrNull
5+
import com.intellij.notification.NotificationAction
6+
import com.intellij.notification.NotificationGroupManager
7+
import com.intellij.notification.NotificationType
8+
import com.intellij.openapi.actionSystem.ActionManager
9+
import com.intellij.openapi.actionSystem.ActionPlaces
10+
import com.intellij.openapi.actionSystem.AnAction
11+
import com.intellij.openapi.actionSystem.AnActionEvent
12+
import com.intellij.openapi.application.ApplicationManager
13+
import com.intellij.openapi.components.service
14+
import com.intellij.openapi.ui.DialogWrapper
15+
16+
class AddLicenseKey : AnAction() {
17+
18+
override fun actionPerformed(e: AnActionEvent) {
19+
val dialog = AddLicenseKeyDialog(e.project)
20+
dialog.show()
21+
if (dialog.exitCode != DialogWrapper.OK_EXIT_CODE)
22+
return
23+
val messenger = e.project?.service<ContinuePluginService>()?.coreMessenger
24+
?: return
25+
messenger.request("mdm/setLicenseKey", mapOf("licenseKey" to dialog.licenseKey), null) { response ->
26+
val isValid = response.castNestedOrNull<Boolean>("content") ?: false
27+
val notification = if (isValid)
28+
createSuccessAction()
29+
else
30+
createErrorAction()
31+
notification.notify(e.project)
32+
}
33+
}
34+
35+
private fun createSuccessAction() =
36+
getContinueNotifications().createNotification(
37+
"License key is valid. A restart is required for it to take effect.",
38+
NotificationType.INFORMATION
39+
).addAction(
40+
NotificationAction.create("Restart IDE") { event, notification ->
41+
ApplicationManager.getApplication().restart()
42+
}
43+
)
44+
45+
private fun createErrorAction() =
46+
getContinueNotifications().createNotification(
47+
"License key is invalid.",
48+
NotificationType.ERROR
49+
).addAction(
50+
NotificationAction.create("Try again") { event, notification ->
51+
notification.expire()
52+
val action = this@AddLicenseKey
53+
action.actionPerformed(
54+
AnActionEvent(
55+
null,
56+
event.dataContext,
57+
ActionPlaces.UNKNOWN,
58+
action.templatePresentation.clone(),
59+
ActionManager.getInstance(),
60+
0
61+
)
62+
)
63+
}
64+
)
65+
66+
private fun getContinueNotifications() =
67+
NotificationGroupManager.getInstance().getNotificationGroup("Continue")
68+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.continuedev.continueintellijextension.license
2+
3+
import com.intellij.openapi.project.Project
4+
import com.intellij.openapi.ui.DialogWrapper
5+
import com.intellij.openapi.ui.ValidationInfo
6+
import com.intellij.ui.dsl.builder.bindText
7+
import com.intellij.ui.dsl.builder.panel
8+
import com.intellij.ui.dsl.gridLayout.HorizontalAlign
9+
import javax.swing.JComponent
10+
11+
class AddLicenseKeyDialog(project: Project?) : DialogWrapper(project) {
12+
13+
var licenseKey: String = ""
14+
private set
15+
16+
init {
17+
title = "Enterprise License Key"
18+
init()
19+
}
20+
21+
override fun createCenterPanel(): JComponent? =
22+
panel {
23+
row {
24+
textField().bindText(::licenseKey)
25+
.horizontalAlign(HorizontalAlign.FILL)
26+
.focused()
27+
.validation {
28+
if (it.text.isBlank())
29+
ValidationInfo("License key cannot be empty")
30+
else
31+
null
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)