Skip to content

Commit

Permalink
ToolWindow settings to turn on/off validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thsaravana committed May 14, 2020
1 parent 9ffe669 commit f6c82ba
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.madrapps.dagger.actions

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.ToggleAction
import com.madrapps.dagger.services.service

class ValidationSwitchAction : ToggleAction() {

companion object {
const val ID = "com.madrapps.dagger.actions.ValidationSwitchAction"
}

override fun isSelected(e: AnActionEvent): Boolean {
val project = e.project ?: return false
return project.service.settings.isValidationsEnabled
}

override fun setSelected(e: AnActionEvent, state: Boolean) {
val project = e.project
if (project != null) {
project.service.settings.isValidationsEnabled = state
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface DaggerService {
class Storage {
var isAutoScrollToSource = false
var shouldCalculateAfterEveryBuild = false
var isValidationsEnabled = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.SimpleToolWindowPanel
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.openapi.wm.ex.ToolWindowEx
import com.intellij.ui.AutoScrollToSourceHandler
import com.intellij.ui.PopupHandler
import com.intellij.ui.components.JBPanel
Expand Down Expand Up @@ -35,7 +36,8 @@ class DaggerToolWindow : ToolWindowFactory {
}
}

class MyPanel(toolWindow: ToolWindow, project: Project) : SimpleToolWindowPanel(true, true), DaggerWindowPanel {
class MyPanel(private val toolWindow: ToolWindow, project: Project) : SimpleToolWindowPanel(true, true),
DaggerWindowPanel {

override val tree: DaggerTree = DaggerTree(project.service.treeModel)
private val autoScrollHandler: AutoScrollToSourceHandler
Expand Down Expand Up @@ -69,6 +71,7 @@ class MyPanel(toolWindow: ToolWindow, project: Project) : SimpleToolWindowPanel(

val toolbar = JPanel(BorderLayout())
initToolbar(toolbar)
initSettingsOptions()

tree.isRootVisible = false
tree.toggleClickCount = 3
Expand All @@ -93,6 +96,16 @@ class MyPanel(toolWindow: ToolWindow, project: Project) : SimpleToolWindowPanel(
return panel
}

private fun initSettingsOptions() {
val manager = ActionManager.getInstance()
val validationSwitchAction = manager.getAction(ValidationSwitchAction.ID)

val toolbarActionGroup = DefaultActionGroup().apply {
add(validationSwitchAction)
}
(toolWindow as? ToolWindowEx)?.setAdditionalGearActions(toolbarActionGroup)
}

private fun initToolbar(toolbar: JPanel) {
val manager = ActionManager.getInstance()
val refreshAction = manager.getAction(RefreshAction.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.madrapps.dagger.validation

import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.lang.annotation.HighlightSeverity.ERROR
import com.intellij.psi.PsiElement
import com.madrapps.dagger.services.service
import com.madrapps.dagger.validation.component.InterfaceOrAbstract
import com.madrapps.dagger.validation.component.MultipleBuilderOrFactory
import com.madrapps.dagger.validation.component.NoModuleAnnotation
Expand All @@ -21,13 +21,15 @@ class ValidationAnnotator : Annotator {
)

override fun annotate(element: PsiElement, holder: AnnotationHolder) {
problems.forEach {
val errors = it.isError(element)
errors.forEach { (range, message) ->
holder.createErrorAnnotation(range, message)
if (element.project.service.settings.isValidationsEnabled) {
problems.forEach {
val errors = it.isError(element)
errors.forEach { (range, message) ->
holder.createErrorAnnotation(range, message)
// holder.newAnnotation(ERROR, message)
// .range(range)
// .create()
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@
class="com.madrapps.dagger.actions.GenerateGraphEveryBuildAction"
icon="AllIcons.Toolwindows.ToolWindowBuild"
text="Generate Graph on Every Build"/>
<action id="com.madrapps.dagger.actions.ValidationSwitchAction"
class="com.madrapps.dagger.actions.ValidationSwitchAction"
text="Turn On Validation"/>
</actions>
</idea-plugin>

0 comments on commit f6c82ba

Please sign in to comment.