/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.group.ui.wizard;
import java.util.ArrayList;
import java.util.List;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.translator.Translator;
import org.olat.core.logging.AssertException;
import org.olat.core.util.StringHelper;
import org.olat.group.BusinessGroup;
/**
* Description:<BR>
* Form to enter multiple group names
* <P>
* Initial Date: Oct 20, 2004
*
* @author gnaegi
*/
public class GroupNamesForm extends Form {
private TextAreaElement groupNames;
private TextElement bgMax;
private List groupNamesList;
/**
* @param name The form name
* @param trans The form translator
* @param defaultMaxValue The max value taken from original group
*/
public GroupNamesForm(String name, Translator translator, Integer defaultMaxValue) {
super(name, translator);
groupNames = new TextAreaElement("bgcopywizard.multiple.groupnames", 4, 40);
groupNames.setMandatory(true);
groupNames.setExample(translate("bgcopywizard.multiple.groupnames.example"));
addFormElement("groupNames", groupNames);
String maxString = (defaultMaxValue == null ? null : defaultMaxValue.toString());
bgMax = new TextElement("create.form.title.max", maxString, false, 6, 5);
addFormElement("fe_bgMax", bgMax);
setSubmitKey("finish"); // wizard style
}
/**
* @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
*/
public boolean validate() {
List namesList = new ArrayList();
String groupNamesString = groupNames.getValue();
String[] groups = groupNamesString.split("[\t\n\f\r]");
for (int i = 0; i < groups.length; i++) {
String groupName = groups[i].trim();
if (!groupName.matches(BusinessGroup.VALID_GROUPNAME_REGEXP)) {
groupNames.setErrorKey("bgcopywizard.multiple.groupnames.illegalGroupname");
return false;
}
// ignore lines that contains only whitespace, groupname must have
// non-whitespace
if (StringHelper.containsNonWhitespace(groupName)) {
namesList.add(groupName);
}
}
// list seems to be valid. store for later retrival
this.groupNamesList = namesList;
if (bgMax.matches("^(\\p{Digit}*)|(\\p{Space}*)$", "create.form.error.numberOrNull")) return true;
return false;
}
/**
* @return A valid list of groupnames. The list is only valid if the form
* returned the validation ok event!
*/
public List getGroupNamesList() {
if (this.groupNamesList == null) { throw new AssertException("getGroupNamesList() called prior to form EVENT_VALIDATION_OK event"); }
return this.groupNamesList;
}
/**
* @return Integer max number of group participants
*/
public Integer getGroupMax() {
String result = bgMax.getValue();
if (result.length() == 0) return null;
return new Integer(Integer.parseInt(result));
}
}