Skip to content

Commit 5f4b8fa

Browse files
committed
Merge branch 'feature/change-plugin-structure' into develop
- Change CodeXP base structure => Using message bus instead of observer pattern - Add calculate level logic - Improvement of UI layout - Standardization and simplification of overall structure
2 parents 5ade19b + f1bda5d commit 5f4b8fa

File tree

12 files changed

+499
-247
lines changed

12 files changed

+499
-247
lines changed

src/main/java/com/github/ilovegamecoding/intellijcodexp/form/CodeXPDashboard.form

Lines changed: 102 additions & 83 deletions
Large diffs are not rendered by default.

src/main/java/com/github/ilovegamecoding/intellijcodexp/form/CodeXPDashboard.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66
import javax.swing.*;
77

88
public class CodeXPDashboard {
9-
private CodeXPService codeXPService = ApplicationManager.getApplication().getService(CodeXPService.class);
109
public JPanel pMain;
1110
public JProgressBar pbCurrentLevelProgress;
12-
public JLabel lblNextLabel;
11+
public JLabel lblNextLevel;
1312
public JLabel lblCurrentLevel;
14-
public JLabel tvCurrentLevelXP;
15-
public JTextField nicknameTextField;
13+
public JLabel lblCurrentLevelXP;
14+
public JTextField tfNickname;
1615
public JPanel lblCompletedChallenges;
1716
public JLabel lblTotalXP;
1817
public JLabel lblLevel;
19-
public JButton resetButton;
20-
public JPanel pEventStatistics;
2118
public JPanel pCompletedChallenges;
19+
public JPanel pEventStatistics;
2220
public JPanel pChallenges;
23-
24-
public CodeXPDashboard() {
25-
resetButton.addActionListener(e -> {
26-
codeXPService.resetPlugin();
27-
});
28-
}
2921
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.ilovegamecoding.intellijcodexp
2+
3+
object StringUtil {
4+
fun numberToStringWithCommas(number: Long): String {
5+
return number.toString().reversed().chunked(3).joinToString(",").reversed()
6+
}
7+
}
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package com.github.ilovegamecoding.intellijcodexp.listeners
22

33
import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService
4-
import com.github.ilovegamecoding.intellijcodexp.model.CodeXPChallenge
5-
import com.intellij.ide.FrameStateListener
6-
import com.intellij.openapi.components.service
4+
import com.intellij.openapi.actionSystem.AnAction
5+
import com.intellij.openapi.actionSystem.AnActionEvent
6+
import com.intellij.openapi.actionSystem.AnActionResult
7+
import com.intellij.openapi.actionSystem.DataContext
8+
import com.intellij.openapi.actionSystem.ex.AnActionListener
9+
import com.intellij.openapi.application.ApplicationManager
710
import com.intellij.openapi.diagnostic.thisLogger
8-
import com.intellij.openapi.editor.EditorFactory
9-
import com.intellij.openapi.editor.event.DocumentEvent
10-
import com.intellij.openapi.editor.event.DocumentListener
11-
import com.intellij.openapi.editor.event.EditorFactoryEvent
12-
import com.intellij.openapi.editor.event.EditorFactoryListener
1311

14-
internal class CodeXPEventListener : DocumentListener, EditorFactoryListener, FrameStateListener {
15-
private val codeXPService = service<CodeXPService>()
16-
17-
init {
18-
val editorFactory = EditorFactory.getInstance()
19-
for (editor in editorFactory.allEditors) {
20-
editor.document.addDocumentListener(this)
21-
}
22-
editorFactory.addEditorFactoryListener(this) { }
12+
/**
13+
* CodeXPEventListener class
14+
*
15+
* This class listens to events from the IDE and fires them to the message bus.
16+
*/
17+
internal class CodeXPEventListener : AnActionListener {
18+
override fun afterEditorTyping(c: Char, dataContext: DataContext) {
19+
super.afterEditorTyping(c, dataContext)
20+
fireEvent(CodeXPService.Event.TYPING)
2321
}
2422

25-
override fun documentChanged(event: DocumentEvent) {
26-
super.documentChanged(event)
27-
thisLogger().warn("CodeXPEventListener.documentChanged")
28-
codeXPService.increaseChallengeValue(CodeXPChallenge.Type.TYPING_COUNT, 1)
29-
}
23+
override fun afterActionPerformed(action: AnAction, event: AnActionEvent, result: AnActionResult) {
24+
super.afterActionPerformed(action, event, result)
25+
thisLogger().warn("Action performed: ${action.templateText}")
3026

31-
override fun editorCreated(event: EditorFactoryEvent) {
32-
event.editor.document.addDocumentListener(this)
27+
when (action.templateText) { // Fire event based on action.
28+
"Run" -> fireEvent(CodeXPService.Event.RUN)
29+
"Save All" -> fireEvent(CodeXPService.Event.SAVE)
30+
"Debug" -> fireEvent(CodeXPService.Event.DEBUG)
31+
"Build Project" -> fireEvent(CodeXPService.Event.BUILD)
32+
"Rebuild Project" -> fireEvent(CodeXPService.Event.BUILD)
33+
"Paste" -> fireEvent(CodeXPService.Event.PASTE)
34+
"Backspace" -> fireEvent(CodeXPService.Event.BACKSPACE)
35+
"Tab" -> fireEvent(CodeXPService.Event.TAB)
36+
}
3337
}
3438

35-
override fun onFrameActivated() {
36-
39+
/**
40+
* Fire event to the message bus.
41+
*
42+
* @param event The event to fire.
43+
*/
44+
private fun fireEvent(event: CodeXPService.Event) {
45+
ApplicationManager.getApplication().messageBus.syncPublisher(CodeXPListener.CODEXP_EVENT)
46+
.eventOccurred(event)
3747
}
3848
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.ilovegamecoding.intellijcodexp.listeners
2+
3+
import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService
4+
import com.intellij.util.messages.Topic
5+
6+
/**
7+
* CodeXPListener interface
8+
*
9+
* This interface is used to listen to events from the CodeXP plugin.
10+
*/
11+
interface CodeXPListener {
12+
companion object {
13+
/**
14+
* Topic for CodeXP events.
15+
*/
16+
val CODEXP_EVENT: Topic<CodeXPListener> = Topic.create("CodeXP Event", CodeXPListener::class.java)
17+
}
18+
19+
/**
20+
* Function that is called when an event occurs.
21+
*
22+
* @param event The event that occurred.
23+
*/
24+
fun eventOccurred(event: CodeXPService.Event)
25+
}
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.github.ilovegamecoding.intellijcodexp.model
22

3+
import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService
4+
35
/**
46
* CodeXPChallenge class
5-
* CodeXPChallenge is a class that represents a challenge. Challenges are used to track the progress of the user.
67
*
7-
* @constructor Create empty CodeXPChallenge.
8+
* CodeXPChallenge is a class that represents a challenge. Challenges are used to track the progress of the user.
89
*/
910
class CodeXPChallenge() {
1011
/**
11-
* Type of challenge.
12+
* Event of challenge.
1213
*/
13-
var type: Int = -1
14+
var event: CodeXPService.Event = CodeXPService.Event.NONE
1415

1516
/**
1617
* Name of challenge.
@@ -28,9 +29,9 @@ class CodeXPChallenge() {
2829
var rewardXP: Long = 0L
2930

3031
/**
31-
* Current value of challenge.
32+
* Current progress of challenge.
3233
*/
33-
var value: Long = 0L
34+
var progress: Long = 0L
3435

3536
/**
3637
* Goal of challenge.
@@ -41,36 +42,18 @@ class CodeXPChallenge() {
4142
* Constructor for CodeXPChallenge with all parameters.
4243
*/
4344
constructor(
44-
type: Int,
45+
event: CodeXPService.Event,
4546
name: String,
4647
description: String,
4748
rewardXP: Long,
48-
value: Long,
49+
progress: Long,
4950
goal: Long
5051
) : this() {
51-
this.type = type
52+
this.event = event
5253
this.name = name
5354
this.description = description
5455
this.rewardXP = rewardXP
55-
this.value = value
56+
this.progress = progress
5657
this.goal = goal
5758
}
58-
59-
/**
60-
* Type of challenge.
61-
*
62-
* !! Do not ever change the order of the enum values. If you want to add a new challenge type, add it to the end of the enum !!
63-
*/
64-
enum class Type {
65-
TOTAL_XP,
66-
ACTION_COUNT,
67-
BUILD_COUNT,
68-
DEBUG_COUNT,
69-
RUN_COUNT,
70-
COMMIT_COUNT,
71-
PUSH_COUNT,
72-
MERGE_COUNT,
73-
PULL_COUNT,
74-
TYPING_COUNT,
75-
}
7659
}

0 commit comments

Comments
 (0)