View Javadoc
1   /******************************************************************************
2    * HtmlUtils.java - Utilities to make jsp life easier
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2005 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.util.Iterator;
11  import java.util.LinkedList;
12  import java.util.List;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import com.buckosoft.PicMan.db.DatabaseFacade;
18  
19  import com.buckosoft.PicMan.domain.Chain;
20  import com.buckosoft.PicMan.domain.Set;
21  import com.buckosoft.PicMan.domain.System;
22  
23  /** Utilities to make jsp life easier.
24   * @author Dick Balaska
25   * @since 2005/08/05
26   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/HtmlUtils.java">HtmlUtils.java</a>
27   */
28  public class HtmlUtils {
29  	protected final Log logger = LogFactory.getLog(getClass());
30  	static private final String eol = "\r\n";
31  
32  	private	DatabaseFacade	dbf;
33  
34  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
35  	public DatabaseFacade getDatabase() { return(dbf); }
36  
37  	public List<String> getFilterColumnsAsHtml(List<String> in) {
38  		LinkedList<String> out = new LinkedList<String>();
39  		Iterator<String> it = in.iterator();
40  		String[]	b;
41  		String		s;
42  		while (it.hasNext()) {
43  			s = "";
44  			b = ((String)it.next()).split("_");
45  			for (int i=0; i<b.length; i++) {
46  				if (i != 0)
47  					s += "<br/>";
48  				s += b[i];
49  			}
50  			out.add(s);
51  		}
52  		return(out);
53  	}
54  
55  	/** Build a {tr}{td} String of Contact urls.
56  	 * @param chain
57  	 * @param setName
58  	 * @param uri
59  	 * @return A big String
60  	 */
61  	public	String	getContactSelectorHtml(Chain chain, String setName, String uri) {
62  		List<Integer>	sizes = dbf.getSizes();
63  		StringBuffer sb = new StringBuffer();
64  		for (int i=0; i<chain.getContactCount(); i++) {
65  			sb.append("<tr>");
66  			Iterator<Integer> iter = sizes.iterator();
67  			while (iter.hasNext()) {
68  				int size = ((Integer)iter.next()).intValue();
69  				sb.append("<td>");
70  				if (chain.hasSetSize(setName, size)) {
71  					sb.append("<a href=\"");
72  					sb.append(uri);
73  					sb.append("?cid=" + chain.getCid() + "&set=" + setName + "&size=" + size + "&item=" + i + "\">");
74  					sb.append(System.getUuid(setName, size, i));
75  					sb.append("</a>");
76  				} else {
77  					sb.append("&nbsp;");
78  				}
79  				sb.append("</td>");
80  			}
81  			sb.append("</tr>" + eol);
82  		}
83  		return(sb.toString());
84  	}
85  
86  	/** Build a String of {tr}{td} that is a 2x2 array of sets/sizes in this chain.
87  	 * Used by SpinContacts.
88  	 * @param chain The chain to evaluate.
89  	 * @return A big honkin' string that starts with {tr} and ends with {/tr}.
90  	 */
91  	public	String		getSetSizeCheckboxesHtml(Chain chain) {
92  		List<Set>	sets = dbf.getSets();
93  //		List<Integer>	sizes = dbf.getSizes();
94  		int[]			sizes = dbf.getSizeArray();
95  		StringBuffer	sb = new StringBuffer();
96  		StringBuffer	sx = new StringBuffer();
97  		int				i;
98  		
99  		Iterator<Set> iter = sets.iterator();
100 		boolean[]			sizesIncluded = new boolean[sizes.length];
101 		for (i=0; i<sizesIncluded.length; i++)
102 			sizesIncluded[i] = false;
103 		i=0;
104 		while (iter.hasNext()) {
105 			Set	set = (Set)iter.next();
106 			String	setName = set.getName();
107 //			sx.append("<input type=\"hidden\" id=\"set_");
108 //			sx.append(i++);
109 //			sx.append("\" value=\"");
110 //			sx.append(setName);
111 //			sx.append("\" />");
112 //			sx.append(eol);
113 			for (int sizesIndex=0; sizesIndex<sizes.length; sizesIndex++) {
114 				int	size = sizes[sizesIndex];
115 				if (chain.hasSetSize(setName, size)) {
116 					sizesIncluded[sizesIndex] = true;
117 				}
118 			}
119 		}
120 		sb.append("<tr><td>");
121 		sb.append(sx);
122 		sb.append("&nbsp;</td><td>&nbsp;</td>");
123 		sb.append(eol);
124 		for (i=0; i<sizesIncluded.length; i++) {
125 			if (!sizesIncluded[i]) {
126 				sb.append("<td>&nbsp;</td>");
127 			} else {
128 				sb.append("<td>");
129 				sb.append("<input type=\"checkbox\" id=\"col_");
130 				sb.append(sizes[i]);
131 				sb.append("\" name=\"col_");
132 				sb.append(sizes[i]);
133 				sb.append("\" onclick=\"colClick(");
134 				sb.append(sizes[i]);
135 				sb.append(")\"/>");
136 				sb.append(sizes[i]);
137 				sb.append("</td>");
138 			}
139 			sb.append(eol);
140 		}
141 		sb.append("</tr>");
142 		sb.append(eol);
143 /*		sb.append("<tr><td>&nbsp;</td><td>&nbsp;</td>");
144 		for (i=0; i<sizesIncluded.length; i++) {
145 			if (!sizesIncluded[i])
146 				continue;
147 			sb.append("<td>___</td>");
148 		}		
149 		sb.append("</tr>");
150 		sb.append(eol);
151 */
152 		iter = sets.iterator();
153 		while (iter.hasNext()) {
154 			Set	set = (Set)iter.next();
155 			String	setName = set.getName();
156 			boolean included = false;
157 			sx.setLength(0);
158 			for (int sizesIndex=0; sizesIndex<sizes.length; sizesIndex++) {
159 				int	size = sizes[sizesIndex];
160 				sx.append("<td>");
161 				if (chain.hasSetSize(setName, size)) {
162 					included = true;
163 					String ssName = setName + "_" + size;
164 //					<input type="checkbox" id="x_<c:out value="${set.name}"/>_<c:out value="${size}"/>" 
165 					//name="x_<c:out value="${set.name}"/>_<c:out value="${size}"/>"/>
166 //					<c:out value="${set.name}"/>_<c:out value="${size}"/>
167 					sx.append("<input type=\"checkbox\" id=\"x_");
168 					sx.append(ssName);
169 					sx.append("\" name=\"x_");
170 					sx.append(ssName);
171 					sx.append("\"/>");
172 					sx.append(ssName);
173 				} else {
174 					sx.append("&nbsp;");
175 				}
176 				sx.append("</td>");
177 				sx.append(eol);
178 			}
179 			if (included) {
180 				sb.append("<tr><td>");
181 				sb.append("<input type=\"checkbox\" id=\"row_");
182 				sb.append(setName);
183 				sb.append("\" name=\"row_");
184 				sb.append(setName);
185 				sb.append("\" onclick=\"rowClick('");
186 				sb.append(setName);
187 				sb.append("');\"/>");
188 //				sb.append(setName);
189 				sb.append("</td><td>:</td>");
190 				sb.append(eol);
191 
192 				sb.append(sx);
193 				sb.append("</tr>");
194 				sb.append(eol);
195 			}
196 		}
197 		return(sb.toString());
198 	}
199 
200 }