Add tests for original code - which did not use a special icon - for comparison

Bugzilla Id: 54251


git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1417789 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-12-06 10:05:42 +00:00
parent bfc9e01d33
commit 2ca600dc56
2 changed files with 58 additions and 37 deletions

View File

@ -61,12 +61,21 @@ public final class TristateCheckBox extends JCheckBox {
this(text, null, TristateState.DESELECTED);
}
public TristateCheckBox(String text, Icon icon,
TristateState initial) {
// For testing only at present
TristateCheckBox(String text, boolean original) {
this(text, null, TristateState.DESELECTED, original);
}
public TristateCheckBox(String text, Icon icon, TristateState initial) {
this(text, icon, initial, false);
}
// For testing only at present
TristateCheckBox(String text, Icon icon, TristateState initial, boolean original) {
super(text, icon);
//Set default single model
setModel(new TristateButtonModel(initial, this));
setModel(new TristateButtonModel(initial, this, original));
// override action behaviour
super.addMouseListener(new MouseAdapter() {
@ -148,12 +157,14 @@ public final class TristateCheckBox extends JCheckBox {
private TristateState state = TristateState.DESELECTED;
private final TristateCheckBox tristateCheckBox;
private final Icon icon;
private final boolean original;
public TristateButtonModel(TristateState initial,
TristateCheckBox tristateCheckBox) {
TristateCheckBox tristateCheckBox, boolean original) {
setState(TristateState.DESELECTED);
this.tristateCheckBox = tristateCheckBox;
icon = new TristateCheckBoxIcon(tristateCheckBox);
this.original = original;
}
public void setIndeterminate() {
@ -211,15 +222,17 @@ public final class TristateCheckBox extends JCheckBox {
private void displayState() {
super.setSelected(state != TristateState.DESELECTED);
// original used:
// super.setArmed(state == TristateState.INDETERMINATE);
if (state == TristateState.INDETERMINATE) {
tristateCheckBox.setIcon(icon); // Needed for all but Nimbus
tristateCheckBox.setSelectedIcon(icon); // Nimbus works - after a fashion - with this
} else { // reset
if (tristateCheckBox!= null){
tristateCheckBox.setIcon(null);
tristateCheckBox.setSelectedIcon(null);
if (original) {
super.setArmed(state == TristateState.INDETERMINATE);
} else {
if (state == TristateState.INDETERMINATE) {
tristateCheckBox.setIcon(icon); // Needed for all but Nimbus
tristateCheckBox.setSelectedIcon(icon); // Nimbus works - after a fashion - with this
} else { // reset
if (tristateCheckBox!= null){
tristateCheckBox.setIcon(null);
tristateCheckBox.setSelectedIcon(null);
}
}
}
super.setPressed(state == TristateState.INDETERMINATE);

View File

@ -50,7 +50,38 @@ public class TristateCheckBoxTest {
}
private static JPanel makePanel(String name) {
final TristateCheckBox tristateBox = new TristateCheckBox("Tristate checkbox");
final TristateCheckBox tristateBox = new TristateCheckBox("Tristate checkbox (icon)", false);
createTristate(tristateBox);
final TristateCheckBox tristateBoxorig = new TristateCheckBox("Tristate checkbox (original)", true);
createTristate(tristateBoxorig);
final JCheckBox normalBox = new JCheckBox("Normal checkbox");
normalBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
});
final JCheckBox enabledBox = new JCheckBox("Enable", true);
enabledBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
tristateBox.setEnabled(enabledBox.isSelected());
normalBox.setEnabled(enabledBox.isSelected());
}
});
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
panel.add(new JLabel(name));
panel.add(tristateBox);
panel.add(tristateBoxorig);
panel.add(normalBox);
panel.add(enabledBox);
return panel;
}
private static void createTristate(final TristateCheckBox tristateBox) {
tristateBox.setIndeterminate(); // start in new state
tristateBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
@ -79,28 +110,5 @@ public class TristateCheckBoxTest {
}
}
});
final JCheckBox normalBox = new JCheckBox("Normal checkbox");
normalBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
});
final JCheckBox enabledBox = new JCheckBox("Enable", true);
enabledBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
tristateBox.setEnabled(enabledBox.isSelected());
normalBox.setEnabled(enabledBox.isSelected());
}
});
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
panel.add(new JLabel(name));
panel.add(tristateBox);
panel.add(normalBox);
panel.add(enabledBox);
return panel;
}
}