Exception while adding a widget to iframe? - java

I am using GWT. have below code in my host page.
<div id="mainDiv"/>
<iframe id="__printingFrame" style="width:0;height:0;border:0"> </iframe>
in EntryPoint's onModuleLoad() i have below code:
#Override
public void onModuleLoad() {
RootPanel.get("mainDiv").add(new SomePage());
}
in one of the methods of SomePage.java i am doing:
RootPanel rootPanel = RootPanel.get("__printingFrame");
rootPanel.add(new Html(" "));//adding some widget
But bcaz of above line i am getting below exception. Am i missing anything here?
java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
at com.google.gwt.user.client.ui.RootPanel.detachOnWindowClose(RootPanel.java:136)
at com.google.gwt.user.client.ui.RootPanel.get(RootPanel.java:211)
Thanks!

There are 2 issues in your code (but you're only seeing one for now):
You cannot have nested RootPanels (see http://code.google.com/p/google-web-toolkit/issues/detail?id=3511 and http://code.google.com/p/google-web-toolkit/issues/detail?id=3528), this is what's causing the error you're seeing
You cannot add widgets to an iframe. Well, wrapping it within a RootPanel, technically, you could, but they'd be added as children of the iframe element, so they wouldn't be visible to your users (I cannot think of a single browser that interprets JavaScript but doesn't support iframes).

RootPanel.get("id") can only get <div> element.you can wrap existed element like that.
Frame frame=Frame.wrap(DOM.getElementById("__printingFrame"));

Related

GWT History - Why does it change the focus of my screen?

I have a simple application structure that will contain three composites at any one time - my header, footer and content composites. The composites are laid out in the follow manner.
<body>
<div id="header">
<div id="content">
<div id="footer">
</body>
The composites are assigned to the three <div>'s in my EntryPoint. The EntryPoint also contains a History Handler as follows.
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.isEmpty()) {
parseTokens();
ContentContainer.INSTANCE.setContent(new IndexScreen());
}
else if(historyToken.equalsIgnoreCase("registration")) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
}
else if(historyToken.equalsIgnoreCase("login")) {
ContentContainer.INSTANCE.setContent(new LoginScreen());
}
}
});
History.fireCurrentHistoryState();
In my Composites, I have several ClickEvent handlers that look similar to the following.
#UiHandler("registerLink")
public void handleClick(ClickEvent event) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
History.newItem("registration", true);
}
This registers the history very nicely. It also brings me to the appropriate pages when I click the back button. However, it has the very odd effect of focusing the browser on the content composite, with the effect of the browser being scrolled to the bottom. It's not a deal breaker but it kind of breaks the user experience a little. (Do let me know if the explanation isn't clear, I'll add images)
What exactly is causing this and how can I fix it?
GWT history tokens work with the hash # token. In html this was originally intended to use the browser to focus on a specific part of the page, if an element has a name or id with the string after the # token. Could it be you have an id matching your history token?
Also in you handleClick you call setContent. But if I'm correct the call after it triggers a change event and will end up in the onValueChange, which also calls the setContent. So it looks like you are calling setContent twice here.

Hiding a <div> tag using wicket & eclipse

I've tried to hide a tag in wicket, I've managed to accomplish that only in the case that follows:
HTML
...
<div wicket:id="div_id">This is what hides</div>
JAVA
final WebMarkupContainer wmc = new WebMarkupContainer("div_id");
Method
public onSubmit(){
wmc.setVisible(false);
}
This manages to get the div hidden, but my div tag has another wicket components inside, and so I can't seem to get this working, error pops saying inner components are not defined in the component "div_id".
Any idea how to get this working?
Thanks guys!!
If you have other components inside that div, you have to add your other components to the markup container you have for the div instead of adding them to your page. What wicket is looking for is a match between the hierarchy on the Java and HTML part.
This is just a very wild guess, since I don't know how you add the inner components and what exactly fails, but if you add or modify them in onBeforeRender() and you're using Wicket 1.4.x, you'll need to override WebmarkupContainer.callOnBeforeRenderIfNotVisible() to return true...
For Wicket 1.5 this code should be moved to onConfigure()

Richfaces: Link-like text with ContextMenu

I want to render using Richfaces a context menu on left click on a link-appearing text (blue text, and underline and cursor onmouseover). So, imagine a link which when clicked shows a context menu. Note that I don't care if the text is indeed a link, I just want it to appear as a link. So, even normal text would be fine, I would make it appear as a link using CSS.
I have the following conditions:
The context menu must appear on client side, without making a request.
The context menu must appear using a rich:componentControl (these "links") are inside a datatable, so the same rich:contextMenu must be re-used.
I still have not found a satisfactory solution, as each approach I have tried has caused a problem for me:
If I use h:outputText (that would be ideal), I cannot attach on it a rich:componentControl (I guess because it cannot fire an onclick event).
If I use a4j:commandLink, although I can attach a rich:componentControl, it makes a server request. I tried to add onclick="return false;" to prevent the request, but Richfaces adds the JS generated by the rich:componentControl after whatever is inside the onclick, which causes this code not to be reached at all, and of course the context menu not to appear at all.
Is there any way to do this? Please remember, no request!
You may try
<rich:componentControl disableDefault="true" ...>
According to documentation with this param componentControl should add return false; itself.
But be aware of corresponding bug: RF-5607
In case documentation lies you may use html anchors. This answer shows how to create a link with componentControl and without page refresh:
<h:outputLink value="#" id="link" onclick="return false;">
<h:outputText value="Link text"/>
<rich:componentControl attachTo="link" for="panel" operation="show" event="onclick"/>
</h:outputLink>
The onclick="return false;" prevents the anchor from scrolling the page to the clicked link.

GWT adding Event Handler overrides the native one?

lets say i have the following html element on my host page:
<input type="text" onfocus="this.value = ''" id="textField"/>
In gwt i wrap this into an TextBox Widget like this:
final TextBox myTextBox = TextBox.wrap(DOM.getElementById("textField"));
If i now want to add a focus handler what will happen?
myTextBox.addFocusHandler(new FocusHandler() {
public void onFocus(final FocusEvent event) {
// do something
}
});
What i want to achieve is that first the javascript which was defined on the host page should be executed and after that my focus handler should be executed. But what actually is happening is that the javascript from the hostpage is not being executed and only the code within onFocus is executed. Is this behavoir normal? I would expect that add means actually add and not override. Is there a way to work arround this? Or do i maybe have any flaws which induce this behavoir. Any help is appreceated.
kuku

Calling JSF ManageBean after PageLoad using f:event postAddToView

I am writing an app using JSF 2.0.
For one of the page, there is a section of the page that takes a long time to display.
To improve the user experience, I am thinking to load the page first and then automatically do an Ajax call back to the JSF manage bean object once the page is loaded successfully after 1st load.
I am thinking to use f:event with type postAddView.
<h:outputText id="dummyId">
<f:event type="postAddToView" listener="#{mngBean.doSomething}" />
</h:outputText>
However it seems like f:event postAddToView is still being processed before the page is displayed for the first time.
The other options that I have explore is to create a hidden button and get javascript to trigger it. It works however I am just wondering if there is a nice JSF component/event that can do this instead of using java script.
Thanks for your help.
<h:commandButton id="dmyButton"
value="#{mngBean.getSomething}"
actionListener="#{mngBean.doSomething}"
style="display: none"
type="submit">
Java Script
<script language="JavaScript">
$(document).ready(function() {
if (document.getElementById('form:dmyButton').value == 'true') {
document.getElementById('form:dmyButton').click();
}
});
</script>
Thanks for all your help in advance
I think the PreRenderViewEvent is what you want.
http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/event/PreRenderViewEvent.html
Though the docs don't show it, the preRenderViewEvent does work with f:event. I'll fix the docs presently.
PostAddToViewEvent is processed "During Restore View Phase, after a
component has been added to a view." (according to Java Server Faces 2.0 - The Complete Reference) which means it is really early in the lifecycle (Restore View is the first lifecycle phase).
I don't think that any PhaseListener will help you in this case, since they all run on the server side, not on the client side.
However, <f:ajax> can be applied to <h:body>, with event="load". I tried it, but it didn't really work, <f:ajax> wrapping <h:body> nothing happened, the other way around I got Unable to attach <f:ajax> to non-ClientBehaviorHolder parent error.

Categories