refactor ButtonGUI

Add primitive commList size limit
This commit is contained in:
Joseph Garcia 2020-12-18 16:50:33 -06:00
parent 5f6ee2b939
commit 8d711734da
2 changed files with 21 additions and 23 deletions

View File

@ -64,6 +64,7 @@ public class ConfigFile {
JSONObject childObject = (JSONObject)array.get(i); JSONObject childObject = (JSONObject)array.get(i);
commandObjects.add(childObject); commandObjects.add(childObject);
System.out.println(i); System.out.println(i);
if (i >= 19) break;
} }
return commandObjects; return commandObjects;
} catch (IOException e) { } catch (IOException e) {
@ -73,4 +74,13 @@ public class ConfigFile {
} }
return null; return null;
} }
public static void addObjectToCommList(JSONObject jsonObject) {
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
if (commListCopy.size() <= 20) {
commListCopy.add(jsonObject);
MacroButtons.setMasterCommList(commListCopy);
}
}
} }

View File

@ -47,24 +47,21 @@ public class ButtonGUI extends LightweightGuiDescription {
// Add button for command entry // Add button for command entry
WButton addCmdBtn = new WButton(new TranslatableText("+")); WButton addCmdBtn = new WButton(new TranslatableText("+"));
addCmdBtn.setOnClick(() -> { addCmdBtn.setOnClick(() -> {
addGUIButton(root, xValue, nameTextField, commandTextField); addGUIButton(root, nameTextField, commandTextField);
}); });
root.add(addCmdBtn, 18, 12, 1, 1); root.add(addCmdBtn, 18, 12, 1, 1);
} }
// function to save newly added buttons to commands.json // Function to save newly added buttons to commands.json
private void addGUIButton(WGridPanel root, int x, WTextField name, WTextField command) { private void addGUIButton(WGridPanel root, WTextField name, WTextField command) {
// Only add the button if there are contents in both // Only add the button if there are contents in both
if (!name.getText().equals("") && !command.getText().equals("")) { if (!name.getText().equals("") && !command.getText().equals("")) {
//System.out.println("Here, command is " + command.getText()); String commandString = command.getText();
String instancedString = command.getText();
WButton button = new WButton(new TranslatableText(name.getText())); WButton button = new WButton(new TranslatableText(name.getText()));
button.setOnClick(() -> { button.setOnClick(() -> {
MacroButtons.runCommand(instancedString); MacroButtons.runCommand(commandString);
}); });
root.add(button, xValue, yValue, 4, 1); root.add(button, xValue, yValue, 4, 1);
// Create a new Json object & append to masterCommList // Create a new Json object & append to masterCommList
@ -72,10 +69,8 @@ public class ButtonGUI extends LightweightGuiDescription {
newJsonObject.put("name", name.getText()); newJsonObject.put("name", name.getText());
newJsonObject.put("command", command.getText()); newJsonObject.put("command", command.getText());
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList(); // append the buttons to masterList for future loading
ConfigFile.addObjectToCommList(newJsonObject);
commListCopy.add(newJsonObject);
MacroButtons.setMasterCommList(commListCopy);
ConfigFile.appendToFile(newJsonObject); ConfigFile.appendToFile(newJsonObject);
adjustBounds(); adjustBounds();
@ -92,21 +87,15 @@ public class ButtonGUI extends LightweightGuiDescription {
} }
// function to load buttons from commands.json // 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("")) { if (!name.equals("") && !command.equals("")) {
//System.out.println("Here, command is " + command);
String instancedString = command;
WButton button = new WButton(new TranslatableText(name)); WButton button = new WButton(new TranslatableText(name));
button.setOnClick(() -> { button.setOnClick(() -> {
MacroButtons.runCommand(instancedString); MacroButtons.runCommand(command);
}); });
root.add(button, xValue, yValue, 4, 1); root.add(button, xValue, yValue, 4, 1);
adjustBounds(); adjustBounds();
root.validate(this); root.validate(this);
} else { } else {
System.out.println("No name and value entered!"); 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. // Array will contain String class types. Convert these to objects.
private void addSavedButtons(WGridPanel root) { private void addSavedButtons(WGridPanel root) {
//JSONArray stringCommList = MacroButtons.masterCommList;
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList(); ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
// Array will contain String class types. Convert these to objects
// Then convert the objects to buttons // Then convert the objects to buttons
if (commListCopy != null) { if (commListCopy != null) {
for (int i = 0; i < commListCopy.size(); i++) { for (int i = 0; i < commListCopy.size(); i++) {
String name = commListCopy.get(i).get("name").toString(); String name = commListCopy.get(i).get("name").toString();
String command = commListCopy.get(i).get("command").toString(); String command = commListCopy.get(i).get("command").toString();
addGUIButton(root, xValue, name, command); addGUIButton(root, name, command);
if (i >= 19) break;
} }
} }