ValueBoxEditorDecorator<String> does not show errors - gwt

I was under the impression that ValueBoxEditorDecorator would display errors to the right of the text box. I am running this code. When errors are picked up, I get a alert box, but nothing is shown for ValueBoxEditorDecorator.
public class AddressEditor extends Composite implements Editor<Address>, HasEditorErrors<Address>
{
private static final Binder binder = GWT.create(Binder.class);
#UiField ValueBoxEditorDecorator<String> name;
interface Binder extends UiBinder<Widget, AddressEditor> {
}
public AddressEditor() {
initWidget(binder.createAndBindUi(this));
}
#Override
public void showErrors(List<EditorError> errors) {
name.showErrors(errors);
if (errors.size()>0)
{
StringBuilder sb = new StringBuilder();
for(EditorError e : errors)
{
sb.append(e.getMessage());
}
Window.alert(sb.toString());
}
}
This is the xml ui.
<e:ValueBoxEditorDecorator ui:field="name">
<e:valuebox>
<g:TextBox/>
</e:valuebox>
</e:ValueBoxEditorDecorator>
EDIT:
This is my validation detection code. Perhaps I am erasing some data about the path files of the errors.
a = editorDriver.flush();
ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Address>> violations = validator.validate(a);
#SuppressWarnings({ "unchecked", "rawtypes" })
Set<ConstraintViolation<?>> violations2 = (Set<ConstraintViolation<?>>) (Set) violations;
editorDriver.setConstraintViolations(violations2);

Found it. The problem was that my JSR303 annotations where on the private fields, rather than on the getter.

Related

Dynamic injection using #SpringBean in wicket

I have a form that based on collected information generates a report. I have multiple sources from which to generate reports, but the form for them is the same. I tried to implement strategy pattern using an interface implementing report generator services, but that led to wicket complaining about serialization issues of various parts of the report generator. I would like to solve this without duplicating the code contained in the form, but I have not been able to find information on dynamic injection with #SpringBean.
Here is a rough mock up of what I have
public class ReportForm extends Panel {
private IReportGenerator reportGenerator;
public ReportForm(String id, IReportGenerator reportGenerator) {
super(id);
this.reportGenerator = reportGenerator;
final Form<Void> form = new Form<Void>("form");
this.add(form);
...
form.add(new AjaxButton("button1") {
private static final long serialVersionUID = 1L;
#Override
protected void onSubmit(AjaxRequestTarget target)
{
byte[] report = reportGenerator.getReport(...);
...
}
});
}
}
If I do it this way, wicket tries to serialize the concrete instance of reportGenerator. If I annotate the reportGenerator property with #SpringBean I receive Concrete bean could not be received from the application context for class: IReportGenerator
Edit: I have reworked implementations of IRerportGenerator to be able to annotate them with #Component and now I when I use #SpringBean annotation I get More than one bean of type [IReportGenerator] found, you have to specify the name of the bean (#SpringBean(name="foo")) or (#Named("foo") if using #javax.inject classes) in order to resolve this conflict. Which is exactly what I don't want to do.
I think the behavior you're trying to achieve can be done with a slight workaround, by introducing a Spring bean that holds all IReportGenerator instances:
#Component
public class ReportGeneratorHolder {
private final List<IReportGenerator> reportGenerators;
#Autowired
public ReportGeneratorHolder(List<IReportGenerator> reportGenerators) {
this.reportGenerators = reportGenerators;
}
public Optional<IReportGenerator> getReportGenerator(Class<? extends IReportGenerator> reportGeneratorClass) {
return reportGenerators.stream()
.filter(reportGeneratorClass::isAssignableFrom)
.findAny();
}
}
You can then inject this class into your Wicket page, and pass the desired class as a constructor-parameter. Depending on your Spring configuration you might need to introduce an interface for this as well.
public class ReportForm extends Panel {
#SpringBean
private ReportGeneratorHolder reportGeneratorHolder;
public ReportForm(String id, Class<? extends IReportGenerator> reportGeneratorClass) {
super(id);
IReportGenerator reportGenerator = reportGeneratorHolder
.getReportGenerator(reportGeneratorClass)
.orElseThrow(IllegalStateException::new);
// Form logic omitted for brevity
}
}
As far as I am able to find, looking through documentation and even the source for wicket #SpringBean annotation, this isn't possible. The closest I got is with explicitly creating a proxy for a Spring bean based on class passed. As described in 13.2.4 Using proxies from the wicket-spring project chapter in Wicket in Action.
public class ReportForm extends Panel {
private IReportGenerator reportGenerator;
private Class<? extends IReportGenerator> classType;
private static ISpringContextLocator CTX_LOCATOR = new ISpringContextLocator() {
public ApplicationContext getSpringContext() {
return ((MyApplication)MyApplication.get()).getApplicationContext();
}
};
public ReportForm(String id, Class<? extends IReportGenerator> classType) {
super(id);
this.classType = classType;
final Form<Void> form = new Form<Void>("form");
this.add(form);
...
form.add(new AjaxButton("button1") {
private static final long serialVersionUID = 1L;
#Override
protected void onSubmit(AjaxRequestTarget target)
{
byte[] report = getReportGenerator().getReport(...);
...
}
});
}
private <T> T createProxy(Class<T> classType) {
return (T) LazyInitProxyFactory.createProxy(classType, new
SpringBeanLocator(classType, CTX_LOCATOR));
}
private IReportGenerator getReportGenerator() {
if (reportGenerator = null) {
reportGenerator = createProxy(classType);
}
return reportGenerator;
}
}

UiEditor/RequestFactory generate null delegate on ValueProxy

I have a Service.class ValueProxy in a service oriented architecture (i have a server method like "storeService(service)".
I would use an Editor with RequestFactoryEditorDriver.
I expect all work, but when i call driver.edit(service) i get a null delegate exception.
Why? It's a bug?
At http://crazygui.wordpress.com/tag/editor/ i find a sample of implementation...only differece with mine is SimpleBeanRequestEditorDriver.class (i have RequestFactoryEditorDriver.class)
ServiceEditor.class
public class ServiceEditor extends Composite implements Editor<ServiceProxy>{
private static final Logger logger = Logger.getLogger(ServiceEditor.class.getName());
private static ServiceEditorUiBinder uiBinder = GWT
.create(ServiceEditorUiBinder.class);
interface ServiceEditorUiBinder extends
UiBinder<Widget, ServiceEditor> {
}
//~Driver ==========================================================================================================================
interface Driver extends RequestFactoryEditorDriver<ServiceProxy, ServiceEditor> {
}
Driver driver;
//====================================================================================================================================
#UiField
Decorator<String> name;
#UiField
Decorator<String> description;
#UiField
Decorator<String> notes;
#UiField
Decorator<String> citiesString;
// #UiField(provided=true)
// Decorator<String> category;
// MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
// #UiField(provided=true)
// #Ignore
// SuggestBox suggestBox = new SuggestBox(oracle);
private BigInteger organizationId;
private EditorDelegate<ServiceProxy> delegate;
public ServiceEditor() {
initWidget(uiBinder.createAndBindUi(this));
driver = GWT.create(Driver.class);
}
#Override
protected void onLoad() {
driver.initialize(ClientFactory.AppInjector.getRequestFactory(),this);
}
public void edit() {
ServiceRequestContext requestContext = ClientFactory.AppInjector.getRequestFactory().getNewServiceContext();
edit(requestContext.create(ServiceProxy.class),requestContext);
}
public void display(ServiceProxy p){
driver.display(p);
}
public void edit(ServiceProxy p) {
ServiceRequestContext requestContext = ClientFactory.AppInjector.getRequestFactory().getNewServiceContext();
edit(p,requestContext);
}
public void edit(ServiceProxy service,ServiceRequestContext requestContext) {
if(service.getToken()==null) {
requestContext.addServiceToOrganization(organizationId, service);
//TODO: attenzione a tempistiche chiamate;
SessionRPC.Util.getInstance().getOrganizationId(new AsyncCallback<BigInteger>() {
#Override
public void onSuccess(BigInteger result) {
organizationId = result;
}
#Override
public void onFailure(Throwable caught) {
}
});
}
else
requestContext.updateService(service);
driver.edit(service,requestContext);
}
public RequestContext flush2(){
return driver.flush();
}
public void submit(Receiver<Void> receiver){
RequestContext context = driver.flush();
if (driver.hasErrors()) {
Window.alert("Driver errors!");
return;
}
context.fire(receiver);
}
public void notifyErrors(Set<ConstraintViolation<?>> violations) {
driver.setConstraintViolations(violations);
//driver.getErrors().get(0).
logger.info("Validation Errors: /n "+driver.getErrors().toString());
}
// #Override
// public void setDelegate(EditorDelegate<ServiceProxy> delegate) {
// this.delegate = delegate;
// }
}
ActivitySnippet...
ServiceEditor serviceEditor = GWT.create(ServiceEditor.class);
serviceEditor.display(response);
Stack trace...
Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) : Cannot set property 'request' of undefined
at Unknown.$collect(JsArrayString.java:42)
at Unknown.fillInStackTrace_2(StackTraceCreator.java:180)
at Unknown.fillInStackTrace_0(StackTraceCreator.java:518)
at Unknown.fillInStackTrace(Throwable.java:115)
at Unknown.Throwable_0(Throwable.java:51)
at Unknown.Exception_0(Exception.java:25)
at Unknown.RuntimeException_0(RuntimeException.java:25)
at Unknown.JavaScriptException_1(JavaScriptException.java:117)
at Unknown.JavaScriptException_0(JavaScriptException.java:109)
at Unknown.getCachableJavaScriptException(Exceptions.java:45)
at Unknown.wrap(Exceptions.java:29)
at Unknown.$setRequestContext(RequestFactoryEditorDelegate.java:80)
at Unknown.$edit(AbstractRequestFactoryEditorDriver.java:168)
at Unknown.display_0(AbstractRequestFactoryEditorDriver.java:159)
at Unknown.$show_2(ServiceEditor.java:91)
This means that getDelegate() method (line 168 of AbstractRequestFactoryEditorDriver.class) return null.
If you call RequestFactoryEditorDriver.show, it essentially just calls RequestFactoryEditorDriver.edit, but without a requestcontext. This otherwise should follow the same basic path in terms of how the driver needs to be wired up.
In this case, if the delegate is null, then the driver hasn't been initialized. From your code:
public ServiceEditor() {
initWidget(uiBinder.createAndBindUi(this));
driver = GWT.create(Driver.class);
}
#Override
protected void onLoad() {
driver.initialize(ClientFactory.AppInjector.getRequestFactory(),this);
}
public void edit() {
ServiceRequestContext requestContext = ClientFactory.AppInjector.getRequestFactory().getNewServiceContext();
edit(requestContext.create(ServiceProxy.class),requestContext);
}
public void display(ServiceProxy p){
driver.display(p);
}
You never listed the code that shows when edit or display are being called, so I'm mostly guessing here, but it looks like your other code looks roughly like this:
ServiceEditor editor = new ServiceEditor();//create ui, create driver.
editor.display(data);//driver.display, which calls driver.edit
parentWidget.add(editor);// causes onLoad to be called if actually
//attached to the dom, which causes driver.initialize
This is wrong. The driver must be initialized before you attempt to edit or display, since otherwise it doesn't know which editor objects it should be working with.
Instead, move driver.initialize to earlier than onLoad, like in the ServiceEditor constructor itself, or in some init method which allows you to call it earlier deliberately. Another option is to just call parent.add(editor) before editor.display(), though this may not work - make sure you understand what causes onLoad to be called.
(Please note that ServiceEditor.show is in your stack trace but not in your code listing, so I can't tell if the chance would be more obvious in seeing this.)

How to pass additional data to GWT sub-editors?

i have this issue:
I have a PresenterWidget which contains sub-editors.
There are "container" elements which should be editable by this widget. These containers can be assigned to groups. To do so, i would like to fetch a list of all available groups from the server. So the widget is set up like this (i use GWTP):
public class ContainerEditorDialogPresenterWidget extends PresenterWidget<ContainerEditorDialogPresenterWidget.MyView> implements
ContainerEditorDialogUiHandlers {
private final PlaceManager placeManager;
private List<GroupDTO> groupList = new ArrayList<GroupDTO>();
private final DispatchAsync dispatcher;
#Inject
ContainerEditorDialogPresenterWidget(EventBus eventBus,
MyView view, PlaceManager placeManager, DispatchAsync dispatcher) {
super(eventBus, view);
getView().setUiHandlers(this);
this.dispatcher = dispatcher;
fetchGroups();
}
...
public void fetchGroups(){
FetchGroupsAction action = new FetchGroupsAction();
dispatcher.execute(action, new AsyncCallbackImpl<FetchGroupsResult>() {
#Override
public void onSuccess(FetchGroupsResult result) {
groupList = result.getGroupDtos();
eventBus.fireEvent(new GroupListUpdatedEvent(groupList));
}
});
}
So i call fetchGroups in the constructor to get it as early as possible. Since it is an AynchCallback, i get the result back "at some time". I then try to pass the values to the sub-editor with a GroupListUpdatedEvent. In there i have a Editor declared 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> {
}
//Gives us access to the event bus.
#Inject private EventBus eventBus;
...
public GroupListEditor() {
initWidget(uiBinder.createAndBindUi(this));
eventBus.addHandler(GroupListUpdatedEvent.TYPE, new GroupListUpdatedEvent.GroupListUpdatedHandler() {
#Override
public void onGroupListUpdatedEvent(GroupListUpdatedEvent event) {
Log.debug("onContainerUpdatedEvent caught");
allGroups = event.getGroupList();
if(allGroups != null) {
for (GroupDTO g : allGroups) {
lbAllGroups.addItem(g.getName(), g.getId().toString());
}
lbAllGroups.setVisibleItemCount(5);
Log.debug("Item list = " + lbAllGroups.getItemCount());
} else {
Log.debug("GROUP LIST is Null!");
}
}
});
}
When i try to register the handler, i get an exception. So i expect the eventBus is not injected properly. What do i miss, how can i use events and the event bus if i am not in a Presenter?
And: Is this the right way at all to populate Editors with "utility" data? I guess Editor should be related directly to the data they care for. But how do i handle this kind of supplemental data?
Thanks :)
Do you use #UiField in your ContainerEditorDialogPresenterWidgetView for your GroupListEditor ?
If so then Dependency Injection won't work because you basically manually create the GroupListEditor which leads to EventBus being NULL.
I would also use Constructor Injection instead of field injection.
GroupListEditor:
#Inject
public GroupListEditor(EventBus eventBus) {
this.eventBus = eventBus;
}
ContainerEditorDialogPresenterWidgetView:
public class ContainerEditorDialogPresenterWidgetView {
#UiField(provided=true)
GroupListEditor groupListEditor;
#Inject
public ContainerEditorDialogPresenterWidgetView(GroupListEditor groupListEditor);
this.groupListEditor = groupListEditor;
initWidget();
}
}
Alternatively you could get an instance of your GroupListEditor via the Ginjector directly.

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

GIN & GWT: Binding Presentation layer with View

I'm trying to bind a GWT view with its presentation layer, but it doesn't seem to be doing anything.
It's a Spring Roo GWT generated project and I'm trying to use the scaffold given as far as possible.
The view is a simple button (R.ui.xml) and the rest of the view is defined in R.java:
public class R extends Composite implements RPresenter.Display {
interface MyUiBinder extends UiBinder<Widget, R> {}
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
#UiField Button myButton;
private ClickHandler buttonClickHandler = null;
public R(){
initWidget(uiBinder.createAndBindUi(this));
}
#UiHandler("myButton")
void onButtonClick(ClickEvent event){
GWT.log('Button clicked');
if (buttonClickHandler != null){
GWT.log("buttonClickHandler event triggered");
buttonClickHandler.onClick(event);
}
}
#Override
public void setButtonClickHandler(ClickHandler buttonClickHandler) {
GWT.log("setButtonClickHandler");
this.buttonClickHandler = buttonClickHandler;
}
}
The presenter:
public class RPresenter {
public interface Display extends IsWidget {
void setButtonClickHandler(ClickHandler buttonClickHandler);
}
private final Display display;
private final EventBus eventBus;
#Inject
public RPresenter(EventBus eventBus, Display display){
this.display = display;
this.eventBus = eventBus;
bind();
}
private void bind(){
display.setButtonClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
GWT.log("onClick event triggered");
}
});
}
public void go(HasWidgets container){
container.add(display.asWidget());
}
}
And for my GIN module I use the generated ScaffoldModule in the ...client.scaffold.ioc package:
public class ScaffoldModule extends AbstractGinModule {
#Override
protected void configure() {
GWT.log("ScaffoldModule configure");
bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
bind(ApplicationRequestFactory.class).toProvider(RequestFactoryProvider.class).in(Singleton.class);
bind(PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);
//bind(RPresenter.Display.class).to(R.class).in(Singleton.class);
bind(RPresenter.Display.class).to(R.class);
}
static class PlaceControllerProvider implements Provider<PlaceController> {
private final EventBus eventBus;
#Inject
public PlaceControllerProvider(EventBus eventBus) {
this.eventBus = eventBus;
}
public PlaceController get() {
return new PlaceController(eventBus);
}
}
static class RequestFactoryProvider implements Provider<ApplicationRequestFactory> {
private final EventBus eventBus;
#Inject
public RequestFactoryProvider(EventBus eventBus) {
this.eventBus = eventBus;
}
public ApplicationRequestFactory get() {
ApplicationRequestFactory requestFactory = GWT.create(ApplicationRequestFactory.class);
requestFactory.initialize(eventBus);
return requestFactory;
}
}
}
In the GWT development mode console, the "ScaffoldModule configure" never displays, yet the generated scaffold seems to binding just fine as the events get passed along from component to component without a hitch, unless the binding is magically happening somewhere else and that is dead code.
When I put my bind(RPresenter.Display.class).to(R.class) in, it doesn't seem to do the binding. The only output I get in the GWT console is "Button clicked" which is called in the view and then nothing further. I'm clearly missing something, any ideas?
The call to GWT.log() will not output anything from an AbstractGinModule - classes that extend AbstractGinModule (ScaffoldModule in your situation) are used by gin at compile time to decide which concrete implementations to use for injected interfaces. From the rest of your description (i.e. that the UI shows up in the application) it appears that your dependency injection is working correctly.