Setting attribute to the option of DropDownChoice - java

I have a DropDownChoice:
DropDownChoice dateSpanChoice = new DropDownChoice("dateSpan", new PropertyModel(getModel(), "dateSpan"), dateSpans, new IChoiceRenderer() {
private static final long serialVersionUID = 10105L;
#Override
public String getIdValue(Object object, int index) {
return ((DateSpan) object).getId() + "";
}
#Override
public Object getDisplayValue(Object object) {
DateTime today = new DateTime();
DateTime currentDate = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
DateSpan dateSpan = (DateSpan) object;
DateTime fromDate = dateSpan.getFromDate();
DateTime toDate = dateSpan.getToDate();
boolean currentDateIsEqualOrAfterFromDate = currentDate.isEqual(fromDate) | currentDate.isAfter(fromDate);
boolean currentDateIsEqualOrBeforeToDate = currentDate.isEqual(toDate) | currentDate.isBefore(toDate);
boolean currentDateBelongsToCurrentRange = currentDateIsEqualOrAfterFromDate & currentDateIsEqualOrBeforeToDate;
if (currentDateBelongsToCurrentRange) {
return ((DateSpan) object).getDisplayValue() + " *";
} else {
return ((DateSpan) object).getDisplayValue();
}
}
}) {
private static final long serialVersionUID = 10106L;
protected CharSequence getDefaultChoice(final Object selected) {
CharSequence charSequence = super.getDefaultChoice(selected);
System.out.println("=============================================================");
//System.out.println(charSequence);
//THIS CHARSEQUENCE IS RENRING EMPTY STRING
if(StringUtils.isBlank(charSequence.toString())) {
System.out.println("CHARSEQUENCE IS BLANK");
} else {
System.out.println("CHARSEQUENCE IS NOT BLANK");
}
return charSequence;
}
};
dateSpanChoice.setNullValid(false);
I am trying to add style="background-color: red" to the selected option. So I am overriding getDefaultChoice() but as you can see it is commented in code the CharSequence is empty.
Is there any way to set an attribute to a particular option of DropDownChoice?
Thanks and Regards.
Note: the DateTime is Joda time

You need Select/SelectOption instead of DropDownChoice for more complex uses of element.

Have a look at the behavior package in Wicket.
You can use the AttributeAppender-class for instance.

Related

Specify an adapter for multiple xsd types

I'm trying to map different formats onto 3 xsd types, xs:dateTime, xs:date, and xs:time. I'm doing a bit of codegen in my project, and do not have a bindings file, though I do have a package-info.
#javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters({
#javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value = com.codegen.pojo.lib.adapters.XmlDateTimeFormatter.class, type = com.codegen.pojo.lib.types.IXmlDateTime.class)
})
#javax.xml.bind.annotation.XmlSchemaTypes({
#javax.xml.bind.annotation.XmlSchemaType(name = "dateTime", type = com.codegen.pojo.lib.types.XmlDateTime.class),
#javax.xml.bind.annotation.XmlSchemaType(name = "date", type = com.codegen.pojo.lib.types.XmlDate.class),
#javax.xml.bind.annotation.XmlSchemaType(name = "time", type = com.codegen.pojo.lib.types.XmlTime.class)
})
#com.codegen.pojo.lib.annotations.XMLDateTimeFormat(format = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXX")
#com.codegen.pojo.lib.annotations.XMLDateFormat(format = "yyyy-MM-dd'T'")
#com.codegen.pojo.lib.annotations.XMLTimeFormat(format = "'T'HH:mm:ss.SSSXXXX")
package my_pkg;
All 3 of the Xml(Date/Time) classes mentioned in the XmlSchemaType annotations implement the IXmlDateTime interface.
The adapter:
public class XmlDateTimeFormatter extends XmlAdapter<String, IXmlDateTime> {
private final DateTimeFormatter dateTimeFormat;
private final DateTimeFormatter dateFormat;
private final DateTimeFormatter timeFormat;
public XmlDateTimeFormatter(String dateTimeFormatPattern, String dateFormatPattern, String timeFormatPattern) {
dateTimeFormat = dateTimeFormatPattern == null ? null : DateTimeFormatter.ofPattern(dateTimeFormatPattern);
dateFormat = dateFormatPattern == null ? null : DateTimeFormatter.ofPattern(dateFormatPattern);
timeFormat = timeFormatPattern == null ? null : DateTimeFormatter.ofPattern(timeFormatPattern);
}
#Override
public String marshal(IXmlDateTime dateTime) throws Exception {
if (dateTime instanceof XmlDateTime) {
return dateTimeFormat.format(dateTime.getCalendar().toGregorianCalendar().toZonedDateTime());
} else if (dateTime instanceof XmlDate) {
return dateFormat.format(dateTime.getCalendar().toGregorianCalendar().toZonedDateTime());
} else if (dateTime instanceof XmlTime) {
return timeFormat.format(dateTime.getCalendar().toGregorianCalendar().toZonedDateTime());
}
return null;
}
#Override
public IXmlDateTime unmarshal(String dateTime) throws Exception {
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime);
QName xmlFormat = calendar.getXMLSchemaType();
if (xmlFormat.equals(DatatypeConstants.DATETIME)) {
return new XmlDateTime(calendar);
} else if (xmlFormat.equals(DatatypeConstants.DATE)) {
return new XmlDate(calendar);
} else if (xmlFormat.equals(DatatypeConstants.TIME)) {
return new XmlTime(calendar);
}
return null;
}
The adapter is added to a Marshaller/Unmarshaller created from a JAXBContext. Unfortunately, neither the marshal or unmarshal get called. Any thoughts on how I could fix this?

Java check two date as String

simply i have two variable as date which they are date as String, for example
"2016-11-30"
when i try to check two variable as this value i get false,
// serverDateTime value is: "2016-11-30"
// Utils.getCurrentDateTime() return: "2016-11-30"
private boolean checkCurrentDate(String serverDateTime) {
if (serverDateTime.equals(Utils.getCurrentDateTime())) {
return true;
} else {
return false;
}
}
public static String getCurrentDateTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(c.getTime());
}
Try to use moment.js
var stringDateOne = '2016-11-30';
var stringDateTwo = '2016-11-30';
var momentDateOne = moment(stringDateOne, 'YYYY-MM-DD');
var momentDateTwo = moment(stringDateTwo, 'YYYY-MM-DD');
if(momentDateOne.diff(momentDateTwo, 'days') == 0){
//date equal
}else{
//date not equal
}
hi The code is correct and is returning true for the above entries
public class HelloWorld {
public static void main(String[] args) {
String currDate = "2016-11-30";
HelloWorld hw = new HelloWorld();
System.out.println(hw.checkCurrentDate(currDate));
}
private boolean checkCurrentDate(String serverDateTime) {
if (serverDateTime.equals(HelloWorld.getCurrentDateTime())) {
return true;
} else {
return false;
}
}
public static String getCurrentDateTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(c.getTime());
}
}

Creating object array to pass in Constructor

Being a newbie to Java am finding it a bit difficult to come up with a neat code for my purpose.
The problem is,
class ElementID {
String name = "";
String value = "";
public ElementID (String name, String value) {
this.name = name;
this.value = value;
}
}
class HeaderID {
String ID = "";
Date cal;
ElementID[] elementID;
public HeaderID (String ID, Calender cal, ElementID[] elemID) {
this.ID = ID;
this.cal = cal;
this.elementID = elemID;
}
}
Now in another class inside a function i need to instantiate the HeaderID class.
And this is what i did so far and it doesn't work. The problem am facing is with regard to creating a single array of ElementID.
ElementID[] e = new ElementID[1];
e[0] = new ElementID("NAME", "VALUE");
// Error at this line
HeaderID ht = new HeaderID("ID", new Date(), e);
You cannot cast direct from Date to Calendar:
You declared:
public HeaderID (String ID, Calender cal, ElementID[] elemID) {
and you try to call:
HeaderID ht = new HeaderID("ID", new Date(), e); //Error at this line
// ^ this is expecting a Calendar Instance
OPTION A: You can change you declaration
public HeaderID (String ID, Date date, ElementID[] elemID) {
If you do this use your actual call:
HeaderID ht = new HeaderID("ID", new Date(), e);
OPTION B: change what you send to your actual declaration:
// create new calendar instance
Calendar cal = Calendar.getInstance();
// put desired Date
cal.setTime(new Date());
// make call with correct variable types
HeaderID ht = new HeaderID("ID", cal, e);

Add ImageColumn

I should like to use an image in a column.
FastReportBuilder drb = new FastReportBuilder();
drb.addImageColumn("Example", expression, 20, true, ImageScaleMode.NO_RESIZE, myStyle);
CustomExpression iexpressionr = new CustomExpression() {
String ok = "http://....//ok.png";
String ko = "http://....//error.png";
public String getClassName() {
return String.class.getName();
}
public Object evaluate(Map fields, Map variables, Map parameters) {
String result = (String) fields.get("result");
if (result.equals("true")) {
return ok;
} else {
return ko;
}
}
};
My problem is the following: the style of the header is the default.
How Can I insert the header style in this case?
I try
ImageColumn d = new ImageColumn();
d.setExpression(imgExpr);
d.setTitle("Example");
d.setWidth(20);
d.setHeaderStyle(myHeaderStyle);
d.setStyle(myStyle);
but the method "addColumn" for the object FastReportBuilder it is not good.

DatetimeField validator

I've created a custom validator to validate dateTimefield.
My problem is that I can't add it to the
datetimefield variable
The method add(IValidator<? super Date>) in the type FormComponent<Date> is not applicable for the arguments (DateTimeFieldValidator)
This is the error I'm getting.
Is there any standard way to validate DateTimeField?
package validators;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
public class DateTimeFieldValidator implements IValidator<DateTimeField> {
/**
*
*/
private static final long serialVersionUID = 2342344609244L;
public DateTimeFieldValidator() {
}
private void error(IValidatable<DateTimeField> validatable, String errorKey) {
ValidationError error = new ValidationError();
error.addMessageKey(getClass().getSimpleName() + "." + errorKey);
validatable.error(error);
}
public void validate(IValidatable<DateTimeField> validatable) {
DateTimeField dateTime = (DateTimeField) validatable.getValue();
if ( dateTime== null){
error(validatable, "invalid.datetime");
}
else{
if( dateTime.getHours()!=null){
if( dateTime.getHours()>12 || dateTime.getHours()<0){
error(validatable, "invalid.hour");
}
}
else{
error(validatable, "invalid.hour");
}
if(dateTime.getMinutes()!=null){
if( dateTime.getMinutes() > 60 ){
error(validatable, "invalid.hour");
}
}else {
error(validatable, "invalid.minutes");
}
if( dateTime.getDate() == null ){
error(validatable, "invalid.date");
}
}
}
boolean isLegalDate(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
return sdf.parse(s, new ParsePosition(0)) != null;
}
}
This is how I'm adding the validator.
startDateTimeField.add(new DateTimeFieldValidator());
Your DateTimeFieldValidator must implement IValidator<Date> instead of IValidator<DateTimeField>
I end up validating the datetimefield following way ..
And find out the datetimefield( yui ) have built in validation - which actually take care of the all the basic validations.
All you have to do is put a XXX.properties file in the folder -
you can also add your own error key and message - like I used startDate.after.enddate
IFormValidator validator = new AbstractFormValidator() {
/**
*
*/
private static final long serialVersionUID = -3252346839511722L;
public FormComponent<?>[] getDependentFormComponents() {
return new FormComponent[] { startField, endField };
}
ValidationError error = new ValidationError();
public void validate(Form<?> form) {
Date startDate = (Date) startField.getConvertedInput();
if(endField.isEnabled()){
Date endDate = (Date) endField.getConvertedInput();
if (endDate.before(startDate)){
error.addMessageKey(getClass().getSimpleName() + "startDate.after.enddate");
startField.error(error);
}
}
}
};
logForm.add(validator );

Categories