I'm having problems with the Wiquery DatePicker in the following code in a Wicket page (using a CompundPropertyModel, the date property is of type java.util.Date):
DatePicker<Date> datePicker = new DatePicker<Date>("date"){
#Override
public boolean isVisible() {
return selectedType.hasDate();
}
};
datePicker.setDateFormat(DateUtil.DATE_PATTERN); // pattern is "dd.MM.yyyy"
form.add(datePicker);
The HTML this is bound to:
<input wicket:id="date" id="date"/>
The problem is that when editing existing data, the input field displays the time along with the date, and when submitting the form, validation fails because this does not fit the pattern.
How can I get the DatePicker to display the current value correctly?
This seems to be a bug in the relatively obscure Wiquery DatePicker component. I've switched to org.apache.wicket.extensions.yui.calendar.DatePicker, which does not have this problem.
Being the DatePicker component a TextField, why not use a custom IConverter in it to return only dd.MM.yyyy in its convertToObject?
I haven't been able to see browsing its sources any registered IConverter or other way to convert input, so this will probably be conflicting with whatever is formatting input in this component.
UPDATE
After debugging this in a quickstart using WiQuery 1.2.4 and Wicket 1.4.17, it shows that the initial value of the DatePicker (which is a TextField) is the standard conversion performed by Component.getDefaultModelObjectAsString().
Since the TextField has a IModel<Date>, it will use whatever IConverter is registered for the Date class. In your case it might be using a custom IConverter that's formatting with the time. I'd try overriding DatePicker's getConverter() and using a SimpleDateFormat that honours the format specified in setDateFormat().
This issue gives a hint on that an IConverter should be specified along with the DatePicker: Issue 168: Invalid (or uncommon) date format for NL in DatePicker
You might also find this discussion on the Wicket users list useful: DatePicker to pick a year. Julien Roche (one of the owners of the project) states there that setDateFormat only works on the client side with JQuery:
I think you have to set the right converter on your wicket textfield (with
the override of the method getConverter and with the class
PatternDateConvert). The "dateFormat" option works only on the client side
with jQuery.
first take a look at https://cwiki.apache.org/WICKET/using-custom-converters.html
then you know that wicket convert you object to a text from that mechanic so.. all you have to do
Override newConverterLocator() method in Application class to provide custom ConverterLocator.
protected IConverterLocator newConverterLocator() {
ConverterLocator converterLocator = new ConverterLocator();
converterLocator.set(Date.class, new DateConverter());
return converterLocator;
}
tip: watch out with java.sql.Date class converter
Related
I am using Vaadin 15. In my application I am using a grid, which uses a dataprovider (data) to show a bunch of different columns. Further, I am using a filter to filter all columns. One Column consists of dates, with my current Filter Function i can not filter ranges.
I am very new to Java and programming in general. This is the filter function I use currently.
TextField cas=getFilters(casColumn,filter);
cas.addValueChangeListener(event-> data.addFilter(dataRecord-> StringUtils.containsIgnoreCase(dataRecord.getCas(), cas.getValue())));
Now I would like to also be able to filter ranges of dates. So in the filter TextField a date is given and everything till today will be shown, everything older than the date will not be shown.
Thanks for your help. Tipps for some literature on how to do it are also appreciated.
I am using Vaadin 15.
Just pointing out that it makes little sense to use Vaadin 15, as it is not supported anymore. The latest version Vaadin 21.
With the newest Vaadin versions if you are using in memory data provider Grid is set up using
GridListDataView<Data> dataView = setITems(data); // where data is a collection of
items
Then you can set the filter you use via dataView, say
textField.addValueChangeListener(event -> {
dataView.setFilter(item-> item.getProperty().equals(event.getValue));
});
Note, if you have data provider from callbacks, then above method does not apply for you , instead filtering is done as follows, i.e. providing a query that can use the value of the filter
TextField filter = new TextField("Filter");
filter.setValueChangeMode(ValueChangeMode.LAZY);
filter.addValueChangeListener(event -> {
grid.setItems(query -> personService
.fetch(query.getOffset(), query.getLimit(), event.getValue()).stream(),query -> personService.count(event.getValue()));
});
Im trying to put some validation in the date field. The condition is the effective date field should be less or equal to the current date and it should be 1st of month.
Im doing it in tapestry.The data type is DATE. im using tapestry as yo know you will have .html page a, .java file and .page file. Im doing it in the java file . So please help me on this.
When the form is submitted t5 emits various events during the different stages. On EventConstants.VALIDATE is a good place to perform more complex validations that are not supported by t5 out of the box, or to perform cross-field validation serverside.
#Component
private Form myForm;
...
#OnEvent(value = EventConstants.VALIDATE, component = "myForm")
public void onCreateEditValidate() {
// do validation and if any error record it
myForm.record(theDateField, "Dang, try again!");
...
http://tapestry.apache.org/forms-and-validation.html
You can use the onValidate event too as stated at the end of the link jon martin solas posted.
Something like:
void onValidateFromYouDateFieldId(..) throws ValidationException{
//your custom validations
}
you can check out this example for more information:
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/morevalidation
http://tapestry.apache.org/forms-and-validation.html#FormsandValidation-OverridingtheTranslatorwithEvents
I want to write an eclipse plugin for my computer science lecturer. It's a custom editor with an xml based file format.
I've implemented syntax highlighting and other stuff. But I stuck when it comes to the usage of annotations/marker to show invalid content.
This is how it looks like, if the content is valid:
image of valid conent http://image-upload.de/image/aPcsaa/6c799a671c.png
This is how it looks like, if the content is invalid:
image of invalid conent http://image-upload.de/image/4TdooQ/04d662f397.png
As you can see, invalid attributes will get marked, but the problem is, the whole formatting seems to be lost between these annotations.
I use org.eclipse.jface.text.reconciler.Reconciler for delayed parsing of the content to create the document model. The model is used to format the text and display annotations. All this happens in the void void process(DirtyRegion dirtyRegion) method of the Reconciler.
For text formatting I use <ITextViewer>.changeTextPresentation(textAttributes, false) and for annotation handling I use
IAnnotationModel annotationModel = <ISourceViewer>.getAnnotationModel();
IAnnotationModelExtension annotationModelExtension = (IAnnotationModelExtension) annotationModel;
annotationModelExtension.replaceAnnotations(oldAnnotations, newAnnotations);
Since the Reconciler does not use the swt thread I have to use this construct to avoid exceptions:
<ITextViewer>.getTextWidget().getDisplay().asyncExec(new Runnable() {
#Override
public void run() {
<ITextViewer>.changeTextPresentation(textAttributes, false);
updateAnnotations(nodes);
}
});
By the way ITextViewer and ISourceViewer are meant as the same viewer object.
As annotation type I've testet: org.eclipse.ui.workbench.texteditor.spelling and some others, also custom types, but all with the same result.
I'm not quite sure, what I do wrong, could it be because it is all in a single call?
I hope someone can help me with this problem.
Thank you in advance.
I'm using SmartGWT 2.5 with Java & Mozilla FF 3.6.x.
I want to open pickList of ComboboxItem or SelectItem manually that means programatically. Is it possible? It's OK if I need to use JavaScript to achieve this. Any hint or solution is appreciated.
I finally got the answer. Posting it here might be useful to others. I've used
comboxItem.showPicker();
to achieve manual opening of picklist of ComboboxItem.
In SmartGWT 2.4 (I didn't check newer versions), the showPicker() method of SelectItem does only show an empty div, not the pick list of the select item. (It does work for the ComboBoxItem, as mentioned by RAS' answer).
Some digging into the underlying SmartClient code showed that on the JavaScript side, there is a showPickList() method which is called when the icon is clicked (or on some other events), but this is not exposed by the Java class.
So I used a piece of JSNI (modified from the source code of SelectItem.showPicker) to call this method:
public static native void showPickList(SelectItem item) /*-{
var jsItem = item.#com.smartgwt.client.core.DataClass::getJsObj()();
if(jsItem.showPickList) {
jsItem.showPickList();
}
}-*/
Calling showPickList(item) for any such pick list now opens the picker.
In my Wicket application I used one radio button with "yes" and "no" options. If I select "No", I should display one dropdown choice. I wrote code using AjaxFormChoiceComponentUpdatingBehavior. How do I unittest this using WicketTester?
Solution for Wicket 1.5.x:
AbstractAjaxBehavior behavior = (AbstractAjaxBehavior)WicketTesterHelper.
findBehavior(getTester().getComponentFromLastRenderedPage("path:to:component"),
AjaxFormChoiceComponentUpdatingBehavior.class);
getTester().executeBehavior(behavior);
First select the radio button that you want.
form.select("path to radio button", 0/1)
Then execute ajax behaviour:
tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("path to radio buttons").getBehaviors().get(0));
Here is my piece of code which works perfectly for me with select box but should fiat as well for radio button if you change Behaviour class. Needed steps are:
Insert new value into form (use FormTester)
Find behaviour
Execute behaviour on change
Here is an example of code:
//simulate insert new value
FormTester formTester = tester.newFormTester(PANEL_ID + FORM);
formTester.setValue("selectBox", "newValue");
//Find onchange behaviour
AjaxFormComponentUpdatingBehavior behavior =
(AjaxFormComponentUpdatingBehavior) WicketTesterHelper.findBehavior(
tester.getComponentFromLastRenderedPage(PANEL_ID + FORM + ":" + "selectBox"),
ajaxFormComponentUpdatingBehavior.class);
//execute onchange
tester.executeBehavior(behavior);
I missed the par how to update form value in previous answers.
If the radio button is on a form I think you should use the FormTester class:
http://wicket.apache.org/apidocs/1.4/org/apache/wicket/util/tester/FormTester.html
For an example of an Ajax form submit test you can take a look at:
http://www.java2s.com/Open-Source/Java-Document/J2EE/wicket-1.4/org/apache/wicket/ajax/form/AjaxFormSubmitTest.java.htm
Try something like this:
tester.executeAjaxEvent("form:myRadioButtonId", "onchange");
This turns out to be somewhat painful, at least in Wicket 1.4 (I haven't tried with 1.5).
Via a web search, I found hints in Mischa Dasberg's blog. Basically, you can't use the BaseWicketTester.executeAjaxEvent((String componentPath, String event) method because the behavior you're using isn't an AjaxEventBehavior and you can't use the BaseWicketTester.executeBehavior(final AbstractAjaxBehavior behavior) because it wipes out the request parameters.
Mischa's solution was to implement his own executeBehavior method in a parent test case, which worked for his situation, but not for my need, as it assumed the request parameter id was the same as the full component path.
I've done something similar by implementing my own executeAjaxBehavior in an extension of WicketTester, but assuming (as is true in my case) that the request parameter is the last ":" separated section of the component path:
public void executeAjaxBehavior(String path, String value) {
AbstractAjaxBehavior behavior = (AbstractAjaxBehavior) getComponentFromLastRenderedPage(path).getBehaviors().get(0);
CharSequence url = behavior.getCallbackUrl(false);
WebRequestCycle cycle = setupRequestAndResponse(true);
getServletRequest().setRequestToRedirectString(url.toString());
String[] ids = path.split(":");
String id = ids[ids.length-1];
getServletRequest().setParameter(id, value);
processRequestCycle(cycle);
}
Both his solution and mine (based on his) also assume that the behavior is the first (or only) one on the component.
This is a bit clunky, but something like this may work for you.
It might be better if the ids and behavior were gotten separately and passed as parameters, and of course you might do well to find the first behavior that actually was an AjaxFormChoiceComponentUpdatingBehavior instead of blithely assuming it was the first behavior, but this is a start.
This is also similar code to what's inside the BaseWicketTester class for the other behavior testing methods, which might be worth looking through.