From 8d711734da991d7f0f7c5328b4fdf505d6d7f43e Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 16:50:33 -0600 Subject: [PATCH] refactor ButtonGUI Add primitive commList size limit --- .../com/josyf/macrobuttons/ConfigFile.java | 10 ++++++ .../com/josyf/macrobuttons/gui/ButtonGUI.java | 34 ++++++------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 0641748..93a9f17 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -64,6 +64,7 @@ public class ConfigFile { JSONObject childObject = (JSONObject)array.get(i); commandObjects.add(childObject); System.out.println(i); + if (i >= 19) break; } return commandObjects; } catch (IOException e) { @@ -73,4 +74,13 @@ public class ConfigFile { } return null; } + + public static void addObjectToCommList(JSONObject jsonObject) { + ArrayList commListCopy = MacroButtons.getMasterCommList(); + if (commListCopy.size() <= 20) { + commListCopy.add(jsonObject); + MacroButtons.setMasterCommList(commListCopy); + } + + } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 62c1b3b..7f94089 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -47,24 +47,21 @@ public class ButtonGUI extends LightweightGuiDescription { // Add button for command entry WButton addCmdBtn = new WButton(new TranslatableText("+")); addCmdBtn.setOnClick(() -> { - addGUIButton(root, xValue, nameTextField, commandTextField); + addGUIButton(root, nameTextField, commandTextField); }); root.add(addCmdBtn, 18, 12, 1, 1); } - // function to save newly added buttons to commands.json - private void addGUIButton(WGridPanel root, int x, WTextField name, WTextField command) { + // Function to save newly added buttons to commands.json + private void addGUIButton(WGridPanel root, WTextField name, WTextField command) { // Only add the button if there are contents in both if (!name.getText().equals("") && !command.getText().equals("")) { - //System.out.println("Here, command is " + command.getText()); - String instancedString = command.getText(); - + String commandString = command.getText(); WButton button = new WButton(new TranslatableText(name.getText())); button.setOnClick(() -> { - MacroButtons.runCommand(instancedString); + MacroButtons.runCommand(commandString); }); - root.add(button, xValue, yValue, 4, 1); // Create a new Json object & append to masterCommList @@ -72,10 +69,8 @@ public class ButtonGUI extends LightweightGuiDescription { newJsonObject.put("name", name.getText()); newJsonObject.put("command", command.getText()); - ArrayList commListCopy = MacroButtons.getMasterCommList(); - - commListCopy.add(newJsonObject); - MacroButtons.setMasterCommList(commListCopy); + // append the buttons to masterList for future loading + ConfigFile.addObjectToCommList(newJsonObject); ConfigFile.appendToFile(newJsonObject); adjustBounds(); @@ -92,21 +87,15 @@ public class ButtonGUI extends LightweightGuiDescription { } // function to load buttons from commands.json - private void addGUIButton(WGridPanel root, int x, String name, String command) { + private void addGUIButton(WGridPanel root, String name, String command) { if (!name.equals("") && !command.equals("")) { - - //System.out.println("Here, command is " + command); - String instancedString = command; - WButton button = new WButton(new TranslatableText(name)); button.setOnClick(() -> { - MacroButtons.runCommand(instancedString); + MacroButtons.runCommand(command); }); - root.add(button, xValue, yValue, 4, 1); adjustBounds(); root.validate(this); - } else { System.out.println("No name and value entered!"); } @@ -115,15 +104,14 @@ public class ButtonGUI extends LightweightGuiDescription { // Array will contain String class types. Convert these to objects. private void addSavedButtons(WGridPanel root) { - //JSONArray stringCommList = MacroButtons.masterCommList; ArrayList commListCopy = MacroButtons.getMasterCommList(); - // Array will contain String class types. Convert these to objects // Then convert the objects to buttons if (commListCopy != null) { for (int i = 0; i < commListCopy.size(); i++) { String name = commListCopy.get(i).get("name").toString(); String command = commListCopy.get(i).get("command").toString(); - addGUIButton(root, xValue, name, command); + addGUIButton(root, name, command); + if (i >= 19) break; } }