View Javadoc

1   /***********************************************************************************************************************
2   Copyright (c) 2003, International Barcode Consortium
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without modification,
6   are permitted provided that the following conditions are met:
7   
8       * Redistributions of source code must retain the above copyright notice, this list of
9         conditions and the following disclaimer.
10      * Redistributions in binary form must reproduce the above copyright notice, this list of
11        conditions and the following disclaimer in the documentation and/or other materials
12        provided with the distribution.
13      * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14        or promote products derived from this software without specific prior written permission.
15  
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  POSSIBILITY OF SUCH DAMAGE.
25  ***********************************************************************************************************************/
26  
27  package net.sourceforge.barbecue;
28  
29  import net.sourceforge.barbecue.output.Output;
30  import net.sourceforge.barbecue.output.OutputException;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * Specific implementation of Module that allows the grouping of multiple Modules into
38   * one parent module.
39   * <p/>Note: You should not instantiate this class directly.
40   *
41   * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a>
42   */
43  public class CompositeModule extends Module {
44  	private final List modules;
45  
46  	/**
47  	 * Constructs a new Composite module that is initially empty.
48  	 */
49  	public CompositeModule() {
50  		super(new int[0]);
51  		modules = new ArrayList();
52  	}
53  
54  	/**
55  	 * Adds the given module to this composite module.
56  	 * @param module The module to add
57  	 */
58  	public void add(Module module) {
59  		modules.add(module);
60  	}
61  
62  	/**
63  	 * Returns the number of modules currently contained within this composite module.
64  	 * @return The number of child modules
65  	 */
66  	public int size() {
67  		return modules.size();
68  	}
69  
70  	/**
71  	 * Returns the child module at the specified index.
72  	 * @param index The module index
73  	 * @return The module at the given index
74  	 */
75  	public Module getModule(int index) {
76  		return (Module) modules.get(index);
77  	}
78  
79  	/**
80  	 * Returns the symbol group encoded by this module. This is actually a concatenation
81  	 * of the symbols encoded by each child module.
82  	 * @return The symbol encoded by this composite module
83  	 */
84  	public String getSymbol() {
85  		StringBuffer buf = new StringBuffer();
86  		for (Iterator iterator = modules.iterator(); iterator.hasNext();) {
87  			Module module = (Module) iterator.next();
88  			buf.append(module.getSymbol());
89  		}
90  		return buf.toString();
91  	}
92  
93  	/**
94  	 * Returns the underlying total width of the bars from the bar specification (that is, the sum of original bar widths
95  	 * in base bar units).
96  	 *
97  	 * @return The total width of bars in base (unscaled) units
98  	 */
99  	public int widthInBars() {
100 		int width = 0;
101 		for (Iterator iterator = modules.iterator(); iterator.hasNext();) {
102 			Module module = (Module) iterator.next();
103 			width += module.widthInBars();
104 		}
105 		return width;
106 	}
107 
108 	/**
109 	 * Draws the module to the given outputter at the specified origin. This actually
110 	 * draws each child module in turn.
111 	 * @param output The outputter to draw to
112 	 * @param x The X component of the origin
113 	 * @param y The Y component of the origin
114 	 * @param barWidth
115 	 * @param barHeight
116 	 * @return The total width drawn
117 	 */
118 	protected int draw(Output output, int x, int y, int barWidth, int barHeight) throws OutputException {
119 		int sum = 0;
120 		int currentX = x;
121 		
122 		for (Iterator iterator = modules.iterator(); iterator.hasNext();) {
123 			Module module = (Module) iterator.next();
124 			int result = module.draw(output, currentX, y, barWidth, barHeight);
125 			currentX += result;
126 			sum += result;
127 		}
128 
129 		return sum;
130 	}
131 }