Skip to content

Commit

Permalink
Add basic event management system
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Feb 21, 2022
1 parent 6a89251 commit eb579f7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/java/io/github/arrayv/groovyapi/ArrayVEventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.arrayv.groovyapi;

import io.github.arrayv.main.ArrayVisualizer;

public final class ArrayVEventHandler {
public static enum EventType {
SCRIPTS_INSTALLED,
ARRAYV_FULLY_LOADED
}

private final EventType eventType;
private final Runnable callback;

public ArrayVEventHandler(EventType eventType, Runnable cb) {
this.eventType = eventType;
this.callback = cb;
}

public EventType getEventType() {
return eventType;
}

public Runnable getCallback() {
return callback;
}

public void handle() {
callback.run();
}

public void register() {
ArrayVisualizer.getInstance().getScriptManager().registerEventHandlers(this);
}

public void unregister() {
ArrayVisualizer.getInstance().getScriptManager().unregisterEventHandlers(this);
}

@Override
public int hashCode() {
return 31 * eventType.hashCode() + callback.hashCode();
}

@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ArrayVEventHandler)) return false;
ArrayVEventHandler other = (ArrayVEventHandler)o;
return eventType.equals(other.eventType) && callback.equals(other.callback);
}
}
44 changes: 44 additions & 0 deletions src/main/java/io/github/arrayv/groovyapi/ScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
Expand Down Expand Up @@ -47,6 +50,7 @@ public void run() {
private static final File INSTALLED_SCRIPTS_ROOT = new File("scripts");

private final GroovyShell shell;
private final Map<ArrayVEventHandler.EventType, Set<ArrayVEventHandler>> events;
private Map<String, Script> installedScripts;

public ScriptManager() {
Expand All @@ -60,13 +64,52 @@ public ScriptManager() {
compilerConfig.setScriptBaseClass("io.github.arrayv.groovyapi.ArrayVScript");
compilerConfig.getClasspath().add(INSTALLED_SCRIPTS_ROOT.getPath());
this.shell = new GroovyShell(compilerConfig);
this.events = new EnumMap<>(ArrayVEventHandler.EventType.class);
this.installedScripts = null;
}

public GroovyShell getGroovyShell() {
return shell;
}

private Set<ArrayVEventHandler> getEventHandlers0(ArrayVEventHandler.EventType eventType) {
return events.computeIfAbsent(eventType, k -> new HashSet<>());
}

public Set<ArrayVEventHandler> getEventHandlers(ArrayVEventHandler.EventType eventType) {
return Collections.unmodifiableSet(getEventHandlers0(eventType));
}

public void runEventHandlers(ArrayVEventHandler.EventType eventType) {
RuntimeException e = null;
for (ArrayVEventHandler handler : getEventHandlers0(eventType)) {
try {
handler.handle();
} catch (Exception e1) {
if (e == null) {
e = new RuntimeException(e1);
} else {
e.addSuppressed(e1);
}
}
}
if (e != null) {
throw e;
}
}

public void registerEventHandlers(ArrayVEventHandler... handlers) {
for (ArrayVEventHandler handler : handlers) {
getEventHandlers0(handler.getEventType()).add(handler);
}
}

public void unregisterEventHandlers(ArrayVEventHandler... handlers) {
for (ArrayVEventHandler handler : handlers) {
getEventHandlers0(handler.getEventType()).remove(handler);
}
}

public Script loadScript(File path) throws IOException {
Script script = shell.parse(path);
script.run();
Expand Down Expand Up @@ -96,6 +139,7 @@ public Map<String, Script> loadInstalledScripts() throws IOException {
Script script = loadScript(subFile);
installedScripts.put(script.getClass().getName(), script);
}
runEventHandlers(ArrayVEventHandler.EventType.SCRIPTS_INSTALLED);
return installedScripts;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/github/arrayv/main/ArrayVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import io.github.arrayv.frames.ArrayFrame;
import io.github.arrayv.frames.SoundFrame;
import io.github.arrayv.frames.UtilFrame;
import io.github.arrayv.groovyapi.ArrayVEventHandler;
import io.github.arrayv.groovyapi.ScriptManager;
import io.github.arrayv.panes.JErrorPane;
import io.github.arrayv.sortdata.SortInfo;
Expand Down Expand Up @@ -653,6 +654,8 @@ public void run() {

this.SOUNDS.startAudioThread();
this.drawWindows();

SCRIPT_MANAGER.runEventHandlers(ArrayVEventHandler.EventType.ARRAYV_FULLY_LOADED);
}

public static ArrayVisualizer getInstance() {
Expand Down

0 comments on commit eb579f7

Please sign in to comment.