001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.corelib.components;
016:
017: import org.apache.tapestry.ComponentResources;
018: import org.apache.tapestry.MarkupWriter;
019: import org.apache.tapestry.annotations.Environmental;
020: import org.apache.tapestry.annotations.Inject;
021: import org.apache.tapestry.annotations.Parameter;
022: import org.apache.tapestry.corelib.base.AbstractField;
023: import org.apache.tapestry.services.FormSupport;
024: import org.apache.tapestry.services.Heartbeat;
025: import org.apache.tapestry.services.Request;
026:
027: /**
028: * Corresponds to <input type="submit">, a client-side element that can force the enclosing
029: * form to submit. The submit responsible for the form submission will post a notification that
030: * allows the application to know that it was the responsible entity. The notification is named
031: * "selected" and has no context.
032: */
033: public final class Submit extends AbstractField {
034: static final String SELECTED_EVENT = "selected";
035:
036: /**
037: * If true, then any notification sent by the component will be deferred until the end of the
038: * form submission (this is usually desirable).
039: */
040: @Parameter
041: private boolean _defer = true;
042:
043: @Environmental
044: private FormSupport _formSupport;
045:
046: @Environmental
047: private Heartbeat _heartbeat;
048:
049: @Inject
050: private ComponentResources _resources;
051:
052: @Inject
053: private Request _request;
054:
055: public Submit() {
056: }
057:
058: Submit(Request request) {
059: _request = request;
060: }
061:
062: void beginRender(MarkupWriter writer) {
063: writer.element("input", "type", "submit", "name",
064: getElementName(), "id", getClientId());
065: }
066:
067: void afterRender(MarkupWriter writer) {
068: writer.end();
069: }
070:
071: @Override
072: protected void processSubmission(FormSupport formSupport,
073: String elementName) {
074: String value = _request.getParameter(elementName);
075:
076: if (value == null)
077: return;
078:
079: Runnable sendNotification = new Runnable() {
080: public void run() {
081: _resources.triggerEvent(SELECTED_EVENT, null, null);
082: }
083: };
084:
085: // When not deferred, don't wait, fire the event now (actually, at the end of the current
086: // heartbeat). This is most likely because the Submit is inside a Loop and some contextual
087: // information will change if we defer. Another option might be to wait until the next
088: // heartbeak?
089:
090: if (_defer)
091: _formSupport.defer(sendNotification);
092: else
093: _heartbeat.defer(sendNotification);
094:
095: }
096:
097: // For testing:
098:
099: void setDefer(boolean defer) {
100: _defer = defer;
101: }
102:
103: void setup(ComponentResources resources, FormSupport support,
104: Heartbeat heartbeat) {
105: _resources = resources;
106: _formSupport = support;
107: _heartbeat = heartbeat;
108: }
109: }
|