KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > gui > generic > servlet > attribute > ChartValue


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: ChartValue.java,v 1.16 2005/11/01 02:13:23 wfro Exp $
5  * Description: ChartValue class
6  * Revision: $Revision: 1.16 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/11/01 02:13:23 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.gui.generic.servlet.attribute;
57
58 import java.io.BufferedReader JavaDoc;
59 import java.io.ByteArrayInputStream JavaDoc;
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.InputStreamReader JavaDoc;
63 import java.io.Serializable JavaDoc;
64 import java.util.HashMap JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import javax.servlet.http.HttpServletRequest JavaDoc;
68
69 import org.openmdx.application.gui.generic.servlet.Action;
70 import org.openmdx.application.gui.generic.servlet.ApplicationContext;
71 import org.openmdx.application.gui.generic.servlet.attribute.Attribute;
72 import org.openmdx.application.gui.generic.servlet.attribute.BinaryValue;
73 import org.openmdx.application.gui.generic.servlet.attribute.FieldDef;
74 import org.openmdx.application.gui.generic.servlet.view.View;
75 import org.openmdx.application.log.AppLog;
76 import org.openmdx.base.exception.ServiceException;
77 import org.openmdx.base.text.conversion.HtmlEncoder;
78
79 public class ChartValue
80     extends BinaryValue
81     implements Serializable JavaDoc {
82
83     //-------------------------------------------------------------------------
84
public ChartValue(
85         Object JavaDoc object,
86         FieldDef fieldDef,
87         ApplicationContext application
88     ) {
89         super(
90             object,
91             fieldDef,
92             application
93         );
94     }
95
96     //-------------------------------------------------------------------------
97
public String JavaDoc toHtml(
98         HttpServletRequest JavaDoc request,
99         View view,
100         Attribute attribute,
101         Map JavaDoc modifiers,
102         Map JavaDoc popupImages,
103         Map JavaDoc renderingContext
104     ) {
105         int currentChartId = renderingContext.get("currentChartId") != null
106             ? ((Integer JavaDoc)renderingContext.get("currentChartId")).intValue()
107             : 0;
108         String JavaDoc result = "";
109         try {
110             Action binaryValueAction = (Action)this.getValue();
111             if(binaryValueAction != null) {
112                 ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
113                 try {
114                     this.getBinaryValue(os);
115                 }
116                 catch(ServiceException e) {
117                     AppLog.warning(e.getMessage(), e.getCause(), 1);
118                 }
119                 BufferedReader JavaDoc reader = new BufferedReader JavaDoc(
120                     new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(os.toByteArray()))
121                 );
122                 Map JavaDoc data = new HashMap JavaDoc();
123                 String JavaDoc line = null;
124                 while((line = reader.readLine()) != null) {
125                   if(line.indexOf(":") >= 0) {
126                     data.put(
127                       line.substring(0, line.indexOf(":")),
128                       line.substring(line.indexOf(":") + 1)
129                     );
130                   }
131                 }
132         
133                 // single-valued BinaryValue in place -->
134
result += modifiers.get("gapModifier");
135                 result += "<td class=\"label\"><span class=\"nw\">" + attribute.getLabel() + "</span></td>";
136                 result += "<td " + modifiers.get("rowSpanModifier") + " class=\"valueL\" " + modifiers.get("widthModifier") + ">";
137         
138                 // get chart data
139
int nCharts = new Integer JavaDoc(data.get("COUNT").toString().trim()).intValue();
140                 for(int chartIndex = 0; chartIndex < nCharts; chartIndex++) {
141                     String JavaDoc chartName = "CHART[" + chartIndex + "]";
142                     String JavaDoc chartType = data.get(chartName + ".TYPE") + "";
143                     
144                     // TYPE HORIZBAR
145
if("HORIZBAR".equals(chartType.trim())) {
146                         String JavaDoc chartLabel = data.get(chartName + ".LABEL") + "";
147                         String JavaDoc xMinValue = data.get(chartName + ".MINVALUE") + "";
148                         String JavaDoc xMaxValue = data.get(chartName + ".MAXVALUE") + "";
149                         String JavaDoc scaleXTitle = data.get(chartName + ".SCALEXTITLE") + "";
150                         String JavaDoc scaleYTitle = data.get(chartName + ".SCALEYTITLE") + "";
151                         String JavaDoc yLabels = "";
152                         String JavaDoc xValues = "";
153                         String JavaDoc xValues2 = "";
154                         String JavaDoc borderColors = "";
155                         String JavaDoc fillColors = "";
156                         String JavaDoc fillColors2 = "";
157                         int itemCount = data.get(chartName + ".COUNT") == null
158                             ? 0
159                             : new Integer JavaDoc(data.get(chartName + ".COUNT").toString().trim()).intValue();
160                         // spanRow > 1 --> customized. Otherwise derive height from itemCount
161
String JavaDoc styleModifier = attribute.getSpanRow() > 1
162                             ? "style=\"height: " + (1.2 + (attribute.getSpanRow()-1) * 1.5) + "em\""
163                             : "style=\"height: " + (6.0 + itemCount * 1.2) + "em\"";
164                         for(
165                             int count = 0;
166                             count < itemCount;
167                             count++
168                         ) {
169                             yLabels += yLabels.length() == 0 ? "" : ", ";
170                             yLabels += "\"" + data.get(chartName + ".LABEL[" + count + "]") + "\"";
171                             xValues += xValues.length() == 0 ? "" : ", ";
172                             xValues += "\"" + data.get(chartName + ".VAL[" + count + "]") + "\"";
173                             borderColors += borderColors.length() == 0 ? "" : ", ";
174                             borderColors += "\"" + data.get(chartName + ".BORDER[" + count + "]") + "\"";
175                             fillColors += fillColors.length() == 0 ? "" : ", ";
176                             fillColors += "\"" + data.get(chartName + ".FILL[" + count + "]") + "\"";
177                             // VAL2
178
if(data.get(chartName + ".VAL2[" + count + "]") != null) {
179                                 xValues2 += xValues2.length() == 0 ? "" : ", ";
180                                 xValues2 += "\"" + data.get(chartName + ".VAL2[" + count + "]") + "\"";
181                             }
182                             // FILL2
183
if(data.get(chartName + ".FILL2[" + count + "]") != null) {
184                                 fillColors2 += fillColors2.length() == 0 ? "" : ", ";
185                                 fillColors2 += "\"" + data.get(chartName + ".FILL2[" + count + "]") + "\"";
186                             }
187                         }
188                         result += "<div><iframe id=\"Chart" + currentChartId + "\" SRC=\"blank.html\" name=\"Chart" + currentChartId + "\" frameborder=0 scrolling=\"no\" width=\"100%\"" + styleModifier + "></iframe></div>";
189                         result += "<script language=\"javascript\" type=\"text/javascript\">";
190                         result += " function displayChart" + currentChartId + "() {";
191                         if((xValues.length() > 0) && (fillColors2.length() > 0)) {
192                             result += " calcChartTypeHorizontalBarsOverlay('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + xValues + "), " + xMinValue + ", " + xMaxValue + ", Array(" + yLabels + "), Array(" + borderColors + "), Array(" + fillColors + "), Array(" + xValues2 +"), Array(" + fillColors2 + "));";
193                         }
194                         else {
195                             result += " calcChartTypeHorizontalBars('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + xValues + "), " + xMinValue + ", " + xMaxValue + ", Array(" + yLabels + "), Array(" + borderColors + "), Array(" + fillColors + "));";
196                         }
197                         result += " }";
198                         result += "</script>";
199                     }
200         
201                     // TYPE VERTBAR
202
else if("VERTBAR".equals(chartType.trim())) {
203                         String JavaDoc chartLabel = data.get(chartName + ".LABEL") + "";
204                         String JavaDoc yMinValue = data.get(chartName + ".MINVALUE") + "";
205                         String JavaDoc yMaxValue = data.get(chartName + ".MAXVALUE") + "";
206                         String JavaDoc scaleXTitle = data.get(chartName + ".SCALEXTITLE") + "";
207                         String JavaDoc scaleYTitle = data.get(chartName + ".SCALEYTITLE") + "";
208                         String JavaDoc xLabels = "";
209                         String JavaDoc yValues = "";
210                         String JavaDoc borderColors = "";
211                         String JavaDoc fillColors = "";
212                         int itemCount = data.get(chartName + ".COUNT") == null
213                             ? 0
214                             : new Integer JavaDoc(data.get(chartName + ".COUNT").toString().trim()).intValue();
215                         String JavaDoc styleModifier = attribute.getSpanRow() > 1
216                             ? "style=\"height: " + (1.2 + (attribute.getSpanRow()-1) * 1.5) + "em\""
217                             : "15em";
218                         for(int count = 0; count < itemCount; count++) {
219                             xLabels += xLabels.length() == 0 ? "" : ", ";
220                             xLabels += "\"" + data.get(chartName + ".LABEL[" + count + "]") + "\"";
221                             yValues += yValues.length() == 0 ? "" : ", ";
222                             yValues += data.get(chartName + ".VAL[" + count + "]");
223                             borderColors += borderColors.length() == 0 ? "" : ", ";
224                             borderColors += "\"" + data.get(chartName + ".BORDER[" + count + "]") + "\"";
225                             fillColors += fillColors.length() == 0 ? "" : ", ";
226                             fillColors += "\"" + data.get(chartName + ".FILL[" + count + "]") + "\"";
227                         }
228                         result += "<div><iframe id=\"Chart" + currentChartId + "\" SRC=\"blank.html\" name=\"Chart" + currentChartId + "\" frameborder=0 scrolling=\"no\" width=\"100%\" " + styleModifier + "></iframe></div>";
229                         result += "<script language=\"javascript\" type=\"text/javascript\">";
230                         result += " function displayChart" + currentChartId + "() {";
231                         result += " calcChartTypeVerticalBars('Chart" + currentChartId + "', '" + HtmlEncoder.encode(chartLabel, false) + "', '" + scaleXTitle + "', '" + scaleYTitle + "', Array(" + yValues + "), " + yMinValue + ", " + yMaxValue + ", Array(" + xLabels + "), Array(" + borderColors + "), Array(" + fillColors + "));";
232                         result += " }";
233                         result += "</script>";
234                     }
235                 }
236                 result += "</td>";
237             }
238         }
239         catch(IOException JavaDoc e) {}
240         renderingContext.put(
241             "currentChartId",
242             new Integer JavaDoc(currentChartId+1)
243         );
244         return result;
245     }
246     //-------------------------------------------------------------------------
247
// Members
248
//-------------------------------------------------------------------------
249
/**
250      * Comment for <code>serialVersionUID</code>
251      */

252     private static final long serialVersionUID = 3258689922910336053L;
253
254 }
255
256 //--- End of File -----------------------------------------------------------
257
Popular Tags