How to use a method with SafeHtml param instead of String in UiBinder (XML)? - gwt

We're migrating our application from GXT 3 to GXT 4 (and also from GWT 2.5 to 2.8.2) and one of the things that changed is that lot of components' body/text/heading/etc. now have two separate setters. One accepts String param and the other one accepts SafeHtml.
Here's an example:
public void setToolTip(SafeHtml html) {
...
}
public void setToolTip(String text) {
...
}
The difference in those is that the method accepting String does not render html elements. The other one, however, does, which is perfectly fine if one uses Java code to build UI.
Unfortunately, we do have a lot of our UI built using GWT's XML method and I would like it to stay this way.
The problem is that I cannot figure out how to show e.g. a tooltip with SafeHtml body. When I try to do that I get compilation errors.
This is what I put in my XML file:
...
<form:TextArea ui:field="testField" toolTip="{messages.testMesssage}" >
...
And this is the error:
[ERROR] java.lang.String required, but {testMess.test} returns com.google.gwt.safehtml.shared.SafeHtml: <form:TextArea toolTip='{messages.testMesssage}' ui:field='testField'> (:184)
Thanks!

You may want to look at the GXT examples for tooltips in UiBinder. In there, it shows using the ToolTipConfig instead of trying to set "tooltip" directly.
<ui:with type="com.sencha.gxt.widget.core.client.tips.ToolTipConfig"
field="toolTipConfig">
<ui:attributes title="Information" body="Prints the current document" />
</ui:with>
...
<form:TextArea ui:field="testField" toolTipConfig="{toolTipConfig}" />

Related

How do you use in GWT UiBinder XML? Can you escape it?

In my mark-up I want to add a space ( ) between elements without always having to use CSS to do so. If I put in my markup, GWT throws errors. Is there a way around it?
For example:
<g:Label>One </g:Label><g:Label>Two</g:Label>
Should show:
One Two
And not:
OneTwo
As documented here, you just have to add this to the top of your XML file and it will work!
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
Note that the GWT compiler won't actually visit this URL to fetch the file, because a copy of it is baked into the compiler. However, your IDE may fetch it.
Rather than use a Label, which to me shouldn't allow character entities at all, I use an HTML widget. In order to set the content, though, I find I have to do it as the HTML attribute, not the body content (note that the uppercase HTML is important here, since the set method is setHTML, not setHtml)
<g:HTML HTML="One&nbsp;" />

How to detect the browsers language in the EntryPoint SmartGWT

I'm developping a project with SmartGWT and I want to make it international,but how to know the browser's language in my EntryPoint (onModuleLoad) ?
I'm using Spring in the Server Side, and in my onModuleLoad I'm sending an RPC call to my Service which is gathering data from properties Files and respond with a Map contains all my keys value for internationalization, so while creating IU Widgets I'm using my Map like this lables.get("myLabel").
now that everytings is working fine, I want to detect the browser's language and use it for querying the right properties.
Sorry if my english is so bad
com.google.gwt.i18n.client.LocaleInfo#getCurrentLocale()
then I ended with a dirty solution, I created a javascript function as Lt_Shade told me,which put the navigator.language, then I retreive it in my EntryPoint thanks to the Document object, the probleme was when I run with IE or FireFox, the EntryPoint run before my HTML Page, so before my Javascript function and I dont find my navigator.language, so what I did is to create a JSNI function which call my javascript object within my EntryPoint (Calling Javascript function from java:gwt code) so I emphasize and insist the call then I'l sure that my navigator.language is loaded and I can retreive it.
in my HTML page
....
<script language="javascript">
function loadLocaleLanguage(){
document.getElementById("localeLanguage").setAttribute("value",(navigator.language).substring(0,2); // I don't need Country code
}
</script>
...
<input type="hidden" id="localeLanguage" value="" />
....
in my EntryPoint
public static native void getLocaleLanguage() /*-{
$wnd.loadLocaleLanguage();
}-*/;
public void onModuleLoad(){
// I call my javascript function to ensure that the input have my localeLanguage
getLocaleLanguage();
// now I'm retreiving it using DOM thanks to Document object
String localeLanguage = Document.get().getElementById("localeLanguage").getAttribute("value");
....
}
I'm sure that's not the best solution but at least it works
if someone can help me find how to acces the httpRequest within the EntryPoint (it containts the accept-language) or to directly acces the navigator.language in the EntryPoint, it'll be better.
thanks guys
I am not sure if this helps but there is a question like this which includes some java code for getting the language in a Java servlet.
Automatically selecting country and language for user in Java Servlet
You could also try add a JavaScript function into your EntryPoint something like
var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;
alert ("The language is: " + userLang);

GWT Templates & GXT Xtemplates - how to sanitize display fields

I'm just getting into using #Template and #XTemmplate within my code.
I'm always concerned about user originated content vs trusted content.
This is the sample from GWT SafeHtml
public interface MyTemplates extends SafeHtmlTemplates {
#Template("<span class=\"{3}\">{0}: {2}</span>")
SafeHtml messageWithLink(SafeHtml message, String url, String linkText,
String style);
}
This is the sample from the GXT XTemplate doc
public interface SampleXTemplates extends XTemplates {
#XTemplate("<div>Hello, {name}!</div>")
SafeHtml hello(String name);
}
In either, is there a way in the template to declare a display field to be sanitized or trusted?
Something ala HtmlSanitizer
All String values rendered in an XTemplate/SafeHtmlTemplates are automatically sanitized - try passing in <, >, or & characters. This is true whether they are passed in to an attribute or the space between tags. If GWT cannot enure the safety of such a string (such as passing it into the href attribute of the a tag, or into a style attribute), it will emit a warning. Your first example is likely to get such a warning.
SafeHtml instances can be passed in to declare 'I know that this string is safe'. Typically, you create those using SafeHtmlUtils, a SafeHtmlBuilder instance, or another template of some kind.
Safe urls can be SafeUri instances - create them with UriUtils. This will get around the possible warning in your first example.
Safe styles can be SafeStyles instances - create them using SafeStylesBuilder instances or static methods in the SafeStylesUtils class.
And finally, as an implementation detail, XTemplates are generated by parsing out the logic and the named parameters and getters, and turned into SafeHtmlTemplates before generated into JavaScript. This ensures that anything that SafeHtmlTemplates will make safe will also be safe in XTemplates.

How do I set a GWT UiBinder element's inner HTML using SafeHTML instead of a String?

I have the following element created by UiBinder:
#UiField UListElement phones;
With the following markup:
<ul ui:field="phones" class="contact section"></ul>
I had previously been using the method setInnerHtml(String) to set the value. For example:
phones.setInnerHtml("<li><span class='title'>" + title +
"</span><div class='phone'><a href='tel:" + number + "'>" +
number + "</a></div></li>");
I would now like to use SafeHtmlTemplates to reduce the possibility of having XSS (cross-site scripting) problems. Using a SafeHtmlTemplate, I now get back SafeHtml which I would like to stick into my phones element variable. How do I do this? I don't see a method that would take a SafeHtml type variable.
To use SafeHtml you have to stick to GWTs widgets. More specific:
With the introduction of the com.google.gwt.safehtml package, all of the core GWT library's widgets that take String arguments that are interpreted as HTML have been augmented with corresponding methods that take a SafeHtml-typed value. In particular, all widgets that implement the HasHTML (or HasDirectionalHtml) interface also implement the HasSafeHtml (or HasDirectionalSafeHtml, respectively) interface.
If Ui:Binder should generate safe html you have to set the following property in your module xml:
<set-configuration-property name="UiBinder.useSafeHtmlTemplates" value="true" />

Custom tags in UiBinder files

When using a <g:LayoutPanel> in UiBinder.ui.xml files, you can specify <g:layer> tags. Some other Google-built widgets have special tags like that as well - <g:tab> even has a sub-tag, <g:header>.
How can I specify these for my own widgets?
The new answer to this question, after some GWT improvements, is at https://stackoverflow.com/a/11785903/439317 . Copied below to avoid moderator deletion (maybe?).
You can use #UiChild to declare special functions in your widgets accessible in UiBinders.
for example,
class MyPanel extends AbsolutePanel {
#UiChild
public void addAt(Widget w, String parameter1, String parameter2) {
....
Then, in your uiBinder, you can say
<custom:MyPanel>
<custom:at parameter1="HI" parameter2="Anything you like!">
<g:AnySingleWidget />
</custom:at>
</custom:MyPanel>
See #UiChild at http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/uibinder/client/UiChild.html
What you're looking for is a custom element parser for UiBinder. See this issue. Unfortunately it's not supported yet.
You might be interested in this post for some guidance on how to extend the current parser on your own.