1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
59
60
61
62
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
74
75
76
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 }
93
94
95
96
97
98
99
100
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 }
115
116
117 public static PdfPTable createTable(int columns) {
118 return new PdfPTable(columns);
119 }
120
121
122
123
124
125
126
127 public static PdfPTable createGestureSetTable(GestureSet set) {
128 return createGestureSetTable(set, NUMBER_OF_COLUMNS);
129 }
130
131
132
133
134
135
136
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 }
159
160
161
162
163
164
165
166 public static PdfPCell createEmptyCell() {
167 final PdfPCell cell = new PdfPCell();
168 return cell;
169 }
170
171
172
173
174
175
176
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 }
189
190
191
192
193
194
195
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 }
205
206 }