GWT:selection model not working properly - gwt

I have a celltable in GWT which have checkboxes, to select multiple checkboxes i am using selectionModel,once I check any checkbox its values get saved in the selectionModel,but then when i uncheck the checkbox , they never get remove , i want to remove the previous selection , how can it be possible
below is the code
List<Categories> selected;
display.getListWidget().getSelectionModel().addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
selected = new ArrayList<Categories>(
((MultiSelectionModel<Categories>) display.getListWidget().getSelectionModel()).getSelectedSet());
}
});
What i am trying to do is
display.getListWidget().getSelectionModel().setSelected(categories, false);
but its not working , coz i guess categories is not the one which is already added ..
Any Suggestions
Thanks

If you could clarify your question with some more code or by being more specific, we might be able to give a better answer. From your question, I'm guessing that your Categories equals and hashcode are not overwritten in a way that the "categories" you are trying to set is being found.
I'm guessing a bit here but I think a KeyProvider will help you deselect the correct Categories object.
view:
SelectionModel<Categories> selectionModel;
ProvidesKey<Categories> keyProvider = new ProvidesKey<Categories>() {
public Object getKey(Categories categories) {
return item == null ? null : categories.id() // or some unique identifier
}
};
CellTable cellTable = new CellTable<Categories>(keyProvider);
// Omitted..Add columns..
selectionModel = new MultiSelectionModel<Categories>(keyProvider);
cellTable.setSelectionModel(selectionModel);
presenter:
List<Categories> selected;
display.getListWidget().getSelectionMode().addSelectionChangeHandler(
new SelectionChangeHandler() {
public void onSelectionChange() {
MultiSelectionModel selectionModel =
(MultiSelectionModel) display.getListWidget().getSelectionModel();
selected = Lists.newArrayList(selectionModel.getSelectedSet());
}
});

Related

Smart GWT List Grid - Setting a hilite to a list grid on record click

I'm trying to set a hilite inside the record click handler of the list grid. I have tired the following code,
My hilites are as follows,
public static Hilite[] getWayBillSetHilites() {
return new Hilite[]{
new Hilite() {
{
setFieldNames("RECORD_VIEWED_STATUS");
setCriteria(new Criterion("RECORD_VIEWED_STATUS", OperatorId.EQUALS, "TRUE"));
setCssText(Constant.Css.TEXT_ITALIC_GRAY_32);
setTextColor("font-style:italic;color:#525252;");
setId("0");
}
}
};
}
record click handler of the grid appears as follows,
grid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent recordClickEvent) {
//gridWayBillSetGrid.getHiliteState()
//make RECORD_VIEWED_STATUS value "true"
recordClickEvent.getRecord().setAttribute("RECORD_VIEWED_STATUS", true);
gridWayBillSetGrid.enableHilite("0", true);
}
});
But when I click on the record, the styles are not showing up.
Please be kind to advise on this.
I think it's the wrong use case for hilites. Use getCellCSSText instead.
Try this one (override getCellCSSText method of ListGrid class):
ListGrid grid = new ListGrid(...){
#Override
protected String getCellCSSText(ListGridRecord record, int rowNum, int colNum) {
if("true".equalsIgnoreCase(record.getAttribute("RECORD_VIEWED_STATUS"))){
return "font-style:italic;color:#525252;";
}
return super.getCellCSSText(record, rowNum, colNum);
}
};

How to disable(not remove) a column in celltable of gwt

i am new to GWT.I know tablename.removeColumn(columnname) can be used to remove the column, but instead of removing i want to disable it. Can anybody please help
thnx in advance!
There are some ways to do this, but an easy and clean way to do it is the following :
public static class CustomTextInputCell extends TextInputCell {
#Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
String url = Window.Location.getHref();
boolean isEditable = url.contains("xyz");
if (isEditable) //Condition if editable or not
super.render(context, value, sb);
else if (value != null) {
sb.appendEscaped(value);
}
}
}
The render method will be called every time this cell is rendered. So every time it will check if the condition is met to be enabled or not.
This allows you to keep all the functionality of an editable cell but disable it easily when the condition is met.
You use it like this
Column<YOUR_OBJECT_HERE, String> column = new Column<YOUR_OBJECT_HERE, String>(new CustomTextInputCell());
cellTable.addColumn(column , "YOUR_HEADER_HERE");
I ended up creating a new component that has the columns that i want and called that component based on the url
String url = Window.Location.getHref();
boolean value = url.contains("xyz");
if(value)
{
component.setEnable(true);
}
else{
componentprevious.setEnable(true);
}
enter code here

What is the right usage for the SingleSelectionModel?

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());
}
}
});
}
}

GWT CellTable SelectionModel can not deselect item after editing

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.

GWT Header checkbox to check / uncheck all checkboxes in my table

I´ve created an CellTable with the Google Web Toolkit.
I just started using it and my knowledge about it is very small...
However I was searching for a tutorial or just a code example of how to create a checkbox in the CellTable header but everythin I´ve found I didn´t understand or it didn´t worked.
So far I´ve got this code to create a Column for checkboxes and a normal table mostly the same as the Google tutorial for a CellTable:
Column<Contact, Boolean> checkColumn = new Column<Contact, Boolean>(
new CheckboxCell(true, false)) {
#Override
public Boolean getValue(Contact contact) {
return null;
}
};
table.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
table.setColumnWidth(checkColumn, 40, Unit.PX);
Now I´m searching for the code to add a checkbox to the header and how to make it check or uncheck all checkboxes.
Thanks for your time.
From my blog post:
Here is a simple column header that selects/ de-selects all rows in a table. When all rows are checked, the header becomes checked automatically. Clicking the checkbox in the header causes either to select or de-select all rows.
I am using the selection model and the data list provider to do the selection magic. It may not work for everyone.
And here is my custom header:
public final class CheckboxHeader extends Header {
private final MultiSelectionModel selectionModel;
private final ListDataProvider provider;
public CheckboxHeader(MultiSelectionModel selectionModel,
ListDataProvider provider) {
super(new CheckboxCell());
this.selectionModel = selectionModel;
this.provider = provider;
}
#Override
public Boolean getValue() {
boolean allItemsSelected = selectionModel.getSelectedSet().size() == provider
.getList().size();
return allItemsSelected;
}
#Override
public void onBrowserEvent(Context context, Element elem, NativeEvent event) {
InputElement input = elem.getFirstChild().cast();
Boolean isChecked = input.isChecked();
for (TYPE element : provider.getList()) {
selectionModel.setSelected(element, isChecked);
}
}
}
See http://code.google.com/p/google-web-toolkit/issues/detail?id=7014