package net.sf.cannagrower.data;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import net.sf.cannagrower.data.event.Document;
import net.sf.cannagrower.data.event.EnvironmentAirState;
import net.sf.cannagrower.data.event.EnvironmentMediumState;
import net.sf.cannagrower.data.event.EnvironmentLightPeriod;
import net.sf.cannagrower.data.event.Plants;
import net.sf.cannagrower.data.event.PlantsAdd;
import net.sf.cannagrower.data.event.PlantsCut;
import net.sf.cannagrower.data.event.PlantsHarvest;
import net.sf.cannagrower.data.event.PlantsMove;
import net.sf.cannagrower.data.event.PlantsRemove;
import net.sf.cannagrower.data.event.PlantsMoveTo;
import net.sf.cannagrower.data.event.PlantsMoveFrom;
import net.sf.cannagrower.data.event.DocumentPicture;
import net.sf.cannagrower.data.event.Comment;
import net.sf.cannagrower.gui.PanelEvent;
import net.sf.cannagrower.i18n.Messages;
import net.sf.orexio.common.Properties;
import net.sf.orexio.common.PropertiesFormatter;
import net.sf.orexio.common.PropertyFormatException;
import net.sf.orexio.lopf.Data;
import net.sf.orexio.lopf.DataProperties;
import net.sf.orexio.lopf.DataSettings;
//TODO to_v0.2.0_Beta: Add event to manage medium modification (ph+/-,bloom,...)
//TODO to_v0.2.0_Beta: Add event to notice splitting plant (split<>cut<>pince)...
/**
* This class define an event with his properties
* @see net.sf.cannagrower.data.Plantation
* @author alois_cochard@users.sf.net
*
*/
public class Event extends Data implements Serializable{
static final long serialVersionUID = 1L;
private Date date = new Date();
protected DataProperties properties;
public Event(){getProperties();}
/**
* This method return all avaible event class (to be used to add new event in a plantation)
*
* @return Avaible hardwares class
*/
public static Vector<Class<?>> getEvents() {
Vector<Class<?>> events = new Vector<Class<?>>();
// TODO EVENTS / Add here new event type
events.add(PlantsAdd.class);
events.add(PlantsMoveFrom.class);
events.add(PlantsMoveTo.class);
events.add(PlantsRemove.class);
events.add(PlantsCut.class);
events.add(PlantsHarvest.class);
events.add(EnvironmentLightPeriod.class);
events.add(EnvironmentAirState.class);
events.add(EnvironmentMediumState.class);
events.add(DocumentPicture.class);
events.add(Comment.class);
return events;
}
public DataSettings getSettings(){return new DataSettings(Event.class,5);}
public long getRank(){return getDate().getTime();}
public int compare(Data data){
if(data instanceof Event){
Event event=(Event)data;
return this.getDate().compareTo(event.getDate());
}
return super.compare(data);
}
public String toString() {
return toString(null);
}
protected String toString(String append) {
String base=java.text.DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).format(date)
+ " - "
+ getLabel();
if(append==null){return base;}
return base+" - ["+append+"]";
}
protected String getLabel(){
return Messages.getMessage(Event.class.getSimpleName().toLowerCase()+ "." + getClass().getSimpleName().toLowerCase());
}
public static Icon getIcon(Class<?> type){
if(type.equals(Comment.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/klipper_dock_16.png"));}
if(type.equals(Document.class)) {return null;}
if(type.equals(DocumentPicture.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/camera_16.png"));}
if(type.equals(EnvironmentAirState.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/blow_16.png"));}
if(type.equals(EnvironmentLightPeriod.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/light_16.png"));}
if(type.equals(EnvironmentMediumState.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/science_16.png"));}
if(type.equals(Plants.class)) {return null;}
if(type.equals(PlantsAdd.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
if(type.equals(PlantsCut.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/cut_16.png"));}
if(type.equals(PlantsHarvest.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/cut_16.png"));}
if(type.equals(PlantsMove.class)) {return null;}
if(type.equals(PlantsMoveFrom.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
if(type.equals(PlantsMoveTo.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/move_16.png"));}
if(type.equals(PlantsRemove.class)) {return new ImageIcon(Event.class.getResource("/net/sf/cannagrower/images/data/remove_16.png"));}
return null;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
if (date != null) {
if (this.date != null) {
if (DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date).equals(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this.date))) {
return;
}
}
} else {
if (this.date == null) {
return;
}
}
this.date = date;
setModified(true);
}
public PanelEvent getViewer() {
return new PanelEvent();
}
public Properties getProperties() {
if(properties==null){
properties=new DataProperties(new EventPropertiesFormatter(),this);
}
return properties;
}
}
/**
* This class manage hardware properties format
*
* @author alois_cochard@users.sf.net
*
*/
class EventPropertiesFormatter implements PropertiesFormatter,Serializable {
static final long serialVersionUID = 1L;
/**
* This method return formated property value.
* @see net.sf.cannagrower.util.PropertiesFormatter
*/
public String propertyFormat(String key,String value) throws PropertyFormatException{
value=isValueFormated(key,value);
if(value.length()<1){throw new PropertyFormatException(getValueFormat(key));}
return value;
}
public String propertyKeyToTitle(String key){return Messages.getMessage(key);}
public String propertyRawToValue(String key,String raw){return raw;}
public String propertyValueToRaw(String key,String value){return value;}
/**
* This method return a string message corresponding to format used for gived key
* @param key key that format are seek
* @return Message corresponding to format to use
*/
private String getValueFormat(String key) {
String message=key;
if (key.equals(Messages.eventPlants)) {
message=Messages.hardwareMediumCapacityPlants;
}else if (key.equals(Messages.eventAirTemperature)){
message=Messages.hardwareAirThermostat;
}
message+=Messages.hardwarePropertiesValueFormat;
return Messages.getMessage(message);
}
/**
* This method check if the value is formated correctly for gived key
* @param key key used to check valued format
* @param value value to check
* @return New formated value or '' (empty string) if value format is invalid
*/
private String isValueFormated(String key, String value) {
if (key.equals(Messages.eventPlants)) {
// _______________ PLANTS
return Properties.formatNumericInteger(value);
} else if (key.equals(Messages.eventLightPeriod)) {
// ______________ LIGH PERIOD (Hours)
return Properties.formatNumericFloatWithSuffix(value, "h");
} else if (key.equals(Messages.eventMediumEc) || key.equals(Messages.eventMediumPh)) {
// ______________ MEDIUM (EC/PH)
return Properties.formatNumericFloat(value);
} else if (key.equals(Messages.eventAirHygronomy)) {
// ______________ AIR (Hygro)
return Properties.formatNumericFloatWithSuffix(value, "\u0025");
} else if (key.equals(Messages.eventAirTemperature)) {
// ______________ AIR / THERMOSTAT
String[] suffixs = { "\u00B0C", "\u00B0F" };
return Properties.formatNumericFloatWithSuffix(value, suffixs);
}
return value;
}
}