[jboss-cvs] jboss-seam/src/pdf/org/jboss/seam/pdf/ui ...

Norman Richards norman.richards at jboss.com
Sun Dec 24 11:21:33 EST 2006


  User: nrichards
  Date: 06/12/24 11:21:33

  Added:       src/pdf/org/jboss/seam/pdf/ui         ITextComponent.java
                        UIDocument.java UIFont.java UIImage.java
                        UIList.java UIListItem.java UIPage.java
                        UIParagraph.java
  Log:
  check in itext preview
  
  Revision  Changes    Path
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/ITextComponent.java
  
  Index: ITextComponent.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  
  import javax.faces.*;
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.convert.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  
  import java.awt.Color;
  import java.io.*;
  import java.util.List;
  
  import org.jboss.seam.ui.JSF;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public abstract class ITextComponent
      extends UIComponentBase
  {
      public static final String COMPONENT_FAMILY = "org.jboss.seam.pdf";    
  
    
      /**
       * get the current Itext object
       */
      abstract public Object getITextObject();
  
      /**
       * signal that the component should create it's managed object
       */
      abstract public void createITextObject();
  
      /** 
       * subcomponents should implement this
       */ 
      abstract public void add(Object other);
  
  
      /**
       *  look up the tree for an itext font
       */
      public Font getFont() {
          UIFont fontComponent = (UIFont) findITextParent(this, UIFont.class);
          return fontComponent == null ? null : fontComponent.getFont();
      }
  
      /**
       * look up the tree for the itext document
       */
      public Document findDocument() {
          ITextComponent parent = findITextParent(this, UIDocument.class);
          if (parent != null) {
              return (Document) parent.getITextObject();
          } else {
              return null;
          }
      }
  
  
      /**
       * find the first parent that is an itext component);
       */
      public ITextComponent findITextParent(UIComponent parent) {
          return findITextParent(parent, null);
      }
  
      /**
       * find the first parent that is an itext component of a given type
       */
      public ITextComponent findITextParent(UIComponent parent, Class c) {
          if (parent == null) {
              return null;
          }
          
          if (parent instanceof ITextComponent) {
              if (c==null || parent.getClass().equals(c)) {
                  return (ITextComponent) parent;
              }
          }
  
          return findITextParent(parent.getParent(),c);
      }
  
  
      /**
       * add a component (usually the current itext object) to the itext parent's itext object
       */
      public void addToITextParent(Object obj) {
          ITextComponent parent = findITextParent(getParent());
          if (parent != null) {
              parent.add(obj);
          } else {
              throw new RuntimeException("Couldn't find ITextComponent parent for component " + 
                                         this.getClass().getName());
          }
          
      }
  
      // ------------------------------------------------------
  
      @Override
      public String getFamily()
      {
          return COMPONENT_FAMILY;
      }
  
      @Override
      public boolean getRendersChildren() {
          return true;
      }
  
      @Override
      public void encodeBegin(FacesContext context) 
          throws IOException
      {
          createITextObject();
      }
  
      @Override
      public void encodeEnd(FacesContext context) 
          throws IOException
      {
          Object obj = getITextObject();
          if (obj != null) {
              addToITextParent(getITextObject());
          }
      }
  
      @Override
      public void encodeChildren(FacesContext context)
          throws IOException
      {
          for (UIComponent child: (List<UIComponent>) this.getChildren()) {
              // ugly hack to be able to capture facelets text
              if (child.getFamily().equals("facelets.LiteralText")) {
                  ResponseWriter response = context.getResponseWriter();
                  StringWriter stringWriter = new StringWriter();
                  ResponseWriter cachingResponseWriter = response.cloneWithWriter(stringWriter);
                  context.setResponseWriter(cachingResponseWriter);
  
                  JSF.renderChild(context, child);
  
                  context.setResponseWriter(response);
  
                  String text = stringWriter.getBuffer().toString();
                  Font   font = getFont();
                  if (font == null) {
                      Chunk chunk = new Chunk(text);
                      //chunk.setBackground(new Color(140,50,50));
                      add(chunk);
                  } else {
                      add(new Chunk(text, getFont()));
                  }
              } else {
                  encode(context, child);
              }
          }
      }
  
  
      public void encode(FacesContext context,
                         UIComponent component) 
          throws IOException, 
                 FacesException 
      {
          if (!component.isRendered()) {
              return;
          }
  
          component.encodeBegin(context);
  
          if (component.getChildCount() > 0) {
              if (component.getRendersChildren()) {
                  component.encodeChildren(context);
              } else {
                  for (UIComponent child: (List<UIComponent>) this.getChildren()) {
                      encode(context, child);
                  }
              }
          }
  
          component.encodeEnd(context);
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIDocument.java
  
  Index: UIDocument.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import org.jboss.seam.pdf.PDFStore;
  
  import javax.faces.context.*;
  import javax.faces.component.*;
  
  import java.io.*;
  import java.util.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIDocument 
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIDocument";
  
      Document document;
      ByteArrayOutputStream stream;
      String id;
  
      public Object getITextObject() {
          return document;
      }
  
      public void createITextObject() {
          document=new Document();
      }
  
      public void add(Object o) {
          if (o instanceof Element) {
              try {
                  document.add((Element) o);
              } catch (DocumentException e) {
                  throw new RuntimeException(e);
              }
          } else {
              throw new IllegalArgumentException("cannot add " + o);
          }
      }
  
      @Override
      public void encodeBegin(FacesContext context) 
          throws IOException
      {
          id = PDFStore.instance().newId();
          document = new Document();
          stream = new ByteArrayOutputStream();
  
          try {
              PdfWriter.getInstance(document, stream);
  
              document.open();
          } catch (DocumentException e) {
              throw new RuntimeException(e);
          }
  
          ResponseWriter response = context.getResponseWriter();
          response.startElement("html", this);
          response.startElement("head", this);
          response.startElement("meta", this);
          response.writeAttribute("http-equiv", "Refresh", null);
          response.writeAttribute("content", "0; URL=seam-pdf.seam?pdfId="+id, null);
  
          response.endElement("meta");
          response.endElement("head");
  
          response.startElement("body",this);
      }
  
      @Override
      public void encodeEnd(FacesContext context) 
          throws IOException
      {
          document.close();
  
          PDFStore.instance().saveData(id,stream.toByteArray());
          
          ResponseWriter response = context.getResponseWriter();
          response.endElement("body");
          response.endElement("html");
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIFont.java
  
  Index: UIFont.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  import java.io.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIFont
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIParagraph";
  
  
      Font   font; 
      int    family = Font.UNDEFINED;
      int    size   = Font.UNDEFINED;
      String style; 
  
      public void setFamily(String name) {
          family = Font.getFamilyIndex(name);
      }
  
      public void setSize(int size) {
          this.size = size;
      }
  
      public void setStyle(String style) {
          this.style = style;
      }
  
      public Font getFont() {
          return font;
      }
         
              
      public Object getITextObject() {
          return null; // we don't add to this component, so skip
      }
  
      public void createITextObject() {
          font = new Font(family, size);
          if (style != null) {
              font.setStyle(style);
          }
      }
  
      public void add(Object o) {
          addToITextParent(o);
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIImage.java
  
  Index: UIImage.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  
  import java.io.*;
  import java.net.URL;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIImage
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIImage";
  
      Image image;
      
      String resource;
      float  rotation;
      float  height;
      float  width;
  
      public void setResource(String resource) {
          this.resource = resource;
      }
  
      public void setRotation(float rotation) {
          this.rotation = rotation;
      }
  
      public void setHeight(float height) {
          this.height = height;
      }
  
      public void setWidth(float width) {
          this.width = width;
      }
  
      public Object getITextObject() {
          return image;
      }
  
      public void createITextObject() {
          
          URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
          if (url == null) {
              throw new RuntimeException("cannot locate image resource " + resource);
          }
          try {
              image = Image.getInstance(url);
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
  
          if (rotation != 0) {
              image.setRotationDegrees(rotation);
          }
  
          if (height>0 || width > 0) {
              image.scaleAbsolute(width, height);
          }
      }
  
      public void add(Object o) {
          throw new RuntimeException("can't add " + o.getClass().getName() + " to image");
          //image.add(o);
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIList.java
  
  Index: UIList.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  import java.io.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIList
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIList";
  
      List list;
      
      boolean numbered = true;
      boolean lettered = false;
      float   indent   = 20f;
  
      public void setNumbered(boolean numbered) {
          this.numbered = numbered;
      }
  
      public void setLettered(boolean lettered) {
          this.lettered = lettered;
      }
  
      public void setIndent(float indent) {
          this.indent = indent;
      }
  
      public Object getITextObject() {
          return list;
      }
  
      public void createITextObject() {
          list = new List(numbered, lettered, indent);
      }
  
      public void add(Object o) {
          list.add(o);
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIListItem.java
  
  Index: UIListItem.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  import java.io.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIListItem
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE = "org.jboss.seam.pdf.ui.UIListItem";
  
      ListItem listItem;
      
      public Object getITextObject() {
          return listItem;
      }
  
      public void createITextObject() {
          listItem = new ListItem();
      }
  
      public void add(Object o) {
          listItem.add(o);
      }
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIPage.java
  
  Index: UIPage.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  import java.io.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIPage
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIParagraph";
  
      public Object getITextObject() {
          return null;
      }
  
      public void createITextObject() {
          
      }
  
      public void add(Object o) {
          addToITextParent(o);
      }
  
      @Override
      public void encodeBegin(FacesContext context) 
          throws IOException
      {
          super.encodeBegin(context);
          Document document = findDocument();
          if (document != null) {
              try {
                  document.newPage();
              } catch (DocumentException e) {
                  throw new RuntimeException(e);
              }
          } else {
              throw new IllegalArgumentException("Cannot find parent document");
          }
      }
  
  }
  
  
  
  1.1      date: 2006/12/24 16:21:33;  author: nrichards;  state: Exp;jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIParagraph.java
  
  Index: UIParagraph.java
  ===================================================================
  package org.jboss.seam.pdf.ui;
  
  import javax.faces.event.*;
  import javax.faces.context.*;
  import javax.faces.component.*;
  import javax.servlet.http.*;
  import java.io.*;
  
  import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  
  public class UIParagraph
      extends ITextComponent
  {
      public static final String COMPONENT_TYPE   = "org.jboss.seam.pdf.ui.UIParagraph";
  
      Paragraph paragraph;
      String alignment;
  
      public void setAlignment(String alignment) {
          this.alignment = alignment;
      }
  
      public Object getITextObject() {
          return paragraph;
      }
  
      public void createITextObject() {
          Font font = getFont();
          if (font == null) {
              paragraph = new Paragraph();
          } else {
              paragraph = new Paragraph("", font);
          }
  
          if (alignment !=null) {
              paragraph.setAlignment(alignment);
          }
      }
  
      public void add(Object o) {
          paragraph.add(o);
      }
  }
  
  
  



More information about the jboss-cvs-commits mailing list