Package net.sf.cannagrower.data

Source Code of net.sf.cannagrower.data.HardwarePropertiesFormatter

package net.sf.cannagrower.data;

import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;
import java.io.Serializable;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import net.sf.cannagrower.data.hardware.Room;
import net.sf.cannagrower.data.hardware.Lamp;
import net.sf.cannagrower.data.hardware.LampCoolTube;
import net.sf.cannagrower.data.hardware.Intractor;
import net.sf.cannagrower.data.hardware.Extractor;
import net.sf.cannagrower.data.hardware.Ventilator;
import net.sf.cannagrower.data.hardware.Medium;
import net.sf.cannagrower.data.hardware.MediumAerophonic;
import net.sf.cannagrower.data.hardware.MediumHydrophonic;
import net.sf.cannagrower.data.hardware.MediumSoil;
import net.sf.cannagrower.data.hardware.Cooler;
import net.sf.cannagrower.data.hardware.Heater;
import net.sf.cannagrower.data.hardware.Humidifier;
import net.sf.cannagrower.data.hardware.Dehumidificator;
import net.sf.cannagrower.data.hardware.Diverse;
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;

/**
* This class define a hardware with his property
* @see net.sf.cannagrower.data.Culture
* @author alois_cochard@users.sf.net
*
*/
public class Hardware extends Data implements Comparable<Data> {
  static final long serialVersionUID = 1L;


  /**
   * This method return all avaible Hardware class
   *
   * @return Avaible hardwares class
   */
  public static Vector<Class<?>> getHardwares() {
    Vector<Class<?>> hardwares = new Vector<Class<?>>();
    // TODO HARDWARES / Add here new hardware type
    hardwares.add(Room.class);
   
    hardwares.add(Lamp.class);
    hardwares.add(LampCoolTube.class);
   
    hardwares.add(Intractor.class);
    hardwares.add(Extractor.class);
    hardwares.add(Ventilator.class);
   
    hardwares.add(MediumAerophonic.class);
    hardwares.add(MediumHydrophonic.class);
    hardwares.add(MediumSoil.class);
   
    hardwares.add(Cooler.class);
    hardwares.add(Heater.class);
   
    hardwares.add(Humidifier.class);
    hardwares.add(Dehumidificator.class);
   
    hardwares.add(Diverse.class);
   
    return hardwares;
  }

  public static Icon getIcon(Class<?> type){
    if(type.equals(Cooler.class))        {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/cold_16.png"));}
    if(type.equals(Dehumidificator.class))    {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/wet_16.png"));}
    if(type.equals(Diverse.class))        {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/window_new_16.png"));}
    if(type.equals(Extractor.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/blow_16.png"));}
    if(type.equals(Heater.class))        {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/fire_16.png"));}
    if(type.equals(Humidifier.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/wet_16.png"));}
    if(type.equals(Intractor.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/blow_16.png"));}
    if(type.equals(Lamp.class))          {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/light_16.png"));}
    if(type.equals(LampCoolTube.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/light_16.png"));}
    if(type.equals(Medium.class))        {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
    if(type.equals(MediumAerophonic.class))    {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
    if(type.equals(MediumHydrophonic.class))  {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
    if(type.equals(MediumSoil.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/plants_16.png"));}
    if(type.equals(Room.class))          {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/box_16.png"));}
    if(type.equals(Ventilator.class))      {return new ImageIcon(Hardware.class.getResource("/net/sf/cannagrower/images/data/blow_16.png"));}
   
    return null;
  }
 
  private Date installation = new Date();

  private Date uninstallation = null;

  private Properties properties = new DataProperties(new HardwarePropertiesFormatter(),this);

  public DataSettings getSettings(){return new DataSettings(Hardware.class,5);}
 
  public long getRank(){return getInstallation().getTime();}
 
  public int compare(Data data){
    if(data instanceof Hardware){
      Hardware hardware=(Hardware)data;
      return this.installation.compareTo(hardware.installation);
    }
    return super.compare(data);
  }
 
  /**
   * This return hardware unique attribute
   *
   * @return True if more than one hardware can't be installed
   */
  public boolean isUnique() {
    return false;
  }

  /**
   * This method return hardware installation date
   *
   * @return Hardware installation date
   */
  public Date getInstallation() {
    return installation;
  }

  /**
   * This method define hardware installation date
   *
   * @param installation hardware installation date
   */
  public void setInstallation(Date installation) {
    if (installation == null) {
      return;
    }

    if (this.installation != null) {
      if (DateFormat.getDateInstance(DateFormat.SHORT).format(this.installation).equals(
          DateFormat.getDateInstance(DateFormat.SHORT).format(installation))) {
        return;
      }
    }

    if (installation == this.installation) {
      return;
    }
    if (uninstallation != null) {
      if (installation.after(uninstallation)) {
        return;
      }
    }
    this.installation = installation;
    setModified(true);
  }

  /**
   * This method return hardware uninstallation date
   *
   * @return hardware uninstallation date
   */
  public Date getUninstallation() {
    return uninstallation;
  }

  /**
   * This method define hardware uninstallation date
   *
   * @param uninstallation hardware uninstallation date
   */
  public void setUninstallation(Date uninstallation) {

    if (uninstallation == null && this.uninstallation == null) {
      return;
    }

    if (this.uninstallation != null && uninstallation!=null) {
      if (DateFormat.getDateInstance(DateFormat.SHORT).format(this.uninstallation).equals(
          DateFormat.getDateInstance(DateFormat.SHORT).format(uninstallation))) {
        return;
      }
    }
   
    if(uninstallation!=null){
      if(uninstallation.before(installation)){uninstallation=installation;}
    }

    this.uninstallation = uninstallation;
    setModified(true);
  }

  /**
   * This method check if the hardware is actually installed
   *
   * @return True if hardware is actually installed
   */
  public boolean isInstalled() {
    if (installation.after(new Date())) {
      return false;
    }
    if (uninstallation == null) {
      return true;
    }
    return uninstallation.after(new Date());
  }

  /**
   * This method return hardware properties
   *
   * @return Hardware properties
   */
  public Properties getProperties() {
    return properties;
  }

  /**
   * This method convert hardware to a human readable string
   *
   * @return Description of hardware
   */
  public String toString() {
    return toPrefix()
        + Messages.getMessage(Hardware.class.getSimpleName()
            .toLowerCase()
            + "." + getClass().getSimpleName().toLowerCase());
  }
 
  public String toPrefix(){
    String prefix = "";
    if (!isInstalled()) {
      if (uninstallation == null) {
        // To be installed
        prefix = Messages.getMessage(Messages.hardwareInstallToDo);
      } else {
        // Uninstalled
        prefix = Messages.getMessage(Messages.hardwareUninstalled);
      }
      prefix = "(" + prefix.toLowerCase() + ") ";
    }
    return prefix;
  }
}

/**
* This class manage hardware properties format
*
* @author alois_cochard@users.sf.net
*
*/
class HardwarePropertiesFormatter 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){
    String value=raw;
    if(key.equals(Messages.hardwareMediumSubstrat)){
      // MEDIUM / Substrat
      value=Messages.getMessage(raw);
    }
    return value;
  }
 
  public String propertyValueToRaw(String key,String value){
    String raw=value;
    if(key.equals(Messages.hardwareMediumSubstrat)){
      // MEDIUM / Substrat
      if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratClay))){
        // Clay
        raw=Messages.hardwareMediumSubstratClay;
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratCoco))){
        // Coco
        raw=Messages.hardwareMediumSubstratCoco;
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratNone))){
        // None
        raw=Messages.hardwareMediumSubstratNone;
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratRockwool))){
        // Rockwool
        raw=Messages.hardwareMediumSubstratRockwool;
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratSoil))){
        // Soil
        raw=Messages.hardwareMediumSubstratSoil;
      }
    }
    return raw;
  }
 
  /**
   * 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) {
    return Messages.getMessage(key + Messages.hardwarePropertiesValueFormat);
  }

  /**
   * 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.hardwareHeight)
        || key.equals(Messages.hardwareLength)
        || key.equals(Messages.hardwareWidth)) {
      // _______________ LENGHT / HEIGHT / WIDTH
      String[] suffixs = { "m", "f" };
      return Properties.formatNumericFloatWithSuffix(value, suffixs);
    } else if (key.equals(Messages.hardwarePower)) {
      // ______________ POWER
      return Properties.formatNumericFloatWithSuffix(value, "W");
    } else if (key.equals(Messages.hardwareLampType)) {
      // ______________ LAMP / TYPE
      value=value.toLowerCase();
      if(value.equals(Messages.getMessage(Messages.hardwareLampTypeHps).toLowerCase())){
        return Messages.getMessage(Messages.hardwareLampTypeHps);
      }else if(value.equals(Messages.getMessage(Messages.hardwareLampTypeMh).toLowerCase())){
        return Messages.getMessage(Messages.hardwareLampTypeMh);
      }else if(value.equals(Messages.getMessage(Messages.hardwareLampTypeNeon).toLowerCase())){
        return Messages.getMessage(Messages.hardwareLampTypeNeon);
      }else{
        return "";
      }
    } else if (key.equals(Messages.hardwareLampColour)) {
      // ______________ LAMP / COLOUR
      return Properties.formatNumericFloatWithSuffix(value, "K");
    } else if (key.equals(Messages.hardwareAirFlowRate)) {
      // ______________ AIR / FLOW RATE
      return Properties.formatNumericFloatWithSuffix(value, "m3/h");
    } else if (key.equals(Messages.hardwareAirThermostat)) {
      // ______________ AIR / THERMOSTAT
      String[] suffixs = { "�C", "�F" };
      return Properties.formatNumericFloatWithSuffix(value, suffixs);
    } else if (key.equals(Messages.hardwareAirCapacityHumidity)) {
      // ______________ AIR / HUMIDITY
      return Properties.formatNumericFloatWithSuffix(value, "l/h");
    } else if (key.equals(Messages.hardwareMediumSubstrat)) {
      // ______________ MEDIUM / SUBSTRAT
      value=value.toLowerCase();
      if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratClay).toLowerCase())){
        return Messages.getMessage(Messages.hardwareMediumSubstratClay);
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratCoco).toLowerCase())){
        return Messages.getMessage(Messages.hardwareMediumSubstratCoco);
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratNone).toLowerCase())){
        return Messages.getMessage(Messages.hardwareMediumSubstratNone);
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratRockwool).toLowerCase())){
        return Messages.getMessage(Messages.hardwareMediumSubstratRockwool);
      }else if(value.equals(Messages.getMessage(Messages.hardwareMediumSubstratSoil).toLowerCase())){
        return Messages.getMessage(Messages.hardwareMediumSubstratSoil);
      }else{
        return "";
      }
    } else if (key.equals(Messages.hardwareMediumCapacityPlants)) {
      // ______________ MEDIUM / CAPACITY
      return Properties.formatNumericInteger(value);
    } else if (key.equals(Messages.hardwareMediumCapacityTank)) {
      // ______________ MEDIUM / TANK
      return Properties.formatNumericFloatWithSuffix(value, "l");
    }
    return value;
  }
}
TOP

Related Classes of net.sf.cannagrower.data.HardwarePropertiesFormatter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.