Skip to content

Commit

Permalink
clockpicker: set value type from Object to LocalTime, update correspo…
Browse files Browse the repository at this point in the history
…nding built-in converters
  • Loading branch information
jxmai authored and mai committed Jan 25, 2024
1 parent 067ab8c commit 0bc04a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
package org.primefaces.extensions.component.clockpicker;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
Expand All @@ -36,7 +34,6 @@

import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.LangUtils;
import org.primefaces.util.MessageFactory;
import org.primefaces.util.WidgetBuilder;

public class ClockPickerRenderer extends CoreRenderer {
Expand Down Expand Up @@ -131,20 +128,20 @@ protected static String getValueAsString(final FacesContext context, final Clock
}
else {
// use built-in converter
SimpleDateFormat timeFormat;
timeFormat = new SimpleDateFormat("HH:mm", clockPicker.calculateLocale());

return timeFormat.format(value);
if (value instanceof LocalTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format((LocalTime) value);
}
}
}
return "";
}

@Override
public Object getConvertedValue(FacesContext context, UIComponent component,
Object value) throws ConverterException {
final ClockPicker clockPicker = (ClockPicker) component;
String submittedValue = (String) value;
SimpleDateFormat format = null;

if (isValueBlank(submittedValue)) {
return null;
Expand All @@ -165,36 +162,19 @@ public Object getConvertedValue(FacesContext context, UIComponent component,
try {
ValueExpression ve = clockPicker.getValueExpression("value");
if (ve != null) {
Class type = ve.getType(context.getELContext());
if (type != null && type != Object.class && type != Date.class) {
Converter converter = context.getApplication().createConverter(type);
if (converter != null) {
return converter.getAsObject(context, clockPicker, submittedValue);
}
Class<?> type = ve.getType(context.getELContext());
if (type != null && type != Object.class && type.isAssignableFrom(LocalTime.class)) {
// Use built-in converter for LocalTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return LocalTime.parse(submittedValue, formatter);
}
}
}
catch (ConverterException e) {
throw e;
}
return null;

// Use built-in converter
format = new SimpleDateFormat("HH:mm", clockPicker.calculateLocale());
format.setLenient(false);
try {
return format.parse(submittedValue);
}
catch (ParseException e) {
String message = null;
Object[] params = new Object[3];
params[0] = submittedValue;
params[1] = format.format(new Date());
params[2] = MessageFactory.getLabel(context, clockPicker);

message = MessageFactory.getMessage("javax.faces.converter.DateTimeConverter.DATE", FacesMessage.SEVERITY_ERROR, params);

throw new ConverterException(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10438,7 +10438,7 @@ See showcase for an example. Default null.]]>
</description>
<name>value</name>
<required>false</required>
<type>java.lang.Object</type>
<type>java.time.LocalTime</type>
</attribute>
<attribute>
<description><![CDATA[Name of the client side widget.]]></description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

import java.io.Serializable;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
Expand All @@ -44,20 +41,16 @@ public class ClockPickerController implements Serializable {

private static final long serialVersionUID = 897540091000342926L;

private Date time;
private LocalTime time;

public ClockPickerController() {
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, 15);
time = calendar.getTime();
// Initialize time to 8:15 AM
time = LocalTime.of(8, 15);
}

public void showTime() {
LocalTime localTime = time.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
int hour = localTime.getHour();
int min = localTime.getMinute();
int hour = time.getHour();
int min = time.getMinute();

String message = String.format("Selected hour: %d, Selected min: %d", hour, min);
addMessage(FacesMessage.SEVERITY_INFO, "Info Message", message);
Expand All @@ -67,11 +60,11 @@ private void addMessage(FacesMessage.Severity severity, String summary, String d
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, summary, detail));
}

public Date getTime() {
public LocalTime getTime() {
return time;
}

public void setTime(Date time) {
public void setTime(LocalTime time) {
this.time = time;
}

Expand Down

0 comments on commit 0bc04a8

Please sign in to comment.