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