001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.riotfamily.common.util.FormatUtils;
031: import org.riotfamily.forms.Container;
032: import org.riotfamily.forms.ContainerElement;
033: import org.riotfamily.forms.DHTMLElement;
034: import org.riotfamily.forms.Editor;
035: import org.riotfamily.forms.Element;
036: import org.riotfamily.forms.ErrorUtils;
037: import org.riotfamily.forms.MessageUtils;
038: import org.riotfamily.forms.TemplateUtils;
039: import org.riotfamily.forms.event.Button;
040: import org.riotfamily.forms.event.JavaScriptEvent;
041: import org.riotfamily.forms.request.FormRequest;
042:
043: /**
044: * Element that visually groups other elements.
045: */
046: public class ElementGroup extends TemplateElement implements
047: ContainerElement, DHTMLElement {
048:
049: private Container container = new Container();
050:
051: private String labelKey;
052:
053: private boolean labelItems = true;
054:
055: private boolean collapsible;
056:
057: private boolean expanded = true;
058:
059: private boolean clientHasExpandedState;
060:
061: public ElementGroup() {
062: super ("group");
063: addComponent("elements", container);
064: addComponent("expandButton", new ExpandButton());
065: }
066:
067: public List getElements() {
068: return container.getElements();
069: }
070:
071: public void addElement(Element element) {
072: container.addElement(element);
073: }
074:
075: public void removeElement(Element element) {
076: container.removeElement(element);
077: }
078:
079: public void setLabelKey(String key) {
080: labelKey = key;
081: }
082:
083: public boolean isLabelItems() {
084: return labelItems;
085: }
086:
087: public void setLabelItems(boolean labelItems) {
088: this .labelItems = labelItems;
089: }
090:
091: public String getLabel() {
092: if (labelKey == null) {
093: return "";
094: }
095: return MessageUtils.getMessage(this , labelKey);
096: }
097:
098: public String getStyleClass() {
099: if (labelKey != null) {
100: return FormatUtils.toCssClass(labelKey);
101: }
102: return null;
103: }
104:
105: public void setCollapsible(boolean collapsible) {
106: this .collapsible = collapsible;
107: this .expanded = !collapsible;
108: }
109:
110: public boolean isCollapsible() {
111: return this .collapsible;
112: }
113:
114: public boolean isExpanded() {
115: return this .expanded;
116: }
117:
118: protected void processRequestCompontents(FormRequest request) {
119: if (clientHasExpandedState) {
120: super .processRequestCompontents(request);
121: }
122: }
123:
124: protected void renderComponents(PrintWriter writer) {
125: if (!expanded) {
126: Iterator it = getElements().iterator();
127: while (it.hasNext()) {
128: Element element = (Element) it.next();
129: if (ErrorUtils.hasErrors(element)) {
130: expanded = true;
131: break;
132: }
133: if (element.isRequired() && element instanceof Editor) {
134: Editor editor = (Editor) element;
135: if (editor.getValue() == null) {
136: expanded = true;
137: break;
138: }
139: }
140: }
141: }
142: clientHasExpandedState = expanded;
143: super .renderComponents(writer);
144: }
145:
146: protected void toggle() {
147: expanded = !expanded;
148: if (expanded && !clientHasExpandedState) {
149: if (getFormListener() != null) {
150: getFormListener().elementChanged(this );
151: }
152: focus();
153: }
154: }
155:
156: public String getInitScript() {
157: return collapsible && expanded ? TemplateUtils
158: .getInitScript(this ) : null;
159: }
160:
161: private class ExpandButton extends Button {
162:
163: private ExpandButton() {
164: }
165:
166: public String getLabel() {
167: return "toggle";
168: }
169:
170: public String getCssClass() {
171: return expanded ? "button button-collapse"
172: : "button button-expand";
173: }
174:
175: protected void onClick() {
176: toggle();
177: }
178:
179: public int getEventTypes() {
180: return JavaScriptEvent.ON_CLICK;
181: }
182: }
183: }
|