View Javadoc
1   /**
2    * 
3    */
4   package de.japrost.excerpt;
5   
6   import java.awt.Font;
7   import java.awt.Graphics2D;
8   import java.util.Properties;
9   
10  
11  /**
12   *
13   */
14  public class PropertyTextBlockBuilder {
15  
16  	public PropertyTextBlockBuilder(final Graphics2D g, final ImageMaker m) {
17  		super();
18  		this.g = g;
19  		this.m = m;
20  	}
21  
22  	private Graphics2D g;
23  	private ImageMaker m;
24  
25  	private String prefix;
26  	private String blockName;
27  	private Properties props;
28  	private String template;
29  
30  	TextBlock buildTextBlock(final String prefix, final String name, final Properties props) {
31  		this.prefix = prefix;
32  		this.props = props;
33  		blockName = name;
34  		template = null;
35  		TextBlock textBlock = new TextBlock();
36  		// 
37  		textBlock.setGraphics(g);
38  		// template
39  		template = getProperty("template", null);
40  		System.out.println("Using template: '" + template + "'");
41  		// text
42  		String text = "No text found :(";
43  		text = getProperty("text", text);
44  		textBlock.setText(text);
45  		// align
46  		int align = 0;
47  		align = Integer.parseInt(getProperty("align", "" + align));
48  		textBlock.setAlign(align);
49  		// font
50  		double factor = 1;
51  		factor = Double.parseDouble(getProperty("scale", "" + factor));
52  		int style = 0;
53  		style = Integer.parseInt(getProperty("style", "" + style));
54  		String fontName = "Times New Roman";
55  		fontName = getProperty("name", fontName);
56  		Font font = new Font(fontName, style, (int)(m.getSizeBase() * factor));
57  		textBlock.setFont(font);
58  		System.out.println(font);
59  		textBlock.setPageWidth(m.getWidth());
60  		textBlock.init();
61  		return textBlock;
62  	}
63  
64  	private String getProperty(final String property, final String defaultValue) {
65  		String rValue = props.getProperty(prefix + "." + blockName + "." + property);
66  		System.out.println(prefix + "." + blockName + "." + property + "=" + rValue);
67  		if (rValue != null) {
68  			// we have a real - real value
69  			return rValue;
70  		}
71  		String tValue = props.getProperty(prefix + "." + template + "." + property);
72  		if (tValue != null) {
73  			// we have a template value
74  			return tValue;
75  		}
76  		// nothing left, use default
77  		return defaultValue;
78  	}
79  }