Skip to content

Commit

Permalink
clockpicker: implementation update upon code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jxmai authored and melloware committed Jan 27, 2024
1 parent 04ae4df commit a3d3c3b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,30 @@ private void encodeScript(final FacesContext context, final ClockPicker clockPic
}

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

final Object value = clockPicker.getValue();
Object value = clockPicker.getValue();
if (value == null) {
return null;
}
else {

try {
if (clockPicker.getConverter() != null) {
// convert via registered converter
return clockPicker.getConverter().getAsString(context, clockPicker, value);
}
else {
// use built-in converter
if (value instanceof LocalTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format((LocalTime) value);
}
else if (value instanceof LocalTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format((LocalTime) value);
}
}
return "";
catch (Exception e) {
return null;
}

return null;
}

@Override
Expand All @@ -155,7 +156,7 @@ public Object getConvertedValue(FacesContext context, UIComponent component,
}
}
catch (ConverterException e) {
throw e;
return null;
}

// Delegate to global defined converter (e.g. joda or java8)
Expand All @@ -170,11 +171,9 @@ public Object getConvertedValue(FacesContext context, UIComponent component,
}
}
}
catch (ConverterException e) {
throw e;
catch (Exception e) {
return null;
}
return null;

}

}
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.time.LocalTime</type>
<type>java.lang.Object</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 @@ -16,10 +16,56 @@ PrimeFaces.widget.ExtClockPicker = PrimeFaces.widget.BaseWidget.extend({
this.jqEl = this.jqId + '_input';
this.jq = $(this.jqEl);

this.jq.clockpicker(this.cfg);
this.clockpicker = this.jq.clockpicker(this.cfg);
// pfs metadata
$(this.jqId + '_input').data(PrimeFaces.CLIENT_ID_DATA, this.id);
this.originalValue = this.jq.val();

},

// @override
refresh: function(cfg) {
// Destroy previous instance
if (this.clockpicker) {
this.clockpicker.remove()
}
// Reinitialize with new configuration
this._super(cfg);
},

// @override
destroy: function() {
this._super();
// Destroy clockpicker instance
this.remove()
},

/**
* Hides the clockpicker if it exists.
* @function hide
*/
hide: function() {
if (this.clockpicker) {
this.clockpicker.hide()
}
},

/**
* Shows the clockpicker if it exists.
* @function show
*/
show: function() {
if (this.clockpicker) {
this.clockpicker.show()
}
},
/**
* Removes the clockpicker and its event listeners if it exists.
* @function remove
*/
remove: function() {
if (this.clockpicker) {
this.clockpicker.remove()
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ public ClockPickerController() {
}

public void showTime() {
int hour = time.getHour();
int min = time.getMinute();
if (time != null) {
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);
String message = String.format("Selected hour: %d, Selected min: %d", hour, min);
addMessage(FacesMessage.SEVERITY_INFO, "Info Message", message);
}
else {
addMessage(FacesMessage.SEVERITY_ERROR, "Error Message", "Time is not selected.");
}
}

private void addMessage(FacesMessage.Severity severity, String summary, String detail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</p:growl>

<!-- EXAMPLE-SOURCE-START -->
<pe:clockpicker autoclose="true" value="#{clockPickerController.time}"></pe:clockpicker>
<pe:clockpicker id="clockpicker" autoclose="true" value="#{clockPickerController.time}" widgetVar="clockpickerWidget"></pe:clockpicker>
<p:commandButton actionListener="#{clockPickerController.showTime}" update="growl" value="Show Time" />
<!-- EXAMPLE-SOURCE-END -->
</ui:composition>

0 comments on commit a3d3c3b

Please sign in to comment.