Skip to content

Commit

Permalink
add ui for 1st task
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Jan 14, 2024
1 parent 3f82b84 commit 43f3af8
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
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)
}

}
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")
}
}
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)
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ files:
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/ElementsCounter.kt
visible: true
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelService.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelWindow.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelFactory.kt
visible: false
- name: src/main/resources/META-INF/plugin.xml
visible: false

0 comments on commit 43f3af8

Please sign in to comment.