How to remove default value('0') in WicketTextField - wicket

Here iam using a wicketText Textfield component to which an Integer type is mapped, Now on load of page iam getting the '0' as default value in that textfield. How to remove that 0 from the textfield.

Don't use int as the property type: that can't be null. Use Integer instead.

The TextField converts the value of the object in the model to a String. In case the object is null, the String will be empty.
You are probably using a int as Matrijn indicated. The java built-in primitive type can never be null but defaults to 0
So use a Integer. (Or you could create a custom Converter, but that is just messy)

what about using NumberTextField like this
NumberTextField numberField = (NumberTextField) new NumberTextField("numberField",
new Model(10));

I believe You may be using int instead of Integer. (int cannot be null, while Integer can)

Try to customize the Converter of your TextField
public class Converter<C> implements IConverter<C> {
#Override
public C convertToObject(String s, Locale locale) throws ConversionException {
return (C) s;
}
#Override
public String convertToString(C c, Locale locale) {
Class<? extends Object> aclass = c.getClass();
if (aclass == Long.class) {
return ((Long) c != 0) ? String.valueOf(c) : "";
} else if (aclass == Integer.class) {
return ((Integer) c != 0) ? String.valueOf(c) : "";
} else if (aclass == BigDecimal.class) {
return (!BigDecimal.ZERO.equals(c)) ? String.valueOf(c) : "";
}
return c.toString();
}
}
and then, override the default IConverter
new TextField<BigDecimal>("id") {
#Override
public <C> IConverter<C> getConverter(Class<C> type) {
return new Converter<C>();
}
}

The generic type of text field should be a reference. The Integer is good choice. But for a good programming choice it is better to use String.The new Model("") make your text field empty.
TextField<Integer> a = new TextField<Integer>("id",new Model(""));
you can get the integer value of your textfield by a.getValue() method.You can parse the such strings to int by Integer.parseInt(a.getValue()); property.
TextField<String> a = new TextField<String>("id",new Model(""));

Related

Overriding Celltable Column getCellStyleNames method returns null

I tried to override the getCellStyleNames(Context context, T object) method of Column. But its not working.
I tried to return super.getCellStyleNames(), but it returns null.
What is the problem? What am I supposed to do to fix this problem?
I am using GWT 2.6.rc1.
TextColumn<ContactInfo> firstNameColumn = new TextColumn<ContactInfo>(
) {
#Override
public String getValue(ContactInfo object) {
return object.getFirstName() ;
}
#Override
public String getCellStyleNames(Context context, ContactInfo object) {
Window.alert("X ::: "
+ super.getCellStyleNames(context, object));
return "cellTableCell1";
// object.getAge() % 2 == 0 ? "cellTableCell2"
// : "cellTableCell1";
// return "styleName";
}
};
Thanks In Advance,
Bennet.
After a discussion in chat we discovered the issue: the style name "cellTableCell1" was declared inside the table CSS resource, therefore the name was obfuscated. Getting the name from the CSS resource solved the issue.

How to compare all properties values from a two equal classes

I've a class defined as follows:
Public Class DeviceConfig
Private _maxNumCodesGlobal As Integer
Private _maxNumCodesDataMatrix As Integer
Private _maxNumCodesQR As Integer
Private _maxNumCodesBarcode As Integer
Private _partialResults As String
Private _allowIdenticalSymbols As String
Private _datamatrixValidation As Integer
Private _datamatrixValidationType
'AND MUCH MORE PROPERTIES
'GETTERS & SETTERS
End Class
as you can see it's a long list of properties in this class.
I need to compare the values of the properties from an instance with the values of the properties of another instance.
Is there a way to iterate through all of them, or even better, just comparing both classes and get true/false if they have the same properties values or not?
if instance1=instance2 then true
Thank you
I encountered the same problem and created this method. Hopefully it will help you.
It uses reflections to iterate through the public fields, ignoring those with the JsonIgnore annotation.
This method is not considering fields as List, Set, etc.
You can change it to work for properties instead of fields.
protected <T> boolean equals(T object1, T object2) {
Field[] fields = object1.getClass().getFields();
for (Field field : fields) {
if (field.getAnnotation(JsonIgnore.class)!= null) continue; //do not check the fields with JsonIgnore
Object value1;
Object value2;
try {
value1 = field.get(object1);
value2 = field.get(object2);
} catch (Exception e) {
logger.error("Error comparing objects. Exception: " + e.getMessage());
return false;
}
//comparing
if (value1 == null) {
if (value2 != null)
return false;
} else if (!value1.equals(value2))
return false;
}
return true;
}

Case-insensitive indexing with Hibernate-Search?

Is there a simple way to make Hibernate Search to index all its values in lower case ? Instead of the default mixed-case.
I'm using the annotation #Field. But I can't seem to be able to configure some application-level set
Fool that I am ! The StandardAnalyzer class is already indexing in lowercase. It's just a matter of setting the search terms in lowercase too. I was assuming the query would do that.
However, if a different analyzer were to be used, application-wide, then it can be set using the property hibernate.search.analyzer.
Lowercasing, term splitting, removing common terms and many more advanced language processing functions are applied by the Analyzer.
Usually you should process user input meant to match indexed strings with the same Analyzer used at indexing; configuring hibernate.search.analyzer sets the default (global) Analyzer, but you can customize it per index, per entity type, per field and even on different entity instances.
It is for example useful to have language specific analysis, so to process Chinese descriptions with Chinese specific routines, Italian descriptions with Italian tokenizers.
The default analyzer is ok for most use cases, and does lowercasing and splits terms on whitespace.
Consider as well that when using the Lucene Queryparser the API requests you the appropriate Analyzer.
When using the Hibernate Search QueryBuilder it attempts to apply the correct Analyzer on each field; see also http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html_single/#search-query-querydsl .
There are multiple way to make sort insensitive in string type field only.
1.First Way is add #Fields annotation in field/property on entity.
Like
#Fields({#Field(index=Index.YES,analyze=Analyze.YES,store=Store.YES),
#Field(index=Index.YES,name = "nameSort",analyzer = #Analyzer(impl=KeywordAnalyzer.class), store = Store.YES)})
private String name;
suppose you have name property with custom analyzer and sort on that. so it's not possible then you can add new Field in index with nameSort apply sort on that field.
you must apply Keyword Analyzer class because that is not tokeniz field and by default apply lowercase factory class in field.
2.Second way is that you can implement your comparison class on sorting like
#Override
public FieldComparator newComparator(String field, int numHits, int sortPos, boolean reversed) throws IOException {
return new StringValComparator(numHits, field);
}
Make one class with extend FieldComparatorSource class and implement above method.
Created new Class name with StringValComparator and implements FieldComparator
and implement following method
class StringValComparator extends FieldComparator {
private String[] values;
private String[] currentReaderValues;
private final String field;
private String bottom;
StringValComparator(int numHits, String field) {
values = new String[numHits];
this.field = field;
}
#Override
public int compare(int slot1, int slot2) {
final String val1 = values[slot1];
final String val2 = values[slot2];
if (val1 == null) {
if (val2 == null) {
return 0;
}
return -1;
} else if (val2 == null) {
return 1;
}
return val1.toLowerCase().compareTo(val2.toLowerCase());
}
#Override
public int compareBottom(int doc) {
final String val2 = currentReaderValues[doc];
if (bottom == null) {
if (val2 == null) {
return 0;
}
return -1;
} else if (val2 == null) {
return 1;
}
return bottom.toLowerCase().compareTo(val2.toLowerCase());
}
#Override
public void copy(int slot, int doc) {
values[slot] = currentReaderValues[doc];
}
#Override
public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
#Override
public void setBottom(final int bottom) {
this.bottom = values[bottom];
}
#Override
public String value(int slot) {
return values[slot];
}
}
Apply sorting on Fields Like
new SortField("name",new StringCaseInsensitiveComparator(), true);

Data is Null. This method or property cannot be called on Null values

if (!string.IsNullOrEmpty(rd.GetString(2)))
{
StrBcc = rd.GetString(2);
}
Error: System.Data.SqlTypes.SqlNullValueException: Data is Null. This
method or property cannot be called on Null values.
My solution was to create an extension method:
static class DataReaderExtensions
{
public static string GetStringNullCheck(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? null : reader.GetString(ordinal);
}
}
So I can use it as:
StrBcc = rd.GetStringNullCheck(2);
You should use
if (!rd.IsDBNull(2))
StrBcc = rd.GetString(2);
That's because when you use string.IsNullOrEmpty(x) you are telling your app that x is a string, while is a database null value, which is different from a string whose value is null.
If your values can be NULL then using SqlTypes instead can be a safer solution as they all implement INullable:
StrBcc = rd.GetSqlString(2);
or if you like extension methods:
public static class DataReaderExtensions
{
public static T GetValueOrNull<T>(this SqlDataReader reader, int ordinal) where T : class
{
return !reader.IsDBNull(ordinal) ? reader.GetFieldValue<T>(ordinal) : null;
}
public static T? GetValueOrNullable<T>(this SqlDataReader reader, int ordinal) where T : struct
{
if (reader.IsDBNull(ordinal)) return null;
return reader.GetFieldValue<T>(ordinal);
}
}

GWT CellTable - add column depending on row

Does anybody know if this is possible to add a column to CellTable depending on the some value od displayed row?
Normally addColumn is used but the access to row properties is enabled only in getValue method. I need to gain this access earlier to decide either to add some value to column or lewave it blank.
The answer is to write custom cell class that extends the appropriate cell class (provided with GWT). Then in render method the column's content might be empty or not depending on the value of displayed/rendered object. E.g.
private class VersionCell<T> extends ActionCell<MovieDTO> {
public VersionCell(String text, Delegate<MovieDTO> delegate) {
super(text, delegate);
}
#Override
public void render(MovieDTO m, Object key, SafeHtmlBuilder sb) {
if (m != null && m.getId() != -1) {
super.render(m, key, sb);
} else if (m != null && m.getId() == -1) {
sb.append(new SafeHtmlBuilder().appendHtmlConstant("").toSafeHtml());
}
}
}