Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SquareButtonGrouppo #85

Merged
merged 7 commits into from
Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.readytalk.examples.swt.SwtBlingExample;
import com.readytalk.swt.util.ColorFactory;
import com.readytalk.swt.widgets.buttons.SquareButton;
import com.readytalk.swt.widgets.buttons.SquareButtonGroup;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
Expand Down Expand Up @@ -134,7 +135,7 @@ public void run(Display display, final Shell shell) {
.setHoverColors(BUTTON_HOVER_COLOR_GROUP)
.setDefaultColors(BUTTON_DEFAULT_COLOR_GROUP)
.setClickedColors(FUN_BUTTON_CLICK_COLOR_GROUP)
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.DefaultButtonClickHandler() {
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.ButtonClickHandler() {
@Override
public void clicked() {
openTestDialog(shell);
Expand Down Expand Up @@ -168,7 +169,7 @@ public void clicked() {
.setDefaultColors(FUN_BUTTON_DEFAULT_COLOR_GROUP)
.setSelectedColors(FUN_BUTTON_SELECTED_COLOR_GROUP)
.setClickedColors(FUN_BUTTON_CLICK_COLOR_GROUP)
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.DefaultButtonClickHandler() {
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.ButtonClickHandler() {
@Override
public void clicked() {
openTestDialog(shell);
Expand All @@ -190,7 +191,7 @@ public void clicked() {
.setDefaultColors(MORE_FUN_BUTTON_DEFAULT_COLOR_GROUP)
.setClickedColors(FUN_BUTTON_CLICK_COLOR_GROUP)
.setSelectedColors(MORE_FUN_BUTTON_SELECTED_COLOR_GROUP)
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.DefaultButtonClickHandler() {
.setDefaultMouseClickAndReturnKeyHandler(new SquareButton.ButtonClickHandler() {
@Override
public void clicked() {
openTestDialog(shell);
Expand Down Expand Up @@ -260,60 +261,7 @@ public void clicked() {
formData.right = new FormAttachment(90);
bigButtonTwo.setLayoutData(formData);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to discourage users of SquareButton from using addMouseListener in favor of setDefaultMouseClickAndReturnKeyHandler? If so, we might want to document that somewhere, or forbid them from doing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will discourage them via @deprecated and a note... thoughts?

bigButtonOne.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if(bigButtonTwo.isToggled()) {
bigButtonTwo.setToggled(false);
}
}
});

bigButtonOne.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
switch (keyEvent.character) {
case ' ':
case '\r':
case '\n':
if(bigButtonTwo.isToggled()) {
bigButtonTwo.setToggled(false);
}
break;
default:
break;
}
}
});

bigButtonTwo.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if(bigButtonOne.isToggled()) {
bigButtonOne.setToggled(false);
}
}
});

bigButtonTwo.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
switch (keyEvent.character) {
case ' ':
case '\r':
case '\n':
if(bigButtonOne.isToggled()) {
bigButtonOne.setToggled(false);
}
break;
default:
break;
}
}
});

bigButtonOne.setToggled(true);
// shell.setSize(1200, 200);
new SquareButtonGroup(bigButtonOne, bigButtonTwo);

topGroup.pack();
leftComposite.pack();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.readytalk.swt.widgets.buttons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SquareButtonGroupIntegTest {

private Display display;
private Shell shell;

private SquareButton toggleOne, toggleTwo, toggleThree, nonToggled;

private SquareButtonGroup group;

@Before
public void setUp() {
display = Display.getDefault();
shell = new Shell(display);
shell.setLayout(new FillLayout());


SquareButton.SquareButtonBuilder builder = new SquareButton.SquareButtonBuilder();
builder.setParent(shell)
.setText("one")
.setToggleable(true);
toggleOne = builder.build();

builder = new SquareButton.SquareButtonBuilder();
builder.setParent(shell)
.setText("two")
.setToggleable(true);
toggleTwo = builder.build();

builder = new SquareButton.SquareButtonBuilder();
builder.setParent(shell)
.setText("three")
.setToggleable(true);
toggleThree = builder.build();

builder = new SquareButton.SquareButtonBuilder();
builder.setParent(shell)
.setText("non toggle");
nonToggled = builder.build();

group = new SquareButtonGroup(toggleOne, toggleTwo, toggleThree, nonToggled);

shell.open();
}

@Test
public void testDefaultToggle() {
Assert.assertTrue(toggleOne.isToggled());
Assert.assertFalse(toggleTwo.isToggled());
Assert.assertFalse(toggleThree.isToggled());
Assert.assertFalse(nonToggled.isToggled());
}

@Test
public void testBasicToggle() {
toggleTwo.notifyListeners(SWT.MouseUp, new Event());
Assert.assertFalse(toggleOne.isToggled());
Assert.assertTrue(toggleTwo.isToggled());
Assert.assertFalse(toggleThree.isToggled());
Assert.assertFalse(nonToggled.isToggled());
}

@Test
public void testDoubleToggle() {
toggleTwo.notifyListeners(SWT.MouseUp, new Event());
Assert.assertFalse(toggleOne.isToggled());
Assert.assertTrue(toggleTwo.isToggled());
Assert.assertFalse(toggleThree.isToggled());
Assert.assertFalse(nonToggled.isToggled());

toggleTwo.notifyListeners(SWT.MouseUp, new Event());
Assert.assertFalse(toggleOne.isToggled());
Assert.assertTrue(toggleTwo.isToggled());
Assert.assertFalse(toggleThree.isToggled());
Assert.assertFalse(nonToggled.isToggled());
}

@Test
public void testForcedSelection() {
group.setCurrentlyToggledButton(toggleThree);
Assert.assertFalse(toggleOne.isToggled());
Assert.assertFalse(toggleTwo.isToggled());
Assert.assertTrue(toggleThree.isToggled());
Assert.assertFalse(nonToggled.isToggled());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to add an additional test to ensure that if you click the same button twice, it's still toggled. That was the original problem that brought up the SquareButtonGroup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I can do that. It works in the example, but I can check it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that. The example looks to do exactly what I was hoping. Good to enforce that behavior in the test, too.

@After
public void tearDown() {
shell.close();
display.dispose();
}
}
Loading