/**
* 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 ch.unizh.campusmgnt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.RadioButtonGroupElement;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.translator.Translator;
/**
*
* Description:<br>
* Display a textarea for separated values and two radios for chosing tab or comma as delimiter
*
* <P>
* Initial Date: 19.12.2005 <br>
*
* @author Alexander Schneider
*/
public class SeparatedValueInputForm extends Form {
private TextAreaElement idata;
private RadioButtonGroupElement delimiter;
private List rows;
private int numOfValPerLine;
private int numOfLines;
/**
* @param name
* @param trans
*/
public SeparatedValueInputForm(String name, Translator translator) {
super(name, translator);
idata = new TextAreaElement("form.step1.sepvalin", 5, 80);
addFormElement("addsepval", idata);
String[] keys = new String[] {"tab","comma"};
String[] values = new String[] {translate("form.step1.delimiter.tab"),translate("form.step1.delimiter.comma")};
delimiter = new RadioButtonGroupElement(true, "form.step1.delimiter", keys, values);
delimiter.select("tab", true);
addFormElement("delimiter", delimiter);
addSubmitKey("next"); // wizard style
}
/**
* @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
*/
public boolean validate() {
if (idata.isEmpty("form.legende.mandatory")) return false;
String errorKey = processInput();
if(errorKey != null){
idata.setErrorKey(errorKey);
return false;
}else{
return true;
}
}
/**
* add input values to a list
* @return String error, if return null, input values are added to list successfully
*/
private String processInput(){
String error = null;
String[] lines = idata.getValue().split("\r?\n");
this.numOfLines = lines.length;
this.rows = new ArrayList(this.numOfLines);
List inputRows = new ArrayList(this.numOfLines);
String d;
if (delimiter.getSelectedKey().startsWith("t")) d = "\t"; else d = ",";
int maxNumOfCols = 0;
for (int i = 0; i < numOfLines; i++) {
String line = lines[i];
List lineFields;
if(!line.equals("")){
Object[] values = line.split(d,-1);
if(values.length > maxNumOfCols) maxNumOfCols = values.length;
lineFields = new ArrayList(Arrays.asList(values));
}else{
lineFields = new ArrayList(maxNumOfCols);
lineFields.add(" ");
}
inputRows.add(lineFields);
}
this.numOfValPerLine = maxNumOfCols;
for (Iterator iter = inputRows.iterator(); iter.hasNext();) {
List lineFields = (ArrayList) iter.next();
int numOfLineFields = lineFields.size();
if (numOfLineFields != maxNumOfCols){
for(int i=0; i < maxNumOfCols - numOfLineFields; i++){
lineFields.add(" ");
}
}
// add an additional column to reduce number of preconditions
// e.g. user adds lines with only one value; user adds lines with no empty value in a line
lineFields.add(" ");
}
for (Iterator iter = inputRows.iterator(); iter.hasNext();) {
List lineFields = (List) iter.next();
rows.add(lineFields.toArray());
}
return error;
}
/**
*
* @return a list containing every input line as an object array. The fields of an object array are the
* separared values
*/
public List getInputRows(){
return rows;
}
/**
*
* @return int number of separated values per line
*/
public int getNumOfValPerLine(){
return numOfValPerLine;
}
/**
*
* @return int number of all lines with separated values
*/
public int getNumOfLines(){
return numOfLines;
}
}