Skip to content

Commit

Permalink
[ML4SE-169] States of the panel are separated from the MainPluginPane…
Browse files Browse the repository at this point in the history
…lFactory.
  • Loading branch information
mikrise2 committed Oct 18, 2023
1 parent cee4b4f commit 4b34bd5
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBPanel
import com.intellij.ui.jcef.JBCefApp
import com.intellij.util.ui.JBUI
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.content.task.base.Task
import org.jetbrains.research.tasktracker.config.content.task.base.TaskWithFiles
import org.jetbrains.research.tasktracker.modelInference.model.EmoModel
import org.jetbrains.research.tasktracker.requests.FileRequests.send
import org.jetbrains.research.tasktracker.tracking.BaseTracker
import org.jetbrains.research.tasktracker.tracking.TaskFileHandler
import org.jetbrains.research.tasktracker.tracking.activity.ActivityTracker
import org.jetbrains.research.tasktracker.tracking.fileEditor.FileEditorTracker
import org.jetbrains.research.tasktracker.tracking.toolWindow.ToolWindowTracker
import org.jetbrains.research.tasktracker.tracking.webcam.WebCamTracker
import org.jetbrains.research.tasktracker.tracking.webcam.collectAllDevices
import org.jetbrains.research.tasktracker.ui.main.panel.panelStates.welcomePage
import org.jetbrains.research.tasktracker.ui.main.panel.storage.GlobalPluginStorage
import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage
import org.jetbrains.research.tasktracker.ui.main.panel.template.*
import org.jetbrains.research.tasktracker.util.UIBundle
import org.jetbrains.research.tasktracker.util.survey.SurveyParser
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.event.ActionListener
Expand All @@ -51,10 +46,10 @@ class MainPluginPanelFactory : ToolWindowFactory {
private val backButton = createJButton("ui.button.back", isVisibleProp = false)
private val logger: Logger = Logger.getInstance(MainPluginPanelFactory::class.java)

private val trackers: MutableList<BaseTracker> = mutableListOf()
val trackers: MutableList<BaseTracker> = mutableListOf()

private lateinit var mainWindow: MainPluginWindow
private lateinit var project: Project
lateinit var mainWindow: MainPluginWindow
lateinit var project: Project

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
this.project = project
Expand All @@ -74,7 +69,7 @@ class MainPluginPanelFactory : ToolWindowFactory {

override fun isApplicable(project: Project) = super.isApplicable(project) && JBCefApp.isSupported()

private fun startTracking() {
fun startTracking() {
if (trackers.isNotEmpty()) { // Otherwise we can lose data
return
}
Expand All @@ -95,19 +90,19 @@ class MainPluginPanelFactory : ToolWindowFactory {
trackers.forEach { it.startTracking() }
}

private fun loadBasePage(template: HtmlTemplate, buttonTextKey: String, isVisibleBackButton: Boolean) {
fun loadBasePage(
template: HtmlTemplate,
buttonTextKey: String,
isVisibleBackButton: Boolean = true,
backButtonText: String? = null,
isVisibleNextButton: Boolean = true
) {
mainWindow.loadHtmlTemplate(template)
nextButton.text = UIBundle.message(buttonTextKey)
backButton.isVisible = isVisibleBackButton
}

/**
* Switches the panel to the plugin description window.
*/
private fun welcomePage() {
loadBasePage(MainPageTemplate.loadCurrentTemplate(), "ui.button.next", false)
nextButton.addListener {
selectTask()
nextButton.isVisible = isVisibleNextButton
backButtonText?.let {
backButton.text = backButtonText
}
}

Expand All @@ -127,44 +122,7 @@ class MainPluginPanelFactory : ToolWindowFactory {
})
}

// TODO refactor it for many configs
/**
* Switches the panel to the task selection window.
*/
private fun selectTask() {
loadBasePage(
TasksPageTemplate(MainPanelStorage.taskIdTask.values.toList()), "ui.button.select", true
)
nextButton.addListener {
mainWindow.getElementValue("tasks").onSuccess { name ->
processTask(name.toString())
}.onError {
error(it.localizedMessage)
}
}
backButton.addListener {
welcomePage()
}
}

/**
* Loads configs by selected task and language
*/
private fun processTask(name: String) {
startTracking()
// TODO: change to task by id
val task =
MainPanelStorage.taskIdTask.values.find { it.name == name } ?: error("Can't find task with name '$name'")
ApplicationManager.getApplication().invokeAndWait {
TaskFileHandler.initTask(project, task)
}
(task as? TaskWithFiles)?.focusFileId?.let { id ->
focusOnfFileById(task, id)
}
solveTask(task)
}

private fun focusOnfFileById(task: Task, id: String?) {
fun focusOnfFileById(task: Task, id: String?) {
id?.let {
TaskFileHandler.getVirtualFileByProjectTaskId(project, task, id)?.let {
focusOnFile(it)
Expand All @@ -185,69 +143,7 @@ class MainPluginPanelFactory : ToolWindowFactory {
}
}

private fun survey() {
loadBasePage(
SurveyTemplate.loadCurrentTemplate(), "ui.button.submit", true
)
nextButton.addListener {
val surveyParser = SurveyParser(mainWindow, project)
// TODO: rewrite
GlobalScope.launch {
surveyParser.parseAndLog()
// TODO: unify
val isSuccessful = surveyParser.send()
if (!isSuccessful) {
serverErrorPage()
}
trackers.forEach {
it.stopTracking()
}
trackers.forEach {
if (!it.send()) {
serverErrorPage()
}
}
trackers.clear()
}
}
backButton.addListener {
}
}

private fun serverErrorPage() {
trackers.clear()
loadBasePage(
ServerErrorPage(), "ui.button.welcome", false
)
nextButton.isVisible = false
}

/**
* Switches the panel to the task solving window.
* It contains task name, description and I/O data.
*/
private fun solveTask(task: Task) {
val activityTracker = ActivityTracker(project)
activityTracker.startTracking()
loadBasePage(
SolvePageTemplate(task), "ui.button.submit", true
)
backButton.addListener {
TaskFileHandler.disposeTask(project, task)
mainWindow.removeHandlers()
selectTask()
activityTracker.stopTracking()
}
nextButton.addListener {
TaskFileHandler.disposeTask(project, task)
mainWindow.removeHandlers()
welcomePage()
activityTracker.stopTracking()
}
listenFileRedirection(task)
}

private fun listenFileRedirection(task: Task) {
fun listenFileRedirection(task: Task) {
mainWindow.executeJavascript(
"""
const files = document.getElementsByClassName('file');
Expand All @@ -269,6 +165,10 @@ class MainPluginPanelFactory : ToolWindowFactory {
}
}

fun setNextAction(listener: ActionListener) = nextButton.addListener(listener)

fun setBackAction(listener: ActionListener) = backButton.addListener(listener)

private fun JButton.addListener(listener: ActionListener) {
actionListeners.forEach {
removeActionListener(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.jetbrains.research.tasktracker.ui.main.panel.panelStates

import com.intellij.openapi.application.ApplicationManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.research.tasktracker.config.content.task.base.Task
import org.jetbrains.research.tasktracker.config.content.task.base.TaskWithFiles
import org.jetbrains.research.tasktracker.requests.FileRequests.send
import org.jetbrains.research.tasktracker.tracking.TaskFileHandler
import org.jetbrains.research.tasktracker.tracking.activity.ActivityTracker
import org.jetbrains.research.tasktracker.ui.main.panel.MainPluginPanelFactory
import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage
import org.jetbrains.research.tasktracker.ui.main.panel.template.*
import org.jetbrains.research.tasktracker.util.survey.SurveyParser

typealias Panel = MainPluginPanelFactory

/**
* Switches the panel to the plugin description window.
*/
fun Panel.welcomePage() {
loadBasePage(MainPageTemplate.loadCurrentTemplate(), "ui.button.next", false)
setNextAction {
selectTask()
}
}

// TODO refactor it for many configs
/**
* Switches the panel to the task selection window.
*/
private fun Panel.selectTask() {
loadBasePage(
TasksPageTemplate(MainPanelStorage.taskIdTask.values.toList()), "ui.button.select", true
)
setNextAction {
mainWindow.getElementValue("tasks").onSuccess { name ->
processTask(name.toString())
}.onError {
error(it.localizedMessage)
}
}
setBackAction {
welcomePage()
}
}

/**
* Loads configs by selected task and language
*/
private fun Panel.processTask(name: String) {
startTracking()
// TODO: change to task by id
val task =
MainPanelStorage.taskIdTask.values.find { it.name == name } ?: error("Can't find task with name '$name'")
ApplicationManager.getApplication().invokeAndWait {
TaskFileHandler.initTask(project, task)
}
(task as? TaskWithFiles)?.focusFileId?.let { id ->
focusOnfFileById(task, id)
}
solveTask(task)
}

/**
* Switches the panel to the task solving window.
* It contains task name, description and I/O data.
*/
private fun Panel.solveTask(task: Task) {
val activityTracker = ActivityTracker(project)
activityTracker.startTracking()
loadBasePage(
SolvePageTemplate(task), "ui.button.submit", true
)
setBackAction {
TaskFileHandler.disposeTask(project, task)
mainWindow.removeHandlers()
selectTask()
activityTracker.stopTracking()
}
setNextAction {
TaskFileHandler.disposeTask(project, task)
mainWindow.removeHandlers()
welcomePage()
activityTracker.stopTracking()
}
listenFileRedirection(task)
}

private fun Panel.survey() {
loadBasePage(
SurveyTemplate.loadCurrentTemplate(), "ui.button.submit", true
)
setNextAction {
val surveyParser = SurveyParser(mainWindow, project)
// TODO: rewrite
GlobalScope.launch {
surveyParser.parseAndLog()
// TODO: unify
val isSuccessful = surveyParser.send()
if (!isSuccessful) {
serverErrorPage()
}
trackers.forEach {
it.stopTracking()
}
trackers.forEach {
if (!it.send()) {
serverErrorPage()
}
}
trackers.clear()
}
}
}

fun Panel.serverErrorPage() {
trackers.clear()
loadBasePage(
ServerErrorPage(), "ui.button.welcome", false, isVisibleNextButton = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.jetbrains.research.tasktracker.ui.main.panel.MainPluginWindow
import java.io.File

class SurveyParser(private val mainWindow: MainPluginWindow, project: Project) : Loggable {
val surveyLogger = SurveyLogger(project)
private val surveyLogger = SurveyLogger(project)

private val items = TaskTrackerPlugin.mainConfig.surveyConfig?.htmlQuestions ?: emptyList()

Expand Down

0 comments on commit 4b34bd5

Please sign in to comment.