001: /*
002: * File : $Source: /usr/local/cvs/alkacon/com.alkacon.opencms.formgenerator/src/com/alkacon/opencms/formgenerator/CmsSelectionField.java,v $
003: * Date : $Date: 2008-02-19 11:55:26 $
004: * Version: $Revision: 1.4 $
005: *
006: * This file is part of the Alkacon OpenCms Add-On Module Package
007: *
008: * Copyright (c) 2007 Alkacon Software GmbH (http://www.alkacon.com)
009: *
010: * The Alkacon OpenCms Add-On Module Package is free software:
011: * you can redistribute it and/or modify
012: * it under the terms of the GNU General Public License as published by
013: * the Free Software Foundation, either version 3 of the License, or
014: * (at your option) any later version.
015: *
016: * The Alkacon OpenCms Add-On Module Package is distributed
017: * in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with the Alkacon OpenCms Add-On Module Package.
024: * If not, see http://www.gnu.org/licenses/.
025: *
026: * For further information about Alkacon Software GmbH, please see the
027: * company website: http://www.alkacon.com.
028: *
029: * For further information about OpenCms, please see the
030: * project website: http://www.opencms.org.
031: */
032:
033: package com.alkacon.opencms.formgenerator;
034:
035: import org.opencms.i18n.CmsMessages;
036: import org.opencms.util.CmsStringUtil;
037:
038: import java.util.Iterator;
039: import java.util.List;
040:
041: /**
042: * Represents a selection of options.<p>
043: *
044: * @author Thomas Weckert
045: *
046: * @version $Revision: 1.4 $
047: *
048: * @since 7.0.4
049: */
050: public class CmsSelectionField extends A_CmsField {
051:
052: /** HTML field type: select box. */
053: private static final String TYPE = "select";
054:
055: /**
056: * Returns the type of the input field, e.g. "text" or "select".<p>
057: *
058: * @return the type of the input field
059: */
060: public static String getStaticType() {
061:
062: return TYPE;
063: }
064:
065: /**
066: * @see com.alkacon.opencms.formgenerator.I_CmsField#buildHtml(CmsFormHandler, org.opencms.i18n.CmsMessages, String, boolean)
067: */
068: public String buildHtml(CmsFormHandler formHandler,
069: CmsMessages messages, String errorKey, boolean showMandatory) {
070:
071: StringBuffer buf = new StringBuffer();
072: String fieldLabel = getLabel();
073: String errorMessage = "";
074: String mandatory = "";
075:
076: if (CmsStringUtil.isNotEmpty(errorKey)) {
077:
078: if (CmsFormHandler.ERROR_MANDATORY.equals(errorKey)) {
079: errorMessage = messages.key("form.error.mandatory");
080: } else if (CmsStringUtil.isNotEmpty(getErrorMessage())) {
081: errorMessage = getErrorMessage();
082: } else {
083: errorMessage = messages.key("form.error.validation");
084: }
085:
086: errorMessage = messages.key("form.html.error.start")
087: + errorMessage
088: + messages.key("form.html.error.end");
089: fieldLabel = messages.key("form.html.label.error.start")
090: + fieldLabel
091: + messages.key("form.html.label.error.end");
092: }
093:
094: if (isMandatory() && showMandatory) {
095: mandatory = messages.key("form.html.mandatory");
096: }
097:
098: // line #1
099: if (showRowStart(messages.key("form.html.col.two"))) {
100: buf.append(messages.key("form.html.row.start"))
101: .append("\n");
102: }
103:
104: // line #2
105: buf.append(messages.key("form.html.label.start")).append(
106: fieldLabel).append(mandatory).append(
107: messages.key("form.html.label.end")).append("\n");
108:
109: // line #3
110: buf.append(messages.key("form.html.field.start")).append(
111: "<select name=\"").append(getName()).append("\"")
112: .append(
113: formHandler.getFormConfiguration()
114: .getFormFieldAttributes()).append(">")
115: .append("\n");
116:
117: List selected = getSelectedItems();
118: // add the options
119: Iterator i = getItems().iterator();
120: while (i.hasNext()) {
121:
122: CmsFieldItem curOption = (CmsFieldItem) i.next();
123: String selStr = "";
124: if (selected.contains(curOption)) {
125: selStr = " selected=\"selected\"";
126: }
127:
128: buf.append("<option value=\"").append(curOption.getValue())
129: .append("\"").append(selStr).append(">").append(
130: curOption.getLabel()).append("</option>\n");
131: }
132:
133: buf.append("</select>").append(errorMessage).append("\n");
134:
135: buf.append(messages.key("form.html.field.end")).append("\n");
136:
137: if (showRowEnd(messages.key("form.html.col.two"))) {
138: buf.append(messages.key("form.html.row.end")).append("\n");
139: }
140:
141: return buf.toString();
142: }
143:
144: /**
145: * @see com.alkacon.opencms.formgenerator.I_CmsField#getType()
146: */
147: public String getType() {
148:
149: return TYPE;
150: }
151:
152: /**
153: * @see com.alkacon.opencms.formgenerator.A_CmsField#needsItems()
154: */
155: public boolean needsItems() {
156:
157: return true;
158: }
159:
160: }
|