Editor not getting ListBox changes - gwt

Issue: On load of a form, ParentEditor, a sub-editor, ThisEditor, properly
popluates all its fields including a listbox(dropdown) widget, MyWidget. However,if I select a new option in the listbox and save, it doesn't save the newly selected option; though edits to other widgets are saving fine. It appears that on a driver flush, the editor does not get the value in my listBox. In debug mode, on driver.edit, I can see the TakesValueEditor call setValue(value) on all form widgets including the listbox. But on flush, I can see the TakesValueEditor call its getValue() on other form widgets but
not on my listbox.
The editor hierarchy: ParentEditor > ThisEditor > MyWidget. ParentEditor is the entire form. ThisEditor is a sub-section of the form. MyWidget is a custom listbox in ThisEditor section.
I'm using MVP pattern. Below are sample code snippets of the View and Presenter:
VIEW:
/** ThisEditor is a sub-section of ParentEditor (the Form) and contains a
MyWidget (custom listbox). */
public class ThisEditor extends Composite implements Editor<ThisProxy>, ThisView {
... //rfeDriver interface defined and created here
#UiField
MyWidget my; //getMy and setMy methods in ThisProxy
... //other field declarations
public ThisEditor() {
initWidget(binder.createAndBindUi(this));
}
#Override
public MyView getMy() {
return my;
}
... //other methods
}
/** This is the View interface that MyWidget implements */
public interface MyView extends HasOptions, HasValue<MyProxy>, Focusable {
interface Presenter {
...
}
...
}
public class MyWidget extends Composite implements MyView,
IsEditor<LeafValueEditor<MyProxy>> {
...
#UiField
ListBox listBox; //single-select dropdown
...
public MyWidget() {
initWidget(binder.createAndBindUi(this));
addChangeHandler(); //listen to changes to listBox and setSelectedIndex (?)
}
...
#Override
public int getSelectedIndex() {
return listBox.getSelectedIndex();
}
#Override
public void setSelectedIndex(int index) {
listBox.setSelectedIndex(index);
}
...
/**
* Called by the TakesValueEditor on rfeDriver.edit.
*/
#Override
public MyProxy getValue() {
//ask presenter for the MyProxy object -- presenter calls
//getSelectedIndex() on this widget and returns the object associated
//with the index
return presenter.getValue();
}
/**
* Called by the TakesValueEditor on rfeDriver.flush.
*/
#Override
public void setValue(MyProxy value) {
//pass the value to the presenter to parse and set the index that corresponds
//to this object
presenter.setValue(value);
}
PRESENTER
public class MyPresenter implements MyView.Presenter,
ValueLookupCompleteEventHandler {
...
protected HasOptions view;
private List<MyProxy> myList;
public MyPresenter(ParentPresenter parent) {
//setParent for this child presenter
}
... //methods to set view and create association between view and presenter
#Override
public MyProxy getValue() {
//identify the current selection
String selectedId = view.getValue(view.getSelectedIndex());
if (selectedId != null) {
//iterate myList to find the MyProxy object whose id.equals(selectedId)
for (Iterator<MyProxy> i = myList.iterator(); i.hasNext();) {
MyProxy value = i.next();
if (selectedId.equals(value.getCode().toString())) {
return value;
}
}
}
return null;
}
#Override
public void setValue(MyProxy value) {
//handle null value
String selectedId = value.getCode().toString();
... //verify the value is in myList
//traverse dropdown list and set selected index corresponding to value object
for (int i = 0; i < view.getItemCount(); ++i) {
if (selectedId.equals(view.getValue(i))) {
view.setSelectedIndex(i);
}
}
}
}

I just ran into this same issue. If you look at the source code for the GWT ListBox, you'll se e that it does not implement isEditor. I actually don't think it is a GWT editor component. Hard to understand. I had to code around it.

Related

Breaking cyclic dependency in Dagger

I am very new to dagger--I don't even know yet if it will work for my application
I have a search page that returns the latest news about a given celebrity.
I have written a test to verify that results appear on the page when we search for a popular celebrity.
The page has a searchField, which requires page in its constructor so the web driver I use for my tests can select it.
Celebrity Search Page Test
public class CelebritySearchPageTest {
#Test
public void testSearchResultsForKevinBaconVerifyHisPopularity() {
CelebritySearchPage searchPage = new CelebritySearchPage();
searchPage.searchFor("Kevin Bacon");
Assert.assertTrue(searchPage.getNumberOfResults() > 9999999, "Verify that Kevin Bacon is still relevant");
}
}
Celebrity Search Page
public class CelebritySearchPage extends Page {
#Inject
#Named("search field")
TextField searchField;
public void searchFor(String text) {
searchField.setText(text);
// ...
}
public int getNumberOfResults() {
// ...
}
}
Celebrity Search Page Module
#Module(injects = CelebritySearchPage.class)
public class CelebritySearchPageModule {
#Provides
#Named("search field")
public TextField provideSearchField() {
return new TextField(/* How do I get the page? */, "#searchField");
}
}
Page
public abstract class Page {
// ...
}
Text Field
public class TextField {
protected Page page;
protected String selector;
public TextField(Page page, String selector) {
this.page = page;
this.selector = selector;
}
public void setText(String text) {
// ...
}
}
The problem is that page needs searchField, but searchField needs page. How do I get over this cyclic dependency?
I can't initialize searchField inside of CelebritySearchPage
Consider this:
CelebritySearchPage
public class CelebritySearchPage extends Page {
private final Lazy<TextField> searchField;
// always prefer constructor injection
// avoid #Named if possible, since the compiler cannot check the string
#Inject
CelebritySearchPage(#Named("search field") Lazy<TextField> searchField) {
this.searchField = searchField;
}
}
Text Field
public class TextField {
protected final Lazy<Page> page;
protected final String selector;
#Inject TextField(Lazy<Page> page, String selector) {
this.page = page;
this.selector = selector;
}
/*
Lazy::get()
Return the underlying value, computing the value if necessary. All calls to the same Lazy instance will return the same result.
*/
}
I guess one Lazy should suffice as well.

GWT: Showing a String list in DataGrid

i got a Presenter that is supposed to present a popup window what contains a DataGrip to show log file entries from a String list. I try to set the appropriate settings, but the number of log file lines that are displayed do not match the String list. I tried to enhance the data assignment, resulting in the Presenter not being shown any more.
Could you please give me a hint what i am doing wrong?
The parts of my presenter related to the DataGrid are:
// Create a list data provider.
final ListDataProvider<String> dataProvider = new ListDataProvider<String>();
public interface MyView extends PopupView, HasUiHandlers<DeviceLogfileUiHandlers> {
DataGrid<String> getDataGrid();
}
#Inject
DeviceLogfilePresenterWidget(final EventBus eventBus, final MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
protected void onBind() {
super.onBind();
// Add the cellList to the dataProvider.
dataProvider.addDataDisplay(getView().getDataGrid());
TextColumn<String> stringColumn = new TextColumn<String>() {
#Override
public String getValue(String s) {
return s;
}
};
getView().getDataGrid().addColumn(stringColumn);
}
#Override
protected void onReveal() {
super.onReveal();
}
public void setDeviceLog(List<String> logEntries) {
getView().getDataGrid().setRowData(0, logEntries);
//These entries make the presenter not show up any more:
dataProvider.addDataDisplay(getView().getDataGrid());
dataProvider.setList(logEntries);
getView().getDataGrid().setRowCount(logEntries.size(), true);
getView().getDataGrid().setVisibleRange(0, logEntries.size());
getView().getDataGrid().setPageSize(logEntries.size());
getView().getDataGrid().redraw();
}

How to set a backing list for a ListEditor

how already stated here i am currently trying to get into gwt editors.
I figured i was missing a backing list to hold the data i manipulate.
I tried to assign that backing list with a setValue call from the parent view. Now the compiler complains it is missing the getter for groupList.
I understand that by convention the groupList property is derived by naming the Editor groupListEditor. What would be the right way to attach the lists? It seems i need to somehow call setValue with a list or else it does not seem to work. What would be the right way to do it?
My Editor looks like this:
public class GroupListEditor extends Composite implements
IsEditor<ListEditor<String, GroupItemEditor>> {
private static StringListEditorUiBinder uiBinder = GWT
.create(StringListEditorUiBinder.class);
interface StringListEditorUiBinder extends
UiBinder<Widget, GroupListEditor> {
}
#UiField
FlowPanel pWidget;
#UiField
PushButton bAdd;
#UiField
FlowPanel pList;
private class StringItemEditorSource extends EditorSource<GroupItemEditor> {
#Override
public GroupItemEditor create(final int index) {
GroupItemEditor subEditor = new GroupItemEditor();
pList.insert(subEditor, index);
subEditor
.addDeleteHandler(new EditorDeleteEvent.EditorDeleteHandler() {
public void onEditorDeleteEvent(EditorDeleteEvent event) {
remove(index);
}
});
return subEditor;
}
#Override
public void dispose(GroupItemEditor subEditor) {
subEditor.removeFromParent();
}
#Override
public void setIndex(GroupItemEditor editor, int index) {
pList.insert(editor, index);
}
}
private ListEditor<String, GroupItemEditor> editor = ListEditor
.of(new StringItemEditorSource());
public GroupListEditor() {
initWidget(uiBinder.createAndBindUi(this));
}
#UiHandler("bAdd")
void onBAddClick(ClickEvent event) {
Log.debug("Add button clicked");
add();
}
private void add() {
String s = "";
//TODO: Problem is there is no backing list, FIx this
editor.getList().add(s);
}
#Override
public ListEditor<String, GroupItemEditor> asEditor() {
return editor;
}
private void remove(final int index) {
editor.getList().remove(index);
}
}
The Editor is used as a sub-editor in a editor widget like this:
I tried to set the backing list:
public class ContainerEditorDialogPresenterWidget extends PresenterWidget<ContainerEditorDialogPresenterWidget.MyView> implements
ContainerEditorDialogUiHandlers {
private final PlaceManager placeManager;
#Inject
ContainerEditorDialogPresenterWidget(EventBus eventBus,
MyView view, PlaceManager placeManager) {
super(eventBus, view);
getView().setUiHandlers(this);
this.eventBus = eventBus;
this.placeManager = placeManager;
}
/**
* {#link LocalDialogPresenterWidget}'s PopupView.
*/
public interface MyView extends PopupView, ContainerEditView<ContainerDto>, HasUiHandlers<ContainerEditorDialogUiHandlers> {
}
private ContainerDto currentContainerDTO = null;
private DeviceDto currentDeviceDTO = null;
private final EventBus eventBus;
private SimpleBeanEditorDriver<ContainerDto, ?> driver;
public ContainerDto getCurrentContainerDTO() {
return currentContainerDTO;
}
public void setCurrentContainerDTO(ContainerDto currentContainerDTO) {
this.currentContainerDTO = currentContainerDTO;
}
public void setCurrentDeviceDTO(DeviceDto currentDeviceDTO) {
this.currentDeviceDTO = currentDeviceDTO;
}
#Override
public void onReveal() {
super.onReveal();
driver.edit(currentContainerDTO);
}
#Override
protected void onBind() {
super.onBind();
driver = getView().createEditorDriver();
}
#Override
public void updateContainer() {
ContainerDto dev = driver.flush();
eventBus.fireEvent(new ContainerUpdatedEvent(dev));
}
}
I tried to assign the backing list (just an empty string list) in the view:
public class ContainerEditorDialogView extends
PopupViewWithUiHandlers<ContainerEditorDialogUiHandlers> implements
ContainerEditorDialogPresenterWidget.MyView, Editor<ContainerDto> {
interface Binder extends UiBinder<PopupPanel, ContainerEditorDialogView> {
}
public interface Driver extends SimpleBeanEditorDriver<ContainerDto, ContainerEditorDialogView> {
}
#UiField
TextBox uuid;
#UiField
TextBox name;
#UiField
TextBox groups;
//#UiField
//TextBox device;
//public TextBox getDevice() {
// return device;
//}
#UiField
GroupListEditor groupListEditor;
#UiField
TextBox imei;
#UiField
TextBox type;
#UiField
TextBox user;
#UiField
Button okButton;
#UiField
Button cancelButton;
#Inject
ContainerEditorDialogView(Binder uiBinder, EventBus eventBus) {
super(eventBus);
initWidget(uiBinder.createAndBindUi(this));
ListEditor<String, GroupItemEditor> ed = null;
groupListEditor.asEditor().setValue(new ArrayList<String>());
}
#Override
public SimpleBeanEditorDriver<ContainerDto, ?> createEditorDriver() {
Driver driver = GWT.create(Driver.class);
driver.initialize(this);
return driver;
}
//Should this handled by a presenter?
#UiHandler("okButton")
void okButtonClicked(ClickEvent event) {
getUiHandlers().updateContainer();
hide();
}
#UiHandler("cancelButton")
void cancelButtonClicked(ClickEvent event) {
hide();
}
}
Thank you!
Update:
A version of the GroupListEditor without a separate ArrayList would look like this and is what i started with:
public class GroupListEditor extends Composite implements
IsEditor<ListEditor<String, GroupItemEditor>> {
private static StringListEditorUiBinder uiBinder = GWT
.create(StringListEditorUiBinder.class);
interface StringListEditorUiBinder extends
UiBinder<Widget, GroupListEditor> {
}
#UiField
FlowPanel pWidget;
#UiField
PushButton bAdd;
#UiField
FlowPanel pList;
private class StringItemEditorSource extends EditorSource<GroupItemEditor> {
#Override
public GroupItemEditor create(final int index) {
GroupItemEditor subEditor = new GroupItemEditor();
pList.insert(subEditor, index);
subEditor
.addDeleteHandler(new EditorDeleteEvent.EditorDeleteHandler() {
public void onEditorDeleteEvent(EditorDeleteEvent event) {
remove(index);
}
});
return subEditor;
}
#Override
public void dispose(GroupItemEditor subEditor) {
subEditor.removeFromParent();
}
#Override
public void setIndex(GroupItemEditor editor, int index) {
pList.insert(editor, index);
}
}
private ListEditor<String, GroupItemEditor> editor = ListEditor
.of(new StringItemEditorSource());
public GroupListEditor() {
initWidget(uiBinder.createAndBindUi(this));
}
#UiHandler("bAdd")
void onBAddClick(ClickEvent event) {
Log.debug("Add button clicked");
add();
}
private void add() {
String s = "Stuff";
editor.getList().add(s);
}
#Override
public ListEditor<String, GroupItemEditor> asEditor() {
return editor;
}
private void remove(final int index) {
editor.getList().remove(index);
}
}
Since i declare the Editor as
#UiField
GroupListEditor groupListEditor;
Should the required getter of ContainerDto not be named getGroupList() ?
You refer to DeviceDto, which is just carried around and should not interfere with the editor.
Since the GroupListEditor implements
IsEditor<ListEditor<String, GroupItemEditor>>
it should expect a List, right?
My ContainerDto has the field
protected ArrayList<String> groupList;
So that should be fine, i guess. This was my starting point before trying to manually call setValue.
When i that, i get this error, when clicking the "Add" button.
Caused by: java.lang.NullPointerException: null at
testproject.client.application.containers.editor.GroupListEditor.add(GroupListEditor.java:81)
at testproject.client.application.containers.editor.GroupListEditor.onBAddClick(GroupListEditor.java:76)
which refers to editor.getList().add(s) which means there is no list...
Update 2:
I changed the declaration of the UIField to:
#UiField
#Path("groupList")
GroupListEditor groupListEditor;
But i still get the NullPointerException when trying to add stuff to the list like before: editor.getList().add(s);
You don't have to manually call setList to pass the list to your SubEditor. That should be taken care of for you by the parent/container Editor.
However the Editor will use the UiField name to match the corresponding getter in your backing DTO.
In your above example this will only work if your DeviceDTO has a getGroupListEditor() getter that returns the type that your ListEditor expects (beacuse of #UiField GroupListEditor groupListEditor;).
If your DeviceDTO doesn't contain the corresponding getter you can do 3 things:
Rename your #UiField GroupListEditor groupListEditor; (i.e. groupItems)
Rename the getter in your DeviceDTO to getGroupListEditor
add Path('groupItems') to your #UiField GroupListEditor groupListEditor;
Solution 3 is usually the way to go:
#UiField
#Path('groupItems')
GroupListEditor groupListEditor;
Note: Change groupItems has to match the getter in your DeviceDTO that returns the group items.
Update:
This is how your DeviceDTO looks like and how you use Path to point your editor to the correct getter. As long as your groupList class variable is initialized to an empty ArrayList everything should work fine.
class DeviceDTO {
protected List<String> groupList = new ArrayList<String>();
public List<String> getGroupList() {
return groupList;
}
}
public class ContainerEditorDialogView extends
PopupViewWithUiHandlers<ContainerEditorDialogUiHandlers> implements
ContainerEditorDialogPresenterWidget.MyView, Editor<ContainerDto> {
....
#UiField
#Path("groupList")
GroupListEditor groupListEditor;
}

GWT: Adding a SubmitHandler to a Form without putting logic in View

I have a GWTP PresenterWidget and View pair that contains a simple search form.
Currently I am adding the SubmitHandler to the form by calling the getSearchForm() method of my View, which is bad practice as it references the actual class instead of an interface:
public class HeaderPresenter extends PresenterWidget<HeaderPresenter.MyView>
{
public interface MyView extends View
{
void submitForm();
Form getSearchForm();
}
// ...omitted for brevity
#Inject
public HeaderPresenter(EventBus eventBus, DispatchAsync dispatchAsync, MyView view, PlaceManager placeManager)
{
// ...omitted for brevity
}
#Override
protected void onBind()
{
super.onBind();
getView().getSearchForm().addSubmitHandler(new SubmitHandler()
{
#Override
public void onSubmit(SubmitEvent event)
{
// stops the form submission
event.cancel();
// now we can do our stuff
String query = getView().getSearchQuery();
if(query != "") // don't search for a blank string
{
PlaceRequest request = new PlaceRequest.Builder().nameToken(NameTokens.search).with("q", query).build();
placeManager.revealPlace(request);
}
}
});
}
Is there a way to add the SubmitHandler in the HeaderPresenter, or will I have to put that code in the View?
I'd like to keep as much logic in the Presenter as possible.
I found a discussion on this here, and ended up using option 4 as suggested by Thomas Broyer:
https://groups.google.com/forum/#!topic/google-web-toolkit/Fbo-SEDjRa4

GWT's Editor Framework and GWTP

building on this answer, i try to integrate the GWT editors into a popup presenter widget. What is the right way to do that?
My view looks like this:
public class DeviceEditorDialogView extends
PopupViewWithUiHandlers<DeviceEditorDialogUiHandlers> implements
DeviceEditorDialogPresenterWidget.MyView {
interface Binder extends UiBinder<PopupPanel, DeviceEditorDialogView> {
}
public interface Driver extends SimpleBeanEditorDriver<DeviceDto, DeviceEditorDialogView> {
}
#Inject
DeviceEditorDialogView(Binder uiBinder, EventBus eventBus) {
super(eventBus);
initWidget(uiBinder.createAndBindUi(this));
}
#Override
public SimpleBeanEditorDriver<DeviceDto, ?> createEditorDriver() {
Driver driver = GWT.create(Driver.class);
driver.initialize(this);
return driver;
}
}
and my presenter looks like this:
public class DeviceEditorDialogPresenterWidget extends PresenterWidget<DeviceEditorDialogPresenterWidget.MyView> implements
DeviceEditorDialogUiHandlers {
#Inject
DeviceEditorDialogPresenterWidget(EventBus eventBus,
MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
/**
* {#link LocalDialogPresenterWidget}'s PopupView.
*/
public interface MyView extends PopupView, DevicesEditView<DeviceDto>, HasUiHandlers<DeviceEditorDialogUiHandlers> {
}
private DeviceDto currentDeviceDTO = null;
private SimpleBeanEditorDriver<DeviceDto, ?> driver;
public DeviceDto getCurrentDeviceDTO() {
return currentDeviceDTO;
}
public void setCurrentDeviceDTO(DeviceDto currentDeviceDTO) {
this.currentDeviceDTO = currentDeviceDTO;
}
#Override
protected void onBind() {
super.onBind();
driver = getView().createEditorDriver();
}
//UiHandler Method: Person person = driver.flush();
}
Is this the right approach? What is missing? Currently nothing happens when i try to use it like this:
#Override
public void showDeviceDialog() {
deviceEditorDialog.setCurrentDeviceDTO(new DeviceDto());
addToPopupSlot(deviceEditorDialog);
}
showDeviceDialog is in the parent presenter and called when clicking a button in that parent Presenter, that instantiates the dialog with private final DeviceEditorDialogPresenterWidget deviceEditorDialog;
Thanks!
Here are a few key points that are missing from your code above:
Your DeviceEditorDialogView should implement Editor<DeviceDto>. This is required in order for the fields of DeviceEditorDialogView to be populated with data from you POJO.
Your DeviceEditorDialogView should have child editors that are mapped to fields in your POJO. For example, given the field deviceDto.modelName (type String), you could have a GWT Label named modelName in your DeviceEditorDialogView. This Label implements Editor<String> and will be populated with the modelName from your DeviceDto when you call driver.edit(deviceDto)
You should call driver.initialize(this) only once, in DeviceEditorDialogView's constructor
You should override onReveal() like this:
#Override
public void onReveal() {
super.onReveal();
driver.edit(currentDeviceDTO); // this will populate your view with the data from your POJO
}
This method will be called when the popup is displayed, just after your DeviceEditorDialogPresenterWidget has been addToPopupSlot