Search in sources :

Example 1 with InputTime

use of com.liferay.faces.alloy.component.inputtime.InputTime in project liferay-faces-alloy by liferay.

the class InputTimeRenderer method getAlloyClassName.

@Override
public String getAlloyClassName(FacesContext facesContext, UIComponent uiComponent) {
    String alloyClassName = super.getAlloyClassName(facesContext, uiComponent);
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    InputTime inputTime = (InputTime) uiComponent;
    if (isNative(browserSniffer, inputTime)) {
        alloyClassName = alloyClassName.concat("Native");
    }
    return alloyClassName;
}
Also used : BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) InputTime(com.liferay.faces.alloy.component.inputtime.InputTime)

Example 2 with InputTime

use of com.liferay.faces.alloy.component.inputtime.InputTime in project liferay-faces-alloy by liferay.

the class InputTimeRenderer method getModules.

@Override
public String[] getModules(FacesContext facesContext, UIComponent uiComponent) {
    String[] modules = getModules(MODULES[0], facesContext, uiComponent);
    InputTime inputTime = (InputTime) uiComponent;
    String filterType = inputTime.getFilterType();
    if ((filterType != null) && (filterType.length() > 0)) {
        modules = StringHelper.append(modules, "autocomplete-filters");
    }
    String highlighterType = inputTime.getHighlighterType();
    if ((highlighterType != null) && (highlighterType.length() > 0)) {
        modules = StringHelper.append(modules, "autocomplete-highlighters");
    }
    return modules;
}
Also used : InputTime(com.liferay.faces.alloy.component.inputtime.InputTime)

Example 3 with InputTime

use of com.liferay.faces.alloy.component.inputtime.InputTime in project liferay-faces-alloy by liferay.

the class InputTimeRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    InputTime inputTime = (InputTime) uiComponent;
    String clientVarName = getClientVarName(facesContext, inputTime);
    String clientKey = inputTime.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    if (isNative(browserSniffer, inputTime)) {
        JavaScriptFragment liferayComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
        String clientId = uiComponent.getClientId(facesContext);
        String inputClientId = clientId.concat(INPUT_SUFFIX);
        // Get the max and min times in the HTML5 format which does not include seconds.
        int defaultHTML5PatternLength = InputTime.DEFAULT_HTML5_TIME_PATTERN.length();
        String maxTime = inputTime.getMaxTime().substring(0, defaultHTML5PatternLength);
        String minTime = inputTime.getMinTime().substring(0, defaultHTML5PatternLength);
        encodeFunctionCall(responseWriter, "LFAI.initDateTimePickerMobile", liferayComponent, inputClientId, maxTime, minTime);
    } else {
        encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
        // Replace the default TimePicker._setValues() method with setValues() (defined in alloy.js) which simply
        // passes values through to the autocomplete without processing them.
        responseWriter.write(clientVarName);
        responseWriter.write("._setValues=LFAI.timePickerSetValues;");
        // Set the values of the timePicker.
        responseWriter.write(clientVarName);
        responseWriter.write(".set('values',[");
        String minTimeStamp = inputTime.getMinTime();
        String maxTimeStamp = inputTime.getMaxTime();
        long minTime;
        long maxTime;
        try {
            minTime = getMillisFromTimeStamp(minTimeStamp);
            maxTime = getMillisFromTimeStamp(maxTimeStamp);
        } catch (ParseException e) {
            throw new IOException(e);
        }
        if (minTime > maxTime) {
            throw new IOException("minTime must not be later than maxTime.");
        }
        String timePattern = inputTime.getPattern();
        Object objectLocale = inputTime.getLocale();
        Locale locale = inputTime.getObjectAsLocale(objectLocale);
        TimeZone timeZone = TimeZone.getTimeZone("Greenwich");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern, locale);
        simpleDateFormat.setTimeZone(timeZone);
        Integer millisecondStep = inputTime.getStep() * 1000;
        boolean firstTimeStamp = true;
        if (millisecondStep < 1) {
            throw new IOException("step cannot be less than 1.");
        }
        // step, and printing each resulting value.
        for (long milliseconds = minTime; milliseconds <= maxTime; milliseconds = milliseconds + millisecondStep) {
            if (!firstTimeStamp) {
                responseWriter.write(",");
            } else {
                firstTimeStamp = false;
            }
            Date time = new Date(milliseconds);
            String dateString = simpleDateFormat.format(time);
            String escapedDateString = RendererUtil.escapeJavaScript(dateString);
            responseWriter.write("'");
            responseWriter.write(escapedDateString);
            responseWriter.write("'");
        }
        responseWriter.write("]);");
    }
}
Also used : Locale(java.util.Locale) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) InputTime(com.liferay.faces.alloy.component.inputtime.InputTime) IOException(java.io.IOException) Date(java.util.Date) TimeZone(java.util.TimeZone) InputDateTimeResponseWriter(com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with InputTime

use of com.liferay.faces.alloy.component.inputtime.InputTime in project liferay-faces-alloy by liferay.

the class InputTimeRendererBase method encodeAlloyAttributes.

@Override
public void encodeAlloyAttributes(FacesContext facesContext, ResponseWriter responseWriter, UIComponent uiComponent) throws IOException {
    InputTime inputTime = (InputTime) uiComponent;
    boolean first = true;
    String pattern = inputTime.getPattern();
    if (pattern != null) {
        encodeMask(responseWriter, inputTime, pattern, first);
        first = false;
    }
    encodeHiddenAttributes(facesContext, responseWriter, inputTime, first);
}
Also used : InputTime(com.liferay.faces.alloy.component.inputtime.InputTime)

Example 5 with InputTime

use of com.liferay.faces.alloy.component.inputtime.InputTime in project liferay-faces-alloy by liferay.

the class WorkHoursValidator method validate.

@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object object) throws ValidatorException {
    if (object != null) {
        Date date;
        InputTime inputTime = (InputTime) uiComponent;
        String timeZoneString = inputTime.getTimeZone();
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
        if (object instanceof Date) {
            date = (Date) object;
        } else {
            String dateString = object.toString();
            String datePattern = inputTime.getPattern();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
            simpleDateFormat.setTimeZone(timeZone);
            try {
                date = simpleDateFormat.parse(dateString);
            } catch (ParseException e) {
                String message = AlloyValidatorHelper.getMessage(facesContext, inputTime, "invalid-selection");
                FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
                throw new ValidatorException(facesMessage, e);
            }
        }
        Calendar calendar = new GregorianCalendar(timeZone);
        calendar.setTime(date);
        final int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        int minOfHour = calendar.get(Calendar.MINUTE);
        if ((hourOfDay == 12) || ((hourOfDay == 13) && (minOfHour == 0))) {
            String message = AlloyValidatorHelper.getMessage(facesContext, inputTime, "lunchtime-is-not-valid");
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, message, message);
            throw new ValidatorException(facesMessage);
        }
    }
}
Also used : TimeZone(java.util.TimeZone) ValidatorException(javax.faces.validator.ValidatorException) InputTime(com.liferay.faces.alloy.component.inputtime.InputTime) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) FacesMessage(javax.faces.application.FacesMessage) Date(java.util.Date)

Aggregations

InputTime (com.liferay.faces.alloy.component.inputtime.InputTime)5 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 InputDateTimeResponseWriter (com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Locale (java.util.Locale)1 FacesMessage (javax.faces.application.FacesMessage)1 ResponseWriter (javax.faces.context.ResponseWriter)1 ValidatorException (javax.faces.validator.ValidatorException)1