KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > DashboardWidgetComponent


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  * @created Aug 15, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.component;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.dom4j.Document;
27 import org.dom4j.DocumentHelper;
28 import org.dom4j.Element;
29 import org.pentaho.messages.Messages;
30 import org.pentaho.plugin.jfreechart.DialWidgetDefinition;
31 import org.pentaho.plugin.jfreechart.WidgetDefinition;
32 import org.pentaho.core.solution.ActionResource;
33 import org.pentaho.core.solution.IActionResource;
34 import org.pentaho.core.system.PentahoSystem;
35 import org.pentaho.core.ui.IPentahoUrlFactory;
36 import org.pentaho.plugin.jfreechart.JFreeChartEngine;
37 import org.pentaho.ui.XmlComponent;
38
39 /**
40  * This class is a Pentaho user interface component. <p/> It generates dial
41  * images that can be embedded into JSPs, portals or other HTML supporting user
42  * interface. <p/>
43  * <ol>
44  * <li> The creating object sets the width, height, the type of the dial, and
45  * the name of the dial.xml file that contains the definition of the dial.</li>
46  * <li> This class creates an instance of a DialWidgetDefinition using the
47  * specified XML definition file. The XML files are located in the solution
48  * folders and have .dial.xml extenstions. The dial XML files define the
49  * attributes that define how the dial looks. </li>
50  * <li> It uses the JFreeChartEngine to create an image of the dial.</li>
51  * <li> Once the image has been created this class creates an XML document
52  * describing the dial
53  * <li> It uses an XSL to tranforms the XML description into HTML.
54  * </ol>
55  * This is an example image
56  */

57 public class DashboardWidgetComponent extends XmlComponent {
58
59     /**
60      *
61      */

62     private static final long serialVersionUID = 3060729271469984040L;
63
64     public static final int TYPE_DIAL = 1;
65
66     public static final int TYPE_THERMOMETER = 2;
67
68     private static final Log logger = LogFactory.getLog(DashboardWidgetComponent.class);
69
70     private int type;
71
72     private double value = TYPE_DIAL;
73
74     private String JavaDoc definitionPath;
75
76     private String JavaDoc title = ""; //$NON-NLS-1$
77

78     private String JavaDoc units = ""; //$NON-NLS-1$
79

80     private int width;
81
82     private int height;
83
84     /**
85      * Creates a DashboardWidgetComponent.
86      * <p>
87      * After creating an instance of this class <CODE>validate</CODE> should
88      * be called.
89      *
90      * @param type
91      * The type of the widget, currently only TYPE_DIAL is supported
92      * @param definitionPath
93      * The path and name of the XML definition of the dial
94      * @param width
95      * The width of the image to be created
96      * @param height
97      * The height of the image to be created
98      * @param urlFactory
99      * The urlFactory for the content
100      * @param messages
101      * The messages list for any logger messages
102      */

103     public DashboardWidgetComponent(int type, String JavaDoc definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
104         super(urlFactory, messages, null);
105         this.type = type;
106         this.definitionPath = definitionPath;
107         this.width = width;
108         this.height = height;
109         String JavaDoc info[] = PentahoSystem.parseActionString(definitionPath);
110         if (info != null && info.length == 3) {
111             setSourcePath(info[0] + File.separator + info[1]);
112         }
113         // Set the XSL file to be used to generate the HTML
114
setXsl("text/html", "DialWidget.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
115
}
116
117     /**
118      * Sets the value to be displayed by the dial.
119      *
120      * @param value
121      * The dial value
122      */

123     public void setValue(double value) {
124         this.value = value;
125     }
126
127     /**
128      * Sets the title for the dial
129      *
130      * @param title
131      * The title of the dial
132      */

133     public void setTitle(String JavaDoc title) {
134         this.title = title;
135     }
136
137     /**
138      * Sets the unit for the dial value
139      *
140      * @param units
141      * The dial units
142      */

143     public void setUnits(String JavaDoc units) {
144         this.units = units;
145     }
146
147     /**
148      * Gets the logger for his component.
149      *
150      * @return logger This component's logger
151      */

152     public Log getLogger() {
153         return logger;
154     }
155
156     /**
157      * Validate that this component can generate the requested dial
158      */

159     public boolean validate() {
160         // TODO
161
return true;
162     }
163
164     /**
165      * Create a dial image.
166      * <ul>
167      * <li>Load the specified XML document describing the dial definition</li>
168      * <li>Create a dial definition object from the XML definition</li>
169      * <li>Use the JFreeChartEngine to create a dial image</li>
170      * <li>Create an XML document describing the dial</li>
171      * <li>Return the XML document</li>
172      * </ul>
173      *
174      * @return The XML document describing this dial
175      */

176     public Document getXmlContent() {
177
178         WidgetDefinition widget = null;
179         if (type == TYPE_DIAL) {
180
181             // load the XML document that defines the dial
182
ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
183
definitionPath);
184             Document dialDefinition = null;
185             try {
186                 dialDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource);
187             } catch (IOException JavaDoc e) {
188                 error(Messages.getErrorString("Widget.ERROR_0002_INVALID_RESOURCE", definitionPath), e); //$NON-NLS-1$
189
}
190
191             if (dialDefinition == null) {
192                 error(Messages.getErrorString("Widget.ERROR_0002_INVALID_RESOURCE", definitionPath) ); //$NON-NLS-1$
193
return null;
194             }
195             // create a dial definition from the XML definition
196
widget = new DialWidgetDefinition(dialDefinition, 0, width, height, getSession());
197
198             if (widget != null) {
199                 // set the value to be displayed on the dial
200
widget.setValue(new Double JavaDoc(value));
201
202             } else {
203                 error(Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE")); //$NON-NLS-1$
204
return null;
205             }
206         }
207         /*
208          * else if( type == TYPE_THERMOMETER ) { // load the XML document that
209          * defines the thermometer
210          *
211          * ActionResource resource = new ActionResource( title,
212          * IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
213          * PentahoSystem.getApplicationContext().getSolutionPath( definitionPath ) );
214          * //$NON-NLS-1$ Document thermometerDefinition = null; try {
215          * thermometerDefinition = PentahoSystem.getResourceAsDocument( resource ); }
216          * catch (IOException e) {} // create a dial definition from the XML
217          * definition widget = createThermometer( thermometerDefinition );
218          *
219          * if( widget != null ) { // set the value to be displayed on the dial
220          * widget.setValue( new Double(value) ); // Set the XSL file to be used
221          * to generate the HTML setXsl( "text/html", "DialWidget.xsl" );
222          * //$NON-NLS-1$ //$NON-NLS-2$ } else { error(
223          * Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE") );
224          * //$NON-NLS-1$ return null; } }
225          */

226         if (widget == null) {
227             error(Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE")); //$NON-NLS-1$
228
return null;
229         }
230         // create an image for the dial using the JFreeChart engine
231
StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
232         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
233         // create temporary file names
234
String JavaDoc solutionDir = "system/tmp/"; //$NON-NLS-1$
235
String JavaDoc fileNamePrefix = "tmp_pie_"; //$NON-NLS-1$
236
String JavaDoc extension = ".png"; //$NON-NLS-1$
237
String JavaDoc fileName = null;
238         String JavaDoc filePathWithoutExtension = null;
239         try {
240             File JavaDoc file = File.createTempFile(fileNamePrefix, extension, new File JavaDoc(PentahoSystem.getApplicationContext().getFileOutputPath(solutionDir)));
241             file.deleteOnExit();
242             fileName = file.getName();
243             filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.'));
244         } catch (IOException JavaDoc e) {
245             // TODO Auto-generated catch block
246
e.printStackTrace();
247         }
248         String JavaDoc dialTitle = ""; //$NON-NLS-1$
249
JFreeChartEngine.saveChart(widget, dialTitle, units, filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, this);
250
251         // Create a document that describes the result
252
Document result = DocumentHelper.createDocument();
253         String JavaDoc baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
254         setXslProperty("baseUrl", baseUrl); //$NON-NLS-1$
255

256         Element root = result.addElement("widget"); //$NON-NLS-1$
257
root.addElement("title").setText(title); //$NON-NLS-1$
258
root.addElement("units").setText(units); //$NON-NLS-1$
259
root.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
260
root.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
261
Element valueNode = root.addElement("value");//$NON-NLS-1$
262
valueNode.setText(Double.toString(value));
263         valueNode.addAttribute("in-image", Boolean.toString(widget.getValueFont() != null)); //$NON-NLS-1$
264
root.addElement("image").setText(fileName); //$NON-NLS-1$
265
return result;
266
267     }
268
269
270
271     public void dispose() {
272         
273     }
274     
275 }
276
Popular Tags