GWTP: Correct information flow for popup presenter - gwt

i had this task: I have Presenter/View Couple (lets call it ItemListPresenter) showing a Celltable. Now i wanted to edit an item by double-clicking it (or pressing a button, whatever). Then a popup dialog should appear (Lets call it PopupWidget), letting me edit my item properties.
i found a solution how to do it, but i am not sure it is the "right" way. Since i am trying to learn the philosophy behind GWT/GWTP, i would appreciate if you could give me hints what i did right and what i did wrong:
In the onbind method of ItemListPresenter, i wire the CellTable with a DoubleClick handler:
getView().getCellTable().setSelectionModel(selectionModel);
getView().getCellTable().addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(final DoubleClickEvent event) {
DeviceDto selectedDeviceDto = selectionModel.getSelectedObject();
//TODO: Figure out how to best handle editing of DeviceDto
if (selectedDeviceDto != null) {
devicesDialog.setCurrentDeviceDTO(selectedDeviceDto);
addToPopupSlot(devicesDialog);
}
} }, DoubleClickEvent.getType());
What does not feel right is setting the object i want to edit (selectedDeviceDto) in the Dialog Presenter Widget. Is this the "right" way?
My popup presenter which is defined as
public class DeviceEditDialogPresenterWidget extends PresenterWidget<DeviceEditDialogPresenterWidget.MyView> implements
DeviceEditDialogUiHandlers {
is still ugly, since i just set every property into a text box and after editing, i recollect the properties and rebuild the object. This is messy and i guess i should GWT Editors for that. However, when i click the "Save" Button in the dialog, an UiHandler is triggered:
#UiHandler("okButton")
void okButtonClicked(ClickEvent event) {
DeviceDto dev = new DeviceDto(idBox.getText(), deviceIdBox.getText(), typeBox.getText(), firmwareVersionBox.getText(), userBox.getText(), statusBox.getText());
getUiHandlers().updateDevice(dev);
hide();
}
This triggers my DeviceEditDialogPresenterWidget, which itself fires and event:
#Override
public void updateDevice(DeviceDto device) {
eventBus.fireEvent(new DeviceUpdatedEvent(device));
}
This event is caught by a handler in the "mother" presenter with the CellTable, which is wired in the onBind method again:
addRegisteredHandler(DeviceUpdatedEvent.TYPE, new DeviceUpdatedEvent.DeviceUpdatedHandler() {
#Override
public void onDeviceUpdatedEvent(DeviceUpdatedEvent event) {
updateDevice(event.getDevice());
}
});
I would really like to avoid going down a road of messy mistakes, so any hints would be appreciated.
Thanks
Arthur

PresenterWidgets are usually designed to have a setter which is used to set a Model or DTO that the PresenterWidget works on (the same way you did it).
Alternatively you can avoid a PresenterWidget and use an Editor (extends Composite) that you manually add to a PopupPanel or DialogBox in your ListItemEditor.
This way you avoid the complexity of a PresenterWidget. But you have to handle the clicks (i.e. save button) from the ListItemPresenter. I would always try to start small (use a composite) and if you realize that you might need the functionality also in other places, create a PresenterWidget.
Also you don't need the updateDevice method because you pass a reference to your DTO. You only need to fresh the CellTable.
But apart from that your approach looks fine.

Related

GWT Detect DOM changes or modifications

What am I trying to do?
I have an existing page (generated by system automatically and I don't have any control on it) in which I am injecting GWT code to modify the behaviour of the page after it loads based on certain columns and augment the functionality of the page. For example after adding my GWT code, cells in one of the table columns become clickable and when the user clicks it, additional information is displayed to the user in a pop-up panel. All that is working fine.
What is the issue?
The generic page in which I am injecting my code has paginated table which shows 15 rows at a time. Now, when I load/refresh the page, my GWT code kicks in and sinks events in the specific column which adds functionality (described above) to the cells. However, when the user uses the left and right buttons to navigate the paginated result, the page does not refresh as it is an asynchronous call. The specific column in the new set of 15 rows is now without the sunk events as the GWT code does not know that the page changed.
I am trying to find a way to tell my GWT code that page has changed and it should sink events to the cells of specific column for the new 15 rows but unable to find any method or mechanism to help me capture a DOM/Document change event. I tried doing this but did not help:
new ChangeHandler(){
#Override
public void onChange(ChangeEvent event) {
Window.alert("Something Changed");
}
It is possible I am missing something very obvious. Posting this question to know if there is an easy way to figure out DOM changes in GWT. Have searched for DOM/Document change/mutation/ etc. without luck.
If anyone knows how to detect DOM changes in GWT would really appreciate otherwise would go ahead writing native code using native mutation observers.
You can try something like this:
First get the input elements with:
InputElement goOn = DOM.getElementById("IdOfGoOnButton").cast();
InputElement goBack = DOM.getElementById("IdOfGoBackButton").cast();
Next add a native EventHandler:
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
if (event.getTypeInt() == Event.ONCLICK) {
if (event.getNativeEvent()
.getEventTarget() != null) {
Element as = Element.as(event.getNativeEvent()
.getEventTarget());
if (as.getTagName()
.toLowerCase()
.equals("input")) {
InputElement clickedElement = as.cast();
if (clickedElement.getId().equals(goOn.getId()) ||
clickedElement.getId().equals(goBack.getId())) {
// one of the paging button is pressed
}
}
}
}
}
});
Hope that helps.

Eclipse Scout check when leave page

I am writing a scout application, and I stumbled upon some problem.
In my standard Outline I have more than one page. In page A I have some editable table with save button. What is in page B is not important for this discussion.
Outline
page A
page B
If page A is selected and I edit some data I would like to be notified if I click on page B that some data are not saved.
So before Outline switch between page A and B I would like to have control to not switch to page B because same data in A are not saved.
I have actually solve this problem with extending pages, but I an looking if there is some standard pre-defined way for this.
Unfortunately, there is no way to prevent the node selection from really happening. As you mentioned, you can listen for activation and deactivation events in your Page by overriding the methods execPageActivatedand execPageDeactivated, respectively. But by using this approach, you cannot take control over node switching.
A bit more of control you get by providing your own implementation of createPageChangeStrategy in your Outline class by injecting a custom DefaultPageChangeStrategy. So you get informed every time a node change happens with a respective pageChange event. As long as your page is invalid, you prevent the page switching from happening and restore the origin tree selection.
Please take a look at the following example:
#Override
IPageChangeStrategy createPageChangeStrategy() {
return new DefaultPageChangeStrategy() {
#Override
public void pageChanged(IOutline outline, IPage deselectedPage, IPage selectedPage) {
if (deselectedPage instanceof APage && !((APage) deselectedPage).isValid()) { // #isValid is your check method for validity.
// Do not propagate the PageChangeEvent and restore the selection of the invalid page.
// Uninstall the PageChangeStrategy to ignore the event of restoring the selection.
final IPageChangeStrategy pageChangeStrategy = this;
setPageChangeStrategy(null);
// Restore the selection and install the PageChangeStrategy anew.
new ClientSyncJob("Restore node selection", ClientSession.get()) {
#Override
protected void runVoid(IProgressMonitor monitor) throws Throwable {
YourOutline.this.selectNode(deselectedPage);
setPageChangeStrategy(pageChangeStrategy);
}
}.schedule();
}
else {
super.pageChanged(outline, deselectedPage, selectedPage);
}
}
};
}

What is the best practice for coding addChangeHandler in Gwt?

in UiBinder
#UiField ListBox testListBox;
in presenter, I have a method getData() that put data into the testListBox.
public void getData(){
getView().getTestListBox().clear();
getView().getTestListBox().addItem("itm 1");
getView().getTestListBox().addItem("itm 2");
getView().getTestListBox().addItem("itm 3");
getView().getTestListBox().addChangeHandler(new ChangeHandler(){
#Override
public void onChange(ChangeEvent event) {
// TODO Auto-generated method stub
int ind=getView().getTestListBox().getSelectedIndex();
System.out.println(getView().getTestListBox().getValue(ind));
}
});
}
Now, i also have a button to call getData(). If i click that button 1 time, then I select "itm 1" in the testListBox everything is fine as it will print out:
itm 1
However, if I click that button 2nd times, & select "itm 1" then it prints out twice "itm 1":
itm 1
itm 1
If i click it 3rd time, it will print out triple "itm 1"....
But if i put the getView().getTestListBox().addChangeHandler outside getData() method before i add the item into the listbox (ie I do the addChangeHandler before there is actual item inside the listbox), then everything is fine as it print out only 1 time.
So What is the best practice for coding addChangeHandler in Gwt?
The important thing is that you call addChangeHandler() only once. If you call it multiple times (as in your scenario) you end up having multiple handlers all getting invoked at the same time when the value changes (hence the repeted values getting printed).
It doesn't really matter if you call addChangeHandler() before or after adding the actual items. I usually add the change handler right after creating the ListBox instance, and that's what I have seen done most often.
Since you are using UIBinder, the ListBox instance gets created automatically for you. In this case a good place to call addChangeHandler() would be in the UI container's constructor. When using MVP, it should probably go in the presenter's bind method.

Gwt, How to code the Right-ClickHandler for Label?

Ok, for label, we got ClickHandler, ie, when we click on the label it will do something.
But I want to do something like Right-ClickHandler for Label, ie, when user right click on the label, it will do something.
Some people say put the widget into DeckPanel & do the RightClick Hanler on it. But if we have a lot of labels, then
does each label have to be put into a deck panel?
If that is the case then the code maybe complicated, so I want to do the RightClick handler for label just like i do normal ClickHandler. How to do do?
I am heavily recommending this example (Which is bit old but the right way to deal with context menu).
lable.sinkEvents(Event.ONCONTEXTMENU);
lable.addHandler(
new ContextMenuHandler() {
#Override
public void onContextMenu(ContextMenuEvent event) {
event.preventDefault();
event.stopPropagation();
popupMenu.setPopupPosition( //custom menu here
event.getNativeEvent().getClientX(),
event.getNativeEvent().getClientY());
popupMenu.show();
}
}, ContextMenuEvent.getType())
Continue reading ....

GWT ClickableTextCell

I am a newbie to GWT ... need some help in understanding the below class -
What is the use of GWT ClickableTextCell ??
Is there something specific use of it. It uses something called FieldUpdater, why is that used ?
Think of a ClickableTextCell as hot spot. It looks like a regular bit of screen real estate with text in it, but it responds to clicks. What happens when you click it? The click "updates" the field. An update to a field calls the method update(). What does update() do? Whatever you want it to. You provide it by specifying the FieldUpdater. FieldUpdater is an interface, so you can construct one anonymously. Say you have a CellTable, and you have a Column that displays a String inside a ClickableTextCell. You provide your FieldUpdater to the Column:
Column<DataType, String> myIntegerColumn
= new Column<DataType, String>(new ClickableTextCell());
myIntegerColumn.setFieldUpdater(new FieldUpdater<DataType, String>(){
#Override
public void update(int index, DataType object, String value){
// execute code that reacts to a click on this hot spot
}
});
Now whenever the cell gets clicked, that code in update() fires.
A ClickableTextCell is a specific kind of cell. You can see a demo of all the different kinds of cells in this GWT showcase.
This GWT documentation explains what cell widgets are for, goes over all of the different types, and also has examples of how to use them and the ValueUpdater type.