PDA

View Full Version : radio group that allow remove radios or add news



diegolovison
15 Oct 2009, 6:21 AM
public class XRadioGroup extends RadioGroup {

private static int autoId = 0;

public XRadioGroup() {
this("gxt.RadioGroup." + autoId++);
}

public XRadioGroup(final String name) {
setGroupName(name);
messages = new RadioGroupMessages();
setSpacing(5);
}

public void addField(final Field<?> f) {
addInternalField(f);
}

public void addField(final Radio radio) {

setGroup(radio, this);
radio.setName(getGroupName());

addInternalField(radio);
}

private void addInternalField(final Field<?> f) {

fields.add(f);

final boolean vertical = orientation == Orientation.VERTICAL;

TableData data = (TableData) ComponentHelper.getLayoutData(f);
if (data == null) {
data = new TableData();
}
String style = "position: static;";

if (vertical && spacing > 0) {
style += "paddingTop:" + spacing + "px;";
} else if (!vertical && spacing > 0) {
style += "paddingLeft:" + spacing + "px;";
}
data.setStyle(style);

lc.add(f, data);
lc.layout();
}

private native String getGroupName() /*-{
return this.@com.extjs.gxt.ui.client.widget.form.RadioGroup::groupName;
}-*/;

public boolean removeAllFields() {
return lc.removeAll();
}

public void removeField(final Field<?> f) {
lc.remove(f);
lc.layout();
}

private native void setGroup(Radio radio, RadioGroup radioGroup) /*-{
radio.@com.extjs.gxt.ui.client.widget.form.Radio::group = radioGroup;
}-*/;

private native void setGroupName(String groupName) /*-{
this.@com.extjs.gxt.ui.client.widget.form.RadioGroup::groupName = groupName;
}-*/;
}

diegolovison
15 Oct 2009, 6:22 AM
example



@Override
public void onModuleLoad() {

final Radio rd1 = new Radio();
final Radio rd2 = new Radio();
final Radio rd3 = new Radio();
final Radio rd4 = new Radio();

final XRadioGroup rg = new XRadioGroup();
rg.add(rd1);
rg.add(rd2);
rg.add(rd3);
rg.add(rd4);

final Button btn1 = new Button("add field", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(final ButtonEvent ce) {
rg.addField(new Radio());
}
});

final Button btn2 = new Button("remove field", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(final ButtonEvent ce) {
rg.removeField(rd1);
}
});

final Button btn3 = new Button("remove all", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(final ButtonEvent ce) {
rg.removeAllFields();
}
});

RootPanel.get().add(rg);
RootPanel.get().add(btn1);
RootPanel.get().add(btn2);
RootPanel.get().add(btn3);
}