Iceface: reset old values while update - java

I have information of user in bean, and I want to update this user.
but my problem is: when the value of inputtext changed I want to put validation on it.
and if the new value is wrong I want to reset the old value.
please can any one help me

You must have ValueChangeListener property in your "InputText" tag.
In your method, declared as listener you have ValueChangeEvent object wich contains old value. You can do something like this:
public void myValChanged(ValueChangeEvent event) {
try {
validate(event.getNewValue());
myValue = event.getNewValue();
} catch (Exception ex) {
/*
Listeners are called before update model values in the request lifecycle so any changes you make in that phase are overwritten by the actual values in the page.
By changing the event's phase to UPDATE_MODEL_VALUES or INVOKE_APPLICATION your changes will overwrite those currently set in the page, which is what you need.
*/
myValue = event.getOldValue();
if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
return;
}
}
}
The idea with PhaseId operations is - not to allow your ValueChangeListener override your
variable set "myValue = event.getOldValue(); "

Related

Get a variable in CaretListener (like getActionCommand)

I want to pass a variable into a CaretListener when my JTextFiled is update.
My listener work perfectly thanks to "caretUpdate".
However, I don't know how to pass a variable to my "caretUpdate", like "e.getActionCommand()" when I used "actionPerformed" but it's seem to not exist for "caretUpdate", is there a solution ?
Edit :
In My view I have a textField witch is attach to a listener :
textField.setActionCommand("test");
textField.addCaretListener(this.controller);
In My controller I have the listener, and I want to get the value "test" :
public void caretUpdate(CaretEvent e) {
// here I want to get the "test" value
String maVar = e.getActionCommand();
}
But e.getActionCommand() doesn't exist for CaretEvent. Is there an alternative ?

Getting all return values of LOV programmatically

I'm currently working on a CRUD application and I have defined a LOV like this:
My question is how can I get all these return values in for example a ValueChangeListener defined like this:
public void onValueChanged(ValueChangeEvent ev){
BindingContext bctx = BindingContext.getCurrent();
oracle.binding.BindingContainer bindings = bctx.getCurrentBindingsEntry();
DCIteratorBinding iterBind = (DCIteratorBinding)bindings.get("MpStavkeulazaView5Iterator");
System.out.println("Vrijednost je" + ev.getNewValue());
}
This code only gives me the value of the list attribute, but I want the other values too.
Any other info please tell me.
First of all - using backing bean's value change listener is not ideal for such use case:
Try instead the setter on your Row Impl for the same purpose.
Remember: if you can't test your use case from BC tester, your ADF design is flawed.
Second of all: your LOV can return multiple values:
http://adfbugs.blogspot.co.uk/2009/11/returning-multiple-values-from-lov-in.html
You can bind row attributes and then get values of this bindings or just get this attributes from iterator. If you going to handle it in valueChangeListener you'll have to process updates, before getting this values:
public void onValueChanged(ValueChangeEvent ev){
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iterBind = (DCIteratorBinding)bindings.get("MpStavkeulazaView5Iterator");
System.out.println("Vrijednost je" + ev.getNewValue());
ev.processUpdates(FacesContext.getCurrentInstance());
Row row = iterBind.getCurrentRow();
System.out.println("Proizvod: " + row.getAttribute("Proizvod"));
System.out.println("Jmjere: " + row.getAttribute("Jmjere"));
}
However its may be better to use Transient attribute in your ViewObject and do calculations there?

javafx choice box is empty on setValue()

In my project I have a table. When the user double clicks on the row, an editing dialog opens. On the opening this dialog I set values to fields, few of which are ChoiceBoxes. The type of ChoiceBox's field is a custom object, not a string.
The code that creates the ChoiceBox follows:
trade_point.setConverter(new TradePointConverter());
trade_point.setItems(FXCollections.observableArrayList(tradePointsService.getTradePoints()));
TradePoint currentTradePoint = tradePointsService.getTradePoint(typeOfPriceByTradePoint.getTradePoint());
trade_point.setValue(currentTradePoint);
Where trade_point is a choice box of TradePoint type. currentTradePoint is not null, when I look at trade_point's value equals currentTradePoint's value but in the dialog I see no value. Items are set correctly. in other case with same choice box filling everything is correct and here is not.
UPD: TradePointConverter class:
class TradePointConverter extends StringConverter<TradePoint>{
public TradePoint fromString(String name){
try {
List<TradePoint> tradePoints = tradePointsService.getTradePoints();
for (TradePoint tradePoint1: tradePoints){
if (tradePoint1.getName().equals(name)){
return tradePoint1;
}
}
}catch (SQLException e){
}
return null;
}
public String toString(TradePoint tradePoint1){
return tradePoint1.getName();
}
}
I am not sure if this converter is correct as far as converting from string by name is totally incorrect. I do not understand how to make it correctly visible to the user (to see the name of an object) and how to store its id to get the definite object at the same time. I can convert to string by getting an id but in this case user won't understand which object to choose.
trade_point is a ChoiceBox:
private ChoiceBox<TradePoint> trade_point = new ChoiceBox<TradePoint>();

How to set the DateField value to the Previous Value if certain condition fails in GXT

I am creating a DateField and adding listener to it.If certain conditions fails I need to reset the value of DateField with previous value which is there in that field.Below is my code
final DateField dateField1 = new DateField();
dateField1.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MMM/yyyy"));
dateField1.getDatePicker().addListener(Events.Select, new Listener<DatePickerEvent>() {
#Override
public void handleEvent(DatePickerEvent dpe) {
// Window.alert("Getting Roster Date here-->"+grid.getColumnModel().);
Window.alert("Getting RosterDate-->"+ caseStoreModule.getModifiedRecords().get(0).get("rosterDate"));
if(caseStoreModule.getModifiedRecords().get(0).get("rosterDate")!=null){
DateTimeFormat format = DateTimeFormat.getFormat("dd/MMM/yyyy");
rosterdate=format.parse(caseStoreModule.getModifiedRecords().get(0).get("rosterDate").toString());
nextdate.setTime(rosterdate.getTime()+(1000*60*60*24));
prevdate.setTime(rosterdate.getTime()-(1000*60*60*24));
}
checkInDate=(Date)(dateField1.getValue());
if(checkInDate.getTime()<rosterdate.getTime() || checkInDate.getTime()>nextdate.getTime()){
MsgBox.info("Enter valid Check In Date");
dateField1.setValue();//here i need to reset the value to the previous value.
return ;
}
}
});
If the if condition is true,then I need to put the previous value which is there in the Field instead of reseting it.Please suggest how to do this.
One solution is having a String variable that holds the field value before doing any manipulation. So if you need to reset the field just use it. Something like this
String tmpStringValue = dateField1.getValue();
...
if(something went wrong){
dateField1.setValue(tmpStringValue );
}
You can store values as an attribute of Element.
At first time this attribute oldValue will be blank.
Set this attribute every time if validation is successful.
For more information read inline comments.
Here is the sample code:
dateField1.getCell().getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
DateTimeFormat format = DateTimeFormat.getFormat("yyyy-dd-MM");
String oldValue = dateField1.getElement().getAttribute("oldValue");
if (oldValue == null || oldValue.length() == 0) {
// initially date field is empty
dateField1.getElement().setAttribute("oldValue",
format.format(event.getValue()));
} else {
Date oldDate = format.parse(oldValue);
Date newDate = event.getValue();
// you validation logic
if (oldDate.getTime() < newDate.getTime()) {
// revert back to last value if validation is failed
dateField1.getCell().getDatePicker().setValue(oldDate);
} else {
// set the old value with new one
dateField1.getElement().setAttribute("oldValue",
format.format(event.getValue()));
}
}
}
});
Call below line if value is initially populated by its default value just after setting the value.
dateField1.getElement().setAttribute("oldValue",
format.format(event.getValue()));
In GXT, every Field has a setOriginalValue(D originalValue) method which could help you in this matter too. You can always return to the original value by reset()ing the Field. Please keep in mind that reset() will clear the validation messages too!
How to use it in situations like this?
Set the original value when the entered value is validated fine
Call Field.reset(), if anything went wrong to reload the last working value in your field.

Wicket - AjaxFormComponentUpdatingBehavior and backspace

I have a TextField where I have added an AjaxFormComponentUpdatingBehavior to get the current value when user write some string.
filterByObject = new TextField<String>("filterByObject", true, new PropertyModel<String>(searchParams, "objectFilter"));
AjaxFormComponentUpdatingBehavior changeFilterBinded = new AjaxFormComponentUpdatingBehavior ("onkeyup") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
target.addComponent(componentToUpdate);
}
};
filterByObject.add(changeFilterBinded);
When I put some chars inside textfield, onUpdate method is correctly called and my component, based on the current state of searchParams, changes correctly.
Unfortunally when I use Backspace to cancel what I have inserted, the onUpdate is not called.
I tried changing event (onkeypress, onkeydown, onchange etc...) but it doesn't work. Only onChange works but I have to change focus to another component.
How can I save the day?
Is the input in the field invalid (according to setRequired or IValidators added to the field) as a result of pressing the backspace key? If it is, the onError method will be called instead of onUpdate, because user input will be invalid and therefore will not reach the ModelObject of the component with the AjaxFormComponentUpdatingBehavior.
AjaxFormComponentUpdatingBehavior changeFilterBinded =
new AjaxFormComponentUpdatingBehavior ("onkeyup") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
// Here the Component's model object has already been updated
target.addComponent(componentToUpdate);
}
#Override
protected void onError(AjaxRequestTarget target, RuntimeException e){
// Here the Component's model object will remain unchanged,
// so that it doesn't hold invalid input
}
};
Remember that any IFormValidator involving the ajax-ified component will not execute automatically, so you might be interested in checking the input for yourself manually before updating model objects if it's the case. You can tell AjaxFormComponentBehavior not to update model objects automatically by overriding getUpdateModel(). Then, in the onUpdate method, get the component's new input by means of getConvertedInput().
As a side note, onkeyup should be getting fired when pressing the backspace key. At least it does in this fiddle, and onchange is generally triggered on an <input type="text"> when focusing out of it.
Also, HTML5 introduces the oninput event handler, which may better suit your needs. It will get fired even when copying/pasting in the text field. See the following link for more information: Using the oninput event handler with onkeyup/onkeydown as its fallback.

Categories

Resources