Initial test for GUI configuration tool

This commit is contained in:
Evenprime 2011-05-02 19:04:34 +02:00
parent b656644b29
commit f13fcf39e1
13 changed files with 520 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package cc.co.evenprime.bukkit.nocheat;
import cc.co.evenprime.bukkit.nocheat.wizard.Wizard;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Wizard w = new Wizard();
w.setVisible(true);
}
}

View File

@ -0,0 +1,41 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class BooleanOption extends ChildOption {
/**
*
*/
private static final long serialVersionUID = 2258827414736580449L;
public BooleanOption(String name, boolean initialValue) {
super(name, String.valueOf(initialValue));
this.setLayout(new BorderLayout());
JLabel l = new JLabel(this.getIdentifier());
l.setPreferredSize(new Dimension(l.getPreferredSize().width+10, l.getPreferredSize().height));
this.add(l, BorderLayout.CENTER);
JCheckBox checkBox = new JCheckBox();
checkBox.setSelected(initialValue);
checkBox.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
setValue(String.valueOf(((JCheckBox)arg0.getSource()).isSelected()));
}
});
this.add(checkBox, BorderLayout.WEST);
}
}

View File

@ -0,0 +1,24 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
public abstract class ChildOption extends Option {
/**
*
*/
private static final long serialVersionUID = -4648294833934457776L;
private String value;
public ChildOption(String identifier, String value) {
super(identifier);
this.value = value;
}
public final void setValue(String value) {
this.value = value;
}
public final String getValue() {
return value;
}
}

View File

@ -0,0 +1,16 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
public class EmptyOption extends Option {
public EmptyOption() {
super("");
}
/**
*
*/
private static final long serialVersionUID = 5058363675498808020L;
}

View File

@ -0,0 +1,24 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
public class EmptyVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
JTextField text = (JTextField)input;
ChildOption parent = (ChildOption) text.getParent();
try{
parent.setValue(text.getText());
return true;
}
catch(Exception e) {
text.setText(parent.getValue());
return false;
}
}
}

View File

@ -0,0 +1,16 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
public class IntegerOption extends TextFieldOption {
/**
*
*/
private static final long serialVersionUID = 2258827414736580449L;
public IntegerOption(String name, int initialValue, int width) {
super(name, String.valueOf(initialValue), width, new IntegerVerifier("Only integers are allowed in this field."));
}
}

View File

@ -0,0 +1,34 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class IntegerVerifier extends InputVerifier {
private String errorMessage;
public IntegerVerifier(String errorMessage) {
this.errorMessage = errorMessage;
}
@Override
public boolean verify(JComponent input) {
JTextField text = (JTextField)input;
ChildOption parent = (ChildOption) text.getParent();
try{
Integer.parseInt(text.getText());
parent.setValue(text.getText());
return true;
}
catch(Exception e) {
JOptionPane.showMessageDialog(text, errorMessage);
text.setText(parent.getValue());
return false;
}
}
}

View File

@ -0,0 +1,51 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
public class LogLevelOption extends ChildOption {
/**
*
*/
private static final long serialVersionUID = -1609308017422576285L;
public LogLevelOption(String identifier, String initialValue) {
super(identifier, initialValue);
this.setLayout(new BorderLayout());
this.add(new JLabel(this.getIdentifier()), BorderLayout.CENTER);
JComboBox box = new JComboBox();
String options[] = { "off", "low", "med", "high" };
boolean found = false;
box.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
setValue(arg0.getItem().toString());
}
});
for(String s : options) {
box.addItem(s);
if(s.equals(initialValue)) { box.setSelectedItem(s); found = true; }
}
if(!found) box.setSelectedItem("off");
this.add(box, BorderLayout.WEST);
}
}

View File

@ -0,0 +1,25 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.awt.Dimension;
import javax.swing.JPanel;
public abstract class Option extends JPanel {
/**
*
*/
private static final long serialVersionUID = 6710455693749974103L;
private final String identifier;
public Option(String identifier) {
this.identifier = identifier;
this.setMinimumSize(new Dimension(this.getPreferredSize().height, this.getPreferredSize().width));
}
public String getIdentifier() {
return identifier;
}
}

View File

@ -0,0 +1,44 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
public class ParentOption extends Option {
/**
*
*/
private static final long serialVersionUID = 3162246550749560727L;
private LinkedList<Option> children = new LinkedList<Option>();
public ParentOption(String identifier) {
super(identifier);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setAlignmentX(1.0F);
if(identifier.length() > 0) {
this.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(identifier),
BorderFactory.createEmptyBorder(5,5,5,5)));
}
else
this.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
public final Collection<Option> getChildOptions() {
return Collections.unmodifiableCollection(children);
}
public final void add(Option option) {
children.addLast(option);
super.add(option);
}
}

View File

@ -0,0 +1,14 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
public class StringOption extends TextFieldOption {
/**
*
*/
private static final long serialVersionUID = 2258827414736580449L;
public StringOption(String name, String initialValue, int width) {
super(name, initialValue, width, new EmptyVerifier());
}
}

View File

@ -0,0 +1,32 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.awt.BorderLayout;
import javax.swing.InputVerifier;
import javax.swing.JLabel;
import javax.swing.JTextField;
public abstract class TextFieldOption extends ChildOption {
/**
*
*/
private static final long serialVersionUID = -8189248456599421250L;
public TextFieldOption(String name, String initialValue, int width, final InputVerifier inputVerifier) {
super(name, String.valueOf(initialValue));
this.setLayout(new BorderLayout());
this.add(new JLabel(this.getIdentifier()), BorderLayout.CENTER);
JTextField textField = new JTextField();
textField.setText(this.getValue());
textField.setColumns(width);
textField.setInputVerifier(inputVerifier);
this.add(textField, BorderLayout.WEST);
}
}

View File

@ -0,0 +1,184 @@
package cc.co.evenprime.bukkit.nocheat.wizard;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Wizard extends JFrame {
/**
*
*/
private static final long serialVersionUID = 8798111079958779207L;
private static final String pre = " ";
private static final int numberWidth = 4;
private static final int wordWidth = 10;
private static final int fileWidth = 20;
private static final int textWidth = 60;
public Wizard() {
JScrollPane scrollable = new JScrollPane();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(scrollable);
JPanel inside = new JPanel();
scrollable.setViewportView(inside);
inside.setLayout(new FlowLayout());
final ParentOption root = new ParentOption("");
inside.add(root);
ParentOption loggingNode = new ParentOption("logging");
root.add(loggingNode);
loggingNode.add(new StringOption("filename", "plugins/NoCheat/nocheat.log", fileWidth));
loggingNode.add(new LogLevelOption("logtofile", "low"));
loggingNode.add(new LogLevelOption("logtoconsole", "high"));
loggingNode.add(new LogLevelOption("logtochat", "med"));
loggingNode.add(new LogLevelOption("logtoirc", "med"));
loggingNode.add(new StringOption("logtoirctag", "nocheat", wordWidth));
ParentOption activeNode = new ParentOption("active");
root.add(activeNode);
activeNode.add(new BooleanOption("speedhack", true));
activeNode.add(new BooleanOption("moving", true));
activeNode.add(new BooleanOption("airbuild", false));
activeNode.add(new BooleanOption("bedteleport", true));
activeNode.add(new BooleanOption("itemdupe", true));
activeNode.add(new BooleanOption("bogusitems", false));
ParentOption speedhackNode = new ParentOption("speedhack");
root.add(speedhackNode);
speedhackNode.add(new StringOption("logmessage", "\"%1$s sent %2$d move events, but only %3$d were allowed. Speedhack?\"", textWidth));
{
ParentOption speedhackLimitsNode = new ParentOption("limits");
speedhackNode.add(speedhackLimitsNode);
speedhackLimitsNode.add(new IntegerOption("low", 30, numberWidth));
speedhackLimitsNode.add(new IntegerOption("med", 45, numberWidth));
speedhackLimitsNode.add(new IntegerOption("high", 60, numberWidth));
ParentOption speedhackActionNode = new ParentOption("action");
speedhackNode.add(speedhackActionNode);
speedhackActionNode.add(new StringOption("low", "loglow cancel", fileWidth));
speedhackActionNode.add(new StringOption("med", "logmed cancel", fileWidth));
speedhackActionNode.add(new StringOption("high", "loghigh cancel", fileWidth));
}
ParentOption movingNode = new ParentOption("moving");
root.add(movingNode);
movingNode.add(new StringOption("logmessage", "\"Moving violation: %1$s from %2$s (%4$.1f, %5$.1f, %6$.1f) to %3$s (%7$.1f, %8$.1f, %9$.1f)\"", textWidth));
movingNode.add(new StringOption("summarymessage", "\"Moving summary of last ~%2$d seconds: %1$s total Violations: (%3$d,%4$d,%5$d)\"", textWidth));
movingNode.add(new BooleanOption("allowflying", false));
movingNode.add(new BooleanOption("allowfakesneak", true));
{
ParentOption movingActionNode = new ParentOption("action");
movingNode.add(movingActionNode);
movingActionNode.add(new StringOption("low", "loglow cancel", fileWidth));
movingActionNode.add(new StringOption("med", "logmed cancel", fileWidth));
movingActionNode.add(new StringOption("high", "loghigh cancel", fileWidth));
}
ParentOption airbuildNode = new ParentOption("airbuild");
root.add(airbuildNode);
{
ParentOption airbuildLimitsNode = new ParentOption("limits");
airbuildNode.add(airbuildLimitsNode);
airbuildLimitsNode.add(new IntegerOption("low", 30, numberWidth));
airbuildLimitsNode.add(new IntegerOption("med", 45, numberWidth));
airbuildLimitsNode.add(new IntegerOption("high", 60, numberWidth));
ParentOption airbuildActionNode = new ParentOption("action");
airbuildNode.add(airbuildActionNode);
airbuildActionNode.add(new StringOption("low", "loglow cancel", fileWidth));
airbuildActionNode.add(new StringOption("med", "logmed cancel", fileWidth));
airbuildActionNode.add(new StringOption("high", "loghigh cancel", fileWidth));
}
ParentOption bedteleportNode = new ParentOption("bedteleport");
root.add(bedteleportNode);
bedteleportNode.add(new EmptyOption());
ParentOption itemdupeNode = new ParentOption("itemdupe");
root.add(itemdupeNode);
itemdupeNode.add(new EmptyOption());
ParentOption bogusitemsNode = new ParentOption("bogusitems");
root.add(bogusitemsNode);
bogusitemsNode.add(new EmptyOption());
JButton b = new JButton("TEST");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String s = parseParent(root, "");
JOptionPane.showMessageDialog(null, s);
} });
inside.add(b);
inside.doLayout();
this.pack();
}
private String parseParent(ParentOption option, String prefix) {
String s = "";
if(option.getIdentifier().length() > 0) {
s += prefix + option.getIdentifier() + ":\r\n";
}
for(Option o : option.getChildOptions()) {
if(o instanceof ChildOption)
s += parseChild((ChildOption)o, prefix + pre);
else if(o instanceof ParentOption)
s += parseParent((ParentOption)o, prefix + pre);
else
parse(o, prefix + " ");
}
return s;
}
private String parseChild(ChildOption option, String prefix) {
return prefix + option.getIdentifier() + ": " + option.getValue() + "\r\n";
}
private String parse(Object option, String prefix) {
return "";
}
}