Package org.connla

Source Code of org.connla.Connlon

package org.connla;

import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;

public class Connlon {
  // value
  private String itsValue = "";

  // header
  private boolean isHeader = false;

  // align
  private static final short ALIGN_NONE = 0;

  private static final short ALIGN_LEFT = 1;

  private static final short ALIGN_CENTER = 2;

  private static final short ALIGN_RIGHT = 3;

  private short itsAlign = ALIGN_NONE;

  // bold
  private boolean itsBold = false;

  // italics
  private boolean itsItalic = false;

  // underline
  private boolean itsUnderline = false;

  // strikethru
  private boolean itsStrikethru = false;

  // foreground color
  private String itsForegroundColor = null;

  // background color
  private String itsBackgroundColor = null;

  // value/name?
  private String itsName = null;

  // disable
  private boolean itsDisable = false;

  // URL
  private String itsURL = null;
  private boolean itsURLinPDF = false;

  public static final short MODUS_VIEW = 1;

  public static final short MODUS_EDIT = 2;

  public static final short TYPE_TEXT = 1;

  public static final short TYPE_COMBOBOX = 2;

  private String itsType = null;

  public static final short INPUT_HIDDEN = 1;

  public static final short INPUT_TEXT = 2;

  public static final short INPUT_CHECKBOX = 3;

  public static final short INPUT_SELECT = 4;

  // constructors
  public Connlon(String theValue) {
    itsValue = theValue;
  }

  public Connlon() {
    itsValue = "";
  }

  /*
   * public Viewon(String theName, String theLink, String theColor, String
   * theBackgroundColor) { itsName = theName; // itsLink = theLink; //
   * itsColor = theColor; itsBackgroundColor = theBackgroundColor; }
   *
   * Viewon(String theName, String theLink, String theColor, String
   * theBackgroundColor, short theAlign, short theFont) { itsName = theName; //
   * itsLink = theLink; // itsColor = theColor; itsBackgroundColor =
   * theBackgroundColor; itsAlign = theAlign; // itsFont = theFont; }
   *
   * Viewon(String theName, String theLink, String theColor, String
   * theBackgroundColor, short theAlign) { itsName = theName; // itsLink =
   * theLink; // itsColor = theColor; itsBackgroundColor = theBackgroundColor;
   * itsAlign = theAlign; }
   *
   * public Viewon(String theName, String theLink, String theColor) { itsName =
   * theName; // itsLink = theLink; // itsColor = theColor; }
   *
   * public Viewon(String theName, String theLink, short theAlign, short
   * theFont) { itsName = theName; // itsLink = theLink; itsAlign = theAlign; //
   * itsFont = theFont; }
   *
   * public Viewon(String theName, String theLink, short theAlign) { itsName =
   * theName; // itsLink = theLink; itsAlign = theAlign; }
   *
   * public Viewon(String theName, String theLink) { itsName = theName; //
   * itsLink = theLink; }
   *
   * public Viewon(Short theName, String theLink) { itsName =
   * theName.toString(); // itsLink = theLink; itsAlign = ALIGN_RIGHT; }
   */
  public Connlon(Short theName) {
    itsName = theName.toString();
    itsAlign = ALIGN_RIGHT;
  }

  public Connlon(Double theName) {
    itsName = theName.toString();
    itsAlign = ALIGN_RIGHT;
  }

  public Connlon(Float theName) {
    itsName = theName.toString();
    itsAlign = ALIGN_RIGHT;
  }

  /*
   * // text or checkbox public Viewon(String theName, int theInput, String
   * theParameter, String theBackgroundColor) { this(theName, theInput,
   * theParameter, false, theBackgroundColor); }
   *
   * public Viewon(String theName, int theInput, String theParameter) {
   * this(theName, theInput, theParameter, false, null); }
   *
   * public Viewon(String theName, int theInput, boolean theDisabled, String
   * theParameter) { this(theName, theInput, theParameter, theDisabled, null); } //
   * text or checkbox public Viewon(String theName, int theInput, String
   * theParameter, boolean theDisabled, String theBackgroundColor) { switch
   * (theInput) { case INPUT_TEXT: itsName = " <input type='text'"; if
   * (theParameter != null) { itsName += " name='" + theParameter + "'"; } if
   * (theName != null) { itsName += " value='" + theName + "'"; } if
   * (theDisabled) { itsName += " disabled"; } if (theBackgroundColor != null) {
   * itsName += " style='background-color:" + theBackgroundColor + "'"; }
   * itsName += ">"; break; case INPUT_CHECKBOX: itsName = " <input
   * type='checkbox'"; if (theParameter != null) { itsName += " name='" +
   * theParameter + "'"; } if (theName != null) { itsName += " value='" +
   * theName + "'"; } /* todo if (theName != null &&
   * theName.toLowerCase().equals("true")) { itsName += " checked"; } if
   * (theDisabled) { itsName += " disabled"; }
   *//*
      * itsName += ">"; break; case INPUT_HIDDEN: itsName = " <input
      * type='hidden' name='" + theParameter + "' value='" + theName + "'>" +
      * theName;//todo disabled?? break; default: break; } } // select public
      * Viewon(String theName, int theInput, String theParameter, String[]
      * theValues, String[] theLabels, int theSelected) { this(theName,
      * theInput, theParameter, theValues, theLabels, theSelected, false); } //
      * select public Viewon(String theName, int theInput, String
      * theParameter, String[] theValues, String[] theLabels) { this(theName,
      * theInput, theParameter, theValues, theLabels, 0, false); } // select
      * public Viewon(String theName, int theInput, String theParameter,
      * String[] theValues, String[] theLabels, boolean theDisabled) {
      * this(theName, theInput, theParameter, theValues, theLabels, 0,
      * theDisabled); } // select public Viewon(String theName, int theInput,
      * String theParameter, String[] theValues, String[] theLabels, int
      * theSelected, boolean theDisabled) { this(theName, null, null, null);
      * switch (theInput) { case INPUT_SELECT: itsName = " <select name='" +
      * theParameter + "'"; if (theDisabled) { itsName += " disabled"; }
      * itsName += ">"; for (int i = 0; i < theValues.length; i++) { itsName += "
      * <option value='" + theValues[i] + "'"; if (i == theSelected) { itsName += "
      * selected"; } itsName += ">" + theLabels[i] + " </option>"; } itsName += "
      * </select>"; break; } }
      *
      * protected String getName() { if (itsName == null ||
      * itsName.equals("null")) {//TODO return ""; } return itsName; }
      *
      * protected String getType() { return itsType; }
      *
      * protected boolean hasType() { if (itsType == null) { return false; }
      * return true; }
      */

  // value
  protected int length() {
    return itsValue.length();
  }

  public String getName() {
    return itsValue;
  }//TODO

  // header
  public void setHeader() {
    isHeader = true;
  }

  // align
  protected short getAlign() {
    return itsAlign;
  }

  public void setAlignLeft() {
    itsAlign = ALIGN_LEFT;
  }

  public void setAlignCenter() {
    itsAlign = ALIGN_CENTER;
  }

  public void setAlignRight() {
    itsAlign = ALIGN_RIGHT;
  }

  // URL
  protected boolean hasURL() {
    if (itsURL == null) {
      return false;
    }
    return true;
  }

  protected String getURL() {
    return itsURL;
  }

  protected void setURL(String theURL) {
    itsURL = theURL;
  }

  protected void showURLinPDF() {
    itsURLinPDF = true;
  }

  protected void hideURLinPDF() {
    itsURLinPDF = false;
  }

  // foreground color
  protected boolean hasForegroundColor() {
    if (itsForegroundColor == null) {
      return false;
    }
    return true;
  }

  protected String getForegroundColor() {
    return itsForegroundColor;
  }

  protected void setForegroundColor(String theColor) {
    itsForegroundColor = theColor;
  }

  // background color
  protected boolean hasBackgroundColor() {
    if (itsBackgroundColor == null) {
      return false;
    }
    return true;
  }

  protected String getBackgroundColor() {
    return itsBackgroundColor;
  }

  protected void setBackgroundColor(String theColor) {
    itsBackgroundColor = theColor;
  }

  // bold
  public boolean isBold() {
    return itsBold;
  }

  public void setBold() {
    itsBold = true;
  }

  public void setBold(boolean aBold) {
    itsBold = aBold;
  }

  // italics
  public boolean isItalic() {
    return itsItalic;
  }

  public void setItalic() {
    itsItalic = true;
  }

  public void setItalics(boolean aItalic) {
    itsItalic = aItalic;
  }

  // underline
  public boolean isUnderline() {
    return itsUnderline;
  }

  public void setUnderline() {
    itsUnderline = true;
  }

  public void setUnderline(boolean aUnderline) {
    itsUnderline = aUnderline;
  }

  // striekthru
  public boolean isStrikethru() {
    return itsStrikethru;
  }

  public void setStrikethru() {
    itsStrikethru = true;
  }

  public void setStrikethru(boolean aStrikethru) {
    itsStrikethru = aStrikethru;
  }

  // exports
  protected String toHTML() {
    StringBuffer aSB = new StringBuffer("<");
    if (isHeader) {
      aSB.append("th");
    } else {
      aSB.append("td");
    }
    //align
    // isHeader will be taken care of by CSS
    // TODO turn into switch statement?
    if (itsAlign == ALIGN_LEFT || itsAlign == ALIGN_NONE) {
      aSB.append(" align='left'");
    } else if (itsAlign == ALIGN_CENTER) {
      aSB.append(" align='center'");
    } else if (itsAlign == ALIGN_RIGHT) {
      aSB.append(" align='right'");
    } else {
      //TODO warning?
    }
    // background color
    if (hasBackgroundColor()) {
      aSB.append(" bgcolor='");
      aSB.append(getBackgroundColor());
      aSB.append("'");
    }
    aSB.append(">");
    // foreground color
    if (hasForegroundColor()) {
      aSB.append("<font color='");
      aSB.append(getForegroundColor());
      aSB.append("'>");
    }
    // bold
    if (itsBold) {
      aSB.append("<b>");
    }
    // italics
    if (itsItalic) {
      aSB.append("<i>");
    }
    // underline
    if (itsUnderline) {
      aSB.append("<u>");
    }
    // strikethru
    if (itsStrikethru) {
      aSB.append("<strike>");
    }
    //url
    if (itsURL != null) {
      aSB.append("<a href='");
      aSB.append(itsURL);
      aSB.append("'>");
    }
    // value
    aSB.append(itsValue);
    //url
    if (itsURL != null) {
      aSB.append("</a>");
    }
    if (hasForegroundColor()) {
      aSB.append("</font>");
    }
    // strikethru
    if (itsStrikethru) {
      aSB.append("</strike>");
    }
    // underline
    if (itsUnderline) {
      aSB.append("</u>");
    }
    // italics
    if (itsItalic) {
      aSB.append("</i>");
    }
    // bold
    if (itsBold) {
      aSB.append("</b>");
    }
    if (isHeader) {
      aSB.append("</th>");
    } else {
      aSB.append("</td>");
    }

    return aSB.toString();
  }

  protected String toCSV() {
    return itsValue;
  }

  protected String toTXT(int theWidth) {
    StringBuffer aSB = new StringBuffer(itsValue);
    int aEmpty = theWidth - itsValue.length();

    // align
    if (itsAlign == ALIGN_LEFT || itsAlign == ALIGN_NONE) {
      for (int i = 0; i < aEmpty; i++) {
        aSB.append(" ");
      }
    } else if (itsAlign == ALIGN_CENTER) {
      for (int i = 0; i < aEmpty / 2; i++) {
        aSB.insert(0, " ");
      }
      for (int i = 0; i < aEmpty / 2 + (aEmpty % 2 == 0 ? 0 : 1); i++) {
        aSB.append(" ");
      }
    } else if (itsAlign == ALIGN_RIGHT) {
      for (int i = 0; i < theWidth - itsValue.length(); i++) {
        aSB.insert(0, " ");
      }
    } else {
      //TODO warning?
    }

    return aSB.toString();
  }

  public HSSFCell toXLS(HSSFCell theCell, HSSFWorkbook theWorkbook) {
    // TODO, recycle styles via cache? research how much extra time and space is used in both cases
    HSSFCellStyle aStyle = theWorkbook.createCellStyle();

    // align
    if (itsAlign == ALIGN_LEFT || itsAlign == ALIGN_NONE) {
      aStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    } else if (itsAlign == ALIGN_CENTER) {
      aStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    } else if (itsAlign == ALIGN_RIGHT) {
      aStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    } else {
      //TODO warning?
    }
    if (isHeader) {
      aStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    }
    //TODO bold, italics, underline
    if (itsForegroundColor != null) {
      aStyle
          .setFillForegroundColor(getHSSFColorIndex(itsForegroundColor));
    }
    if (itsBackgroundColor != null) {
      aStyle
          .setFillBackgroundColor(getHSSFColorIndex(itsBackgroundColor));
    }
    theCell.setCellStyle(aStyle);
    theCell.setCellValue(itsValue);

    return theCell;
  }

  public Cell toPDF() {
    Cell aCell = new Cell();
    String aFontName = FontFactory.HELVETICA;//TODO make configurable
    int aFontSize = 12;
    int aFontFace = Font.NORMAL;
    // align
    if (itsAlign == ALIGN_LEFT || itsAlign == ALIGN_NONE ) {
      aCell.setHorizontalAlignment(Element.ALIGN_LEFT);
    } else if (itsAlign == ALIGN_CENTER) {
      aCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } else if (itsAlign == ALIGN_RIGHT) {
      aCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    } else {
      //TODO warning?
    }
    if (isHeader) {
//todo set bottom line
      aFontFace = Font.BOLD;
    }
   
    if (itsBold) {
      if (itsItalic) {
        aFontFace = Font.BOLDITALIC;
      } else {
        aFontFace = Font.BOLD;
      }
    } else if (itsItalic) {
      aFontFace = Font.ITALIC;
    } else if (itsUnderline) {
      aFontFace = Font.UNDERLINE;
    } else if (itsStrikethru) {
      aFontFace = Font.STRIKETHRU;
    }
   
    //TODO black and white option?
    Chunk aChunk = null;
    if (itsURL != null && itsURLinPDF) {
      try {
        aChunk = new Chunk(itsValue, FontFactory.getFont(aFontName, aFontSize, Font.UNDERLINE, getColor("blue")));
        aChunk.setAnchor(new URL("http://www.lowagie.com/iText/"));
      } catch (MalformedURLException e) {
        //TODO
      }
    } else {
      if (itsForegroundColor != null) {
        aChunk = new Chunk(itsValue, FontFactory.getFont(aFontName, aFontSize, aFontFace, getColor(itsForegroundColor)));
      } else {
        aChunk = new Chunk(itsValue, FontFactory.getFont(aFontName, aFontSize, aFontFace));
      }
    }
    if (itsBackgroundColor != null) {
      //TODO
    }
    aCell.add(aChunk);
   
    return aCell;
  }

  //todo unify colors
 
  private static Color getColor(String theColor) {
    theColor = theColor.toLowerCase();

    if ("black".equals(theColor)) {
      return Color.black;
    } else if ("blue".equals(theColor)) {
      return Color.blue;
    } else if ("cyan".equals(theColor)) {
      return Color.cyan;
    } else if ("darkgray".equals(theColor)) {
      return Color.darkGray;
    } else if ("gray".equals(theColor)) {
      return Color.gray;
    } else if ("green".equals(theColor)) {
      return Color.green;
    } else if ("lightgray".equals(theColor)) {
      return Color.lightGray;
    } else if ("magenta".equals(theColor)) {
      return Color.magenta;
    } else if ("orange".equals(theColor)) {
      return Color.orange;
    } else if ("pink".equals(theColor)) {
      return Color.pink;
    } else if ("red".equals(theColor)) {
      return Color.red;
    } else if ("white".equals(theColor)) {
      return Color.white;
    } else if ("yellow".equals(theColor)) {
      return Color.yellow;
    } else {
      try {
        return Color.decode("#" + theColor);
      } catch (NumberFormatException e) {
        return Color.black;  //TODO exception or warning?
      }
    }
  }
 
  private static short getHSSFColorIndex(String theColor) {
    theColor = theColor.toUpperCase();

    if ("AQUA".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.AQUA().getIndex();
    } else if ("BLACK".equals(theColor) || "000000".equals(theColor)) {
      return new HSSFColor.BLACK().getIndex();
    } else if ("BLUE".equals(theColor) || "0000FF".equals(theColor)) {
      return new HSSFColor.BLUE().getIndex();
    } else if ("BLUE_GREY".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.BLUE_GREY().getIndex();
    } else if ("BRIGHT_GREEN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.BRIGHT_GREEN().getIndex();
    } else if ("BROWN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.BROWN().getIndex();
    } else if ("CORAL".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.CORAL().getIndex();
    } else if ("CORNFLOWER_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.CORNFLOWER_BLUE().getIndex();
    } else if ("DARK_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.DARK_BLUE().getIndex();
    } else if ("DARK_GREEN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.DARK_GREEN().getIndex();
    } else if ("DARK_RED".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.DARK_RED().getIndex();
    } else if ("DARK_TEAL".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.DARK_TEAL().getIndex();
    } else if ("DARK_YELLOW".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.DARK_YELLOW().getIndex();
    } else if ("GOLD".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.GOLD().getIndex();
    } else if ("GREEN".equals(theColor) || "00FF00".equals(theColor)) {
      return new HSSFColor.GREEN().getIndex();
    } else if ("GREY_25_PERCENT".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.GREY_25_PERCENT().getIndex();
    } else if ("GREY_40_PERCENT".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.GREY_40_PERCENT().getIndex();
    } else if ("GREY_50_PERCENT".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.GREY_50_PERCENT().getIndex();
    } else if ("GREY_80_PERCENT".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.GREY_80_PERCENT().getIndex();
    } else if ("INDIGO".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.INDIGO().getIndex();
    } else if ("LAVENDER".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LAVENDER().getIndex();
    } else if ("LEMON_CHIFFON".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LEMON_CHIFFON().getIndex();
    } else if ("LIGHT_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIGHT_BLUE().getIndex();
    } else if ("LIGHT_CORNFLOWER_BLUE".equals(theColor)
        || "".equals(theColor)) {
      return new HSSFColor.LIGHT_CORNFLOWER_BLUE().getIndex();
    } else if ("LIGHT_GREEN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIGHT_GREEN().getIndex();
    } else if ("LIGHT_ORANGE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIGHT_ORANGE().getIndex();
    } else if ("LIGHT_TURQUOISE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIGHT_TURQUOISE().getIndex();
    } else if ("LIGHT_YELLOW".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIGHT_YELLOW().getIndex();
    } else if ("LIME".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.LIME().getIndex();
    } else if ("MAROON".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.MAROON().getIndex();
    } else if ("OLIVE_GREEN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.OLIVE_GREEN().getIndex();
    } else if ("ORANGE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.ORANGE().getIndex();
    } else if ("ORCHID".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.ORCHID().getIndex();
    } else if ("PALE_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.PALE_BLUE().getIndex();
    } else if ("PINK".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.PINK().getIndex();
    } else if ("PLUM".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.PLUM().getIndex();
    } else if ("RED".equals(theColor) || "FF0000".equals(theColor)) {
      return new HSSFColor.RED().getIndex();
    } else if ("ROSE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.ROSE().getIndex();
    } else if ("ROYAL_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.ROYAL_BLUE().getIndex();
    } else if ("SEA_GREEN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.SEA_GREEN().getIndex();
    } else if ("SKY_BLUE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.SKY_BLUE().getIndex();
    } else if ("TAN".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.TAN().getIndex();
    } else if ("TEAL".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.TEAL().getIndex();
    } else if ("TURQUOISE".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.TURQUOISE().getIndex();
    } else if ("VIOLET".equals(theColor) || "".equals(theColor)) {
      return new HSSFColor.VIOLET().getIndex();
    } else if ("WHITE".equals(theColor) || "FFFFFF".equals(theColor)) {
      return new HSSFColor.WHITE().getIndex();
    } else if ("YELLOW".equals(theColor) || "00FFFF".equals(theColor)) {
      return new HSSFColor.YELLOW().getIndex();
    }

    return 0;//TODO exception or warning?
  }
}
TOP

Related Classes of org.connla.Connlon

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.