package com.nexirius.multimail.datamodel;
import com.nexirius.framework.datamodel.*;
import com.nexirius.util.XString;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MailModel extends StructModel {
public static String[] MAIL_FIELDS = new String[]{
"greeting", "name", "firstName", "email", "text1", "text2", "text3", "text4", "ps"
};
public static final String FIELD_doNotSend = "doNotSend";
public static final String FIELD_send = "sent";
public static final String FIELD_email = "email";
public static final String FIELD_greeting = "greeting";
public static final String FIELD_name = "name";
public static final String FIELD_firstName = "firstName";
public static final String FIELD_text1 = "text1";
public static final String FIELD_text2 = "text2";
public static final String FIELD_text3 = "text3";
public static final String FIELD_text4 = "text4";
public static final String FIELD_ps = "ps";
public static final String FIELD_sentDate = "sentDate";
public static final String FIELD_fullName = "fullName";
public static ModelFlag SELECTED_FLAG = new ModelFlag("SELECED");
BooleanModel doNotSend;
BooleanModel sent;
StringModel email;
StringModel greeting;
StringModel firstName;
StringModel name;
TextModel ps;
StringModel sentDate;
StringModel fullName;
public static final String FIELD_selected = "selected";
protected SelectedModel selected;
public MailModel() {
super("Mail");
init();
}
static class SelectedModel extends StringModel {
public SelectedModel() {
}
public SelectedModel(String value, String fieldName) {
super(value, fieldName);
init();
}
private void init() {
addDataModelListener(new DataModelAdaptor() {
public void dataModelGrabFocus(DataModelEvent event) {
setFlag(SELECTED_FLAG, !getFlag(SELECTED_FLAG));
}
});
}
public String getIconId() {
return getFlag(SELECTED_FLAG) ? "selected" : "notSelected";
}
}
private void init() {
append(selected = new SelectedModel("", FIELD_selected));
append(doNotSend = new BooleanModel(false, FIELD_doNotSend));
append(sent = new BooleanModel(false, FIELD_send));
append(email = new StringModel("@", FIELD_email));
append(greeting = new StringModel("", FIELD_greeting));
append(name = new StringModel("", FIELD_name));
append(firstName = new StringModel("", FIELD_firstName));
append(new StringModel("", FIELD_text1));
append(new StringModel("", FIELD_text2));
append(new StringModel("", FIELD_text3));
append(new StringModel("", FIELD_text4));
append(ps = new TextModel("", FIELD_ps));
append(sentDate = new StringModel("", FIELD_sentDate));
sentDate.setFlag(ModelFlag.GRAY, true);
append(fullName = new StringModel("", FIELD_fullName));
fullName.setTransient(true);
name.addDataModelListener(new NameListener());
firstName.addDataModelListener(new NameListener());
appendMethod(new DefaultDataModelCommand("doResetSent"));
}
public boolean needSend() {
return !doNotSend.getBoolean() && !sent.getBoolean();
}
public void setSent(boolean on) {
sent.setBoolean(on);
if (on) {
sentDate.setText(new SimpleDateFormat("dd.MM.yyy HH:mm.ss").format(new Date()));
//((ArrayModel)getParentDataModel()).setHightlightedItem(this);
}
}
public boolean isSelected() {
return selected.getFlag(SELECTED_FLAG);
}
public String getText(String template, ReplaceStringsModel replaceStrings) {
String ret = replaceMembers(template);
ret = replaceStrings.replace(ret);
ret = replaceMembers(ret);
return ret;
}
private String replaceMembers(String template) {
XString xs = new XString(template);
DataModelEnumeration e = this.getEnumeration();
while (e.hasMore()) {
DataModel child = e.next();
xs.replace("$(" + child.getFieldName() + ")", child.getChildText(null));
}
String ret = xs.toString();
return ret;
}
public String getBackgroundColorId() {
if (doNotSend.getBoolean()) {
return "doNotSend";
}
return sent.getBoolean() ? "sent" : "notSent";
}
public void doResetSent() {
sent.setBoolean(false);
sentDate.setText("");
}
public void toggleDoNotSend() {
doNotSend.setBoolean(!doNotSend.getBoolean());
}
class NameListener extends DataModelAdaptor {
public void dataModelChangeValue(DataModelEvent event) {
StringBuffer sb = new StringBuffer(256);
sb.append(name.getText());
sb.append(' ');
sb.append(firstName.getText());
fullName.setText(sb.toString());
}
}
}