From f13fcf39e1ed2ec6343dc3ea947c27d2bab02030 Mon Sep 17 00:00:00 2001 From: Evenprime Date: Mon, 2 May 2011 19:04:34 +0200 Subject: [PATCH 1/9] Initial test for GUI configuration tool --- src/cc/co/evenprime/bukkit/nocheat/Main.java | 15 ++ .../bukkit/nocheat/wizard/BooleanOption.java | 41 ++++ .../bukkit/nocheat/wizard/ChildOption.java | 24 +++ .../bukkit/nocheat/wizard/EmptyOption.java | 16 ++ .../bukkit/nocheat/wizard/EmptyVerifier.java | 24 +++ .../bukkit/nocheat/wizard/IntegerOption.java | 16 ++ .../nocheat/wizard/IntegerVerifier.java | 34 ++++ .../bukkit/nocheat/wizard/LogLevelOption.java | 51 +++++ .../bukkit/nocheat/wizard/Option.java | 25 +++ .../bukkit/nocheat/wizard/ParentOption.java | 44 +++++ .../bukkit/nocheat/wizard/StringOption.java | 14 ++ .../nocheat/wizard/TextFieldOption.java | 32 +++ .../bukkit/nocheat/wizard/Wizard.java | 184 ++++++++++++++++++ 13 files changed, 520 insertions(+) create mode 100644 src/cc/co/evenprime/bukkit/nocheat/Main.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/BooleanOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/ChildOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyVerifier.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerVerifier.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/LogLevelOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/Option.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/ParentOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/StringOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/TextFieldOption.java create mode 100644 src/cc/co/evenprime/bukkit/nocheat/wizard/Wizard.java diff --git a/src/cc/co/evenprime/bukkit/nocheat/Main.java b/src/cc/co/evenprime/bukkit/nocheat/Main.java new file mode 100644 index 00000000..fbfd9094 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/Main.java @@ -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); + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/BooleanOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/BooleanOption.java new file mode 100644 index 00000000..514f8807 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/BooleanOption.java @@ -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); + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/ChildOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/ChildOption.java new file mode 100644 index 00000000..5b9c6d4d --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/ChildOption.java @@ -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; + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyOption.java new file mode 100644 index 00000000..72488c3c --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyOption.java @@ -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; + + + +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyVerifier.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyVerifier.java new file mode 100644 index 00000000..70f4b1b7 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/EmptyVerifier.java @@ -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; + } + } + +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerOption.java new file mode 100644 index 00000000..ffc78d59 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerOption.java @@ -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.")); + + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerVerifier.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerVerifier.java new file mode 100644 index 00000000..22ebafdf --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/IntegerVerifier.java @@ -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; + } + } + +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/LogLevelOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/LogLevelOption.java new file mode 100644 index 00000000..83ebde56 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/LogLevelOption.java @@ -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); + + + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/Option.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/Option.java new file mode 100644 index 00000000..1c0245ee --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/Option.java @@ -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; + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/ParentOption.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/ParentOption.java new file mode 100644 index 00000000..989e8f25 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/ParentOption.java @@ -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