Skip to content

Commit

Permalink
Clockpicker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jxmai authored and mai committed Jan 24, 2024
1 parent ad5024f commit 2168e75
Show file tree
Hide file tree
Showing 13 changed files with 1,529 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (c) 2011-2024 PrimeFaces Extensions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.extensions.component.clockpicker;

import java.util.Locale;

import javax.faces.application.ResourceDependency;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;

import org.primefaces.component.api.Widget;
import org.primefaces.util.Constants;
import org.primefaces.util.LocaleUtils;

@ResourceDependency(library = "primefaces", name = "components.css")
@ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
@ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
@ResourceDependency(library = "primefaces", name = "core.js")
@ResourceDependency(library = "primefaces-extensions", name = "clockpicker/0-clockpicker.css")
@ResourceDependency(library = "primefaces-extensions", name = "clockpicker/0-clockpicker.js")
@ResourceDependency(library = "primefaces-extensions", name = "clockpicker/1-clockpicker-widget.js")
public class ClockPicker extends HtmlInputText implements Widget {
public static final String CONTAINER_CLASS = "pe-clockpicker ui-widget ui-corner-all input-group clockpicker";

public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.ClockPicker";
public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.ClockPickerRenderer";

private Locale appropriateLocale;

protected enum PropertyKeys {

//@formatter:off
widgetVar,
placement,
align,
donetext,
autoclose,
locale,
vibrate;

private String toString;

PropertyKeys(final String toString) {
this.toString = toString;
}

PropertyKeys() {
}

@Override
public String toString() {
return toString != null ? toString : super.toString();
}
}

public ClockPicker() {
setRendererType(DEFAULT_RENDERER);
}


private boolean isSelfRequest(final FacesContext fc) {
return this.getClientId(fc).equals(
fc.getExternalContext().getRequestParameterMap().get(Constants.RequestParams.PARTIAL_SOURCE_PARAM));
}

@Override
public String getFamily() {
return COMPONENT_FAMILY;
}

public String getWidgetVar() {
return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
}

public void setWidgetVar(final String widgetVar) {
getStateHelper().put(PropertyKeys.widgetVar, widgetVar);
}

public String getPlacement() {
return (String) getStateHelper().eval(PropertyKeys.placement, "bottom");
}

public void setPlacement(final String placement) {
getStateHelper().put(PropertyKeys.placement, placement);
}

public String getAlign() {
return (String) getStateHelper().eval(PropertyKeys.align, "left");
}

public void setAlign(final String align) {
getStateHelper().put(PropertyKeys.align, align);
}

public String getDonetext() {
return (String) getStateHelper().eval(PropertyKeys.donetext, "Done");
}

public void setDonetext(final String donetext) {
getStateHelper().put(PropertyKeys.donetext, donetext);
}

public Boolean getAutoclose() {
return (Boolean) getStateHelper().eval(PropertyKeys.autoclose, false);
}

public void setAutoclose(final Boolean autoclose) {
getStateHelper().put(PropertyKeys.autoclose, autoclose);
}

public Boolean getVibrate() {
return (Boolean) getStateHelper().eval(PropertyKeys.vibrate, true);
}

public void setVibrate(final Boolean vibrate) {
getStateHelper().put(PropertyKeys.vibrate, vibrate);
}

public Object getLocale() {
return getStateHelper().eval(PropertyKeys.locale, null);
}

public void setLocale(final Object locale) {
getStateHelper().put(PropertyKeys.locale, locale);
}

public Locale calculateLocale() {
if (appropriateLocale == null) {
final FacesContext fc = FacesContext.getCurrentInstance();
appropriateLocale = LocaleUtils.resolveLocale(fc, getLocale(), getClientId(fc));
}
return appropriateLocale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*
* Copyright (c) 2011-2024 PrimeFaces Extensions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.extensions.component.clockpicker;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

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 {

@Override
public void decode(FacesContext context, UIComponent component) {

final ClockPicker clockPicker = (ClockPicker) component;

if (clockPicker.isDisabled() || clockPicker.isReadonly()) {
return;
}
final String param = clockPicker.getClientId(context) + "_input";
final String submittedValue = context.getExternalContext().getRequestParameterMap().get(param);

if (null != submittedValue) {
clockPicker.setSubmittedValue(submittedValue);
}
decodeBehaviors(context, component);
}

@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
final ClockPicker clockPicker = (ClockPicker) component;

final String value = getValueAsString(context, clockPicker);
encodeMarkup(context, clockPicker, value);
encodeScript(context, clockPicker, value);
}

private void encodeMarkup(FacesContext context, ClockPicker clockPicker, final String value) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = clockPicker.getClientId();
String inputId = clientId + "_input";

writer.startElement("div", clockPicker);
writer.writeAttribute("class", ClockPicker.CONTAINER_CLASS, null);

writer.startElement("input", null);
writer.writeAttribute("id", inputId, null);
writer.writeAttribute("name", inputId, null);
writer.writeAttribute("type", "text", null);
// writer.writeAttribute("class", "form_control", null);
writer.writeAttribute("class", "ui-inputfield ui-widget ui-state-default ui-corner-all", null);
writer.writeAttribute("size", 5, null);
writer.writeAttribute("maxlength", 5, null);

if (LangUtils.isNotBlank(value)) {
writer.writeAttribute("value", value, null);
}

writer.endElement("input");

writer.startElement("span", null);
writer.writeAttribute("class", "input-group-addon", null);
writer.startElement("span", null);
writer.writeAttribute("class", "glyphicon glyphicon-time", null);
writer.endElement("span");
writer.endElement("span");
writer.endElement("div");
}

private void encodeScript(final FacesContext context, final ClockPicker clockPicker, final String value) throws IOException {
final WidgetBuilder wb = getWidgetBuilder(context);
final String clientId = clockPicker.getClientId(context);

wb.init("ExtClockPicker", clockPicker.resolveWidgetVar(), clientId);
encodeClientBehaviors(context, clockPicker);

wb.attr("placement", clockPicker.getPlacement());
wb.attr("align", clockPicker.getAlign());
wb.attr("donetext", clockPicker.getDonetext());
wb.attr("autoclose", clockPicker.getAutoclose());
wb.attr("vibrate", clockPicker.getVibrate());

wb.finish();
}

protected static String getValueAsString(final FacesContext context, final ClockPicker clockPicker) {
final Object submittedValue = clockPicker.getSubmittedValue();
if (submittedValue != null) {
return submittedValue.toString();
}

final Object value = clockPicker.getValue();
if (value == null) {
return null;
}
else {
if (clockPicker.getConverter() != null) {
// convert via registered converter
return clockPicker.getConverter().getAsString(context, clockPicker, value);
}
else {
// use built-in converter
SimpleDateFormat timeFormat;
timeFormat = new SimpleDateFormat("HH:mm", clockPicker.calculateLocale());

return timeFormat.format(value);
}
}
}

@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;
}

// Delegate to user supplied converter if defined
try {
Converter converter = clockPicker.getConverter();
if (converter != null) {
return converter.getAsObject(context, clockPicker, submittedValue);
}
}
catch (ConverterException e) {
// clockPicker.setConversionFailed(true);

throw e;
}

// Delegate to global defined converter (e.g. joda or java8)
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);
}
}
}
}
catch (ConverterException e) {
// clockPicker.setConversionFailed(true);

throw e;
}

// Use built-in converter
format = new SimpleDateFormat("HH:mm", clockPicker.calculateLocale());
format.setLenient(false);
// format.setTimeZone(clockPicker.calculateTimeZone());
try {
return format.parse(submittedValue);
}
catch (ParseException e) {
// clockPicker.setConversionFailed(true);

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);
}
}

}
9 changes: 9 additions & 0 deletions core/src/main/resources/META-INF/faces-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@
<component-type>org.primefaces.extensions.component.SunEditor</component-type>
<component-class>org.primefaces.extensions.component.suneditor.SunEditor</component-class>
</component>
<component>
<component-type>org.primefaces.extensions.component.ClockPicker</component-type>
<component-class>org.primefaces.extensions.component.clockpicker.ClockPicker</component-class>
</component>

<converter>
<converter-id>org.primefaces.extensions.converter.JsonConverter</converter-id>
Expand Down Expand Up @@ -464,6 +468,11 @@
<renderer-type>org.primefaces.extensions.component.SunEditorRenderer</renderer-type>
<renderer-class>org.primefaces.extensions.component.suneditor.SunEditorRenderer</renderer-class>
</renderer>
<renderer>
<component-family>org.primefaces.extensions.component</component-family>
<renderer-type>org.primefaces.extensions.component.ClockPickerRenderer</renderer-type>
<renderer-class>org.primefaces.extensions.component.clockpicker.ClockPickerRenderer</renderer-class>
</renderer>

<client-behavior-renderer>
<client-behavior-renderer-type>org.primefaces.extensions.behavior.JavascriptBehaviorRenderer</client-behavior-renderer-type>
Expand Down
Loading

0 comments on commit 2168e75

Please sign in to comment.