Gwt iterate on text controls in a Composite - gwt

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.

Related

Internal mechanism of TreeViewer for updte

I am new to SWT and RCP I am trying to use TreeViewer.
By referring to some documents, I came to know there is method:
treeViewer.Updte(Object , Properties).
I need to know how SWT figure out which data is for which field.
The method is called update:
public void update(Object element, String[] properties)
Here element must be an object that equals one of the objects returned by the content provider for the tree.
If you have called
treeViewer.setUseHashlookup(true);
then a hash table (similar to HashMap) is used to find the tree element corresponding to the element. Otherwise the tree is just searched exhaustively to find the element.

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.

GWT Widget ID and HTML standard

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)

Spring List Binding in Form

I'm trying to bind a list/arraylist/hashmap/etc of custom objects to my form in JSP using Spring. Right now, the controller creates a Map of the two lists (Boolean list and custom object list) in referenceData(), and provides it to the form which uses those values to populate the fields. The values are initialized from a MySQL database using Hibernate, and all that works fine. The list is a known length before the form is initialized, so that part is easier. Now what I'd like to do is correctly bind those objects in the form, so that when there are changes made, I can detect that in onSubmit() (or wherever is appropriate), and update the database accordingly. I can't seem to bind them correctly in the form so that I can see changes made. I tried just using a list of the form fields as the model, but even that wasn't working correctly. Do I just need to inject the list in a particular way? Any ideas or examples here? Any help would be greatly appreciated.
UPDATE: At Ralph's request here is the solution I used:
In my data object class, I lazy loaded a map using MapUtils.lazyMap(), with a String key and other custom object value. The other custom object is just a class that contains List<String> and getters/setters. In the corresponding .jsp file, I just nest several loops to loop through the keys first using loop.current.key and then loop2.current.value.paramsList to loop through the values for that key. This was not really what I asked for in my original post, as I was looking for a more general solution, and the lazy loading pointed me in the right direction.
In Spring 2 you need a special List in your Command object, that is able to grow if one add the x-th element event if the list has not this size yet.
One way to do that is to use LayzList decorator from commons-collections.
#Override
protected Object formBackingObject(final HttpServletRequest request)
throws Exception {
List<PosterSelectionRow> posterSelectionRowList = LazyList.decorate(
new ArrayList<PosterSelectionRow>(),
new PosterSelectionRowListFactory());
return new PosterSelectionCommand(posterSelectionRowList);
//PosterSelectionCommand contains a list of selected poster rows
}
private static class PosterSelectionRowListFactory
implements org.apache.commons.collections.Factory {
/** Invoked if the list need a new element */
public Object create() {
return = new PosterSelectionRow();
}
}
When I remember right, there is a way without that Factory stuff, but I am not sure.

How to create instance to zend controller

I have a controller named class TestController which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically.
Please suggest how I can do this. Thanx in advance.
Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.
A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:
$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);
Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.
A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):
Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.