Skip to content

Commit a103bee

Browse files
authored
Fix error with tab counter (#58)
* Fix error with tab counter * Remove redundant semicolon
1 parent 71c1e5d commit a103bee

7 files changed

Lines changed: 135 additions & 65 deletions

File tree

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,48 @@
11
package no.spk.fiskeoye.plugin
22

3-
import com.intellij.openapi.actionSystem.AnActionEvent
43
import com.intellij.openapi.actionSystem.DefaultActionGroup
54
import com.intellij.openapi.project.DumbAware
65
import com.intellij.openapi.project.Project
76
import com.intellij.openapi.wm.ToolWindow
87
import com.intellij.openapi.wm.ToolWindowFactory
98
import com.intellij.openapi.wm.ex.ToolWindowEx
109
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
10+
import no.spk.fiskeoye.plugin.actions.window.AddFileContentTabAction
11+
import no.spk.fiskeoye.plugin.actions.window.AddFilenameTabAction
12+
import no.spk.fiskeoye.plugin.enum.ContentType
1413
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Add
14+
import no.spk.fiskeoye.plugin.listeners.window.TabListener
1515
import no.spk.fiskeoye.plugin.ui.FileContentPanel
1616
import no.spk.fiskeoye.plugin.ui.FilenamePanel
17+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILENAME_COUNTER_KEY
18+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILE_CONTENT_COUNTER_KEY
19+
import no.spk.fiskeoye.plugin.util.createContent
1720

1821
internal class FiskeoyeToolWindowFactory : ToolWindowFactory, DumbAware {
1922

20-
private var fileContentCounter = 1
21-
private var filenameCounter = 1
22-
2323
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
2424
val contentFactory: ContentFactory = ContentFactory.getInstance()
2525
val toolWindowEx = toolWindow as ToolWindowEx
2626

27-
val fileContent = contentFactory.createContent(FileContentPanel(), "File Content", false).apply {
28-
isCloseable = false
29-
putUserData(CONTENT_TYPE_KEY, ContentType.FILE_CONTENT)
30-
}
27+
project.putUserData(FILE_CONTENT_COUNTER_KEY, 1)
28+
project.putUserData(FILENAME_COUNTER_KEY, 1)
29+
30+
val fileContent = contentFactory.createContent(FileContentPanel(), "File Content", ContentType.FILE_CONTENT)
3131
toolWindowEx.contentManager.addContent(fileContent)
3232

33-
val filename = contentFactory.createContent(FilenamePanel(), "File Name", false).apply {
34-
isCloseable = false
35-
putUserData(CONTENT_TYPE_KEY, ContentType.FILENAME)
36-
}
33+
val filename = contentFactory.createContent(FilenamePanel(), "File Name", ContentType.FILENAME)
3734
toolWindowEx.contentManager.addContent(filename)
3835

39-
toolWindowEx.contentManager.addContentManagerListener(object : ContentManagerListener {
40-
override fun contentRemoved(event: ContentManagerEvent) {
41-
val content = event.content
42-
when (content.getUserData(CONTENT_TYPE_KEY)) {
43-
ContentType.FILE_CONTENT -> if (fileContentCounter > 1) fileContentCounter--
44-
ContentType.FILENAME -> if (filenameCounter > 1) filenameCounter--
45-
else -> {}
46-
}
47-
}
48-
})
49-
toolWindowEx.setTabActions(buildNewTabActionGroup(toolWindow, contentFactory))
36+
toolWindowEx.contentManager.addContentManagerListener(TabListener(project))
37+
toolWindowEx.setTabActions(buildTabActionGroup(toolWindow, contentFactory))
5038
}
5139

52-
private fun buildNewTabActionGroup(toolWindow: ToolWindow, contentFactory: ContentFactory): DefaultActionGroup {
53-
val addFileContentAction = object : FiskeoyeAction("File Content") {
54-
override fun actionPerformed(e: AnActionEvent) {
55-
fileContentCounter++
56-
val newFileContent = contentFactory.createContent(
57-
FileContentPanel(),
58-
"File Content ($fileContentCounter)",
59-
false
60-
).apply {
61-
isCloseable = true
62-
putUserData(CONTENT_TYPE_KEY, ContentType.FILE_CONTENT)
63-
}
64-
toolWindow.contentManager.addContent(newFileContent)
65-
toolWindow.contentManager.setSelectedContent(newFileContent, true)
66-
}
67-
}
68-
69-
val addFilenameAction = object : FiskeoyeAction("File Name") {
70-
override fun actionPerformed(e: AnActionEvent) {
71-
filenameCounter++
72-
val newFilename = contentFactory.createContent(
73-
FilenamePanel(),
74-
"File Name ($filenameCounter)",
75-
false
76-
).apply {
77-
isCloseable = true
78-
putUserData(CONTENT_TYPE_KEY, ContentType.FILENAME)
79-
}
80-
toolWindow.contentManager.addContent(newFilename)
81-
toolWindow.contentManager.setSelectedContent(newFilename, true)
82-
}
83-
}
84-
40+
private fun buildTabActionGroup(toolWindow: ToolWindow, contentFactory: ContentFactory): DefaultActionGroup {
8541
return DefaultActionGroup("New Fiskeoye Tab", true).apply {
8642
templatePresentation.icon = Add
87-
add(addFileContentAction)
88-
add(addFilenameAction)
43+
add(AddFileContentTabAction(toolWindow, contentFactory))
44+
add(AddFilenameTabAction(toolWindow, contentFactory))
8945
}
9046
}
9147

92-
private enum class ContentType { FILE_CONTENT, FILENAME }
93-
private companion object {
94-
val CONTENT_TYPE_KEY = com.intellij.openapi.util.Key.create<ContentType>("FISKEOYE_CONTENT_TYPE")
95-
}
9648
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package no.spk.fiskeoye.plugin.actions.window
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent
4+
import com.intellij.openapi.wm.ToolWindow
5+
import com.intellij.ui.content.ContentFactory
6+
import no.spk.fiskeoye.plugin.actions.FiskeoyeAction
7+
import no.spk.fiskeoye.plugin.enum.ContentType
8+
import no.spk.fiskeoye.plugin.ui.FileContentPanel
9+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILE_CONTENT_COUNTER_KEY
10+
import no.spk.fiskeoye.plugin.util.createContent
11+
12+
internal class AddFileContentTabAction(val toolWindow: ToolWindow, val contentFactory: ContentFactory) : FiskeoyeAction("File Content") {
13+
14+
override fun actionPerformed(e: AnActionEvent) {
15+
val project = e.project ?: return
16+
val currentCount = project.getUserData(FILE_CONTENT_COUNTER_KEY) ?: 1
17+
val newCount = currentCount + 1
18+
project.putUserData(FILE_CONTENT_COUNTER_KEY, newCount)
19+
20+
val newFileContent = contentFactory.createContent(
21+
fiskeoyePanel = FileContentPanel(),
22+
title = "File Content ($newCount)",
23+
contentType = ContentType.FILE_CONTENT,
24+
closeable = true
25+
)
26+
toolWindow.contentManager.addContent(newFileContent)
27+
toolWindow.contentManager.setSelectedContent(newFileContent, true)
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package no.spk.fiskeoye.plugin.actions.window
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent
4+
import com.intellij.openapi.wm.ToolWindow
5+
import com.intellij.ui.content.ContentFactory
6+
import no.spk.fiskeoye.plugin.actions.FiskeoyeAction
7+
import no.spk.fiskeoye.plugin.enum.ContentType
8+
import no.spk.fiskeoye.plugin.ui.FilenamePanel
9+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILENAME_COUNTER_KEY
10+
import no.spk.fiskeoye.plugin.util.createContent
11+
12+
internal class AddFilenameTabAction(val toolWindow: ToolWindow, val contentFactory: ContentFactory) : FiskeoyeAction("File Name"){
13+
14+
override fun actionPerformed(e: AnActionEvent) {
15+
val project = e.project ?: return
16+
val currentCount = project.getUserData(FILENAME_COUNTER_KEY) ?: 1
17+
val newCount = currentCount + 1
18+
project.putUserData(FILENAME_COUNTER_KEY, newCount)
19+
20+
val newFilename = contentFactory.createContent(
21+
fiskeoyePanel = FilenamePanel(),
22+
title = "File Name ($newCount)",
23+
contentType = ContentType.FILENAME,
24+
closeable = true
25+
)
26+
toolWindow.contentManager.addContent(newFilename)
27+
toolWindow.contentManager.setSelectedContent(newFilename, true)
28+
}
29+
30+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package no.spk.fiskeoye.plugin.enum
2+
3+
internal enum class ContentType {
4+
FILE_CONTENT,
5+
FILENAME
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package no.spk.fiskeoye.plugin.listeners.window
2+
3+
import com.intellij.openapi.project.Project
4+
import com.intellij.ui.content.ContentManagerEvent
5+
import com.intellij.ui.content.ContentManagerListener
6+
import no.spk.fiskeoye.plugin.enum.ContentType
7+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.CONTENT_TYPE_KEY
8+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILENAME_COUNTER_KEY
9+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILE_CONTENT_COUNTER_KEY
10+
11+
internal class TabListener(val project: Project) : ContentManagerListener {
12+
override fun contentRemoved(event: ContentManagerEvent) {
13+
when (event.content.getUserData(CONTENT_TYPE_KEY)) {
14+
ContentType.FILE_CONTENT -> {
15+
val currentCount = project.getUserData(FILE_CONTENT_COUNTER_KEY) ?: 1
16+
if (currentCount > 1) {
17+
project.putUserData(FILE_CONTENT_COUNTER_KEY, currentCount - 1)
18+
}
19+
}
20+
21+
ContentType.FILENAME -> {
22+
val currentCount = project.getUserData(FILENAME_COUNTER_KEY) ?: 1
23+
if (currentCount > 1) {
24+
project.putUserData(FILENAME_COUNTER_KEY, currentCount - 1)
25+
}
26+
}
27+
28+
else -> {}
29+
}
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package no.spk.fiskeoye.plugin.util
2+
3+
import com.intellij.openapi.util.Key
4+
import no.spk.fiskeoye.plugin.enum.ContentType
5+
6+
internal object FiskeoyeKeys {
7+
val CONTENT_TYPE_KEY = Key.create<ContentType>("FISKEOYE_CONTENT_TYPE")
8+
val FILE_CONTENT_COUNTER_KEY = Key.create<Int>("FISKEOYE_FILE_CONTENT_COUNTER")
9+
val FILENAME_COUNTER_KEY = Key.create<Int>("FISKEOYE_FILENAME_COUNTER")
10+
}

src/main/kotlin/no/spk/fiskeoye/plugin/util/FiskeoyeUtil.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import com.intellij.openapi.project.ProjectManager
99
import com.intellij.openapi.wm.ToolWindow
1010
import com.intellij.openapi.wm.ToolWindowManager
1111
import com.intellij.openapi.wm.WindowManager
12+
import com.intellij.ui.content.Content
13+
import com.intellij.ui.content.ContentFactory
1214
import com.intellij.ui.table.JBTable
1315
import com.intellij.ui.util.preferredHeight
1416
import no.spk.fiskeoye.plugin.component.LabelIcon
17+
import no.spk.fiskeoye.plugin.enum.ContentType
1518
import no.spk.fiskeoye.plugin.enum.ScrollDirection
1619
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Bitbucket
1720
import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Github
@@ -20,6 +23,7 @@ import no.spk.fiskeoye.plugin.settings.FiskeoyeState
2023
import no.spk.fiskeoye.plugin.ui.FileContentPanel
2124
import no.spk.fiskeoye.plugin.ui.FilenamePanel
2225
import no.spk.fiskeoye.plugin.ui.FiskeoyePanel
26+
import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.CONTENT_TYPE_KEY
2327
import java.awt.Desktop
2428
import java.awt.Dimension
2529
import java.awt.Window
@@ -147,6 +151,13 @@ internal fun JBTable.update(width: Int) {
147151

148152
internal fun Boolean.toOnOff(): String = if (this) "on" else "off"
149153

154+
internal fun ContentFactory.createContent(fiskeoyePanel: FiskeoyePanel, title: String, contentType: ContentType, closeable: Boolean = false): Content {
155+
return this.createContent(fiskeoyePanel, title, false).apply {
156+
isCloseable = closeable
157+
putUserData(CONTENT_TYPE_KEY, contentType)
158+
}
159+
}
160+
150161
fun htmlToText(html: String): String {
151162
val editorPane = JEditorPane().apply {
152163
editorKit = HTMLEditorKit()

0 commit comments

Comments
 (0)