added first version of project 81 plugin

This commit is contained in:
2014-01-06 23:55:19 +01:00
parent d9156b87e8
commit 814cff94c8
4 changed files with 160 additions and 50 deletions

View File

@@ -0,0 +1,110 @@
import de.frosch95.geofrogger.plugins.Plugin
import javafx.concurrent.Service
import javafx.concurrent.Task
import javafx.concurrent.WorkerStateEvent
import javafx.event.EventHandler
import javafx.scene.control.Label
import javafx.scene.control.ScrollPane
import javafx.scene.control.TextArea
import javafx.scene.layout.Pane
import javafx.scene.layout.Priority
import javafx.scene.layout.VBox
import org.controlsfx.dialog.Dialog
class Project81Plugin implements Plugin {
final static DIFFICULTY_TERRAIN_VALUES = ['1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5']
final String name = "Project81"
final String version = "0.0.1"
final CalculateService service = new CalculateService();
Project81Plugin() {
service.onSucceeded = {
showDialog(createHeader(), createContent(it.source.value))
} as EventHandler<WorkerStateEvent>;
}
@Override
void run(final Map context) {
calculateStats(context.sessionContext)
}
private javafx.scene.Node createHeader() {
def pane = new Pane()
def Label label = new Label()
label.text = "Shows missing difficulty / terrain combination."
pane.children.add(label)
pane
}
/**
* creates the statistic charts to show
* @param sessionContext context with the cache list in it
* @return
*/
private void calculateStats(sessionContext) {
// get the cache list out of the context
def cacheList = sessionContext.getData("cache-list")
service.cacheList = cacheList;
service.restart();
}
/**
* creates the statistic charts to show
* @param sessionContext context with the cache list in it
* @return
*/
private javafx.scene.Node createContent(result) {
// create javafx chart
def text = new TextArea();
result.each { text.appendText("$it.name ($it.difficulty/$it.terrain)\n") }
text
}
private void showDialog(header, content) {
Dialog dialog = new Dialog(null, name + " (" + version + ")", true)
dialog.setMasthead(header)
dialog.setContent(content)
dialog.show()
}
}
class CalculateService extends Service {
def cacheList
@Override
protected Task createTask() {
return new Task() {
@Override
protected ArrayList call() throws Exception {
try {
def dtFound = [:]
for (def difficulty in Project81Plugin.DIFFICULTY_TERRAIN_VALUES) {
for (def terrain in Project81Plugin.DIFFICULTY_TERRAIN_VALUES) {
dtFound[difficulty + "_" + terrain] = false
}
}
for (def cache in cacheList) {
if (cache.found) {
dtFound[cache.difficulty + "_" + cache.terrain] = true
}
}
cacheList.findAll {
cache -> !(cache.found || dtFound[cache.difficulty + "_" + cache.terrain])
}
} catch (Exception e) {
e.printStackTrace()
}
}
};
}
}

View File

@@ -23,6 +23,7 @@ public interface Plugin {
* Run the main method of the plugin. All the logic is done in this method.
* Every run method will get a context map, with all the services inside,
* to use them.
*
* @param context services and data
*/
void run(Map context);

View File

@@ -14,5 +14,4 @@ public interface PluginService {
void executePlugin(Plugin plugin);
}

View File

@@ -12,7 +12,7 @@ import java.util.List;
import java.util.Map;
/**
* TODO: class description
* This service find, load and executes plugins based on the plugin interface.
*
* @author abi
*/
@@ -30,15 +30,15 @@ public class PluginServiceImpl implements PluginService {
try {
File file = new File("./plugins");
if (!file.exists()) {
throw new IllegalArgumentException("plugins folder does not exit");
throw new IllegalArgumentException("plugins folder does not exist");
}
File[] pluginFiles = file.listFiles((dir, name) -> name.endsWith("Plugin.groovy"));
for (File pluginFile: pluginFiles) {
for (File pluginFile : pluginFiles) {
Class clazz = gcl.parseClass(pluginFile);
for (Class interf: clazz.getInterfaces()) {
for (Class interf : clazz.getInterfaces()) {
if (interf.equals(Plugin.class)) {
plugins.add((Plugin)clazz.newInstance());
plugins.add((Plugin) clazz.newInstance());
break;
}
}