From 814cff94c855201d9e1e011c935e2e9ef4493f4c Mon Sep 17 00:00:00 2001 From: Andreas Billmann Date: Mon, 6 Jan 2014 23:55:19 +0100 Subject: [PATCH] added first version of project 81 plugin --- plugins/Project81Plugin.groovy | 110 ++++++++++++++++++ .../frosch95/geofrogger/plugins/Plugin.java | 31 ++--- .../geofrogger/plugins/PluginService.java | 5 +- .../geofrogger/plugins/PluginServiceImpl.java | 64 +++++----- 4 files changed, 160 insertions(+), 50 deletions(-) create mode 100644 plugins/Project81Plugin.groovy diff --git a/plugins/Project81Plugin.groovy b/plugins/Project81Plugin.groovy new file mode 100644 index 0000000..62595f2 --- /dev/null +++ b/plugins/Project81Plugin.groovy @@ -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; + } + + @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() + } + } + }; + } + +} \ No newline at end of file diff --git a/src/de/frosch95/geofrogger/plugins/Plugin.java b/src/de/frosch95/geofrogger/plugins/Plugin.java index 0e00f54..e05ad86 100644 --- a/src/de/frosch95/geofrogger/plugins/Plugin.java +++ b/src/de/frosch95/geofrogger/plugins/Plugin.java @@ -9,22 +9,23 @@ import java.util.Map; */ public interface Plugin { - /** - * @return name - */ - String getName(); + /** + * @return name + */ + String getName(); - /** - * @return version - */ - String getVersion(); + /** + * @return version + */ + String getVersion(); - /** - * 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); + /** + * 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); } diff --git a/src/de/frosch95/geofrogger/plugins/PluginService.java b/src/de/frosch95/geofrogger/plugins/PluginService.java index 3a58b9c..01c3d5c 100644 --- a/src/de/frosch95/geofrogger/plugins/PluginService.java +++ b/src/de/frosch95/geofrogger/plugins/PluginService.java @@ -9,10 +9,9 @@ import java.util.List; */ public interface PluginService { - List getAllPlugins(); - - void executePlugin(Plugin plugin); + List getAllPlugins(); + void executePlugin(Plugin plugin); } diff --git a/src/de/frosch95/geofrogger/plugins/PluginServiceImpl.java b/src/de/frosch95/geofrogger/plugins/PluginServiceImpl.java index 4aab363..5d7d18d 100644 --- a/src/de/frosch95/geofrogger/plugins/PluginServiceImpl.java +++ b/src/de/frosch95/geofrogger/plugins/PluginServiceImpl.java @@ -12,50 +12,50 @@ 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 */ public class PluginServiceImpl implements PluginService { - private final GroovyClassLoader gcl = new GroovyClassLoader(); - private final ServiceManager serviceManager = ServiceManager.getInstance(); + private final GroovyClassLoader gcl = new GroovyClassLoader(); + private final ServiceManager serviceManager = ServiceManager.getInstance(); - @Override - public List getAllPlugins() { + @Override + public List getAllPlugins() { - List plugins = new ArrayList<>(); + List plugins = new ArrayList<>(); - try { - File file = new File("./plugins"); - if (!file.exists()) { - throw new IllegalArgumentException("plugins folder does not exit"); - } + try { + File file = new File("./plugins"); + if (!file.exists()) { + throw new IllegalArgumentException("plugins folder does not exist"); + } - File[] pluginFiles = file.listFiles((dir, name) -> name.endsWith("Plugin.groovy")); - for (File pluginFile: pluginFiles) { - Class clazz = gcl.parseClass(pluginFile); - for (Class interf: clazz.getInterfaces()) { - if (interf.equals(Plugin.class)) { - plugins.add((Plugin)clazz.newInstance()); - break; - } + File[] pluginFiles = file.listFiles((dir, name) -> name.endsWith("Plugin.groovy")); + for (File pluginFile : pluginFiles) { + Class clazz = gcl.parseClass(pluginFile); + for (Class interf : clazz.getInterfaces()) { + if (interf.equals(Plugin.class)) { + plugins.add((Plugin) clazz.newInstance()); + break; + } + } + } + + } catch (IOException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); } - } - } catch (IOException | InstantiationException | IllegalAccessException e) { - e.printStackTrace(); + return plugins; } - return plugins; - } - - @Override - public void executePlugin(final Plugin plugin) { - Map context = new HashMap<>(); - context.put("sessionContext", SessionContext.getInstance()); - context.put("cacheService", serviceManager.getCacheService()); - plugin.run(context); - } + @Override + public void executePlugin(final Plugin plugin) { + Map context = new HashMap<>(); + context.put("sessionContext", SessionContext.getInstance()); + context.put("cacheService", serviceManager.getCacheService()); + plugin.run(context); + } }