Calendar with Content.

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

@ -24,51 +24,68 @@ import net.sf.jasperreports.view.JasperViewer;
public class CalendarCreator { public class CalendarCreator {
private Date date; // private final Date date;
private YearMonth yearMonthObject; private final YearMonth yearMonthObject;
private int daysInMonth;
public static JasperPrint createCalendar(Date date) throws JRException { public static JasperPrint createCalendar(Date date) throws JRException {
return new CalendarCreator(date).createCalendar(); return new CalendarCreator(date).createCalendar();
} }
CalendarCreator(Date date) { CalendarCreator(Date date) {
this.date = date; // this.date = date;
yearMonthObject = YearMonth.from(date.toInstant() yearMonthObject = YearMonth
.atZone(ZoneId.systemDefault()) .from(date.toInstant()
.toLocalDate()); .atZone(ZoneId.systemDefault())
.toLocalDate()
);
daysInMonth = yearMonthObject.lengthOfMonth();
} }
JasperPrint createCalendar() throws JRException { JasperPrint createCalendar() throws JRException {
Map<String, Object> parameters = new HashMap<>(); 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); parameters.put("Date", timestamp);
InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml"); InputStream jrxmlResource = CalendarCreator.class.getResourceAsStream("/jasper/calendar_month.jrxml");
JasperReport report = JasperCompileManager JasperReport report = JasperCompileManager
.compileReport(jrxmlResource); .compileReport(jrxmlResource);
Map<Integer, String> values = createValues();
JasperPrint print = JasperFillManager.fillReport(report, JasperPrint print = JasperFillManager.fillReport(report,
parameters, new CalendarSource(yearMonthObject.atDay(1).getDayOfWeek().getValue() -1, daysInMonth)); parameters, new CalendarSource(yearMonthObject, values ));
return print; 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 static class CalendarSource implements JRDataSource {
private final List<Integer> days; private final List<Integer> days;
private final Map<Integer, String> dayContent;
private int index = -1; private int index = -1;
private int prefix;
public CalendarSource(int prefix, int daysInMonth) { public CalendarSource(YearMonth yearMonthObject, Map<Integer, String> dayContent) {
days = new ArrayList<>(); 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++) { for (int i=0, limit = daysInMonth + prefix; i<limit; i++) {
days.add(i+1); days.add(i+1);
} }
} }
@Override @Override
public boolean next() throws JRException { public boolean next() throws JRException {
@ -81,8 +98,13 @@ public class CalendarCreator {
@Override @Override
public Object getFieldValue(JRField jrField) throws JRException { 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); return days.get(index);
} else if (jrField.getName().equals("Field_Value") && days.get(index)>0) {
return dayContent.get(index - prefix);
} else {
return "";
}
} }
} }
@ -91,13 +113,9 @@ public class CalendarCreator {
Locale.setDefault(Locale.GERMANY); 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())); JasperViewer v1 = new JasperViewer(createCalendar(cal.getTime()));
v1.setVisible(true); 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); cal = new GregorianCalendar(2018, Calendar.FEBRUARY, 1);
JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime())); JasperViewer v3 = new JasperViewer(createCalendar(cal.getTime()));
@ -132,12 +150,11 @@ public class CalendarCreator {
date.set(Calendar.DAY_OF_MONTH, 1); date.set(Calendar.DAY_OF_MONTH, 1);
dow = date.get(Calendar.DAY_OF_WEEK); dow = date.get(Calendar.DAY_OF_WEEK);
// dow =new Date(date.getYear(), date.getMonth(), 1).getDay();
dom = date.get(Calendar.MONTH) + 1; dom = date.get(Calendar.MONTH) + 1;
doy = date.get(Calendar.YEAR); doy = date.get(Calendar.YEAR);
days = YearMonth.of(doy, dom).lengthOfMonth(); days = YearMonth.of(doy, dom).lengthOfMonth();
cells = dow-1+days; cells = dow - 1 + days;
limit = cells>28?(cells>35?42:35):28; limit = cells > 28 ? (cells>35?42:35) : 28;
} }
/** /**

@ -170,16 +170,25 @@
<rightPen lineWidth="1.0" lineStyle="Solid"/> <rightPen lineWidth="1.0" lineStyle="Solid"/>
</box> </box>
<textField> <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> <printWhenExpression><![CDATA[$V{Date}.intValue()>0&&$V{Date}.intValue()<=$P{Days}.intValue()]]></printWhenExpression>
</reportElement> </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"> <textElement textAlignment="Right">
<font size="16" isBold="true"/> <font size="16" isBold="true"/>
<paragraph rightIndent="5"/>
</textElement> </textElement>
<textFieldExpression><![CDATA[$V{Date}]]></textFieldExpression> <textFieldExpression><![CDATA[$V{Date}]]></textFieldExpression>
</textField> </textField>
<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> <textFieldExpression><![CDATA[$F{Field_Value}]]></textFieldExpression>
</textField> </textField>
</frame> </frame>

Loading…
Cancel
Save