/** @return A synthetic close tag for this tag */ public final CharSequence syntheticCloseTagString() { AppendingStringBuffer buf = new AppendingStringBuffer(); buf.append("</"); if (getNamespace() != null) { buf.append(getNamespace()).append(':'); } buf.append(getName()).append('>'); return buf; }
@Override public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { // render the hidden field AppendingStringBuffer buffer = new AppendingStringBuffer("<div style=\"display:none\"><input type=\"hidden\" name=\""); buffer .append(getActionTokenHiddenFieldId()) .append("\" id=\"") .append(getActionTokenHiddenFieldId()) .append("\" value=\"") .append(actionToken) .append("\" /></div>"); getResponse().write(buffer); super.onComponentTagBody(markupStream, openTag); }
/* * (non-Javadoc) * * @see org.apache.wicket.behavior.Behavior#onComponentTag(org.apache.wicket.Component, * org.apache.wicket.markup.ComponentTag) */ @Override public void onComponentTag(Component component, ComponentTag tag) { super.onComponentTag(component, tag); if (!Form.class.isAssignableFrom(component.getClass())) { throw new WicketRuntimeException("This behavior is only applicable on a Form component"); } Form<?> form = (Form<?>) component; // Retrieve and set form name String formName = verifyFormName(form, tag); tag.put("onsubmit", "return yav.performCheck('" + formName + "', rules);"); // Open the Yav script (inlined JavaScript) AppendingStringBuffer buffer = new AppendingStringBuffer("<script>\n"); buffer.append("var rules=new Array();\n"); // Visit all form components and check for validators (and write the // appropriate Yav rules in the current inlined JavaScript) form.visitFormComponents(new YavFormComponentVisitor(buffer, form)); // Build the call to the yav.init with the proper form name buffer.append("function yavInit() {\n"); buffer.append(" yav.init('" + formName + "', rules);\n"); buffer.append("}\n"); // Close the Yav script buffer.append("</script>\n"); // Write the generated script into the response Response response = RequestCycle.get().getResponse(); response.write(buffer.toString()); }
/** * Get the client's time zone if that could be detected. * * @return The client's time zone */ public TimeZone getTimeZone() { if (timeZone == null) { String utc = getUtcOffset(); if (utc != null) { // apparently it is platform dependent on whether you get the // offset in a decimal form or not. This parses the decimal // form of the UTC offset, taking into account several // possibilities // such as getting the format in +2.5 or -1.2 int dotPos = utc.indexOf('.'); if (dotPos >= 0) { String hours = utc.substring(0, dotPos); String hourPart = utc.substring(dotPos + 1); if (hours.startsWith("+")) { hours = hours.substring(1); } int offsetHours = Integer.parseInt(hours); int offsetMins = (int) (Double.parseDouble(hourPart) * 6); // construct a GMT timezone offset string from the retrieved // offset which can be parsed by the TimeZone class. AppendingStringBuffer sb = new AppendingStringBuffer("GMT"); sb.append(offsetHours > 0 ? "+" : "-"); sb.append(Math.abs(offsetHours)); sb.append(":"); if (offsetMins < 10) { sb.append("0"); } sb.append(offsetMins); timeZone = TimeZone.getTimeZone(sb.toString()); } else { int offset = Integer.parseInt(utc); if (offset < 0) { utc = utc.substring(1); } timeZone = TimeZone.getTimeZone("GMT" + ((offset > 0) ? "+" : "-") + utc); } String dstOffset = getUtcDSTOffset(); if (timeZone != null && dstOffset != null) { TimeZone dstTimeZone = null; dotPos = dstOffset.indexOf('.'); if (dotPos >= 0) { String hours = dstOffset.substring(0, dotPos); String hourPart = dstOffset.substring(dotPos + 1); if (hours.startsWith("+")) { hours = hours.substring(1); } int offsetHours = Integer.parseInt(hours); int offsetMins = (int) (Double.parseDouble(hourPart) * 6); // construct a GMT timezone offset string from the // retrieved // offset which can be parsed by the TimeZone class. AppendingStringBuffer sb = new AppendingStringBuffer("GMT"); sb.append(offsetHours > 0 ? "+" : "-"); sb.append(Math.abs(offsetHours)); sb.append(":"); if (offsetMins < 10) { sb.append("0"); } sb.append(offsetMins); dstTimeZone = TimeZone.getTimeZone(sb.toString()); } else { int offset = Integer.parseInt(dstOffset); if (offset < 0) { dstOffset = dstOffset.substring(1); } dstTimeZone = TimeZone.getTimeZone("GMT" + ((offset > 0) ? "+" : "-") + dstOffset); } // if the dstTimezone (1 July) has a different offset then // the real time zone (1 January) try to combine the 2. if (dstTimeZone != null && dstTimeZone.getRawOffset() != timeZone.getRawOffset()) { int dstSaving = dstTimeZone.getRawOffset() - timeZone.getRawOffset(); String[] availableIDs = TimeZone.getAvailableIDs(timeZone.getRawOffset()); for (String availableID : availableIDs) { TimeZone zone = TimeZone.getTimeZone(availableID); if (zone.getDSTSavings() == dstSaving) { // this is a best guess... still the start and end of the DST should // be needed to know to be completely correct, or better yet // not just the GMT offset but the TimeZone ID should be transfered // from the browser. timeZone = zone; break; } } } } } } return timeZone; }
/** * @see org.apache.wicket.Component#onComponentTagBody(org.apache.wicket.markup.MarkupStream, * org.apache.wicket.markup.ComponentTag) */ @Override protected final void onComponentTagBody( final MarkupStream markupStream, final ComponentTag openTag) { // Iterate through choices final List<? extends T> choices = getChoices(); // Buffer to hold generated body final AppendingStringBuffer buffer = new AppendingStringBuffer(70 * (choices.size() + 1)); // Value of this choice final String selected = getValue(); // Loop through choices for (int index = 0; index < choices.size(); index++) { // Get next choice final T choice = choices.get(index); Object displayValue = getChoiceRenderer().getDisplayValue(choice); Class<?> objectClass = displayValue == null ? null : displayValue.getClass(); // Get label for choice String label = ""; if (objectClass != null && objectClass != String.class) { IConverter converter = getConverter(objectClass); label = converter.convertToString(displayValue, getLocale()); } else if (displayValue != null) { label = displayValue.toString(); } // If there is a display value for the choice, then we know that the // choice is automatic in some way. If label is /null/ then we know // that the choice is a manually created checkbox tag at some random // location in the page markup! if (label != null) { // Append option suffix buffer.append(getPrefix()); String id = getChoiceRenderer().getIdValue(choice, index); final String idAttr = getMarkupId() + "-" + getInputName() + "_" + id; // Add checkbox element buffer .append("<input name=\"") .append(getInputName()) .append("\"") .append(" type=\"checkbox\""); if (!Strings.isEmpty(getCheckBoxCssClass())) { buffer.append(" class=\"").append(getCheckBoxCssClass()).append("\""); } buffer .append((isSelected(choice, index, selected) ? " checked=\"checked\"" : "")) .append( (isEnabledInHierarchy() && !isDisabled(choice, index, selected) ? "" : " disabled=\"disabled\"")) .append(" value=\"") .append(id) .append("\" id=\"") .append(idAttr) .append("\"/>"); // Add label for checkbox String display = label; if (localizeDisplayValues()) { display = getLocalizer().getString(label, this, label); } CharSequence escaped = display; if (getEscapeModelStrings()) { escaped = Strings.escapeMarkup(display); } buffer.append("<label for=\"").append(idAttr).append("\""); if (!Strings.isEmpty(getCheckBoxLabelCssClass())) { buffer.append(" class=\"").append(getCheckBoxLabelCssClass()).append("\""); } buffer.append(">").append(escaped).append("</label>"); // Append option suffix buffer.append(getSuffix()); } } // Replace body replaceComponentTagBody(markupStream, openTag, buffer); }