From c03e191c36c5ed13189587f289b4e91b8d850edd Mon Sep 17 00:00:00 2001 From: Riley Jones Date: Wed, 2 Aug 2017 14:31:29 -0700 Subject: [PATCH] add live load and unload --- src/main/java/org/nwapw/abacus/Abacus.java | 14 +++++-- .../org/nwapw/abacus/fx/AbacusController.java | 42 +++++++++++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 13 ++++++ src/main/resources/abacus.fxml | 3 ++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/Abacus.java b/src/main/java/org/nwapw/abacus/Abacus.java index bf8328f..9449047 100644 --- a/src/main/java/org/nwapw/abacus/Abacus.java +++ b/src/main/java/org/nwapw/abacus/Abacus.java @@ -9,7 +9,7 @@ import org.nwapw.abacus.parsing.ShuntingYardParser; import org.nwapw.abacus.parsing.TreeBuilder; import org.nwapw.abacus.plugin.ClassFinder; import org.nwapw.abacus.plugin.PluginManager; -import org.nwapw.abacus.plugin.StandardPlugin; +//import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.tree.NumberReducer; import org.nwapw.abacus.tree.TreeNode; @@ -67,16 +67,22 @@ public class Abacus { pluginManager.addListener(lexerTokenizer); pluginManager.addListener(shuntingYardParser); - pluginManager.addInstantiated(new StandardPlugin(pluginManager)); + //pluginManager.addInstantiated(new StandardPlugin(pluginManager)); + /* try { ClassFinder.loadJars("plugins") .forEach(plugin -> pluginManager.addClass(plugin)); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); - } + }//*/ pluginManager.load(); } - + public void loadClass(Class newClass){ + pluginManager.addClass(newClass); + } + public void unloadClass(Class newClass){ + pluginManager.removeClass(newClass); + } public static void main(String[] args) { AbacusApplication.launch(AbacusApplication.class, args); } diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index aa898d8..2dbadfc 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -8,8 +8,11 @@ import javafx.scene.text.Text; import javafx.util.Callback; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.number.NumberInterface; +import org.nwapw.abacus.plugin.ClassFinder; import org.nwapw.abacus.tree.TreeNode; +import java.io.IOException; + /** * The controller for the abacus FX UI, responsible @@ -42,6 +45,12 @@ public class AbacusController { private Button inputButton; @FXML private ComboBox numberImplementationBox; + @FXML + private Button loadButton; + @FXML + private Button unloadButton; + @FXML + private TextField loadField; /** * The list of history entries, created by the users. @@ -106,5 +115,38 @@ public class AbacusController { inputButton.setDisable(false); inputField.setText(""); } + @FXML + private void loadClass(){ + try { + for(Class plugin :ClassFinder.loadJars("plugins")){ + String name = ""; + //String name = plugin.getName(); + while(!(name.indexOf('/') ==-1)){ + name=name.substring(name.indexOf('/')+1); + } + if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){ + //abacus.loadClass(plugin); + } + } + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + @FXML + private void unloadClass(){ + try { + for(Class plugin :ClassFinder.loadJars("plugins")){ + String name = plugin.getName(); + while(!(name.indexOf('/') ==-1)){ + name=name.substring(name.indexOf('/')+1); + } + if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){ + //abacus.unloadClass(plugin); + } + } + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 757ac67..8f33f9c 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -140,6 +140,11 @@ public class PluginManager { plugins.add(plugin); loadedPluginClasses.add(plugin.getClass()); } + public void removeInstantiated(Plugin plugin){ + if (loadedPluginClasses.contains(plugin.getClass())) return; + plugins.remove(plugin); + loadedPluginClasses.remove(plugin.getClass()); + } /** * Instantiates a class of plugin, and adds it to this @@ -155,6 +160,14 @@ public class PluginManager { e.printStackTrace(); } } + public void removeClass(Class newClass){ + if (!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return; + try { + removeInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this)); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + } /** * Loads all the plugins in the PluginManager. diff --git a/src/main/resources/abacus.fxml b/src/main/resources/abacus.fxml index 183e247..bdbdf1b 100644 --- a/src/main/resources/abacus.fxml +++ b/src/main/resources/abacus.fxml @@ -46,6 +46,9 @@