KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > plan > SimplePlanDrawer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: SimplePlanDrawer.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.plan;
21
22 import java.text.DateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.w3c.dom.DOMImplementation JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 import org.apache.batik.dom.svg.SVGDOMImplementation;
32 import org.apache.fop.svg.SVGUtilities;
33
34 /**
35  * Simple plan drawer implementation.
36  */

37 public class SimplePlanDrawer implements PlanDrawer {
38     
39     private static final String JavaDoc SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
40     
41     private float fontSize;
42     private HashMap JavaDoc hints;
43     private java.awt.Font JavaDoc 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 JavaDoc[] colours;
52     private String JavaDoc[] darkcolours;
53
54     private Date JavaDoc startDate;
55     private Date JavaDoc endDate;
56
57     /**
58      * Sets the start date.
59      * @param sd start date
60      */

61     public void setStartDate(Date JavaDoc sd) {
62         startDate = sd;
63     }
64
65     /**
66      * Sets the end date.
67      * @param ed end date
68      */

69     public void setEndDate(Date JavaDoc ed) {
70         endDate = ed;
71     }
72
73     /**
74      * @see org.apache.fop.plan.PlanDrawer#createDocument(EventList, float, float, HashMap)
75      */

76     public Document JavaDoc createDocument(EventList data, float w, float h,
77                                    HashMap JavaDoc hints) {
78         this.width = w;
79         this.height = h;
80         this.hints = hints;
81         fontSize = ((Float JavaDoc) hints.get(PlanHints.FONT_SIZE)).floatValue();
82         bord = ((Boolean JavaDoc) hints.get(PlanHints.PLAN_BORDER)).booleanValue();
83
84         String JavaDoc title = "";
85
86         DOMImplementation JavaDoc impl =
87             SVGDOMImplementation.getDOMImplementation();
88         Document JavaDoc doc = impl.createDocument(SVG_NAMESPACE, "svg", null);
89
90         Element JavaDoc 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 JavaDoc((String JavaDoc)hints.get(PlanHints.FONT_FAMILY),
99                                   java.awt.Font.PLAIN, (int)fontSize);
100
101         if (bord) {
102             Element JavaDoc 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 JavaDoc 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         // add the actual pie chart
122
addPlan(doc, svgRoot, data);
123         //addLegend(doc, svgRoot, data);
124

125         return doc;
126     }
127
128     protected void addPlan(Document JavaDoc doc, Element JavaDoc svgRoot, EventList data) {
129         Date JavaDoc currentDate = new Date JavaDoc();
130
131         Date JavaDoc lastWeek = startDate;
132         Date JavaDoc future = endDate;
133         Calendar JavaDoc 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 JavaDoc g;
147         Element JavaDoc 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 JavaDoc clip1 = SVGUtilities.createClip(doc,
153                 SVGUtilities.createPath(doc,
154                     "m0 0l126 0l0 " + height + "l-126 0z"), "clip1");
155         Element JavaDoc clip2 = SVGUtilities.createClip(doc,
156                 SVGUtilities.createPath(doc,
157                     "m130 0l66 0l0 " + height + "l-66 0z"), "clip2");
158         Element JavaDoc 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 JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT);
167         Element JavaDoc 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 JavaDoc 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 JavaDoc name = act.getOwner();
222                 String JavaDoc 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 JavaDoc start = act.getStartDate();
233                 Date JavaDoc 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 JavaDoc 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