parent
ab9255d3d4
commit
f87854d02f
4 changed files with 95 additions and 13 deletions
@ -0,0 +1,53 @@ |
||||
/** |
||||
* Copyright (C) 2022, Tim Holloway |
||||
* |
||||
* Date written: Jan 9, 2022 |
||||
* Author: Tim Holloway <timh@mousetech.com> |
||||
*/ |
||||
package com.mousetech.gourmetj.utils; |
||||
|
||||
import javax.faces.application.FacesMessage; |
||||
import javax.faces.component.UIComponent; |
||||
import javax.faces.context.FacesContext; |
||||
import javax.faces.convert.Converter; |
||||
import javax.faces.convert.ConverterException; |
||||
import javax.faces.convert.FacesConverter; |
||||
import javax.faces.validator.ValidatorException; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
|
||||
/** |
||||
* Converts complex time durations, using TimeFormatter |
||||
* |
||||
* @author timh |
||||
* @since Jan 9, 2022 |
||||
*/ |
||||
@FacesConverter("com.mousetech.gourmetj.utils.TimeConverter") |
||||
public class TimeConverter implements Converter<Integer> { |
||||
|
||||
/** |
||||
* Parse incoming time string before passing to backing bean. |
||||
*/ |
||||
@Override |
||||
public Integer getAsObject(FacesContext context, |
||||
UIComponent component, String value) { |
||||
if (StringUtils.isBlank(value)) { |
||||
return null; |
||||
} |
||||
Long tv = TimeFormatter.parseTime(value); |
||||
if (tv == null) { |
||||
throw new ConverterException( |
||||
new FacesMessage("Invalid time")); |
||||
} |
||||
return tv.intValue(); |
||||
} |
||||
|
||||
/** |
||||
* Format display/edit text from backing bean to View |
||||
*/ |
||||
@Override |
||||
public String getAsString(FacesContext context, |
||||
UIComponent component, Integer value) { |
||||
return TimeFormatter.formatTime(Long.valueOf(value)); |
||||
} |
||||
} |
Loading…
Reference in new issue