KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > reportcharts > AbstractChartExpression


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  */

13 package org.pentaho.plugin.jfreereport.reportcharts;
14
15 import java.awt.BasicStroke JavaDoc;
16 import java.awt.Color JavaDoc;
17 import java.awt.Font JavaDoc;
18 import java.awt.Image JavaDoc;
19 import java.awt.Stroke JavaDoc;
20 import java.awt.Toolkit JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.jfree.chart.ChartRenderingInfo;
28 import org.jfree.chart.ChartUtilities;
29 import org.jfree.chart.JFreeChart;
30 import org.jfree.chart.block.BlockBorder;
31 import org.jfree.chart.plot.Plot;
32 import org.jfree.chart.title.LegendTitle;
33 import org.jfree.fonts.StringUtilities;
34 import org.jfree.report.function.AbstractExpression;
35 import org.jfree.report.function.Expression;
36 import org.jfree.ui.RectangleEdge;
37 import org.jfree.util.Log;
38 import org.pentaho.core.session.IPentahoSession;
39 import org.pentaho.core.session.StandaloneSession;
40 import org.pentaho.core.system.IApplicationContext;
41 import org.pentaho.core.system.PentahoSystem;
42 import org.pentaho.messages.Messages;
43 import org.pentaho.plugin.jfreechart.JFreeChartEngine;
44 import org.pentaho.util.ColorHelper;
45
46 public abstract class AbstractChartExpression extends AbstractExpression {
47     /**
48      * Used for turning off outline in bar legend
49      */

50     protected static final Stroke JavaDoc EmptyStroke = new BasicStroke JavaDoc(0.0f);
51
52     private static Map JavaDoc LEGEND_LOCATIONS;
53
54     private String JavaDoc dataSource;
55
56     private String JavaDoc title;
57
58     private boolean antiAlias = true;
59
60     private String JavaDoc legendLocation;
61
62     private String JavaDoc titleFont = "SansSerif-BOLD-14"; //$NON-NLS-1$
63

64     private String JavaDoc labelFont = "SansSerif--8"; //$NON-NLS-1$
65

66     private String JavaDoc legendFont = "SansSerif--8"; //$NON-NLS-1$
67

68     private int chartWidth;
69
70     private int chartHeight;
71
72     private boolean showBorder;
73
74     private String JavaDoc borderColor;
75
76     private String JavaDoc backgroundColor;
77
78     private boolean drawLegendBorder = true;
79
80     private boolean showLegend;
81
82     private boolean threeD;
83
84     private boolean chartSectionOutline;
85
86     private boolean useDrawable = true;
87
88     private String JavaDoc backgroundImage;
89
90     private String JavaDoc chartDirectory;
91
92     private String JavaDoc chartFile;
93
94     private String JavaDoc chartUrlMask;
95
96     private boolean returnFileNameOnly;
97
98     private boolean returnImageReference;
99
100     private HashMap JavaDoc valueCache = new HashMap JavaDoc();
101
102     public static final String JavaDoc CACHE_ENABLED_CONFIG = "org.pentaho.plugin.jfreereport.CacheChartExpressionResults"; //$NON-NLS-1$
103

104     public static final String JavaDoc CACHE_ENABLED_DEFAULT = "true"; //$NON-NLS-1$
105

106     public static final int MAX_CACHE_ITEMS = 10;
107     
108     private HashMap JavaDoc chartCache = new HashMap JavaDoc();
109     
110     private IPentahoSession session;
111     
112   protected AbstractChartExpression() {
113     session = new StandaloneSession("ChartExpression"); //$NON-NLS-1$
114
}
115   
116     protected AbstractChartExpression(IPentahoSession session) {
117         this.session = session;
118     }
119
120     private RectangleEdge translateEdge(String JavaDoc edge) {
121         if (LEGEND_LOCATIONS == null) {
122             LEGEND_LOCATIONS = new HashMap JavaDoc();
123             LEGEND_LOCATIONS.put("left", RectangleEdge.LEFT); //$NON-NLS-1$
124
LEGEND_LOCATIONS.put("west", RectangleEdge.LEFT); //$NON-NLS-1$
125
LEGEND_LOCATIONS.put("right", RectangleEdge.RIGHT); //$NON-NLS-1$
126
LEGEND_LOCATIONS.put("east", RectangleEdge.RIGHT); //$NON-NLS-1$
127
LEGEND_LOCATIONS.put("top", RectangleEdge.TOP); //$NON-NLS-1$
128
LEGEND_LOCATIONS.put("north", RectangleEdge.TOP); //$NON-NLS-1$
129
LEGEND_LOCATIONS.put("bottom", RectangleEdge.BOTTOM); //$NON-NLS-1$
130
LEGEND_LOCATIONS.put("south", RectangleEdge.BOTTOM); //$NON-NLS-1$
131
}
132         final RectangleEdge translatedEdge = (RectangleEdge) LEGEND_LOCATIONS.get(edge);
133         if (translatedEdge != null) {
134             return translatedEdge;
135         }
136         return RectangleEdge.LEFT;
137     }
138
139     public String JavaDoc getTitleFont() {
140         return titleFont;
141     }
142
143     public void setTitleFont(final String JavaDoc value) {
144         this.titleFont = value;
145     }
146
147     public String JavaDoc getLegendFont() {
148         return legendFont;
149     }
150
151     public void setLegendFont(String JavaDoc value) {
152         legendFont = value;
153     }
154
155     public String JavaDoc getLabelFont() {
156         return labelFont;
157     }
158
159     public void setLabelFont(final String JavaDoc value) {
160         this.labelFont = value;
161     }
162
163     public void setChartDirectory(String JavaDoc value) {
164         chartDirectory = value;
165     }
166
167     public void setChartFile(String JavaDoc value) {
168         chartFile = value;
169     }
170
171     public void setChartUrlMask(String JavaDoc value) {
172         chartUrlMask = value;
173     }
174
175     public String JavaDoc getChartDirectory() {
176         return chartDirectory;
177     }
178
179     public String JavaDoc getChartFile() {
180         return chartFile;
181     }
182
183     public String JavaDoc getChartUrlMask() {
184         return chartUrlMask;
185     }
186
187     public String JavaDoc getDataSource() {
188         return dataSource;
189     }
190
191     public void setDataSource(final String JavaDoc dataSource) {
192         this.dataSource = dataSource;
193     }
194
195     public String JavaDoc getTitle() {
196         return getPossibleExpressionStringValue(title);
197     }
198
199     public boolean isReturnFileNameOnly() {
200         return returnFileNameOnly;
201     }
202
203     public void setReturnFileNameOnly(boolean value) {
204         returnFileNameOnly = value;
205     }
206
207     public String JavaDoc getPossibleExpressionStringValue(String JavaDoc lookupValue) {
208         if (lookupValue == null) {
209             return null;
210         }
211
212         Object JavaDoc maybeExpression = null;
213         try {
214             maybeExpression = getDataRow().get(lookupValue);
215         } catch (Exception JavaDoc ignored) {
216             // ignore the expression
217
}
218         if (maybeExpression != null) {
219             return maybeExpression.toString();
220         } else {
221             return lookupValue;
222         }
223     }
224
225     public void setTitle(final String JavaDoc title) {
226         this.title = title;
227     }
228
229     public void setChartWidth(int value) {
230         chartWidth = value;
231     }
232
233     public int getChartWidth() {
234         return chartWidth;
235     }
236
237     public void setChartHeight(int value) {
238         chartHeight = value;
239     }
240
241     public int getChartHeight() {
242         return chartHeight;
243     }
244
245     public boolean isAntiAlias() {
246         return antiAlias;
247     }
248
249     public void setAntiAlias(final boolean value) {
250         antiAlias = value;
251     }
252
253     public String JavaDoc getBorderColor() {
254         return borderColor;
255     }
256
257     public void setBorderColor(String JavaDoc value) {
258         borderColor = value;
259     }
260
261     public String JavaDoc getBackgroundColor() {
262         return backgroundColor;
263     }
264
265     public void setBackgroundColor(String JavaDoc value) {
266         backgroundColor = value;
267     }
268
269     public boolean isShowBorder() {
270         return showBorder;
271     }
272
273     public void setShowBorder(boolean value) {
274         showBorder = value;
275     }
276
277     public String JavaDoc getLegendLocation() {
278         return legendLocation;
279     }
280
281     public void setLegendLocation(String JavaDoc value) {
282         legendLocation = value;
283     }
284
285     public boolean isDrawLegendBorder() {
286         return drawLegendBorder;
287     }
288
289     public void setDrawLegendBorder(boolean value) {
290         drawLegendBorder = value;
291     }
292
293     public boolean isShowLegend() {
294         return showLegend;
295     }
296
297     public void setShowLegend(final boolean value) {
298         showLegend = value;
299     }
300
301     public boolean isThreeD() {
302         return threeD;
303     }
304
305     public void setThreeD(final boolean value) {
306         threeD = value;
307     }
308
309     public boolean isChartSectionOutline() {
310         return chartSectionOutline;
311     }
312
313     public void setChartSectionOutline(final boolean value) {
314         chartSectionOutline = value;
315     }
316
317     public boolean isUseDrawable() {
318         return useDrawable;
319     }
320
321     public void setUseDrawable(boolean value) {
322         useDrawable = value;
323     }
324
325     public void setBackgroundImage(String JavaDoc value) {
326         this.backgroundImage = value;
327     }
328
329     public String JavaDoc getBackgroundImage() {
330         return this.backgroundImage;
331     }
332
333     public JFreeChart getChartFromCache(Object JavaDoc key) {
334         final Object JavaDoc o = chartCache.get(key);
335         JFreeChart chart = null;
336         if (o != null) {
337             chart = (JFreeChart)o;
338         }
339         return chart;
340     }
341     
342     public void putChartInCache(JFreeChart chart, Object JavaDoc key) {
343         chartCache.put(key, chart);
344     }
345     
346     /**
347      *
348      * @param chart
349      * @return either a chart, a file, an URL or a String (yes, that is insane!)
350      */

351     protected Object JavaDoc getValue(JFreeChart chart) {
352         if (useDrawable) {
353             return chart;
354         }
355
356         final Integer JavaDoc row = new Integer JavaDoc(getRuntime().getCurrentRow());
357         final Object JavaDoc o = valueCache.get(row);
358         if (o != null) {
359             return o;
360         }
361
362         try {
363             ChartRenderingInfo info = new ChartRenderingInfo();
364
365             final boolean chartPng = isPngExport();
366             final File JavaDoc file = createChartFile();
367
368             final int chartWidth = getChartWidth();
369             final int chartHeight = getChartHeight();
370             if (chartPng) {
371                 ChartUtilities.saveChartAsPNG(file, chart, chartWidth, chartHeight, info);
372             } else {
373                 ChartUtilities.saveChartAsJPEG(file, chart, chartWidth, chartHeight, info);
374             }
375
376             Object JavaDoc retval;
377             if (returnFileNameOnly) {
378                 retval = file.getAbsoluteFile();
379             } else {
380                 String JavaDoc locationString = createChartLocationString(file);
381                 if (locationString != null) {
382                     if (returnImageReference) {
383
384                         final Image JavaDoc image = Toolkit.getDefaultToolkit().createImage(file.toURL());
385                         retval = new ChartImageContainer(locationString, image, chartWidth, chartHeight, row.intValue(), getName());
386                     } else {
387                         retval = locationString;
388                     }
389                 } else {
390                     retval = file.toURL();
391                 }
392             }
393
394             if (getReportConfiguration().getConfigProperty(CACHE_ENABLED_CONFIG, CACHE_ENABLED_DEFAULT).equals("true")) { //$NON-NLS-1$
395
valueCache.put(row, retval);
396             }
397             return retval;
398         } catch (IOException JavaDoc ex) {
399             Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART"), ex); //$NON-NLS-1$
400
}
401         return null;
402     }
403
404     private String JavaDoc createChartLocationString(File JavaDoc file) {
405         String JavaDoc fName = file.getName();
406         if (chartUrlMask != null) {
407             return MessageFormat.format(chartUrlMask, new Object JavaDoc[] { fName });
408         }
409
410         IApplicationContext ctx = PentahoSystem.getApplicationContext();
411         if (ctx != null) {
412             return ctx.getBaseUrl() + "getImage?image=" + fName; //$NON-NLS-1$
413
}
414         return null;
415     }
416
417     private File JavaDoc createChartFile() throws IOException JavaDoc {
418         if (chartFile != null) {
419             if (isPngExport() && StringUtilities.endsWithIgnoreCase(chartFile, ".PNG") == false) //$NON-NLS-1$
420
{
421                 chartFile = chartFile + ".png"; //$NON-NLS-1$
422
}
423
424             File JavaDoc file = new File JavaDoc(chartFile);
425             if (file.exists()) {
426                 if (!file.isFile()) {
427                     Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0003_FILE_IS_NOT_FILE"));//$NON-NLS-1$
428
} else if (!file.canWrite()) {
429                     Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0004_FILE_IS_NOT_WRITABLE"));//$NON-NLS-1$
430
} else {
431                     return file;
432                 }
433             } else {
434                 try {
435                     if (file.createNewFile()) {
436                         return file;
437                     }
438
439                     Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0005_FILE_COULD_NOT_BE_CREATED"));//$NON-NLS-1$)
440
} catch (IOException JavaDoc ex) {
441                     Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0005_FILE_COULD_NOT_BE_CREATED"), ex);//$NON-NLS-1$)
442
}
443             }
444         }
445
446         File JavaDoc directory = getStorageDir();
447         File JavaDoc file = File.createTempFile("tmp_chart", ".png", directory); //$NON-NLS-1$ //$NON-NLS-2$
448
file.deleteOnExit();
449         return file;
450     }
451
452     private boolean isPngExport() {
453         boolean chartPng = true;
454         if (chartFile != null && StringUtilities.endsWithIgnoreCase(chartFile, ".jpg")) { //$NON-NLS-1$
455
chartPng = false;
456         }
457         return chartPng;
458     }
459
460     private File JavaDoc getStorageDir() {
461         // If there is a directory defined, try to use it.
462
if (chartDirectory != null) {
463             File JavaDoc directory = new File JavaDoc(chartDirectory);
464             if (directory.exists() && directory.isDirectory()) {
465                 return directory;
466             }
467
468             Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0002_DIRECTORY_NOT_FOUND"));//$NON-NLS-1$
469
}
470
471         // At this point, no directory is defined. The predefined one is invalid.
472
IApplicationContext ctx = PentahoSystem.getApplicationContext();
473         if (ctx != null) {
474             return new File JavaDoc(ctx.getFileOutputPath("system/tmp/")); //$NON-NLS-1$
475
} else {
476             Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0006_PENTAHOSYSTEM_UNINITIALIZED"));//$NON-NLS-1$
477
String JavaDoc tempDir = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
478
return new File JavaDoc(tempDir);
479         }
480     }
481
482     protected void setChartProperties(JFreeChart chart) {
483         // Misc Properties
484
Font JavaDoc tFont = Font.decode(getTitleFont());
485         chart.getTitle().setFont(tFont);
486         if (!isAntiAlias()) {
487             chart.setAntiAlias(false);
488         }
489         chart.setBorderVisible(isShowBorder());
490         Color JavaDoc backCol = getColorFromString(backgroundColor);
491         if (backCol != null) {
492             chart.setBackgroundPaint(backCol);
493         }
494
495         Color JavaDoc borderCol = getColorFromString(borderColor);
496         if (borderCol != null) {
497             chart.setBorderPaint(borderCol);
498         }
499
500         if (legendLocation != null) {
501             LegendTitle chLegend = chart.getLegend();
502             if (chLegend != null) {
503                 RectangleEdge loc = translateEdge(legendLocation.toLowerCase());
504                 if (loc != null) {
505                     chLegend.setPosition(loc);
506                 }
507                 if (getLegendFont() != null) {
508                     chLegend.setItemFont(Font.decode(getLegendFont()));
509                 }
510                 if (!isDrawLegendBorder()) {
511                     chLegend.setBorder(BlockBorder.NONE);
512                 }
513             }
514         }
515         Plot plot = chart.getPlot();
516         if (!isChartSectionOutline()) {
517             plot.setOutlineStroke(EmptyStroke);
518         }
519         if (backgroundImage != null) {
520             plot.setBackgroundImage(JFreeChartEngine.getImage(backgroundImage, getSession()));
521         }
522
523     }
524
525     protected Color JavaDoc getColorFromString(String JavaDoc colStr) {
526         if (colStr == null) {
527             return null;
528         }
529
530         try {
531             return Color.decode(colStr);
532         } catch (NumberFormatException JavaDoc ex) {
533             // Ignored - try other parser...
534
}
535         return ColorHelper.lookupColor(colStr);
536
537     }
538
539     public Expression getInstance() {
540         final AbstractChartExpression instance = (AbstractChartExpression) super.getInstance();
541         instance.valueCache = new HashMap JavaDoc();
542         return instance;
543     }
544
545     public boolean isReturnImageReference() {
546         return returnImageReference;
547     }
548
549     public void setReturnImageReference(final boolean returnImageReference) {
550         this.returnImageReference = returnImageReference;
551     }
552
553     public IPentahoSession getSession() {
554         return session;
555     }
556
557     public void setSession(IPentahoSession session) {
558         this.session = session;
559     }
560 }
561
Popular Tags