Skip to content

Commit

Permalink
add condition to page
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Brunner committed Dec 22, 2011
1 parent 913752c commit 56fd2ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/main/java/org/mapfish/print/config/layout/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public VerticalAlign getVertAlign() {
return vertAlign;
}

private static final Pattern CONDITION_REGEXP = Pattern.compile("^(!?)(.*)$");
public static final Pattern CONDITION_REGEXP = Pattern.compile("^(!?)(.*)$");

public boolean isVisible(RenderingContext context, PJsonObject params) {
public static final boolean testCondition(RenderingContext context, PJsonObject params, String condition) {
if (condition == null) {
return true;
}
Expand All @@ -116,6 +116,10 @@ public boolean isVisible(RenderingContext context, PJsonObject params) {
return result;
}

public boolean isVisible(RenderingContext context, PJsonObject params) {
return testCondition(context, params, condition);
}

public void setCondition(String condition) {
this.condition = condition;
if (condition != null && !CONDITION_REGEXP.matcher(condition).matches()) {
Expand Down
60 changes: 37 additions & 23 deletions src/main/java/org/mapfish/print/config/layout/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,34 @@ public class Page {
private String marginBottom = "20";
private String backgroundPdf = null;
private boolean landscape = false;
private String condition = null;

public void render(PJsonObject params, RenderingContext context) throws DocumentException {
final Document doc = context.getDocument();
doc.setPageSize(getPageSizeRect(context, params));
doc.setMargins(getMarginLeft(context, params), getMarginRight(context, params),
getMarginTop(context, params) + (header != null ? header.getHeight() : 0),
getMarginBottom(context, params) + (footer != null ? footer.getHeight() : 0));

context.getCustomBlocks().setBackgroundPdf(PDFUtils.evalString(context, params, backgroundPdf));
if (doc.isOpen()) {
doc.newPage();
} else {
doc.open();
}
context.getCustomBlocks().setHeader(header, params);
context.getCustomBlocks().setFooter(footer, params);

for (int i = 0; i < items.size(); i++) {
Block block = items.get(i);
if (block.isVisible(context, params)) {
block.render(params, new Block.PdfElement() {
public void add(Element element) throws DocumentException {
doc.add(element);
}
}, context);
if (isVisible(context, params)) {
final Document doc = context.getDocument();
doc.setPageSize(getPageSizeRect(context, params));
doc.setMargins(getMarginLeft(context, params), getMarginRight(context, params),
getMarginTop(context, params) + (header != null ? header.getHeight() : 0),
getMarginBottom(context, params) + (footer != null ? footer.getHeight() : 0));

context.getCustomBlocks().setBackgroundPdf(PDFUtils.evalString(context, params, backgroundPdf));
if (doc.isOpen()) {
doc.newPage();
} else {
doc.open();
}
context.getCustomBlocks().setHeader(header, params);
context.getCustomBlocks().setFooter(footer, params);

for (int i = 0; i < items.size(); i++) {
Block block = items.get(i);
if (block.isVisible(context, params)) {
block.render(params, new Block.PdfElement() {
public void add(Element element) throws DocumentException {
doc.add(element);
}
}, context);
}
}
}
}
Expand All @@ -80,6 +83,17 @@ public Rectangle getPageSizeRect(RenderingContext context, PJsonObject params) {
}
}

protected boolean isVisible(RenderingContext context, PJsonObject params) {
return Block.testCondition(context, params, condition);
}

public void setCondition(String condition) {
this.condition = condition;
if (condition != null && !Block.CONDITION_REGEXP.matcher(condition).matches()) {
throw new InvalidValueException("condition", condition);
}
}

public List<Block> getItems() {
return items;
}
Expand Down

0 comments on commit 56fd2ea

Please sign in to comment.