GWT Widget ID and HTML standard - gwt

I have a custom widget (OrderItem) in a GWT project. This widget has a TextBox. I set it's id to "Navid". But what if I create multiple instances of OrderItem in a panel? The id would be repeated then. This'd make the html invalid.
How do I assign a unique id to the TextBox?

Well, normally, GWT widgets generate their own IDs that will automatically be unique and you would not typically worry about what the ID is. When you say that you are setting the ID of a TextBox, I'm assuming that you're calling something like myTextBox.getElement().setId("Navid").
There are two simple methods I can think of, depending on your use-case. The HTMLPanel class has a static createUniqueId() method on it that you can use either on its own, or to easily create a unique id. Like myTextBox.getElemement().setId("Navid-" + HTMLPanel.createUniqueId()). The only problem with this is that the ID that is generated is not deterministic.
The other common method would be to generate an ID based on the ID of the parent widget. myTextBox.getElement().setId("Navid-" + myTextBox.getParent().getElement().getId()).
However, I'm going to take a guess here and assume that the reason why you're wanting to assign your own custom ID to this widget is so that you can address it from outside of your GWT code, from Javascript, for example, from JQuery. In this case, I would recommend that instead of assigning an ID to it, which has to be unique to be useful, that you instead assign an html class name to the widget's element. You would then address the widget's element relative to the id, or class of your OrderItem's id or class. You can add an html class name to an element as in the example myTextBox.getElement().addStyleName("navid")
So, assuming that you assign an html class of orderItemWidget to the root HTML element of your OrderItem widget, and an html class of navid to the TextBox, you could refer to the textbox from a JQuery with the selector ".orderItemWidget .navid"

This is why GWT makes it doesn't make it that easy to set an ID on a widget. For styling, it makes it much easier to use CSS class names. Actually, there are very few reasons to use an ID to begin with, and almost none for dynamically generated things (like widgets).
Now, to answer your question, browsers tolerate the case of several elements sharing the same ID, and getElementById is defined to return the first of those elements (in document order)

Related

How to extract fieldname of PdfFormField from PdfAnnotation

So I create a radio group using this PdfFormField.createRadioButton() then calling the setFieldName().
However, the PdfAnnotation does not show any keys that stores the field name. I looked at the other dictionaries inside the PdfAnnotation but could not find any.
reader.getAcroFields().getFields().keySet() does list the field names of the form fields but I wish to ask if there is any way via PdfAnnotation?
I tried to put a custom PdfName inside the radio group object but it does not show up in the PdfAnnotation's dictionary.
You are confusing the concept of an annotation (link annotation, file attachment annotation, widget annotation,...) and a form field (text field, choice field, button field, signature field).
In iText 5, annotations are dealt with in a class named PdfAnnotation; form fields are dealt with in a class named PdfFormField. You are trying to do something that is specific for a PdfFormField using the class PdfAnnotation. That's wrong.
I understand the root of the confusion: every visible form field corresponds with at least one widget annotation. Most of the visible form fields correspond with exactly one widget annotation. That's why we made a design choice in iText 5 to have PdfFormField extends PdfAnnotation.
This design choice is in line with the PDF specification where it says that field dictionaries of fields that correspond with a single widget annotation may be merged into a single PDF dictionary.
In practice, you will find PDF dictionaries in a PDF that combine entries typical for a widget annotation dictionary and a field dictionary. (That also explains why there's a getMerged() method in iText: that method gets you the merged dictionary objects.)
I hope this already explains part of your problem. You seem to have another problem too, but I don't understand what you want to do. Please clarify using references to ISO-32000-1 so that people can understand which technical feature you are trying to implement.

In GWT, How to use custom widget tag in an .ui.xml file with and without parameters for the tag in the same file

I am creating a custom widget, say "CustomWid" in UiBinder.
And in CustomWid.java file I am writing two constructors
one with zero args like
CustomWid(){....}
another with some args like
CustomWid(String a,String b){......}
So,Now I am using my custom widget in another .ui.xml file,in that .ui.xml file
it is working fine when we give
<my:CustomWid/> alone,
and also fine when we give like
<my:CustomWid a="srt1" b="str2"/> alone
But "MY PROBLEM" is whenever I am trying to give both the tags in the one .ui.xml as
<my:CustomWid/>
<my:CustomWid a="str1" b="str2"/>
Now it is throwing error when i am using both types of tags in a single .ui.xml
I mean How to use my custom widget tag like a prdefined tag?
I am using #uiConstructor, but it showing error
Please developers... I need answer as early as possible
UiBinder will only ever use a single constructor for a given widget: either its zero-arg constructor, or a #UiConstructor (I'm surprised that you say it works when using either one or the other call but not both: one should fail in every case, and one should succeed in every case; if you haven't annotated a constructor with #UiConstructor, then <my:CustomWid/> should always work and <my:CustomWid a="str1" b="str2"/> should always fail)
There are two solutions here:
use setters for the a and b attributes (void setA(String a) and void setB(String b))), and possibly check later (say, in onLoad or onAttach) that you have either none or both of A and B, but not one without the other (if that's your rule).
use #UiField(provided = true) when you need to use the other constructor (if you choose to have UiBinder use the zero-arg constructor –i.e. no #UiConstructor–, then that means you'll have to move the a="str1" b="str2" from the XML to the Java code: #UiField(provided = true) CustomWid myCustomWid = new CustomWid("str1", "str2")).
The first option has my preference.
It Will not show any errors...'
#UiConstructor
public Component(String displayText,String heading)
{
initWidget(uiBinder.createAndBindUi(this));
this.displayText.setText(displayText);
this.heading.setText(heading);
}`
now use another constructor with default parameters also it will work
public Component()
{
initWidget(uiBinder.createAndBindUi(this));
}
now if you add with xml parameters component and without parameters also works in the same page.

What does wicket:scope attribute do?

There's no documentation for it https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html#Wicket%2527sXHTMLtags-Attributewicket%253Ascope. Does it work only for components?
What does wicket:scope attribute do?
The HtmlHeaderContainer class documents the wicket:scope attribute:
wicket:head tags (components) must only be added once. To allow for a little bit more control, each wicket:head has an associated scope which by default is equal to the java class name directly associated with the markup which contains the wicket:head. It can be modified by means of the scope attribute.
It is further documented in the HeaderPartContainer class simply stating that it is "A kind of namespace."
Does it work only for components?
The wicket:scope attribute is used only for the tag. The tag should only be used in "Panels, Borders and inherited markup (of Panels, Borders and Pages)", as documented in the HtmlHeaderContainer class level javadoc.
In short, the answer to your question is no, the attribute can be used within tags inside of Pages that inherit markup from a parent. The implication here is that a tag only makes sense where you wouldn't just use a tag (meaning in the base page html file).

Gwt iterate on text controls in a Composite

Can one iterate over all the textbox controls defined in a composite widget?
As in I need to extract values of all textboxes to check if they exist - the textboxes should have some data in them.
So I was hoping to have a method like:
for(Widget w: this.getChildren)
{
//if widget is a textbox - check value
}
Composite class does not have a method like getChildren neither a method where I can get elements of a given class - or name and if I get all the elements using NodeList then I need to recursively go until I find a textbox. Is there a more appropriate way to do this?
As the author of the Composite subclass, you can enable this kind of behavior by implementing HasWidgets (or, more specifically, something like getWidgetIterator()).
There's no way to do this for an arbitrary Composite.

Is it possible to get the ui:field value in java code in GWT?

This may sound very weird, but let's start with an example:
<my:MagicWidget ui:field="someFieldName" fieldName="someFieldName"/>
It's pretty much asured that we'll always want to have the same value in ui:field and in fieldName. Clearly there is some duplucation in this code, I'd like to avoid it and make the fieldName optional.
So, this is what I have in the widget's code:
#UiConstructor
public MagicWidget(String fieldName) {
this.fieldName = fieldName;
}
But I'd like, if possible to allow this constructor to be optional, and provide an default constructor that would "by magic" find out it's ui:field value:
#UiConstructor
public MagicWidget() {
this.fieldName = /*some magic to get ui:field's value*/;
}
I was wondering if there is a way to get the value of "ui:field" inside my MagickWidget? (The widget extends Composite). I fear this might not be possible, because most of the time it's not so useful, but if anyone has an idea - feel free to share!
PS: I'm using GWT 2.1.0.RC1.
As you may know, the ui:field is there so you can interact with a UI Object in Java code after you've declared it with UiBinder. So, for example, if you add a MagicWidget in a UiBinder template, you can write
#UiField MagicWidget someWidget
in order to be able to interact with it programatically. Having your magic widget aware of the name of the reference that is pointing to it might not be all that helpful (or possible), as you can pass the reference to that specific MagicWidget back and forth between different parts of your application. A single MagicWidget could easily have several references with different names pointing at is simultaneously. That's why it's difficult to pick it out "by magic" at runtime. I realize this isn't much of an issue if you only want this value when the object is constructed, but keep in mind that you're not required to include a ui:field when you add a widget using UiBinder.
Why is it important that the Widget know its field name? Knowing that might make it easier to provide suggestions about other ways to accomplish what you are looking to do.