Monday, June 3, 2013

Re: Dynamically generated HTML and GWT

Hi,
 
OK, I forgot about that part - I once had to go through these issues as well but forgot soon after.
 
You need to subclass HTMLPanel and SubmitButton to get what you want. The HTMLPanel only supports adding widgets to existing HTML elements, not just make an Element into a widget without detaching.
 
First you need to subclass the SubmitButton so that you gain access to the SubmitButton(Element) protected constructor. Use that constructor to create the SubmitButton instance.
 
You also need to subclass the HTMLPanel class, you need to add a method that contains something like this:

public attachWidget( Widget widget ) {
  // Logical attach.
  getChildren().add(widget);
  adopt(widget);
}

This method does not enforce any checks, so make sure that the widget is a wrapped one of an Element contained in the HTMLPanel.
 
Frankly I don't understand why the HTMLPanel and the Wrapping stuff does not allow you to do this. It is way more useful because you can bulk render and entire GUI and then later attach the widgets.
 
David
 
On Fri, May 31, 2013 at 11:05 PM, Mike Dee <mdichiappari@gmail.com> wrote:
Now, this has gone from fun to just downright frustrating.  I'm stuck at the point of finding and wrapping the submit button.  I get the ubiquitous, "A widget that has an existing parent widget may not be added to the detach list".  Looked through the dozens of posts regarding that message.  The most common response is "don't use wrap() the way are using it".  There's also, "that's not the use case for wrap()".  Maybe one of these is true in my case.

Code is fairly simple.

uibinder:
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui">
<g:ScrollPanel width="100%" height="100%" ui:field="mainScrollPanel"/>
</ui:UiBinder> 

The view class's onLoad() (this is an Activites and Places app that uses MVP).  So this view is on the right side of a splitterpanel.  The left side has a stackpanel (think of the mail example).  When the appropriate stack panel item is selected, this view appears on the right of the splitter.

public class ReviewView extends ResizeComposite
{
  ...

  @UiField ScrollPanel mainScrollPanel; 

private String html;  // Set in a separate method and called before onLoad().  This HTML comes from an external source.

  ...
public void onLoad()
{
mainScrollPanel.clear();
HTMLPanel htmlPanel = new HTMLPanel( html );
mainScrollPanel.add( htmlPanel );
Element submitButtonElement = htmlPanel.getElementById( "save" );    // This appears to work, without being deferred.  I tried it defered also.
Button submitButton = Button.wrap( submitButtonElement );                 // This is where the assert exception occurs.
}
}

Of course the HTMLPanel has a parent widget, the ReviewView. And it is nested inside of a ScrollPanel, which is nested in a SplitterLayoutPanel, which is inside a RootLayoutPanel.  There are probably widgets attached to all of those.  So, can this be done? One solution in the posts is to wrap() from the inside out.  With an A&P app this seems nearly impossible as Widgets are always embedded in Widgets.  What is the point of wrap() if it is so finicky()?  

I also tried deferred method, replacing HTMLPanel with MyHtmlPanel:

onLoad() looks like this:

MyHtmlPanel htmlPanel = new MyHtmlPanel( html );
mainScrollPanel.add( htmlPanel );

And MyHtmlPanel looks like this:

private class MyHtmlPanel extends HTMLPanel
{
public MyHtmlPanel( String html )
{
super( html );
}
protected void onAttach()
{
Element submitButtonElement = this.getElementById( "save" );    // Appears to work.
Button submitButton = Button.wrap( submitButtonElement );        // Same error occurs here.
}
protected void onDetach()
{
}
}




--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment