Example code - `MyToolWindowFactory` tool window basic implementation

pull/361/head
Jakub Chrzanowski 1 year ago committed by Jakub Chrzanowski
parent 5bda710503
commit f686654cbb

@ -7,6 +7,7 @@
### Added
- Migrate to Gradle Provider API improving configuration cache compatibility
- Example code - `FrameStateListener` application listener
- Example code - `MyToolWindowFactory` tool window basic implementation
### Changed
- Update `platformVersion` to `2022.1.4`

@ -10,13 +10,8 @@ class MyProjectService(project: Project) {
init {
thisLogger().info(MyBundle.message("projectService", project.name))
System.getenv("CI")
?: TODO("Don't forget to remove all non-needed sample code files with their corresponding registration entries in `plugin.xml`.")
thisLogger().warn("Don't forget to remove all non-needed sample code files with their corresponding registration entries in `plugin.xml`.")
}
/**
* Chosen by fair dice roll, guaranteed to be random.
*/
fun getRandomNumber() = 4
fun getRandomNumber() = (1..100).random()
}

@ -0,0 +1,47 @@
package org.jetbrains.plugins.template.toolWindow
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBPanel
import com.intellij.ui.content.ContentFactory
import org.jetbrains.plugins.template.MyBundle
import org.jetbrains.plugins.template.services.MyProjectService
import javax.swing.JButton
class MyToolWindowFactory : ToolWindowFactory {
init {
thisLogger().warn("Don't forget to remove all non-needed sample code files with their corresponding registration entries in `plugin.xml`.")
}
private val contentFactory = ContentFactory.SERVICE.getInstance()
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val myToolWindow = MyToolWindow(toolWindow)
val content = contentFactory.createContent(myToolWindow.getContent(), null, false)
toolWindow.contentManager.addContent(content)
}
override fun shouldBeAvailable(project: Project) = true
class MyToolWindow(toolWindow: ToolWindow) {
private val service = toolWindow.project.service<MyProjectService>()
fun getContent() = JBPanel<JBPanel<*>>().apply {
val label = JBLabel(MyBundle.message("randomLabel", "?"))
add(label)
add(JButton(MyBundle.message("shuffle")).apply {
addActionListener {
label.text = MyBundle.message("randomLabel", service.getRandomNumber())
}
})
}
}
}

@ -8,8 +8,11 @@
<resource-bundle>messages.MyBundle</resource-bundle>
<extensions defaultExtensionNs="com.intellij">
<toolWindow factoryClass="org.jetbrains.plugins.template.toolWindow.MyToolWindowFactory" id="MyToolWindow"/>
</extensions>
<applicationListeners>
<listener class="org.jetbrains.plugins.template.listeners.MyFrameStateListener"
topic="com.intellij.ide.FrameStateListener"/>
<listener class="org.jetbrains.plugins.template.listeners.MyFrameStateListener" topic="com.intellij.ide.FrameStateListener"/>
</applicationListeners>
</idea-plugin>

@ -1,3 +1,4 @@
name=My Plugin
applicationService=Application service
projectService=Project service: {0}
randomLabel=The random number is: {0}
shuffle=Shuffle

@ -32,7 +32,7 @@ class MyPluginTest : BasePlatformTestCase() {
fun testProjectService() {
val projectService = project.service<MyProjectService>()
assertEquals(4, projectService.getRandomNumber())
assertNotSame(projectService.getRandomNumber(), projectService.getRandomNumber())
}
override fun getTestDataPath() = "src/test/testData/rename"

Loading…
Cancel
Save