Option to add and remove custom actions in the wizard

This commit is contained in:
Evenprime 2011-05-16 14:27:01 +02:00
parent c6aa6b213b
commit ed0a36879c
4 changed files with 161 additions and 49 deletions

View File

@ -64,12 +64,12 @@ public class NoCheatConfiguration {
} }
root = new ParentOption(""); root = new ParentOption("", false);
/*** LOGGING section ***/ /*** LOGGING section ***/
{ {
ParentOption loggingNode = new ParentOption("logging"); ParentOption loggingNode = new ParentOption("logging", false);
root.add(loggingNode); root.add(loggingNode);
loggingNode.add(new MediumStringOption("filename", loggingNode.add(new MediumStringOption("filename",
@ -90,7 +90,7 @@ public class NoCheatConfiguration {
/*** ACTIVE section ***/ /*** ACTIVE section ***/
{ {
ParentOption activeNode = new ParentOption("active"); ParentOption activeNode = new ParentOption("active", false);
root.add(activeNode); root.add(activeNode);
activeNode.add(new BooleanOption("speedhack", activeNode.add(new BooleanOption("speedhack",
@ -109,7 +109,7 @@ public class NoCheatConfiguration {
/*** SPEEDHACK section ***/ /*** SPEEDHACK section ***/
{ {
ParentOption speedhackNode = new ParentOption("speedhack"); ParentOption speedhackNode = new ParentOption("speedhack", false);
root.add(speedhackNode); root.add(speedhackNode);
speedhackNode.add(new LongStringOption("logmessage", speedhackNode.add(new LongStringOption("logmessage",
@ -117,7 +117,7 @@ public class NoCheatConfiguration {
/*** SPEEDHACK LIMITS section ***/ /*** SPEEDHACK LIMITS section ***/
{ {
ParentOption speedhackLimitsNode = new ParentOption("limits"); ParentOption speedhackLimitsNode = new ParentOption("limits", false);
speedhackNode.add(speedhackLimitsNode); speedhackNode.add(speedhackLimitsNode);
speedhackLimitsNode.add(new IntegerOption("low", speedhackLimitsNode.add(new IntegerOption("low",
@ -130,7 +130,7 @@ public class NoCheatConfiguration {
/*** SPEEDHACK ACTIONS section ***/ /*** SPEEDHACK ACTIONS section ***/
{ {
ParentOption speedhackActionNode = new ParentOption("action"); ParentOption speedhackActionNode = new ParentOption("action", false);
speedhackNode.add(speedhackActionNode); speedhackNode.add(speedhackActionNode);
speedhackActionNode.add(new MediumStringOption("low", speedhackActionNode.add(new MediumStringOption("low",
@ -144,7 +144,7 @@ public class NoCheatConfiguration {
/*** MOVING section ***/ /*** MOVING section ***/
{ {
ParentOption movingNode = new ParentOption("moving"); ParentOption movingNode = new ParentOption("moving", false);
root.add(movingNode); root.add(movingNode);
movingNode.add(new LongStringOption("logmessage", movingNode.add(new LongStringOption("logmessage",
@ -158,7 +158,7 @@ public class NoCheatConfiguration {
/*** MOVING ACTION section ***/ /*** MOVING ACTION section ***/
{ {
ParentOption movingActionNode = new ParentOption("action"); ParentOption movingActionNode = new ParentOption("action", false);
movingNode.add(movingActionNode); movingNode.add(movingActionNode);
movingActionNode.add(new MediumStringOption("low", movingActionNode.add(new MediumStringOption("low",
@ -172,12 +172,12 @@ public class NoCheatConfiguration {
/*** AIRBUILD section ***/ /*** AIRBUILD section ***/
{ {
ParentOption airbuildNode = new ParentOption("airbuild"); ParentOption airbuildNode = new ParentOption("airbuild", false);
root.add(airbuildNode); root.add(airbuildNode);
/*** AIRBUILD LIMITS section ***/ /*** AIRBUILD LIMITS section ***/
{ {
ParentOption airbuildLimitsNode = new ParentOption("limits"); ParentOption airbuildLimitsNode = new ParentOption("limits", false);
airbuildNode.add(airbuildLimitsNode); airbuildNode.add(airbuildLimitsNode);
airbuildLimitsNode.add(new IntegerOption("low", airbuildLimitsNode.add(new IntegerOption("low",
@ -190,7 +190,7 @@ public class NoCheatConfiguration {
/*** AIRBUILD ACTION section ***/ /*** AIRBUILD ACTION section ***/
{ {
ParentOption airbuildActionNode = new ParentOption("action"); ParentOption airbuildActionNode = new ParentOption("action", false);
airbuildNode.add(airbuildActionNode); airbuildNode.add(airbuildActionNode);
airbuildActionNode.add(new MediumStringOption("low", airbuildActionNode.add(new MediumStringOption("low",
@ -204,25 +204,25 @@ public class NoCheatConfiguration {
/*** BEDTELEPORT section ***/ /*** BEDTELEPORT section ***/
{ {
ParentOption bedteleportNode = new ParentOption("bedteleport"); ParentOption bedteleportNode = new ParentOption("bedteleport", false);
root.add(bedteleportNode); root.add(bedteleportNode);
} }
/*** ITEMDUPE section ***/ /*** ITEMDUPE section ***/
{ {
ParentOption itemdupeNode = new ParentOption("itemdupe"); ParentOption itemdupeNode = new ParentOption("itemdupe", false);
root.add(itemdupeNode); root.add(itemdupeNode);
} }
/*** BOGUSITEMS section ***/ /*** BOGUSITEMS section ***/
{ {
ParentOption bogusitemsNode = new ParentOption("bogusitems"); ParentOption bogusitemsNode = new ParentOption("bogusitems", false);
root.add(bogusitemsNode); root.add(bogusitemsNode);
} }
/*** CUSTOMACTIONS section ***/ /*** CUSTOMACTIONS section ***/
{ {
ParentOption customActionsNode = new ParentOption("customactions"); ParentOption customActionsNode = new ParentOption("customactions", true);
root.add(customActionsNode); root.add(customActionsNode);
Set<String> customs = SimpleYaml.getKeys("customactions", yamlContent); Set<String> customs = SimpleYaml.getKeys("customactions", yamlContent);

View File

@ -13,9 +13,11 @@ public class ParentOption extends Option {
private static final long serialVersionUID = 3162246550749560727L; private static final long serialVersionUID = 3162246550749560727L;
private LinkedList<Option> children = new LinkedList<Option>(); private LinkedList<Option> children = new LinkedList<Option>();
private boolean editable;
public ParentOption(String identifier) { public ParentOption(String identifier, boolean editable) {
super(identifier); super(identifier);
this.editable = editable;
} }
public final Collection<Option> getChildOptions() { public final Collection<Option> getChildOptions() {
@ -27,6 +29,16 @@ public class ParentOption extends Option {
children.addLast(option); children.addLast(option);
} }
public final void remove(Option option) {
if(editable)
children.remove(option);
}
public boolean isEditable() {
return editable;
}
@Override @Override
public String toYAMLString(String prefix) { public String toYAMLString(String prefix) {

View File

@ -41,7 +41,7 @@ public class Wizard extends JFrame {
inside.add(root2); inside.add(root2);
JButton b = new JButton("TEST"); JButton b = new JButton("Save");
b.addActionListener(new ActionListener() { b.addActionListener(new ActionListener() {
@ -49,16 +49,22 @@ public class Wizard extends JFrame {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
String s = config.getRoot().toYAMLString(""); String s = config.getRoot().toYAMLString("");
JOptionPane.showMessageDialog(null, s);
NoCheatConfiguration.writeConfigFile(new File("config.yml"), config); NoCheatConfiguration.writeConfigFile(new File("NoCheat/nocheat.yml"), config);
JOptionPane.showMessageDialog(null, "Saved");
} }); } });
b.setAlignmentY(0.0F); b.setAlignmentY(0.0F);
inside.add(b); inside.add(b);
inside.doLayout(); this.doLayout();
this.pack(); this.pack();
this.setSize(900, 700);
this.setTitle("NoCheat configuration utility");
} }
} }

View File

@ -3,15 +3,20 @@ package cc.co.evenprime.bukkit.nocheat.wizard.gui;
import java.awt.Color; import java.awt.Color;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList; import java.util.LinkedList;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextField;
import cc.co.evenprime.bukkit.nocheat.config.ChildOption; import cc.co.evenprime.bukkit.nocheat.config.ChildOption;
import cc.co.evenprime.bukkit.nocheat.config.CustomActionOption;
import cc.co.evenprime.bukkit.nocheat.config.Option; import cc.co.evenprime.bukkit.nocheat.config.Option;
import cc.co.evenprime.bukkit.nocheat.config.ParentOption; import cc.co.evenprime.bukkit.nocheat.config.ParentOption;
@ -21,9 +26,9 @@ public class ParentOptionGui extends JPanel {
* *
*/ */
private static final long serialVersionUID = 5277750257203546802L; private static final long serialVersionUID = 5277750257203546802L;
private ParentOption option; private final ParentOption option;
private LinkedList<Option> children = new LinkedList<Option>(); private final LinkedList<Option> children = new LinkedList<Option>();
public ParentOptionGui(ParentOption option) { public ParentOptionGui(ParentOption option) {
this.option = option; this.option = option;
@ -31,67 +36,156 @@ public class ParentOptionGui extends JPanel {
if(option.getIdentifier().length() > 0) { if(option.getIdentifier().length() > 0) {
this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createCompoundBorder( this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(2, 2, BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(2, 2,
2, 2, Color.BLACK), " " + option.getIdentifier() + ": "), 2, 2, Color.BLACK), " " + option.getIdentifier() + ": "),
BorderFactory.createEmptyBorder(5,5,5,5)))); BorderFactory.createEmptyBorder(5,5,5,5))));
} }
this.setLayout(new GridBagLayout()); this.setLayout(new GridBagLayout());
for(Option o : this.option.getChildOptions()) { recreateContent();
add(o); }
children.add(o);
}
}
private void add(Option option) { private void recreateContent() {
if(option instanceof ParentOption) {
this.removeAll();
this.children.clear();
int line = 0;
if(this.option.isEditable()) {
final JTextField nameField = new JTextField("actionname");
nameField.setColumns(14);
JPanel p2 = new JPanel();
p2.add(nameField);
JButton createNew = new JButton("new");
createNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
option.add(new CustomActionOption(nameField.getText(), "yourcommand [player]"));
recreateContent();
}
});
JPanel p = new JPanel();
p.add(createNew);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = line;
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.ipady = 15;
c.weightx = 1;
this.add(p, c);
c.gridx = 1;
c.gridy = line;
c.gridwidth = 3;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.ipady = 15;
c.weightx = 1;
this.add(p2, c);
line++;
}
for(Option o : this.option.getChildOptions()) {
add(o, line);
children.add(o);
line++;
}
this.revalidate();
}
private void add(final Option child, int line) {
if(child instanceof ParentOption) {
GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridx = 0;
c.gridy = children.size(); c.gridy = line;
c.gridwidth = 3; // Spans over three columns c.gridwidth = 3; // Spans over three columns
if(this.option.isEditable()) c.gridwidth = 4; // Spans over four columns
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
c.ipadx = 5; c.ipadx = 5;
c.ipady = 15; c.ipady = 15;
c.weightx = 1; c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.HORIZONTAL;
this.add(new ParentOptionGui((ParentOption)option), c); this.add(new ParentOptionGui((ParentOption)child), c);
} }
else if(option instanceof ChildOption) else if(child instanceof ChildOption)
{ {
GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridx = 0;
c.gridy = children.size(); c.gridy = line;
c.gridwidth = 1; c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
c.ipadx = 10; c.ipadx = 10;
c.weightx = 0; c.weightx = 0;
this.add(new JLabel(option.getIdentifier() + ":"), c); this.add(new JLabel(child.getIdentifier() + ":"), c);
c.gridx = 1; c.gridx = 1;
c.gridy = children.size(); c.gridy = line;
c.gridwidth = 1; c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
c.ipadx = 5; c.ipadx = 5;
c.weightx = 0; c.weightx = 0;
c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.HORIZONTAL;
JComponent child = ChildOptionGuiFactory.create((ChildOption)option); JComponent tmp = ChildOptionGuiFactory.create((ChildOption)child);
this.add(child, c); this.add(tmp, c);
c.gridx = 2; c.gridx = 2;
c.gridy = children.size(); c.gridy = line;
c.gridwidth = 1; c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
c.ipadx = 5; c.ipadx = 5;
c.weightx = 1; c.weightx = 1;
this.add(Box.createHorizontalGlue(), c); this.add(Box.createHorizontalGlue(), c);
if(this.option.isEditable()) {
c.gridx = 3;
c.gridy = line;
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.weightx = 1;
JButton removeButton = new JButton("delete");
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
option.remove(child);
recreateContent();
}
});
this.add(removeButton, c);
}
} }
} }