001: /*
002: * Copyright 2007 Hippo
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS"
012: * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cocoon.forms;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.cocoon.forms.formmodel.AbstractContainerWidget;
023: import org.apache.cocoon.forms.formmodel.Action;
024: import org.apache.cocoon.forms.formmodel.ActionDefinition;
025: import org.apache.cocoon.forms.formmodel.Form;
026: import org.apache.cocoon.forms.formmodel.Repeater;
027: import org.apache.cocoon.forms.formmodel.RepeaterAction;
028: import org.apache.cocoon.forms.formmodel.RowAction;
029: import org.apache.cocoon.forms.formmodel.Widget;
030: import org.apache.cocoon.forms.formmodel.WidgetState;
031: import org.apache.cocoon.forms.formmodel.Repeater.RepeaterRow;
032: import org.apache.commons.lang.StringUtils;
033:
034: /**
035: * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
036: *
037: * @version $Id: HippoFormHelper.java 8544 2007-10-16 09:48:55Z ddam $
038: */
039: public class HippoFormHelper {
040:
041: public static String ACTION_ADD_AFTER = "add-after";
042: public static String ACTION_DELETE = "delete";
043: public static String ACTION_MOVE_UP = "move-up";
044: public static String ACTION_MOVE_DOWN = "move-down";
045:
046: public static String REPEATER_ACTION_ADD = "add-row";
047: public static String REPEATER_ACTION_DELETE_ROWS = "delete-rows";
048:
049: /**
050: * Tries to retrieve a repeater which is related to a widget.
051: * @param widget a widget
052: * @return the related repeater element
053: */
054: public static Repeater getRelatedRepeater(Widget widget) {
055: if (widget == null) {
056: return null;
057: }
058: if (widget instanceof Repeater) {
059: return (Repeater) widget;
060: } else if (widget instanceof RepeaterAction) {
061: return ((RepeaterAction) widget).getRepeater();
062: }
063: return getRelatedRepeater(widget.getParent());
064: }
065:
066: private static String getActionCommand(Widget w) {
067: if (w instanceof Action) {
068: return ((ActionDefinition) w.getDefinition())
069: .getActionCommand();
070: }
071: return null;
072:
073: }
074:
075: public static boolean isAction(Widget w, String cmd) {
076: String actionCmd = getActionCommand(w);
077: return (actionCmd != null && actionCmd.equals(cmd));
078: }
079:
080: /**
081: * Sets bookmark attributes on the form widget, which is later used to generate a html bookmark.
082: * This is used to "jump back" to a widget context, after form submit.
083: *
084: * @param w the widget which caused the form submit, usually an action widget
085: */
086: public static void setBookmark(Widget w) {
087: if (w != null) {
088: Form form = w.getForm();
089:
090: if (w instanceof RowAction) {
091: int currIndex = new Integer((String) w.getParent()
092: .getId()).intValue();
093: Repeater r = getRelatedRepeater(w);
094: if (r != null) {
095: form.setAttribute("activeElement", r);
096: form.setAttribute("activeRow", new Integer(
097: currIndex));
098: }
099: return;
100: }
101:
102: if (HippoFormHelper.isAction(w,
103: HippoFormHelper.REPEATER_ACTION_ADD)) {
104: Repeater r = getRelatedRepeater(w);
105: form.setAttribute("activeElement", r);
106: form
107: .setAttribute("activeRow", new Integer(r
108: .getSize())); // last row
109: return;
110: }
111: form.setAttribute("activeElement", w);
112: }
113:
114: }
115:
116: /**
117: * Generates a string containing a javascript array of html element ids, which is used to automatically "jump" down to
118: * to the context the user was at before the form was submitted.
119: * @param form The form for which to generate a bookmark
120: * @return a string containing a javascript array of HTML element ids
121: */
122: public static String getBookmark(Form form) {
123: List bookmark = new ArrayList();
124: if (form.getAttribute("activeElement") != null) {
125: Widget widget = (Widget) form.getAttribute("activeElement");
126: Integer activeRowInt = (Integer) form
127: .getAttribute("activeRow");
128:
129: if (activeRowInt != null) {
130: int activeRow = activeRowInt.intValue();
131: Repeater repeater = getRelatedRepeater(widget);
132:
133: if (activeRow >= 0 && repeater.getSize() > 0) {
134: if (activeRow >= repeater.getSize()) {
135: activeRow = repeater.getSize() - 1;
136: }
137: RepeaterRow row = repeater.getRow(activeRow);
138: Iterator it = row.getChildren();
139: Widget currWidget;
140: while (it.hasNext()) {
141: currWidget = (Widget) it.next();
142: if (currWidget.getState().equals(
143: WidgetState.ACTIVE)) {
144: bookmark.add(currWidget
145: .getRequestParameterName());
146: }
147: }
148: } else {
149: // tro to find the add action for the repeater
150: String elementId = StringUtils.substringBefore(
151: repeater.getId(), XSDHelper
152: .getRevSeparator());
153: String repeaterAddId = new StringBuffer(elementId)
154: .append(XSDHelper.getRevSeparator())
155: .append("crepeatadd").toString();
156: Widget addAction = form.lookupWidget(repeaterAddId);
157: if (addAction != null) {
158: bookmark.add(addAction
159: .getRequestParameterName());
160: } else {
161: Widget repParent = repeater.getParent();
162: if (repParent instanceof AbstractContainerWidget) {
163: Iterator it = ((AbstractContainerWidget) repParent)
164: .getChildren();
165: Widget currWidget;
166: while (it.hasNext()) {
167: currWidget = (Widget) it.next();
168: if (currWidget.getState().equals(
169: WidgetState.ACTIVE)) {
170: bookmark.add(currWidget
171: .getRequestParameterName());
172: }
173: }
174: }
175: }
176: }
177: } else {
178: bookmark.add(widget.getRequestParameterName());
179: }
180: }
181: StringBuffer bookmarkStr;
182: if (bookmark.size() > 0) {
183: bookmarkStr = new StringBuffer("[");
184: for (int i = 0; i < bookmark.size(); i++) {
185: if (i > 0)
186: bookmarkStr.append(",");
187: bookmarkStr.append("\"").append(
188: (String) bookmark.get(i)).append("\"");
189: }
190: bookmarkStr.append("]");
191: } else
192: bookmarkStr = new StringBuffer("null");
193: form.removeAttribute("activeElement");
194: form.removeAttribute("gotoFirst");
195: form.removeAttribute("activeRow");
196: return bookmarkStr.toString();
197: }
198: }
|