I have a check box group in my page. After checking the inputs, if they are wrong, an error message will be shown to the user, but the status of check boxes are lost. This is part of my code that shows a group checkbox with three items (CONFIGURATION, TESTER, DEVELOPER):
group = new CheckGroup("group", user.getRoleNames());
group.add(new CheckGroupSelector("groupselector"));
// The list of possible roles
List<String> roleNames = new ArrayList<String>();
roleNames.add(Roles.CONFIGURATION.value());
roleNames.add(Roles.TESTER.value());
roleNames.add(Roles.DEVELOPER.value());
group.add(new ListView("roles", roleNames) {
protected void populateItem(ListItem item) {
item.add(new Check("checkbox", item.getModel()));
item.add(new Label("name", item.getModel()));
}
});
add(group);
Also I have two text boxes of username and password. If user enters a wrong password, she/he will get an error message, but after showing the error message, the checked items become unchecked, Do you know how I can keep the status of check boxes?
I changed my code as the following:
ListView roles = new ListView("roles", roleNames) {
protected void populateItem(ListItem item) {
item.add(new Check("checkbox", item.getModel()));
item.add(new Label("name", item.getModel()));
}
};
roles.setReuseItems(true);
group.add(roles);
and now it works correctly. So, the only required thing is setting setReuseItems to true.
Related
I have a form with a CheckGroup and a PropertyListView. Each item in the list view has a Check belonging to the check group and a AjaxFallbackLink. When I disable the check group, the whole form gets disabled including the links. Similarly, when I set visible to false, the whole form is not visible.
Form form = new Form("form");
CheckGroup checkGroup = new CheckGroup("checkGroup", checkList) {
#Override
protected void onConfigure() {
super.onConfigure();
setEnabled(isAuthorized());
}
};
checkGroup.add(new CheckGroupSelector("groupSelector"));
form.add(checkGroup);
PropertyListView<MyList> tableList = new PropertyListView<MyList>("table", new PropertyModel(lists, "myList")){
#Override
protected void populateItem(ListItem<OfacCustomerMatchFile> item) {
item.add(new Check("check", item.getModel(), checkGroup);
item.add(new AjaxFallbackLink("itemLink") { ... });
}
};
form.add(tableList);
You have to override #isEnabledHierarchy() on your links:
public boolean isEnabledInHierarchy()
{
return true;
}
Watch out, this can be dangerous since you are circumventing Wicket's default authorization handling.
we would like to link from a CellTable to a property editor page. We use the SingleSelectionModel to get notified, when a user clicks on an item.
It is initialized like this:
private final SingleSelectionModel<Device> selectionModel = new SingleSelectionModel<Device>();
We then assign the selection change handler:
selectionModel.addSelectionChangeHandler(this);
Our selection change handler looks like this:
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Log.debug("DevicesPresenter: SelectionChangeEvent caught.");
Device selectedDevice = selectionModel.getSelectedObject();
if (selectedDevice != null) {
selectionModel.clear();
if (selectionModel.getSelectedObject() != null){
Log.debug("DevicesPresenter: selected item is " + selectionModel.getSelectedObject());
}
else{
Log.debug("DevicesPresenter: selected item is null");
}
deviceEditorDialog.setCurrentDevice(selectedDevice.getUuid());
// get the container data for this device
clientModelProvider.fetchContainersForDevice(selectedDevice.getUuid());
PlaceRequest request = new PlaceRequest.Builder()
.nameToken(NameTokens.deviceInfo)
.with("uuid", selectedDevice.getUuid())
.build();
Log.debug("Navigating to " + request.toString());
placeManager.revealPlace(request);
}
}
Now there are two issues: There always seem to be two SelectionChangeEvents at once and i really cannot see why. The other thing is: How is the right way do handle selection of items and the related clearing of the selection model? Do we do that the right way?
Thanks!
If you only want to get notified of "clicks" without keeping the "clicked" item selected, use a NoSelectionModel instead; no need to clear the selection model as soon as something is selected.
As for your other issue with being called twice, double-check that you haven't added your selection handler twice (if you can unit-test your DevicesPresenter, introspect the handlers inside the selection model for example)
In your line selectionModel.addSelectionChangeHandler(this); what does this refer?
Here my code how I use SingleSelectionModel
public class MyClass{
private final SingleSelectionModel<CountryDto> selectionModel = new SingleSelectionModel<CountryDto>();
...
public MyClass(){
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
CountryDto selected = selectionModel
.getSelectedObject();
if (selected != null) {
Window.alert("Selected country "+selected.getTitle());
}
}
});
}
}
I have a checkgroup and on selecting a checkbox in the group I want to get the number of selected/checked check boxes.
By the below code, I am able to get the selected number of checkboxes but on unchecking or removing the selection, i see that its still selected.
Example : I have selected 2 now I get 2 string selected.
Now I unselect one checkbox, even now i get 2 strings selected. While I expect one string to be checked.
The code is given below:
final CheckGroup myGroup = new CheckGroup("group", new ArrayList()) {
#Override
protected Collection<String> convertValue(String[] values) throws ConversionException {
Collection<String> myCollection = super.convertValue(values);
checkedString.addAll(myCollection);
HashMap<Integer, String> myTempList = new HashMap<Integer, String>();
for (String myString : checkedString) {
myTempList.put(myString.getSystemId(), myString);
}
checkedString.clear();
for (Entry<Integer, String> myEntry : myTempList.entrySet()) {
checkedString.add(myEntry.getValue());
}
return checkedString;
}
#Override
protected void onSelectionChanged(Collection newSelection) {
newSelection = checkedString;
}
#Override
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
};
add(myForm);
myForm.add(myGroup);
Add a behavior to your component
sampleChckbox.add(new AjaxFormComponentUpdatingBehavior("onclick"){
#Override
protected void onUpdate(AjaxRequestTarget target) {
//perform your operation here
}
This method will fire an Ajax request on "onClick" event; which will update your backend logic at runtime
edit :
To be very specific to CheckGroup class use the AjaxFormChoiceComponentUpdatingBehavior
AjaxFormChoiceComponentUpdatingBehavior is the behavior to use with CheckGroups and RadioGroups. If you used an AjaxFormComponentUpdatingBehavior with the onchange event, you'd run into this bug with IE
. AjaxFormChoiceComponentUpdatingBehavior handles this properly, adding onclick event handlers to each of the Checks in the CheckGroup.
As a side note, what Igor stated in that mail, is that CheckBox can be replaced with AjaxCheckBox, not Check. AjaxCheckBox is nothing more than a convenience subclass of CheckBox with an AjaxFormComponentUpdatingBehavior("onclick"), as the sources show.
Hello I have a Contact class with informations which i show in a CellTable.
The CellTable has a DataListProvider, MultiSelectionModel and KeyProvider
which checks the id of the Contact.
DataListProvider and CellTable have the same KeyProvider.
if i only select/deselect the items in the CellTable and show them in a TextBox ists working fine. But the when i change the value of the Contact item in the TextBox(Contact instance) and try to deselect the item the selectionmodel says its still selected?
I tried with clear() but its still selected!
GWT 2.5 / FireFox
ProvidesKey<Contact> keyProvider = new ProvidesKey<Contact>(){
#Override
public Object getKey(Contact item) {
return item.getIdContact();
}
};
public MyCellTable(boolean canLoad, Integer pagesize, ProvidesKey<T> keyProvider) {
super(-1, resource, keyProvider);
selectionModel = new MultiSelectionModel<T>();
selectionModel .addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
selectionChange();
}
});
dataProvider = new ListDataProvider<T>(keyProvider);
dataProvider.addDataDisplay(this);
}
in the selection event i call
protected void selectionChange(){
Contact c = grid.getGrid().getSelectedItem();
if(c != null){
cpForm.enable();
cpForm.clear();
form.bind(c); // Formular which updates the selected instance
cpForm.add(form);
}else{
cpForm.disable(noseletionText);
}
}
i have no ValueUpdater
when i select an item i generate a formular and if i change something i call:
#Override
public void save() {
super.save();
ContactServiceStore.get().updateContact(manager.getBean(),
new MyAsyncCallback<Void>() {
#Override
public void onSuccess(Void result) {
onchange();
}
});
}
i if call the method without changes on the contact its still working and i can deselect but when i change the name or something else i cant select other items or deselect the current item!
You're not actually using your ProvidesKeys in your MultiSelectionModel. You need to create your MultiSelectionModel like so:
MultiSelectionModel<T> selectionModel = new MultiSelectionModel<T>(keyProvider);
If you don't supply the MultiSelectionModel with a ProvidesKey it will use the actual object as a key.
Make sure you also add the MultiSelectionModel to the table:
cellTable.setSelectionModel(selectionModel);
The reason selectionModel.clear() wasn't working was because selectionModel was not set to the table.
When the user clicks a button, I want to fire the ListGrid Selection event. I called "resultControl.resultGrid.selectRecord(0);" but it didn't work.
From your initial question and your comment, I understand that you want to simulate a selection event in your ListGrid, through a button. Assuming that I understand well, and you are only interested in one record selection (the first one), all you have to do is the following:
final ListGrid listGrid = new ListGrid();
//Initialize your listgrid's data etc.
listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
#Override
public void onSelectionChanged(SelectionEvent event) {
SC.say("here my code");
}
});
IButton button = new IButton("Select");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
listGrid.selectRecord(0);
}
});
A last note, System.out or System.err won't produce anything when your application runs in production mode. Use a suitable logging solution or the SC.say(), if you want to provide the user with a message, instead.