package net.sf.jasperreports.engine.util;

import net.sf.jasperreports.engine.JRPen;

public class JRPenUtil {
  public static void setLinePenFromPen(byte pen, JRPen linePen) {
    setLinePenFromPen(new Byte(pen), linePen);
  }
  
  public static void setLinePenFromPen(Byte pen, JRPen linePen) {
    if (pen != null)
      switch (pen.byteValue()) {
        case 5:
          linePen.setLineWidth(0.5F);
          linePen.setLineStyle((byte)0);
          break;
        case 1:
          linePen.setLineWidth(1.0F);
          linePen.setLineStyle((byte)0);
          break;
        case 2:
          linePen.setLineWidth(2.0F);
          linePen.setLineStyle((byte)0);
          break;
        case 3:
          linePen.setLineWidth(4.0F);
          linePen.setLineStyle((byte)0);
          break;
        case 4:
          linePen.setLineWidth(1.0F);
          linePen.setLineStyle((byte)1);
          break;
        case 0:
          linePen.setLineWidth(0.0F);
          linePen.setLineStyle((byte)0);
          break;
      }  
  }
  
  public static byte getPenFromLinePen(JRPen linePen) {
    float lineWidth = linePen.getLineWidth().floatValue();
    if (lineWidth <= 0.0F)
      return 0; 
    if (0.0F < lineWidth && lineWidth < 1.0F)
      return 5; 
    if (1.0F <= lineWidth && lineWidth < 2.0F) {
      if (linePen.getLineStyle().byteValue() == 1)
        return 4; 
      return 1;
    } 
    if (2.0F <= lineWidth && lineWidth < 4.0F)
      return 2; 
    return 3;
  }
  
  public static Byte getOwnPenFromLinePen(JRPen linePen) {
    if (linePen.getOwnLineWidth() == null && linePen.getOwnLineStyle() == null)
      return null; 
    return new Byte(getPenFromLinePen(linePen));
  }
}
