Skip to content

Commit 648031d

Browse files
authored
Add new tab action (#47)
1 parent 46618b7 commit 648031d

7 files changed

Lines changed: 94 additions & 7 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = no.spk.fiskeoye.plugin
44
pluginName = fiskeoye-plugin
55
pluginRepositoryUrl = https://github.com/statens-pensjonskasse/fiskeoye-plugin
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.0.2
7+
pluginVersion = 0.0.3
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 233

images/fiskeoye_4.png

120 Bytes
Loading
Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,100 @@
11
package no.spk.fiskeoye.plugin
22

3+
import com.intellij.openapi.actionSystem.AnActionEvent
4+
import com.intellij.openapi.actionSystem.DefaultActionGroup
35
import com.intellij.openapi.project.DumbAware
46
import com.intellij.openapi.project.Project
57
import com.intellij.openapi.wm.ToolWindow
68
import com.intellij.openapi.wm.ToolWindowFactory
9+
import com.intellij.openapi.wm.ex.ToolWindowEx
710
import com.intellij.ui.content.ContentFactory
11+
import com.intellij.ui.content.ContentManagerEvent
12+
import com.intellij.ui.content.ContentManagerListener
13+
import no.spk.fiskeoye.plugin.actions.FiskeoyeAction
14+
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Add
815
import no.spk.fiskeoye.plugin.ui.FileContentPanel
916
import no.spk.fiskeoye.plugin.ui.FilenamePanel
1017

1118
internal class FiskeoyeToolWindowFactory : ToolWindowFactory, DumbAware {
1219

20+
private var fileContentCounter = 1
21+
private var filenameCounter = 1
22+
1323
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
1424
val contentFactory: ContentFactory = ContentFactory.getInstance()
25+
val toolWindowEx = toolWindow as ToolWindowEx
1526

1627
val fileContentPanel = FileContentPanel()
17-
val fileContent = contentFactory.createContent(fileContentPanel, "File Content", false)
18-
toolWindow.contentManager.addContent(fileContent)
28+
val fileContent = contentFactory.createContent(fileContentPanel, "File Content", false).apply {
29+
isCloseable = false
30+
putUserData(CONTENT_TYPE_KEY, ContentType.FILE_CONTENT)
31+
}
32+
toolWindowEx.contentManager.addContent(fileContent)
1933

2034
val filenamePanel = FilenamePanel()
21-
val filename = contentFactory.createContent(filenamePanel, "Filename", false)
22-
toolWindow.contentManager.addContent(filename)
35+
val filename = contentFactory.createContent(filenamePanel, "File Name", false).apply {
36+
isCloseable = false
37+
putUserData(CONTENT_TYPE_KEY, ContentType.FILENAME)
38+
}
39+
toolWindowEx.contentManager.addContent(filename)
40+
41+
toolWindowEx.contentManager.addContentManagerListener(object : ContentManagerListener {
42+
override fun contentRemoved(event: ContentManagerEvent) {
43+
val content = event.content
44+
when (content.getUserData(CONTENT_TYPE_KEY)) {
45+
ContentType.FILE_CONTENT -> if (fileContentCounter > 1) fileContentCounter--
46+
ContentType.FILENAME -> if (filenameCounter > 1) filenameCounter--
47+
else -> {}
48+
}
49+
}
50+
})
51+
52+
val newTabActionGroup = buildNewTabActionGroup(toolWindow, contentFactory)
53+
toolWindowEx.setTabActions(newTabActionGroup)
54+
}
55+
56+
private fun buildNewTabActionGroup(toolWindow: ToolWindow, contentFactory: ContentFactory): DefaultActionGroup {
57+
val addFileContentAction = object : FiskeoyeAction("File Content") {
58+
override fun actionPerformed(e: AnActionEvent) {
59+
fileContentCounter++
60+
val newFileContent = contentFactory.createContent(
61+
FileContentPanel(),
62+
"File Content ($fileContentCounter)",
63+
false
64+
).apply {
65+
isCloseable = true
66+
putUserData(CONTENT_TYPE_KEY, ContentType.FILE_CONTENT)
67+
}
68+
toolWindow.contentManager.addContent(newFileContent)
69+
toolWindow.contentManager.setSelectedContent(newFileContent, true)
70+
}
71+
}
72+
73+
val addFilenameAction = object : FiskeoyeAction("File Name") {
74+
override fun actionPerformed(e: AnActionEvent) {
75+
filenameCounter++
76+
val newFilename = contentFactory.createContent(
77+
FilenamePanel(),
78+
"File Name ($filenameCounter)",
79+
false
80+
).apply {
81+
isCloseable = true
82+
putUserData(CONTENT_TYPE_KEY, ContentType.FILENAME)
83+
}
84+
toolWindow.contentManager.addContent(newFilename)
85+
toolWindow.contentManager.setSelectedContent(newFilename, true)
86+
}
87+
}
88+
89+
return DefaultActionGroup("New Fiskeoye Tab", true).apply {
90+
templatePresentation.icon = Add
91+
add(addFileContentAction)
92+
add(addFilenameAction)
93+
}
2394
}
2495

25-
}
96+
private enum class ContentType { FILE_CONTENT, FILENAME }
97+
private companion object {
98+
val CONTENT_TYPE_KEY = com.intellij.openapi.util.Key.create<ContentType>("FISKEOYE_CONTENT_TYPE")
99+
}
100+
}

src/main/kotlin/no/spk/fiskeoye/plugin/icons/FiskeoyeIcons.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ internal object FiskeoyeIcons {
6868

6969
@JvmField
7070
val Help = IconLoader.getIcon("/icons/help.svg", javaClass)
71+
72+
@JvmField
73+
val Add = IconLoader.getIcon("/icons/add.svg", javaClass)
7174
}

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
<extensions defaultExtensionNs="com.intellij">
99
<toolWindow id="Fiskeoye" factoryClass="no.spk.fiskeoye.plugin.FiskeoyeToolWindowFactory"
10-
icon="no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.ToolWindow" anchor="bottom"/>
10+
icon="no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.ToolWindow" anchor="bottom"
11+
canCloseContents="true"/>
1112

1213
<applicationConfigurable parentId="tools" id="Fiskeoye.Application.Settings"
1314
instance="no.spk.fiskeoye.plugin.settings.FiskeoyeConfigurable"

src/main/resources/icons/add.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)