package examples.rtf2fo;

import de.vc.jfo.rtf.FieldListener;
import de.vc.jfo.rtf.listeners.SimpleDataSource;

/**
 * A simple example for importing an RTF file, writing XSL-FO file and
 * rendering into a PDF-file.
 */
public class RTF2FOFilling extends AbstractFillinExample{

  //---- Attributes ------------------------------------------------------------

  private final static String RTF_FILE  = "docs-in/rtf2fo/Invoice.rtf";
  private final static String BANK_RTF_FILE  = "docs-in/rtf2fo/BankAccount.rtf";
  private final static String FO_FILE  = "docs-out/rtf2fo/Invoice.fo";
  private final static String PDF_FILE  = "docs-out/rtf2fo/Invoice.pdf";

  //---- statics ---------------------------------------------------------------

  public static void main(String[] args) throws Exception{
    new RTF2FOFilling().run();
  }

  //---- Constructors ----------------------------------------------------------

  //---- public  ---------------------------------------------------------------

  //---- protected -------------------------------------------------------------


  /**
   * Gets a field listener that listens for some fields we want to insert
   * values for.
   * @return The field listener.
   */
  protected FieldListener getFieldListener() {

    // The DataMergeFieldListener merges data into the resulting XSL-FO tree.
  	SimpleDataSource p = new SimpleDataSource();
    p.setValue("orderDate", "12.02.2002");
    p.setValue("deliveryDate", "18.02.2002");
    p.setValue("vat", "200.00");
    p.setValue("packaging", "200.00");
    p.setValue("shipping", "200.00");
    p.setValue("total", "6600.00");
    p.setValue("discussdate", "14.02.2002");


    // field "myName" has a default value "Mr. XY" that we dont want to override
    // please uncomment next line to override the value
    //fp.setValue("myName", "Mr. ZZZ");


    // field "myCompany" is a mergefield with support of switches \b and \f.
    // if you uncomment the next line then "*** Digital Dreams ***" shouldn't
    // appear at the end of the document
    p.setValue("myCompany", "Digital Dreams");


    // JFO can also fill in table columns.
    p.setTableColumn("numbers",new String[]{"1.","2.","3."});
    p.setTableColumn("orderNo",new String[]{"1","4","7"});
    p.setTableColumn("articleNo",new String[]{"123-12-323","123-12-333","322-33-124"});
    p.setTableColumn("article",new String[]{"Product 1\n(description)","Product 2","Product 3"});

    //TableCell[] prices = new TableCell[]{
    //    new TableCell("1000.00"),new TableCell("2000.00"),new TableCell("3000.00")
    //};
    //prices[0].getBackground().setColor(Color.yellow.brighter());
    //prices[1].getBackground().setColor(Color.lightGray);
    //prices[2].getBackground().setColor(Color.yellow.brighter());

		String[] prices = new String[]{"1000.00","2000.00","3000.00"};
    p.setTableColumn("price",prices);


    // There is a {INCLUDETEXT {MERGEFIELD bankDetaislFile}}. INCLUDETEXT
    // inserts the text of a RTF file at fields position.
    p.setValue("bankDetailsFile ", BANK_RTF_FILE );


    // Attachments as list items
    // You can use ListItemBody, then jFO continues autonumbering
    // or make use of ListItem, then you can define your own
    // Item-Label
    p.setListItems("attachments", new String[]{"Anything", "Something else"} );

    return p;
  }



  protected String getRTFFile() {
    return RTF_FILE;
  }


  protected String getFOFile() {
    return FO_FILE;
  }


  protected String getPDFFile() {
    return PDF_FILE;
  }

  //---- package ---------------------------------------------------------------

  //---- privat ----------------------------------------------------------------

}
