Skip to content

Commit

Permalink
Tool window factory project service
Browse files Browse the repository at this point in the history
  • Loading branch information
drazumova committed Aug 27, 2019
1 parent 7cc2b26 commit b636202
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.bunches.idea.actions

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.wm.ToolWindowManager
import org.jetbrains.bunches.idea.util.BunchFileUtils
Expand All @@ -22,6 +23,7 @@ class ReduceAction : AnAction() {
Messages.showMessageDialog("No .bunch file found", "Reduce error", Messages.getErrorIcon())
return
}
BunchToolWindowFactory.createReduceActionWindow(window, project, repoPath, bunchPath.parentFile.absolutePath)
ServiceManager.getService(project, BunchToolWindowFactory::class.java)
.createReduceActionWindow(window, project, repoPath, bunchPath.parentFile.absolutePath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.bunches.idea.vcs

import com.intellij.BundleBase.replaceMnemonicAmpersand
import com.intellij.CommonBundle
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.Key
Expand Down Expand Up @@ -106,7 +107,8 @@ class BunchFileCheckInHandlerFactory : CheckinHandlerFactory() {
)) {
YES -> {
val window = ToolWindowManager.getInstance(project).getToolWindow("Bunch Tool")
BunchToolWindowFactory.createCommitCheckToolWindowContent(
ServiceManager.getService(project, BunchToolWindowFactory::class.java)
.createCommitCheckToolWindowContent(
window, forgottenFiles, allFiles,
checkInProjectPanel
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.psi.PsiFile
import com.intellij.ui.content.Content
import com.intellij.ui.content.ContentFactory
import org.jetbrains.bunches.idea.actions.ReduceActionWindow

Expand All @@ -14,64 +15,68 @@ class BunchToolWindowFactory : ToolWindowFactory {
toolWindow.hide(null)
}

companion object {
private const val reduceDisplayName = "Reduce"
private fun displayedCommitMessage(message: String): String {
return "Commit: " +
if (message.length > 20)
message.take(20) + "..."
else
message
}

fun createCommitCheckToolWindowContent(
toolWindow: ToolWindow,
files: Set<VirtualFile>,
all: Map<PsiFile, List<PsiFile>>,
checkinProjectPanel: CheckinProjectPanel
) {
val window = SimpleCheckToolWindow(toolWindow, files, all, checkinProjectPanel)
val contentFactory = ContentFactory.SERVICE.getInstance()
val commitMessage =
"Commit: " +
if (checkinProjectPanel.commitMessage.length > 20)
checkinProjectPanel.commitMessage.take(20) + "..."
else
checkinProjectPanel.commitMessage
val content = contentFactory.createContent(window.content, commitMessage, false)
toolWindow.apply {
private fun ToolWindow.setContent(content: Content, index: Int) {
this.apply {
contentManager.apply {
addContent(content, index)
setSelectedContent(content)
}

isToHideOnEmptyContent = true
show(null)
}
}

contentManager.apply {
var index = contentCount
contents.filter { it.displayName != reduceDisplayName }.forEach {
index = getIndexOfContent(it)
removeContent(it, true)
}
addContent(content, index)
setSelectedContent(content)
}
fun createCommitCheckToolWindowContent(
toolWindow: ToolWindow,
files: Set<VirtualFile>,
all: Map<PsiFile, List<PsiFile>>,
checkinProjectPanel: CheckinProjectPanel
) {
val window = SimpleCheckToolWindow(toolWindow, files, all, checkinProjectPanel)
val contentFactory = ContentFactory.SERVICE.getInstance()
val commitMessage = displayedCommitMessage(checkinProjectPanel.commitMessage)

isToHideOnEmptyContent = true
show(null)
val content = contentFactory.createContent(window.content, commitMessage, false)
toolWindow.contentManager.apply {
var index = contentCount
contents.filter { it.displayName != reduceDisplayName }.forEach {
index = getIndexOfContent(it)
removeContent(it, true)
}
toolWindow.setContent(content, index)
}
}

fun createReduceActionWindow(
toolWindow: ToolWindow,
project: Project,
repoPath: String,
bunchPath: String
) {
val window = ReduceActionWindow(toolWindow, project, repoPath, bunchPath)
val contentFactory = ContentFactory.SERVICE.getInstance()
val content = contentFactory.createContent(window.content, reduceDisplayName, false)
toolWindow.apply {
contentManager.apply {
var index = contentCount
val oldContent = findContent(reduceDisplayName)
if (oldContent != null) {
index = getIndexOfContent(oldContent)
removeContent(oldContent, true)
}
addContent(content, index)
setSelectedContent(content)
}
isToHideOnEmptyContent = true
show(null)
fun createReduceActionWindow(
toolWindow: ToolWindow,
project: Project,
repoPath: String,
bunchPath: String
) {
val window = ReduceActionWindow(toolWindow, project, repoPath, bunchPath)
val contentFactory = ContentFactory.SERVICE.getInstance()
val content = contentFactory.createContent(window.content, reduceDisplayName, false)
toolWindow.contentManager.apply {
var index = contentCount
val oldContent = findContent(reduceDisplayName)
if (oldContent != null) {
index = getIndexOfContent(oldContent)
removeContent(oldContent, true)
}
toolWindow.setContent(content, index)
}
}

companion object {
private const val reduceDisplayName = "Reduce"
}
}
1 change: 1 addition & 0 deletions idea-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
factoryClass="org.jetbrains.bunches.idea.vcs.BunchToolWindowFactory"
secondary="false"
canCloseContents="true"/>
<projectService serviceImplementation="org.jetbrains.bunches.idea.vcs.BunchToolWindowFactory"/>
</extensions>

<project-components>
Expand Down

0 comments on commit b636202

Please sign in to comment.