1 17 18 19 20 package org.apache.fop.plan; 21 22 import java.text.DateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 import java.util.HashMap ; 26 27 import org.w3c.dom.DOMImplementation ; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 31 import org.apache.batik.dom.svg.SVGDOMImplementation; 32 import org.apache.fop.svg.SVGUtilities; 33 34 37 public class SimplePlanDrawer implements PlanDrawer { 38 39 private static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI; 40 41 private float fontSize; 42 private HashMap hints; 43 private java.awt.Font font = null; 44 private boolean bord = false; 45 private float lSpace = 15; 46 private float width; 47 private float height; 48 private float topEdge; 49 private float rightEdge; 50 51 private String [] colours; 52 private String [] darkcolours; 53 54 private Date startDate; 55 private Date endDate; 56 57 61 public void setStartDate(Date sd) { 62 startDate = sd; 63 } 64 65 69 public void setEndDate(Date ed) { 70 endDate = ed; 71 } 72 73 76 public Document createDocument(EventList data, float w, float h, 77 HashMap hints) { 78 this.width = w; 79 this.height = h; 80 this.hints = hints; 81 fontSize = ((Float ) hints.get(PlanHints.FONT_SIZE)).floatValue(); 82 bord = ((Boolean ) hints.get(PlanHints.PLAN_BORDER)).booleanValue(); 83 84 String title = ""; 85 86 DOMImplementation impl = 87 SVGDOMImplementation.getDOMImplementation(); 88 Document doc = impl.createDocument(SVG_NAMESPACE, "svg", null); 89 90 Element svgRoot = doc.getDocumentElement(); 91 svgRoot.setAttributeNS(null, "width", "" + width); 92 svgRoot.setAttributeNS(null, "height", "" + height); 93 svgRoot.setAttributeNS(null, "style", 94 "font-size:" + 8 95 + ";font-family:" 96 + hints.get(PlanHints.FONT_FAMILY)); 97 98 font = new java.awt.Font ((String )hints.get(PlanHints.FONT_FAMILY), 99 java.awt.Font.PLAIN, (int)fontSize); 100 101 if (bord) { 102 Element border = 103 SVGUtilities.createRect(doc, 0, 0, width, height); 104 border.setAttributeNS(null, "style", "stroke:black;fill:none"); 105 svgRoot.appendChild(border); 106 } 107 108 float strwidth = SVGUtilities.getStringWidth(title, font); 109 110 Element text; 111 float pos = (float)(80 - strwidth / 2.0); 112 if (pos < 5) { 113 pos = 5; 114 } 115 text = SVGUtilities.createText(doc, pos, 18, title); 116 text.setAttributeNS(null, "style", "font-size:14"); 117 svgRoot.appendChild(text); 118 119 topEdge = SVGUtilities.getStringHeight(title, font) + 5; 120 121 addPlan(doc, svgRoot, data); 123 125 return doc; 126 } 127 128 protected void addPlan(Document doc, Element svgRoot, EventList data) { 129 Date currentDate = new Date (); 130 131 Date lastWeek = startDate; 132 Date future = endDate; 133 Calendar lw = Calendar.getInstance(); 134 if (lastWeek == null || future == null) { 135 int dow = lw.get(Calendar.DAY_OF_WEEK); 136 lw.add(Calendar.DATE, -dow - 6); 137 lastWeek = lw.getTime(); 138 lw.add(Calendar.DATE, 5 * 7); 139 future = lw.getTime(); 140 } 141 long totalDays = (long)((future.getTime() - lastWeek.getTime() + 43200000) / 86400000); 142 lw.setTime(lastWeek); 143 int startDay = lw.get(Calendar.DAY_OF_WEEK); 144 145 float graphTop = topEdge; 146 Element g; 147 Element line; 148 line = SVGUtilities.createLine(doc, 0, topEdge, width, topEdge); 149 line.setAttributeNS(null, "style", "fill:none;stroke:black"); 150 svgRoot.appendChild(line); 151 152 Element clip1 = SVGUtilities.createClip(doc, 153 SVGUtilities.createPath(doc, 154 "m0 0l126 0l0 " + height + "l-126 0z"), "clip1"); 155 Element clip2 = SVGUtilities.createClip(doc, 156 SVGUtilities.createPath(doc, 157 "m130 0l66 0l0 " + height + "l-66 0z"), "clip2"); 158 Element clip3 = SVGUtilities.createClip(doc, 159 SVGUtilities.createPath(doc, 160 "m200 0l" + (width - 200) + " 0l0 " + height + "l-" 161 + (width - 200) + " 0z"), "clip3"); 162 svgRoot.appendChild(clip1); 163 svgRoot.appendChild(clip2); 164 svgRoot.appendChild(clip3); 165 166 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); 167 Element text; 168 text = SVGUtilities.createText(doc, 201, topEdge - 1, 169 df.format(lastWeek)); 170 svgRoot.appendChild(text); 171 172 text = SVGUtilities.createText(doc, width, topEdge - 1, 173 df.format(future)); 174 text.setAttributeNS(null, "text-anchor", "end"); 175 svgRoot.appendChild(text); 176 177 line = SVGUtilities.createLine(doc, 128, topEdge, 128, height); 178 line.setAttributeNS(null, "style", "fill:none;stroke:rgb(150,150,150)"); 179 svgRoot.appendChild(line); 180 int offset = 0; 181 for (int count = startDay; count < startDay + totalDays - 1; count++) { 182 offset++; 183 if (count % 7 == 0 || count % 7 == 1) { 184 Element rect = SVGUtilities.createRect(doc, 185 200 + (offset - 1) * (width - 200) / (totalDays - 2), 186 (float)(topEdge + 0.5), 187 (width - 200) / (totalDays - 3), 188 height - 1 - topEdge); 189 rect.setAttributeNS(null, "style", "stroke:none;fill:rgb(230,230,230)"); 190 svgRoot.appendChild(rect); 191 } 192 line = SVGUtilities.createLine(doc, 193 200 + (offset - 1) * (width - 200) / (totalDays - 2), 194 (float)(topEdge + 0.5), 195 200 + (offset - 1) * (width - 200) / (totalDays - 2), 196 (float)(height - 0.5)); 197 line.setAttributeNS(null, "style", "fill:none;stroke:rgb(200,200,200)"); 198 svgRoot.appendChild(line); 199 } 200 201 202 for (int count = 0; count < data.getSize(); count++) { 203 GroupInfo gi = data.getGroupInfo(count); 204 g = SVGUtilities.createG(doc); 205 text = SVGUtilities.createText(doc, 1, topEdge + 12, 206 gi.getName()); 207 text.setAttributeNS(null, "style", "clip-path:url(#clip1)"); 208 g.appendChild(text); 209 if (count > 0) { 210 line = SVGUtilities.createLine(doc, 0, topEdge + 2, 211 width, topEdge + 2); 212 line.setAttributeNS(null, "style", "fill:none;stroke:rgb(100,100,100)"); 213 g.appendChild(line); 214 } 215 216 float lastTop = topEdge; 217 topEdge += 14; 218 boolean showing = false; 219 for (int count1 = 0; count1 < gi.getSize(); count1++) { 220 ActionInfo act = gi.getActionInfo(count1); 221 String name = act.getOwner(); 222 String label = act.getLabel(); 223 text = SVGUtilities.createText(doc, 8, topEdge + 12, label); 224 text.setAttributeNS(null, "style", "clip-path:url(#clip1)"); 225 g.appendChild(text); 226 227 text = SVGUtilities.createText(doc, 130, topEdge + 12, 228 name); 229 text.setAttributeNS(null, "style", "clip-path:url(#clip2)"); 230 g.appendChild(text); 231 int type = act.getType(); 232 Date start = act.getStartDate(); 233 Date end = act.getEndDate(); 234 if (end.after(lastWeek) && start.before(future)) { 235 showing = true; 236 int left = 200; 237 int right = 500; 238 239 int daysToStart = (int)((start.getTime() 240 - lastWeek.getTime() + 43200000) / 86400000); 241 int days = (int)((end.getTime() - start.getTime() 242 + 43200000) / 86400000); 243 int daysFromEnd = 244 (int)((future.getTime() - end.getTime() 245 + 43200000) / 86400000); 246 Element taskGraphic; 247 switch (type) { 248 case ActionInfo.TASK: 249 taskGraphic = SVGUtilities.createRect(doc, 250 left + daysToStart * 300 / (totalDays - 2), 251 topEdge + 2, days * 300 / (totalDays - 2), 10); 252 taskGraphic.setAttributeNS(null, 253 "style", 254 "stroke:black;fill:blue;stroke-width:1;clip-path:url(#clip3)"); 255 g.appendChild(taskGraphic); 256 break; 257 case ActionInfo.MILESTONE: 258 taskGraphic = SVGUtilities.createPath(doc, 259 "m " + (left 260 + daysToStart * 300 / (totalDays - 2) - 6) 261 + " " + (topEdge + 6) + "l6 6l6-6l-6-6z"); 262 taskGraphic.setAttributeNS(null, 263 "style", 264 "stroke:black;fill:black;stroke-width:1;clip-path:url(#clip3)"); 265 g.appendChild(taskGraphic); 266 text = SVGUtilities.createText(doc, 267 left + daysToStart * 300 / (totalDays - 2) + 8, 268 topEdge + 9, df.format(start)); 269 g.appendChild(text); 270 271 break; 272 case ActionInfo.GROUPING: 273 taskGraphic = SVGUtilities.createPath(doc, 274 "m " + (left 275 + daysToStart * 300 / (totalDays - 2) - 6) 276 + " " + (topEdge + 6) + "l6 -6l" 277 + (days * 300 / (totalDays - 2)) 278 + " 0l6 6l-6 6l-4-4l" 279 + -(days * 300 / (totalDays - 2) - 8) 280 + " 0l-4 4l-6-6z"); 281 taskGraphic.setAttributeNS(null, 282 "style", 283 "stroke:black;fill:black;stroke-width:1;clip-path:url(#clip3)"); 284 g.appendChild(taskGraphic); 285 break; 286 default: 287 break; 288 } 289 } 290 291 topEdge += 14; 292 } 293 if (showing) { 294 svgRoot.appendChild(g); 295 } else { 296 topEdge = lastTop; 297 } 298 } 299 int currentDays = 300 (int)((currentDate.getTime() - lastWeek.getTime() 301 + 43200000) / 86400000); 302 303 text = SVGUtilities.createText(doc, 304 (float)(200 + (currentDays + 0.5) * 300 / 35), 305 graphTop - 1, df.format(currentDate)); 306 text.setAttributeNS(null, "text-anchor", "middle"); 307 text.setAttributeNS(null, "style", "stroke:rgb(100,100,100)"); 308 svgRoot.appendChild(text); 309 310 line = SVGUtilities.createLine(doc, 311 (float)(200 + (currentDays + 0.5) * 300 / 35), graphTop, 312 (float)(200 + (currentDays + 0.5) * 300 / 35), height); 313 line.setAttributeNS(null, "style", "fill:none;stroke:rgb(200,50,50);stroke-dasharray:5,5"); 314 svgRoot.appendChild(line); 315 316 317 } 318 } 319 | Popular Tags |