View Javadoc

1   package org.musicontroller.gui.admin;
2   
3   import java.util.Set;
4   
5   import org.apache.tapestry.event.PageBeginRenderListener;
6   import org.apache.tapestry.event.PageEndRenderListener;
7   import org.apache.tapestry.event.PageEvent;
8   import org.apache.tapestry.html.BasePage;
9   import org.musicontroller.core.Keyword;
10  import org.musicontroller.dao.Dao;
11  
12  /**
13   * Page that handles the management of Keywords.
14   * 
15   * @author Varienaja
16   * @version $Id: Keywordmanagement.java,v 1.1 2010/03/16 18:55:43 varienaja Exp $
17   */
18  public abstract class Keywordmanagement extends BasePage {
19  	public abstract Dao getDao();
20  	
21  	private Keyword[][] _matrix;
22  	private int _currentRow;
23  	
24  	public abstract int getRow();
25  	public abstract void setRow(int row);
26  
27  	public abstract int getColumn();
28  	public abstract void setColumn(int column);
29  	
30  	public Keywordmanagement() {
31  		super();
32  		addPageBeginRenderListener(
33  			new PageBeginRenderListener() {
34  		        public void pageBeginRender(PageEvent e) {
35  		        	_matrix = null;
36  		        }
37  			}		
38  		);
39  		
40  		this.addPageEndRenderListener(
41  			new PageEndRenderListener() {
42  				public void pageEndRender(PageEvent e) {
43  					_matrix = null;
44  				}
45  			}
46  		);
47  
48  	}
49  	
50  	/**
51  	 * Returns the hierarchical Keyword matrix.
52  	 * @return The keyword matrix.
53  	 */
54  	public Keyword[][] getMatrix() {
55  		if (_matrix==null) {
56  			Set<Keyword> parents = Keyword.constructHierarchy(getDao().listKeywords());
57  			int maxX = getMaxHierarchyDepth(parents);
58  			int maxY = getRowCount(parents);
59  			_matrix = new Keyword[maxX][maxY];
60  			
61  			_currentRow = 0;
62  			fillMatrix(parents, 0);
63  		}
64  		return _matrix;
65  	}
66  	
67  	private void fillMatrix(Set<Keyword> parents, int x) {
68  		for (Keyword k : parents) {
69  			_matrix[x][_currentRow] = k;
70  			if (k.getChildCount()>0) {
71  				fillMatrix(k.getChildren(), x+1);
72  			} else {
73  				_currentRow++;
74  			}
75  		}
76  	}
77  	
78  	/**
79  	 * @return The maximum depth of a Keyword hierarchy.
80  	 */
81  	private int getMaxHierarchyDepth(Set<Keyword> parents) {
82  		int result = 1;
83  		for (Keyword k : parents) {
84  			int depth = getMaxDepth(k);
85  			if (depth > result) {
86  				result = depth;
87  			}
88  		}
89  		return result;
90  	}
91  	
92  	private int getMaxDepth(Keyword k) {
93  		int result = 1;
94  		if (k.getChildCount()>0) {
95  			for (Keyword candidate : k.getChildren()) {
96  				int depth = 1 + getMaxDepth(candidate);
97  				if (depth > result) {
98  					result = depth;
99  				}
100 			}
101 		}
102 		return result;
103 	}
104 	
105 	/**
106 	 * In order to render the table with the keyword-hierarchy, we need to know
107 	 * the amount of rows.
108 	 * @return The amount of rows to render.
109 	 */
110 	private int getRowCount(Set<Keyword> parents) {
111 		int result = 0;
112 		for (Keyword k : parents) {
113 			int childcount = k.getChildCount();
114 			result += childcount==0 ? 1 : childcount;
115 		}
116 		return result;
117 	}
118 
119 	/**
120 	 * Provides an integer-array for looping over all rows for the Keyword
121 	 * hierarchy table.
122 	 * @return
123 	 */
124 	public int[] getRows() {
125 		int length = 0;
126 		
127 		Keyword[][] matrix = getMatrix();
128 		if (matrix.length>0) {
129 			length = matrix[0].length;
130 		}
131 		
132 		int[] result = new int[length];
133 		for (int i=0; i<result.length; i++) {
134 			result[i] = i;
135 		}
136 		return result;
137 	}
138 	
139 	/**
140 	 * Provides an integer-array for looping over all columns for the Keyword
141 	 * hierarchy table.
142 	 * @return
143 	 */
144 	public int[] getColumns() {
145 		int[] result = new int[getMatrix().length];
146 		for (int i=0; i<result.length; i++) {
147 			result[i] = i;
148 		}
149 		return result;
150 	}
151 	
152 	public Keyword getKeyword() {
153 		return getMatrix()[getColumn()][getRow()];
154 	}
155 	
156 	public int getMaxHierarchyDepth() {
157 		return getMatrix().length;
158 	}
159 	
160 	/**
161 	 * The rowspan is the number of childs that do not have children.
162 	 * @return
163 	 */
164 	public int getRowspan() {
165 		return getRowspan(getKeyword());
166 	}
167 	
168 	private int getRowspan(Keyword keyword) {
169 		int result = 0;
170 		
171 		if (keyword.getChildCount()>0) {
172 			for (Keyword child : keyword.getChildren()) {
173 				result += getRowspan(child);	
174 			}
175 		} else {
176 			return 1;
177 		}
178 		
179 		return result;
180 	}
181 
182 }
183