KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > pdf > MonthExporter


1 package org.lucane.applications.calendar.pdf;
2
3 import java.text.DateFormat JavaDoc;
4 import java.util.*;
5 import java.io.*;
6 import java.awt.Color JavaDoc;
7
8 import org.lucane.applications.calendar.CalendarPlugin;
9 import org.lucane.applications.calendar.Event;
10 import org.lucane.applications.calendar.widget.BasicEvent;
11 import org.lucane.applications.calendar.util.*;
12 import org.lucane.client.Client;
13
14 import com.lowagie.text.*;
15 import com.lowagie.text.pdf.*;
16
17 public class MonthExporter
18 {
19     private CalendarPlugin plugin;
20     private Calendar calendar;
21     private String JavaDoc userName;
22     private Document document;
23     private boolean sundayFirst = false;
24     
25     public MonthExporter(CalendarPlugin plugin, Calendar calendar, String JavaDoc userName)
26     {
27         this.plugin = plugin;
28         this.calendar = calendar;
29         this.userName = userName;
30         this.sundayFirst = plugin.getLocalConfig().getInt("sundayFirst", 0) == 1;
31         
32         this.calendar.set(Calendar.DAY_OF_MONTH, 1);
33         this.calendar.set(Calendar.HOUR_OF_DAY, 0);
34         this.calendar.set(Calendar.MINUTE, 0);
35     }
36     
37     public void exportMonth(String JavaDoc filename)
38     throws Exception JavaDoc
39     {
40         Iterator events = this.fetchEvents();
41         ArrayList[] list = CalendarUtils.dispatchEventsInMonth(calendar, events, sundayFirst);
42
43         this.initDocumentAndWriter(new FileOutputStream(filename));
44         
45
46         String JavaDoc user = userName;
47         if(user == null)
48             user = Client.getInstance().getMyInfos().getName();
49         
50         String JavaDoc monthHeader = plugin.getTitle() + " - " + user + " - "
51             + plugin.tr("month." + (this.calendar.get(Calendar.MONTH)+1))
52             + " " + this.calendar.get(Calendar.YEAR);
53         
54         String JavaDoc printDate = "("
55             + DateFormat.getDateInstance(DateFormat.SHORT).format(new Date())
56             + ")";
57
58         Paragraph header = new Paragraph(monthHeader);
59         header.setAlignment(Paragraph.ALIGN_RIGHT);
60         document.add(header);
61
62         Paragraph printMark = new Paragraph(printDate);
63         printMark.setAlignment(Paragraph.ALIGN_RIGHT);
64         document.add(printMark);
65         
66         PdfPTable month = createMonthTable();
67         addHeader(month);
68         addContent(month, list);
69         document.add(month);
70         
71         this.closeDocument();
72     }
73     
74     private void initDocumentAndWriter(OutputStream out)
75     throws DocumentException
76     {
77         Rectangle a4_landscape = PageSize.A4.rotate();
78         float margin = 10f;
79         
80         document = new Document(a4_landscape, margin, margin, margin, margin);
81         
82         PdfWriter.getInstance(document, out);
83         document.addCreator("Lucane PDF export");
84
85         document.open();
86     }
87     
88     private void closeDocument()
89     {
90         document.close();
91     }
92     
93     private PdfPTable createMonthTable()
94     {
95         PdfPTable table = new PdfPTable(7);
96         return table;
97     }
98     
99     private void addHeader(PdfPTable table)
100     {
101         table.getDefaultCell().setFixedHeight(20f);
102         table.getDefaultCell().setBackgroundColor(Color.LIGHT_GRAY);
103         table.setWidthPercentage(100f);
104
105         if(sundayFirst)
106             table.addCell(plugin.tr("day.7"));
107         for(int i=1;i<=6;i++)
108             table.addCell(plugin.tr("day." + i));
109         if(!sundayFirst)
110             table.addCell(plugin.tr("day.7"));
111     }
112     
113     private void addContent(PdfPTable table, ArrayList[] events)
114     throws Exception JavaDoc
115     {
116         BaseFont helvetica = BaseFont.createFont("Helvetica", BaseFont.CP1252,
117             BaseFont.NOT_EMBEDDED);
118         BaseFont helveticaBold = BaseFont.createFont("Helvetica-Bold", BaseFont.CP1252,
119             BaseFont.NOT_EMBEDDED);
120         Font h10b = new Font(helveticaBold, 10, Font.NORMAL);
121         Font h8n = new Font(helvetica, 8, Font.NORMAL);
122         Font h8n_gray = new Font(helvetica, 8, Font.NORMAL, Color.GRAY);
123         Color JavaDoc unusedDay = new Color JavaDoc(220, 220, 220);
124             
125         table.getDefaultCell().setFixedHeight(80f);
126
127         int day = this.getFirstDayOfDisplayedMonth();
128         int max = this.getDaysOfDisplayedMonth();
129         
130         table.getDefaultCell().setBackgroundColor(unusedDay);
131         for(int i=0;i<day;i++)
132             table.addCell("");
133
134         table.getDefaultCell().setBackgroundColor(Color.WHITE);
135         for(int i=0;i<max;i++)
136         {
137             PdfPCell cell = new PdfPCell(table.getDefaultCell());
138             Chunk dayLabel = new Chunk("" + (i+1));
139             dayLabel.setFont(h10b);
140             
141             Phrase phrase = new Phrase();
142             phrase.add(dayLabel);
143
144             Iterator dayEvents = events[day+i].iterator();
145             while(dayEvents.hasNext())
146             {
147                 BasicEvent event = (BasicEvent)dayEvents.next();
148                 
149                 String JavaDoc start = event.getStartHour() + ":" + event.getStartMinute();
150                 if(start.length() < 5)
151                     start += "0";
152                 String JavaDoc end = event.getEndHour() + ":" + event.getEndMinute();
153                 if(end.length() < 5)
154                     end += "0";
155                                 
156                 Chunk hour = null;
157                 if(event.isMultiDay())
158                     hour = new Chunk("\n");
159                 else
160                     hour = new Chunk("\n[" + start + "-" + end + "] ");
161                 hour.setFont(h8n);
162                 Chunk title = new Chunk(event.getTitle());
163                 title.setFont(h8n_gray);
164                 
165                 phrase.add(hour);
166                 phrase.add(title);
167             }
168             cell.setPhrase(phrase);
169             table.addCell(cell);
170         }
171
172         table.getDefaultCell().setBackgroundColor(unusedDay);
173         for(int i=max+day;i<42;i++)
174             table.addCell("");
175     }
176     
177     private Iterator fetchEvents()
178     {
179         //-- get month interval (in milliseconds)
180
long start, end;
181         Calendar cal = Calendar.getInstance();
182         cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
183         cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
184         cal.set(Calendar.DAY_OF_MONTH, 1);
185         cal.set(Calendar.HOUR_OF_DAY, 0);
186         cal.set(Calendar.MINUTE, 0);
187         start = cal.getTimeInMillis();
188         cal.add(Calendar.MONTH, 1);
189         end = cal.getTimeInMillis();
190         
191         //-- fetch and display events
192
ArrayList events = new ArrayList();
193         try {
194             if(userName == null)
195                 events = plugin.getMyEvents(start, end);
196             else
197                 events = plugin.getEventsForUser(userName, start, end);
198         } catch(Exception JavaDoc e) {
199             e.printStackTrace();
200         }
201         
202         return events.iterator();
203     }
204     
205     //--
206

207     /**
208      * Get the first day (of week) for the displayed month
209      *
210      * @return the first day of week
211      */

212     private int getFirstDayOfDisplayedMonth()
213     {
214             return CalendarUtils.getFirstDayOfMonth(calendar, sundayFirst);
215     }
216     
217     /**
218      * Get the number of days for the displayed month
219      *
220      * @return the number of days
221      */

222     private int getDaysOfDisplayedMonth()
223     {
224             return CalendarUtils.getDaysOfMonth(calendar);
225     }
226 }
Popular Tags