generated from jetbrains-academy/kotlin-course-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valerii
committed
Jan 14, 2024
1 parent
3f82b84
commit 43f3af8
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
...ammingTask/src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.jetbrains.academy.plugin.course.dev.ui | ||
|
||
import com.intellij.notification.NotificationGroupManager | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.openapi.application.WriteAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.wm.ToolWindow | ||
import com.intellij.openapi.wm.ToolWindowFactory | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.testFramework.LightVirtualFile | ||
import com.intellij.ui.components.JBPanel | ||
import com.intellij.ui.components.JBScrollPane | ||
import com.intellij.ui.table.JBTable | ||
import com.intellij.util.ui.JBUI | ||
import org.jetbrains.academy.plugin.course.dev.access.countKtClasses | ||
import org.jetbrains.kotlin.idea.KotlinLanguage | ||
import java.awt.BorderLayout | ||
import java.awt.FlowLayout | ||
import java.awt.event.ActionListener | ||
import javax.swing.JButton | ||
import javax.swing.table.DefaultTableModel | ||
|
||
class DemoPanelFactory : ToolWindowFactory { | ||
companion object { | ||
private const val DEMO_PLUGIN_NOTIFICATION = "IdeDevCourseDemo" | ||
} | ||
|
||
|
||
// TODO use UI bundle instead of a string here? | ||
private val runTaskMethodButton = JButton("Run task function").apply { | ||
isBorderPainted = false | ||
isVisible = true | ||
} | ||
|
||
lateinit var demoWindow: DemoPanelWindow | ||
private val tableModel = DefaultTableModel() | ||
private val resultsTable = JBTable(tableModel) | ||
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { | ||
demoWindow = project.getService(DemoPanelService::class.java).demoWindow | ||
demoWindow.jComponent.size = JBUI.size(toolWindow.component.width, toolWindow.component.height) | ||
tableModel.addColumn("Test Number") | ||
tableModel.addColumn("Your Output") | ||
tableModel.addColumn("Expected Output") | ||
val buttonPanel = JBPanel<JBPanel<*>>(FlowLayout()).apply { | ||
add(runTaskMethodButton) | ||
} | ||
// Wrapping the resultsTable in a JScrollPane | ||
val scrollPane = JBScrollPane(resultsTable) | ||
scrollPane.preferredSize = JBUI.size(400, 100) // Set preferred size for scrollPane | ||
|
||
val panel = JBPanel<JBPanel<*>>(BorderLayout()).apply { | ||
add(buttonPanel, BorderLayout.SOUTH) | ||
add(scrollPane, BorderLayout.CENTER) | ||
} | ||
toolWindow.component.add(panel) | ||
|
||
// TODO add another listener? This example as a demo for now | ||
runTaskMethodButton.setListener { | ||
NotificationGroupManager.getInstance().getNotificationGroup(DEMO_PLUGIN_NOTIFICATION) | ||
.createNotification("This panel demonstrates how the plugin works. The current task is a theory task, the plugin does nothing.", NotificationType.INFORMATION) | ||
.notify(project) | ||
} | ||
|
||
runTaskMethodButton.addActionListener { | ||
// Creating a virtual file with Kotlin content | ||
val tempVfsFile = WriteAction.compute<LightVirtualFile, Exception> { | ||
LightVirtualFile("MyClass.kt", KotlinLanguage.INSTANCE, """ | ||
class MyFirstClass { | ||
} | ||
class MySecondClass { | ||
} | ||
""".trimIndent()) | ||
} | ||
|
||
// Convert virtual file to PSI file | ||
val psiFile = PsiManager.getInstance(project).findFile(tempVfsFile) | ||
|
||
// Call your function and get the result | ||
psiFile?.let { | ||
val result = countKtClasses(it) | ||
|
||
// Display the result in the table | ||
tableModel.setRowCount(0) | ||
tableModel.addRow(arrayOf("1", result.toString(), 2)) | ||
} | ||
|
||
} | ||
} | ||
|
||
private fun JButton.setListener(listener: ActionListener) { | ||
actionListeners.forEach { | ||
removeActionListener(it) | ||
} | ||
addActionListener(listener) | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...ammingTask/src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jetbrains.academy.plugin.course.dev.ui | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.diagnostic.logger | ||
import com.intellij.openapi.project.Project | ||
|
||
@Service(Service.Level.PROJECT) | ||
class DemoPanelService : Disposable { | ||
val demoWindow = DemoPanelWindow(this) | ||
override fun dispose() { | ||
logger<Project>().info("Jcef window service was disposed") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...rammingTask/src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.jetbrains.academy.plugin.course.dev.ui | ||
|
||
import com.intellij.openapi.util.Disposer | ||
import com.intellij.ui.jcef.JBCefBrowser | ||
import javax.swing.JComponent | ||
|
||
class DemoPanelWindow(service: DemoPanelService) { | ||
private val windowBrowser: JBCefBrowser = JBCefBrowser() | ||
|
||
val jComponent: JComponent | ||
get() = windowBrowser.component | ||
|
||
init { | ||
// TODO load the panel's content | ||
Disposer.register(service, windowBrowser) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ntsLesson/accessingPsiElementsClassProgrammingTask/src/main/resources/META-INF/plugin.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<idea-plugin> | ||
<id>jetbrains.academy.plugin.course.dev.ui.demo</id> | ||
<name>IDE Development Course Demo</name> | ||
<vendor>JetBrains</vendor> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
<depends>org.jetbrains.kotlin</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<notificationGroup id="IdeDevCourseDemo" displayType="BALLOON"/> | ||
|
||
<toolWindow doNotActivateOnStart="true" | ||
factoryClass="org.jetbrains.academy.plugin.course.dev.ui.DemoPanelFactory" | ||
id="IDECourseDemo" anchor="right"/> | ||
</extensions> | ||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters