View Javadoc

1   /*
2    * @(#)$Id: PDFTool.java 736 2009-08-14 09:20:17Z kurmannu $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      :   PDF tools.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Dec 26, 2006     ukurmann    Initial Release
15   * Mar 24, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package hacks;
28  
29  import java.awt.Color;
30  import java.awt.image.BufferedImage;
31  import java.io.File;
32  import java.io.FileNotFoundException;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  import java.util.logging.Level;
36  import java.util.logging.Logger;
37  
38  import org.sigtec.ink.Note;
39  import org.sigtec.util.Constant;
40  import org.ximtec.igesture.core.GestureClass;
41  import org.ximtec.igesture.core.GestureSet;
42  import org.ximtec.igesture.core.SampleDescriptor;
43  
44  import com.lowagie.text.BadElementException;
45  import com.lowagie.text.Document;
46  import com.lowagie.text.DocumentException;
47  import com.lowagie.text.Element;
48  import com.lowagie.text.Font;
49  import com.lowagie.text.FontFactory;
50  import com.lowagie.text.Image;
51  import com.lowagie.text.Paragraph;
52  import com.lowagie.text.pdf.PdfPCell;
53  import com.lowagie.text.pdf.PdfPTable;
54  import com.lowagie.text.pdf.PdfWriter;
55  
56  
57  /**
58   * PDF tools.
59   * 
60   * @version 1.0 Dec 2006
61   * @author Ueli Kurmann, igesture@uelikurmann.ch
62   * @author Beat Signer, signer@inf.ethz.ch
63   */
64  public class PDFTool {
65  
66     private static final Logger LOGGER = Logger
67           .getLogger(PDFTool.class.getName());
68  
69     public static int NUMBER_OF_COLUMNS = 3;
70  
71  
72     /**
73      * Creates a new PDF document.
74      * 
75      * @param file the file of the new PDF document.
76      * @return the pdf document.
77      */
78     public static Document createDocument(File file) {
79        final Document document = new Document();
80  
81        try {
82           PdfWriter.getInstance(document, new FileOutputStream(file));
83        }
84        catch (final FileNotFoundException e) {
85           LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
86        }
87        catch (final DocumentException e) {
88           LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
89        }
90  
91        return document;
92     } // createDocument
93  
94  
95     /**
96      * Creates a table filled with a sample gesture and the corresponding name.
97      * 
98      * @param set the gesture set.
99      * @param columns the number of columns in the table.
100     * @return the PDF table.
101     */
102    public static PdfPTable createGestureSetTable(GestureSet set, int columns) {
103       final PdfPTable table = createTable(columns);
104 
105       for (final GestureClass gestureClass : set.getGestureClasses()) {
106          table.addCell(createImageCell(gestureClass));
107       }
108 
109       for (int i = 0; i <= set.size() % 3; i++) {
110          table.addCell(Constant.EMPTY_STRING);
111       }
112 
113       return table;
114    } // createGestureSetTable
115 
116 
117    public static PdfPTable createTable(int columns) {
118       return new PdfPTable(columns);
119    } // createTables
120 
121 
122    /**
123     * @see createGestureSetTable
124     * @param set the gesture set.
125     * @return the PDF table.
126     */
127    public static PdfPTable createGestureSetTable(GestureSet set) {
128       return createGestureSetTable(set, NUMBER_OF_COLUMNS);
129    } // createGestureSetTable
130 
131 
132    /**
133     * Creates a table cell with a gesture sample and the name.
134     * 
135     * @param gestureClass the gesture class.
136     * @return the table cell.
137     */
138    public static PdfPCell createImageCell(GestureClass gestureClass) {
139       final PdfPCell cell = new PdfPCell();
140 
141       try {
142          final Image img = Image
143                .getInstance(getImage(gestureClass), Color.black);
144          cell.addElement(img);
145          cell.setBackgroundColor(new Color(0xFF, 0xFF, 0xFF));
146          cell.setHorizontalAlignment(Element.ALIGN_CENTER);
147          cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
148          cell.addElement(createTitle(gestureClass.getName()));
149       }
150       catch (final BadElementException e) {
151          LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
152       }
153       catch (final IOException e) {
154          LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
155       }
156 
157       return cell;
158    } // createImageCell
159 
160 
161    /**
162     * Creates an empty table cell.
163     * 
164     * @return the empty table cell.
165     */
166    public static PdfPCell createEmptyCell() {
167       final PdfPCell cell = new PdfPCell();
168       return cell;
169    } // createEmptyCell
170 
171 
172    /**
173     * Creates a buffered image of the gesture class.
174     * 
175     * @param gestureClass the gesture class.
176     * @return the buffered image of the gesture class.
177     */
178    private static BufferedImage getImage(GestureClass gestureClass) {
179       final JNote jnote = new JNote(200, 200);
180       try{
181       jnote.setNote(gestureClass.getDescriptor(SampleDescriptor.class)
182             .getSamples().get(0).getGesture());
183       }catch(Exception e){
184         e.printStackTrace();
185       }
186       
187       return jnote.getImage();
188    } // getImage
189 
190 
191    /**
192     * Creates the title of the gesture class.
193     * 
194     * @param text the title.
195     * @return the title of the gesture class
196     */
197    private static Element createTitle(String text) {
198       final Font font = FontFactory.getFont("Helvetica", 10, Font.BOLDITALIC,
199             Color.BLACK);
200       final Paragraph p = new Paragraph(text, font);
201       p.setAlignment(Element.ALIGN_CENTER);
202       p.setLeading(font.size() * 1.2f);
203       return p;
204    } // createTitle
205 
206 }