I've a Form where an property of source Item need to be formatter by an custom format.
Source property (of my own bean) is a Integer, but need to be formatted as a Currency-like format.
I tried to implement my own PropertyFormatter, and setup it inside my FieldFactory.createField for this form as
TextField tf = new TextField("Price");
tf.setPropertyDataSource(new MyPriceFormatter());
return tf;
But as I see from the logs, only format() method is called. But parse() method is never used, and setValue is never called
What's wrong with my code? How to use custom PropertyFomatter for forms? Or how to add custom format for form's field?
After some investigation i found that there is something just replaces my formatter, with an new MethodProperty data source. So i'd implemented my own PriceField, with overrided setPropertyDataSource, that fix this situation. btw, it seems to bee hacky, and i'm still looking for an other way
I have also experienced this problem and solved it another way. Actually I also had to make a textfield formated with a currency :-)
The problem is that the datasource in the PropertyFormatter is null at the time you are creating the fields in the FormFieldFactory. You can instead set the datasource on your field after the FormFieldFactory has been called:
addCountryRatesForm.setFormFieldFactory(new MyFormFieldFactory());
Field internationalRate = addCountryRatesForm.getField("internationalRate");
internationalRate.setPropertyDataSource(new CurrencyFormatter("#0.00 ", currency, internationalRate.getPropertyDataSource()));
So unfortunately with Vaadin you cannot create a TextField that sets its own formatter.
Related
Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);
My issue is I have a simple List of Strings say
List<String> names = List.of("Frank","Joe","Eva");
All I want is display it on the UI. With some simple code like
ListComponent lc = new ListComponent.setItems(names);
I have tried it with Table which seems to work but code behind it is a bit boilerplate for this simple task(7-8 line of code).
I have tried also the Grid component and it works well when I want to bind a POJO to it , but with String.class type its a nightmare.
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
it doesnt work because I have to provide getters for the column, which String.class doesnt have for the value. So I did this:
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
listGrid.addColumn(String::toString).setCaption("name");
It works! However unspecified columns also appear in the grid, so now I have 3 columns Byte,Empty,name. And I dont know why. Where are these comes from?
What are the requirements for displaying them? Just to get them on the screen? Is Label enough?
for(String name: names) {
mylayout.addComponent(new Label(name))
}
If you need selection, then maybe ListSelect or ComboBox are the go-to’s.
If you want to avoid the additional columns, one way is to do as was pointed out in a comment, i.e. do removeAllColumns() before you go on creating your own columns.
Another approach would be to do new Grid<>() instead of new Grid<>(String.class). The main difference is that the second constructor uses reflection on the provided class and automatically configures columns for anything that looks like regular Java bean properties.
I would highly prefer to use grid.removeColumnByKey rather than removeAllColumns()
You can also use grid.setColumns to specify order of columns.
I will add link to vaadin documentantion for grid with java examples which is realy helpfull. enter link description here
I did something similar to adding the Strings in TextAreas. Because I needed some formatting, I added the text using StringBuilder.
List<String> details = getDetails();
StringBuilder builder = new StringBuilder();
for (String detail : details) {
TextArea ta = new TextArea();
ta.setSizeFull();
ta.setMaxHeight("100px");
ta.setValue(builder.append(detail).toString());
((Span) content).add(ta);
((Span) content).add(new Hr());
}
The result is like this:
Try out this
final Grid<String> grid = new Grid<>();
grid.setItems(new ArrayList<String>());
grid.addColumn(item -> item).setHeader("Value");
I have a ComboBox in my Vaadin 8 code.
allAtts here is a Set of Attendant-s, and the field theAtt in there is of type String:
private ComboBox<Attendant> theCB = new ComboBox<Attendant>(null,allAtts);
theCB.setEmptySelectionAllowed(false);
Binder<SearchArgs> binder = new Binder<SearchArgs>(SearchArgs.class);
binder.setBean(sas);
binder.forField(theCB).bind("theAtt");
..
VerticalLayout vl = new VerticalLayout(theCB, rb, deleteBtn);
What's more - this exact flow of theCB is a copy-paste from another class i wrote, on the very same type Attendant.
The problem here is the drop-down click on theCB is not working. That is, clicking that little chevron-down icon on the ComboBox is producing no effect. What could this be!?
I tried removing the other items, rb, deleteBtn in vl. Still didn't work.
Nothing else happening to theCB anywhere else.
TIA.
EDIT:
theCB is otherwise functional. typing in the field is allowed, brings the choices and returns the selection accurately.
EDIT-2
https://github.com/vaadin/vaadin-combo-box/issues/680 is not the issue. nothing changing when i pass a non-null label value to the constructor. Besides, it's working fine with null label in that other place in the code.
EDIT-3
Attendant contains several fields, one of which is String att.
SearchArgs composes Attendant in a its field theAtt. Attendant.toString() returns att only.
Once again - this exact same logic is working, as i'm typing these, in another part in the code.
Also note: i since also tried the following without success:
binding theCB on a separate binder, i.e. declare a second binder on sas and use that one for binding theCB
cloning the Attendant objects that go into theCB as data-provider.
In my UI i have 2 text field and and 2 buttons .I am using jface data binding to bind the text field and i am doing the validation and depending on the validation success the model is updated other wise it will not.I want my button to respond accordingly .Like if my model is not updated than i want to disable my button.One more thing that i do anot want to do hard coding .so is there any way to that without hard coding.
In other way I want to bind a button to text field so that when the text field has some unwanted value than the button should be disabled .In the other way i am doing data binding on text field which will take care when the text field does not have proper value than it will not update its model.Then i want to disable the button when the value is not proper can i do that.Any pointer on this helps me a lot.
You can make use of below listener. Add listener to your Observable
org.eclipse.core.databinding.observable.value.IValueChangeListener
After some research, I found that I have to observe the enable/disable property of the button and bind it with the current emf databinding context. Detail of the function which I have used is given below:
void bindEnablementButtonToValidationStatus(Button button, EMFDataBindingContext ctx) {
IObservableValue buttonEnable = SWTObservables.observeEnabled(button);
IObservableValue validationStatus = new AggregateValidationStatus(
ctx.getValidationRealm(),
ctx.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
ctx.bindValue(buttonEnable, validationStatus,
new EMFUpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER),
new EMFUpdateValueStrategy().setConverter(
new Converter(IStatus.class, Boolean.TYPE) {
public Object convert(Object fromObject) {
return new Boolean(((IStatus)fromObject).isOK());
}
}));
}
I'm trying the following code but it doesn't compile:
SimpleIntegerProperty startPageProperty = new SimpleIntegerProperty();
TextField startPageField = new TextField();
Bindings.bindBidirectional(
startPageField.textProperty(), startPageProperty, new IntegerStringConverter()
);
The last static method call does not accept these parameters.
Bindings#bindBidirectional expects a StringConverter[Number], you are providing a StringConverter[Integer]. Though it may not be intuitive, you'll have to use a NumberStringConverter instead.
Bindings.bindBidirectional(startPageField.textProperty(),
startPageProperty,
new NumberStringConverter());
While the previous answer is correct, there is another way to solve this, which works better if you want to format numbers in a specific way (e.g. with thousands separators):
var formatter = new TextFormatter<>(new NumberStringConverter("#,###"));
formatter.valueProperty().bindBidirectional(startPageProperty);
startPageField.setTextFormatter(formatter);
The advantage of using a TextFormatter is that it will reformat any number entered by the user when the text field loses focus.