001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.cocoon.forms.FormsException;
023: import org.apache.cocoon.forms.event.ProcessingPhaseEvent;
024: import org.apache.cocoon.forms.event.ProcessingPhaseListener;
025: import org.apache.cocoon.forms.event.WidgetEventMulticaster;
026: import org.apache.cocoon.forms.formmodel.library.Library;
027: import org.apache.cocoon.forms.formmodel.library.LibraryManager;
028:
029: import org.apache.commons.lang.StringUtils;
030:
031: /**
032: * The {@link WidgetDefinition} part of a Form widget, see {@link Form} for more information.
033: *
034: * @version $Id: FormDefinition.java 449149 2006-09-23 03:58:05Z crossley $
035: */
036: public class FormDefinition extends AbstractContainerDefinition {
037:
038: private ProcessingPhaseListener listener;
039:
040: private Library localLibrary;
041:
042: public FormDefinition(LibraryManager libraryManager) {
043: super ();
044: localLibrary = libraryManager.newLibrary();
045: }
046:
047: public Library getLocalLibrary() {
048: return localLibrary;
049: }
050:
051: public void resolve() throws Exception {
052: List parents = new ArrayList();
053: parents.add(this );
054: resolve(parents, this );
055:
056: // TODO: test if this actually gets called!
057: checkCompleteness();
058: }
059:
060: public Widget createInstance() {
061: Form form = new Form(this );
062: createWidgets(form);
063: return form;
064: }
065:
066: public void addProcessingPhaseListener(
067: ProcessingPhaseListener listener) {
068: this .listener = WidgetEventMulticaster.add(this .listener,
069: listener);
070: }
071:
072: public boolean hasProcessingPhaseListeners() {
073: return this .listener != null;
074: }
075:
076: public void fireEvent(ProcessingPhaseEvent event) {
077: if (this .listener != null) {
078: this .listener.phaseEnded(event);
079: }
080: }
081:
082: public void addWidgetDefinition(WidgetDefinition definition)
083: throws Exception, DuplicateIdException {
084: // Check that no child is named "submit" if this form has no id. This causes some weird behaviour
085: // in HTML as it collides with the submit() function on the <form> element...
086: if ("submit".equals(definition.getId())
087: && StringUtils.isEmpty(this .getId())) {
088: throw new FormsException(
089: "Top-level widgets should not be named 'submit' to avoid problems "
090: + "with HTML <form> elements.", definition
091: .getLocation());
092: }
093:
094: super .addWidgetDefinition(definition);
095: }
096:
097: /**
098: * @return Returns the listener.
099: */
100: public ProcessingPhaseListener getProcessingPhaseListener() {
101: return listener;
102: }
103: }
|