Accessing text in the GWT TextBox from ChangeEvent - java

I'm dynamically generating a form based on data received from an RPC call into a FormFieldData object which has details about the field to be rendered such as, Field Name, expected length and type of input, if the field is a required field or not and valid input Regex in some cases etc.
I'd like to be able to perform validation on the field depending on above attributes.
Here's an example:
private void renderTextField(FormFieldData field){
FormGroup formGroup = new FormGroup();
FormLabel formLabel = new FormLabel();
if(field.isRequired()){
formLabel.setText(field.getName()+"*");
}else{
formLabel.setText(field.getName());
}
formGroup.add(formLabel);
TextBox textBox = new TextBox();
textBox.addChangeHandler(new ChangeHandler(){
#Overrride
public void onChange(ChangeEvent event){
//TODO - find a way to get the text entered in TextBox
// and perform validation on it
//and set the TextBox Style to "Validation-error"
}
});
formGroup.add(textBox);
form.add(formGroup);
}
There're similar methods to render dropdowns, Numeric fields, radio button fields etc. which would need similar validation.
The problem is I can't access the text from the TextBox inside the onChange method without declaring it final, which I can't do because I might be rendering multiple text fields. I don't know much about ChangeEvent and if there's a way to get the text from the that.
I'd really appreciate any pointers to a way to do this in real time as the data is entered into the form, other than having to iterate through the fields and their corresponding FormFieldData object when the form is submitted.

First off, you can make the variable final, no problem.
If you don't want to do that for whatever reason, you can get the TextBox from the event like this:
textBox.addValueChangeHandler(new ValueChangeHandler(){
#Overrride
public void onValueChange(ChangeEvent event){
TextBox box = (TextBox) event.getSource();
// Do whatever you need to here
}
});
You are probably also looking for ValueChangeHandler instead of ChangeHandler.

Related

Vaadin 14 grid with checkboxes as rendered components

I created a grid that has four columns. The first column shows a name, the other three columns represent different roles. Each of those three columns is filled with check boxes in order to assign a specific role to a specific name. That's as far as i have come so far.
In each column and in each row there should only be one selected checkbox allowed. So in total i do have exactly one selection per checkbox column. How do i implement this?
Edit: I realize I might have misunderstood the question entirely. If you want to have 3 columns, with each having multiple CheckBoxes where only 1 can be selected per column, then you should use a RadioButtonGroup in each column and bind each to a different Enum field of your griditem class.
Instead of showing how to do three columns with a CheckBox in each, while only one CheckBox can be selected, I will show a different way to achieve the same information about the item.
The reason for this is the solution that what you want to achieve is not easily doable, since each CheckBox is defined in a scope that does not know the other Checkboxes of the same item. Therefore you would need to implement your only-one-selected rule inside the itemclass' setters, which is not optimal. I mean, it is possible, but I'd rather change the structure to something more apt. Usually you don't want to put this kind of business logic into your bean classes.
How would I solve the problem at hand?
Create a new Enum, which will replace all 3 boolean fields in your item class. Now in your grid you will only need one column for a ComboBox to select the enum.
I chose an Enum because this matches your needs here perfectly. With an Enum, you may have several options, but you can select only one (or none).
To better show what I mean, let's use an example Class for the Grid items, Foo. Your version has 3 booleans which your three grid-CheckBoxes are bound to. Let's call them isA, isB, isC.
// your version of the griditem class
public class Foo {
private boolean isA, isB, isC = false;
// constructor, getters, setters
}
// how the columns are added in the grid (approximately) (without editor):
Grid<Foo> grid = new Grid<>();
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isA());
checkBox.addValueChangeListener(event -> item.setA(event.getValue()); // inside setA() method you need to set isB and isC to false if the new value is true. No good!
return checkBox;
});
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isB());
checkBox.addValueChangeListener(event -> item.setB(event.getValue()); // inside setB() method you need to set isB and isC to false if the new value is true. No good!
return checkBox;
});
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isC());
checkBox.addValueChangeListener(event -> item.setC(event.getValue()); // inside setC() method you need to set isB and isA to false if the new value is true. No good!
return checkBox;
});
And here is how it would look after my changes
public class Foo {
private AbcEnum abcEnum = null;
// constructor, getters, setters
}
public Enum AbcEnum {
A,
B,
C;
}
// how the columns are added (without editor):
Grid<Foo> grid = new Grid<>();
grid.addComponentColumn((item) -> {
ComboBox<AbcEnum> comboBox = new ComboBox<>();
comboBox.setValue(item.getAbcEnum());
comboBox.addValueChangeListener(event -> item.setAbcEnum(item.getValue()));
return comboBox;
});
I wrote "without editor" in the comments about adding the column, because this code will add the ComboBox/CheckBox as clickable and functional components for each grid item, without needing to open the editor for an item to change the values. If you are indeed using an editor, you can add these functional inputs as editorComponents (and bind them to the editor-binder instead of using setValue and addValueChangeListener), and show only the current value in the normal columns (not editable - therefore no inputs like CheckBox or ComboBox are required)

Want to convert java struts 2 static code into dynamic

I have been assign to one struts2 project and its one of jsp contains more than 100 radio buttons and they have handled in statically not dynamically. As jsp contains 100 radio buttons so I am able to see the below list of radio buttons catches in actions with their getter and setter
List selectRadioList001
List selectRadioList002
List selectRadioList003
List selectRadioList004
etc
List selectRadioList100
I want to add these radio button in a list dynamically iterating through 1 to 100 something like below but when I try to access the variable like "searchBoxSelectRadioList"+i then it is pretending like a simple string. I want it to be like a List as shown above.
public class SelectRadioListPOJO {
private List<TicketDesignUtil> selectRadioList;
public List<TicketDesignUtil> getSelectRadioList() {
return selectRadioList;
}
public void setSelectRadioList(List<TicketDesignUtil> selectRadioList) {
this.selectRadioList = selectRadioList;
}
}
Action code:
List<SelectRadioListPOJO> selectRadioListPOJOList = new ArrayList<>();
SelectRadioListPOJO selectRadioListPOJO;
for (int i = 1; i <= 100; i++) {
selectRadioListPOJO = new SelectRadioListPOJO();
selectRadioListPOJO.setSelectRadioList("searchBoxSelectRadioList"+i);// ERROR
selectRadioListPOJOList.add(selectRadioListPOJO);
}
It's not clear what you're asking.
You can't pass arbitrary values to methods; setSelectRadioList takes a list of TicketDesignUtil.
If your action doesn't have getters and setters for all of those radio buttons then you should resort to accessing the request parameters directly, for example, via ParameterAware.
You would then access the radio button parameters by name from the injected parameter map.
Notes:
It's not "pretending" to be a simple string, it is a simple string, because... well, because it is.
Your for loop is wrong; I corrected it in your question to avoid others commenting on it. The POJO should be added to the POJOList inside the loop.
Naming is funky; just call it selectRadioListPojos. Better yet, name it something domain-specific: variables should be semantically meaningful, not just a description of the class(es) involved.
These shouldn't be static in the first place, but a map or array.

Dynamic Fields on JSF

I need to perform a web service (method) call just before load of a jsf page. The call will return a list of input fields that have to be displayed on my jsf page. The user can fill in the form and on click of next I need the values entered on the form to be sent back to another web service (method).
My approach was to have a request scoped bean for the jsf page(which consists of a blank form and a binding to the bean), and perform the web service call in the setter method of my form method and dynamically create UIInput fields
//call web service
//Loop
UIInput input = new HtmlInputText();
//set unique Id
form.getChildren().add(input);
//End Loop
It does create the Input fields, but if I perform browser back or refresh it keeps adding Input fields. So clearly my approach is wrong.
Also I found out that when I try to get the values for these dynamically created Input fields on Submit action like
List<UIComponent> dynamicFields = form.getChildren();
for(int i=0;i<form.getChildCount();i++){
if("javax.faces.Input".equals(componentFamily)){
UIInput input = (UIInput)dynamicFields.get(i);
System.out.println("Input Field: ID = "+input.getId() + " , Value="+ input.getValue());
}
}
The Id of the fields is printed properly, However value is always null. Clearly doing it all wrong.
Kindly let me know when and at what point do I create fields and how do I capture those values
P.S Am using JSF 2.0, Jdeveloper, Glassfish and/or Weblogic Server
From your question I was not able to be sure about what kind of data you expect to get from your webservice, and what kind of component you want to render it in. My answer below assumes that you will always receive a list of String and that you will display them in text boxes.
One possible approach would be to call your webservice and fetch the data in a #PostConstruct method, put this data into a list, and then render the data in a data table. Code below.
Bean:
#ManagedBean(name="bean")
#ViewScoped
public class YourBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> values = new ArrayList<String>();
//The method below #PostConstruct is called after the bean is instantiated
#PostConstruct
public void init(){
//fetch data from source webservice, save it to this.values
}
public void save(){
for(String s: this.values)
// send s to destination webservice
}
public List<String> getValues(){
return this.values;
}
public void setValues(List<String> values){
this.values = values;
}
}
XHTML excerpt:
<h:form>
<h:dataTable value="#{bean.values}" var="s">
<h:column>
<h:inputText value="#{s}" />
</h:column>
</h:dataTable>
<h:commandButton value="Save" action="#{bean.save}" />
</h:form>
This problem because the scope of your bean you are binding on it if it is #RequestScoped this means that every time you refresh or call the page you are going to invoke the post constuctor (#PostConstuct) method again so do the job of the creation again and for the null value of the input fields you should add to every input field value expression to store the value in it.
private String inputValue; //setter() getter()
UIInput input = new HtmlInputText();
#PostCostruct
public void addInput()
{
// your previos create and add input fields to the form + setting value expression
Application app = FacesContext.getCurrentInstance().getApplication();
input.setValueExpression("value",app.getExpressionFactory().createValueExpression(
FacesContext.getCurrentInstance().getELContext(), "#{bean.inputValue}", String.class));
}
The correct answer if you are using binding don't use Request scope use Session Scope and it will work with you and get data not null when retrieve values.

Inputs in Play! for date and time?

I need to have 2 inputs in my form, one for date and one for time. In my model it is just one property of type java.util.Date. What is the best practice to handle generating the html and binding the input fields to the date property in the model using Play framework 2?
Note, if I use field constructors, I can't lay out the form the way I need to. I want a label on the first line, the 2 inputs on the second line, and validation errors on the third line. Should I just use raw html instead? If I do, will I still have access to validation errors and constraints?
It'd be certainly easier to bind if you were using two separate fields in your model. One idea would be to create an intermediate class which binds to the form submission.
// Controller
public static class FormSubmission {
public Date date;
public Date time;
}
public static Result submitForm() {
Form<FormSubmission> filledForm = form(FormSubmission.class).bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest();
} else {
ModelClass model = new ModelClass(); // fetch first if you update
// Copy all values from form submission to the model
model.dateAndTime = combineDateAndTime(filledForm.get().date, filledForm.get().time);
}
return ok();
}
// View
#(form: Form[FormSubmission])
...
(I know this doesn't help, but tasks like this are extremely trivial in Scala.)

vaadin form cannot hide null fields

I have some vaadin forms which are reloaded after an event. The event causes some of the fields to become null. This means the form displays the label for the field and "null". I would like both the label and "null" value to disappear after the event is loaded. I would also like to do this without loading a different form to replace the form with the null values.
I have the following constructor for the form:
public IInfoForm(Info info) {
List<Object> orderedProperties =Arrays.asList(InfoContainer.DISPLAYED_FIELDS);
setItemDataSource(new BeanItem(idInfo), orderedProperties);
The info container retrieves the data from a webservice and DISPLAYED_FIELDS lists the fields to display.
I have tried looking for properties to set on the form fields but to no avail.
You can set for any field a special value that stands for the null value. Example:
TextField textfield = new TextField();
textfield.setNullRepresentation("abc");
So instead of null "abc" is represented in the text field.
Create the fields by yourself and add them or add a FieldFactory to the form.
Form form = new Form();
form.setFormFieldFactory(new FormFieldFactory() {
public Field createField(Item item, Object propertyId,
Component uiContext) {
Field field = DefaultFieldFactory.get().createField(item,
propertyId, uiContext);
if (field instanceof TextField) {
((TextField) field).setNullRepresentation("abc");
}
return field;
}
});
I would try to do so that I remove the fields with null value from the orderedProperties list and call
setItemDataSource(new BeanItem(idInfo), orderedProperties);
with that list.

Categories

Resources