Calendar with Content.

master
Markus Kreth 7 years ago
parent 50a0324756
commit 2b8fe7d44c
  1. 55
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarCreator.java
  2. 13
      src/main/resources/jasper/calendar_month.jrxml

@ -24,46 +24,63 @@ import net.sf.jasperreports.view.JasperViewer;
public class CalendarCreator {
private Date date;
private YearMonth yearMonthObject;
private int daysInMonth;
// private final Date date;
private final YearMonth yearMonthObject;
public static JasperPrint createCalendar(Date date) throws JRException {
return new CalendarCreator(date).createCalendar();
}
CalendarCreator(Date date) {
this.date = date;
// this.date = date;
yearMonthObject = YearMonth.from(date.toInstant()
yearMonthObject = YearMonth
.from(date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate());
.toLocalDate()
);
daysInMonth = yearMonthObject.lengthOfMonth();
}
JasperPrint createCalendar() throws JRException {
Map<String, Object> parameters = new HashMap<>();
Timestamp timestamp = new Timestamp(date.getTime());
Timestamp timestamp = new Timestamp(yearMonthObject.atDay(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
parameters.put("Date", timestamp);
InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml");
JasperReport report = JasperCompileManager
.compileReport(jrxmlResource);
Map<Integer, String> values = createValues();
JasperPrint print = JasperFillManager.fillReport(report,
parameters, new CalendarSource(yearMonthObject.atDay(1).getDayOfWeek().getValue() -1, daysInMonth));
parameters, new CalendarSource(yearMonthObject, values ));
return print;
}
private Map<Integer, String> createValues() {
Map<Integer, String> values = new HashMap<>();
for (int i=1; i<33; i++) {
values.put(i -1, "Termine Tag " + i);
}
return values;
}
private static class CalendarSource implements JRDataSource {
private final List<Integer> days;
private final Map<Integer, String> dayContent;
private int index = -1;
private int prefix;
public CalendarSource(YearMonth yearMonthObject, Map<Integer, String> dayContent) {
public CalendarSource(int prefix, int daysInMonth) {
days = new ArrayList<>();
this.dayContent = dayContent;
prefix = yearMonthObject.atDay(1).getDayOfWeek().getValue() -1;
int daysInMonth = yearMonthObject.lengthOfMonth();
for (int i=0, limit = daysInMonth + prefix; i<limit; i++) {
days.add(i+1);
}
@ -81,8 +98,13 @@ public class CalendarCreator {
@Override
public Object getFieldValue(JRField jrField) throws JRException {
System.err.println(String.format("name=%s, type=%s, value=%d, parentProps=%s", jrField.getName(), jrField.getValueClass(), days.get(index), jrField.getParentProperties()));
if (jrField.getName().equals("id")) {
return days.get(index);
} else if (jrField.getName().equals("Field_Value") && days.get(index)>0) {
return dayContent.get(index - prefix);
} else {
return "";
}
}
}
@ -91,14 +113,10 @@ public class CalendarCreator {
Locale.setDefault(Locale.GERMANY);
Calendar cal = new GregorianCalendar(2018, Calendar.AUGUST, 1);
Calendar cal = new GregorianCalendar(2018, Calendar.OCTOBER, 1);
JasperViewer v1 = new JasperViewer(createCalendar(cal.getTime()));
v1.setVisible(true);
cal = new GregorianCalendar(2018, Calendar.SEPTEMBER, 1);
JasperViewer v2 = new JasperViewer(createCalendar(cal.getTime()));
v2.setVisible(true);
cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1);
JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime()));
v3.setVisible(true);
@ -132,12 +150,11 @@ public class CalendarCreator {
date.set(Calendar.DAY_OF_MONTH, 1);
dow = date.get(Calendar.DAY_OF_WEEK);
// dow =new Date(date.getYear(), date.getMonth(), 1).getDay();
dom = date.get(Calendar.MONTH) + 1;
doy = date.get(Calendar.YEAR);
days = YearMonth.of(doy, dom).lengthOfMonth();
cells = dow-1+days;
limit = cells>28?(cells>35?42:35):28;
cells = dow - 1 + days;
limit = cells > 28 ? (cells>35?42:35) : 28;
}
/**

@ -170,16 +170,25 @@
<rightPen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textField>
<reportElement style="Style1" mode="Transparent" x="60" y="0" width="50" height="25" isRemoveLineWhenBlank="true" backcolor="#C6EDBB" uuid="733ff549-0109-4f55-b9de-476f0ad9fbeb">
<reportElement style="Style1" mode="Transparent" x="80" y="0" width="30" height="21" isRemoveLineWhenBlank="true" backcolor="#C6EDBB" uuid="733ff549-0109-4f55-b9de-476f0ad9fbeb">
<printWhenExpression><![CDATA[$V{Date}.intValue()>0&&$V{Date}.intValue()<=$P{Days}.intValue()]]></printWhenExpression>
</reportElement>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#050505"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#050505"/>
</box>
<textElement textAlignment="Right">
<font size="16" isBold="true"/>
<paragraph rightIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$V{Date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="25" width="110" height="65" uuid="52fccbc5-19e8-40be-b02c-3d23afa55a7d"/>
<reportElement x="0" y="21" width="110" height="69" isRemoveLineWhenBlank="true" uuid="52fccbc5-19e8-40be-b02c-3d23afa55a7d">
<printWhenExpression><![CDATA[$V{Date}.intValue()>0&&$V{Date}.intValue()<=$P{Days}.intValue()]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$F{Field_Value}]]></textFieldExpression>
</textField>
</frame>

Loading…
Cancel
Save