blob: a9e39e5c850241965dd6137c1032dddb1cd8d8bc [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009, 2015 Ketan Padegaonkar and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
* Patrick Tasse - fix click behavior and support click with modifiers
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swtbot.swt.finder.ReferenceBy;
import org.eclipse.swtbot.swt.finder.SWTBotWidget;
import org.eclipse.swtbot.swt.finder.Style;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.results.BoolResult;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
import org.hamcrest.SelfDescribing;
/**
* Represents a tool item of type checkbox
*
* @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
* @version $Id$
*/
@SWTBotWidget(clasz = ToolItem.class, preferredName = "toolbarRadioButton", style = @Style(name = "SWT.RADIO", value = SWT.RADIO), referenceBy = {
ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP }, returnType = SWTBotToolbarRadioButton.class)
public class SWTBotToolbarRadioButton extends SWTBotToolbarButton {
/**
* Constructs an new instance of this item.
*
* @param w the tool item.
* @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
*/
public SWTBotToolbarRadioButton(ToolItem w) throws WidgetNotFoundException {
this(w, null);
}
/**
* Constructs an new instance of this item.
*
* @param w the tool item.
* @param description the description of the widget, this will be reported by {@link #toString()}
* @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
*/
public SWTBotToolbarRadioButton(ToolItem w, SelfDescribing description) throws WidgetNotFoundException {
super(w, description);
Assert.isTrue(SWTUtils.hasStyle(w, SWT.RADIO), "Expecting a radio button."); //$NON-NLS-1$
}
/**
* Toggle the tool item.
*
* @return itself
*/
public SWTBotToolbarRadioButton toggle() {
log.debug(MessageFormat.format("Toggling {0}", this)); //$NON-NLS-1$
waitForEnabled();
internalSetSelection(!isChecked());
sendNotifications();
log.debug(MessageFormat.format("Toggled {0}", this)); //$NON-NLS-1$
return this;
}
@Override
public SWTBotToolbarRadioButton click(int stateMask) {
log.debug(MessageFormat.format("Clicking on {0}" + (stateMask != 0 ? " with stateMask=0x{1}" : ""), this, Integer.toHexString(stateMask))); //$NON-NLS-1$
waitForEnabled();
internalSetSelection(true);
sendNotifications(stateMask);
log.debug(MessageFormat.format("Clicked on {0}" + (stateMask != 0 ? " with stateMask=0x{1}" : ""), this, Integer.toHexString(stateMask))); //$NON-NLS-1$
return this;
}
private void internalSetSelection(final boolean selected) {
if (selected) {
final SWTBotToolbarRadioButton otherSelectedButton = otherSelectedButton();
if (otherSelectedButton != null) {
otherSelectedButton.notify(SWT.Deactivate);
asyncExec(new VoidResult() {
public void run() {
otherSelectedButton.widget.setSelection(false);
}
});
otherSelectedButton.notify(SWT.Selection);
}
}
syncExec(new VoidResult() {
public void run() {
widget.setSelection(selected);
}
});
}
private SWTBotToolbarRadioButton otherSelectedButton() {
ToolItem other = syncExec(new WidgetResult<ToolItem>() {
public ToolItem run() {
Widget[] siblings = SWTUtils.siblings(widget);
boolean ownGroup = false;
ToolItem selected = null;
for (Widget sibling : siblings) {
if (sibling == widget) {
ownGroup = true;
} else if (((sibling instanceof ToolItem) && hasStyle(sibling, SWT.RADIO))) {
if (((ToolItem) sibling).getSelection()) {
selected = (ToolItem) sibling;
}
} else if ((sibling instanceof ToolItem) && hasStyle(sibling, SWT.SEPARATOR)) {
ownGroup = false;
selected = null;
}
if (ownGroup && selected != null) {
return selected;
}
}
return null;
}
});
if (other != null)
return new SWTBotToolbarRadioButton(other);
return null;
}
/**
* Selects the checkbox button.
*/
public void select() {
if (!isChecked())
toggle();
}
/**
* Deselects the checkbox button.
*/
public void deselect() {
if (isChecked())
toggle();
}
/**
* @return <code>true</code> if the button is checked, <code>false</code> otherwise.
*/
public boolean isChecked() {
return syncExec(new BoolResult() {
public Boolean run() {
return widget.getSelection();
}
});
}
}