//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.htmlview;
import com.nexirius.framework.datamodel.*;
import com.nexirius.util.XString;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
/**
* This class initializes the variables (in VariableStore) which hold variable specific information
* <PRE>
* $(FIELDNAME)
* $(VALUE)
* $(LABEL)
* $(TIP)
* </PRE>
*/
public class DataModelVariableCreator {
/**
* get the class which is handled by this creator
*
* @return null (default handler) or the associated DataModel class
*/
public Class getDataModelClass() {
return null;
}
/**
* stores all needed variables into a VariableStore and saves the original values in the old values store
*
* @param model
* @param variableStore the target variable store
* @param oldValues where the old values are saved to
* @param translator the translator which is used to access locale specific translations
*/
public void setVariablesFor(DataModel model, VariableStore variableStore, VariableStore oldValues, HTMLTranslator translator) {
String value = null;
if (model instanceof SimpleModel) {
value = model.getChildText(null);
if (model.getFlag(ModelFlag.TRANSLATE_VALUE)) {
value = translator.translate(value);
}
if (value.indexOf("'") > 0) {
XString xstr = new XString(value);
xstr.replace("'", "'");
value = xstr.toString();
}
// try {
// value = URLEncoder.encode(value, "UTF-8");
// }
// catch (UnsupportedEncodingException ex) {
// ex.printStackTrace();
// }
}
// this implementation sets the default set of variables for the basic DataModel types
String fieldName = model.getFieldName();
String oldFieldName = variableStore.setVariable(VariableStore.FIELDNAME, fieldName);
String oldValue = variableStore.setVariable(VariableStore.VALUE, value);
String oldLabel = variableStore.setVariable(VariableStore.LABEL, translator.translate(fieldName));
String oldTip = variableStore.setVariable(VariableStore.TIP, translator.translate(fieldName + ".tip"));
if (model instanceof ComboBoxModel || model instanceof SimpleArrayModel) {
ComboBoxModel comboBox = null;
Object array[] = null;
if (model instanceof ComboBoxModel) {
comboBox = (ComboBoxModel) model;
array = comboBox.getValueArray().getArray();
} else {
array = ((SimpleArrayModel) model).getArray();
}
boolean translate = model.getFlag(ModelFlag.TRANSLATE_VALUE);
StringBuffer option = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
option.append("<OPTION value='" + i + "'");
if (comboBox != null && i == comboBox.getInt()) {
option.append(" SELECTED>");
} else {
option.append(">");
}
if (translate) {
option.append(translator.translate(array[i].toString()));
} else {
option.append(array[i].toString());
}
option.append("</OPTION>");
}
String oldOption = variableStore.setVariable(VariableStore.OPTION, option.toString());
oldValues.setVariable(VariableStore.OPTION, oldOption);
}
oldValues.setVariable(VariableStore.FIELDNAME, oldFieldName);
oldValues.setVariable(VariableStore.VALUE, oldValue);
oldValues.setVariable(VariableStore.LABEL, oldLabel);
oldValues.setVariable(VariableStore.TIP, oldTip);
}
}