KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > charts > ChartMediator


1 /*
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * "The contents of this file are subject to the Mozilla Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11 * License for the specific language governing rights and limitations under
12 * the License.
13 *
14 * The Original Code is ICEfaces 1.5 open source software code, released
15 * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16 * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17 * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18 *
19 * Contributor(s): _____________________.
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23 * License), in which case the provisions of the LGPL License are
24 * applicable instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the LGPL License and not to
26 * allow others to use your version of this file under the MPL, indicate
27 * your decision by deleting the provisions above and replace them with
28 * the notice and other provisions required by the LGPL License. If you do
29 * not delete the provisions above, a recipient may use your version of
30 * this file under either the MPL or the LGPL License."
31 *
32 */

33
34 /*
35  * ChartMediator.java
36  *
37  * Created on January 24, 2007, 10:13 AM
38  *
39  * This class is the governing class for the chart component in the Component Showcase.
40  *It is used to set up the types of charts, create the charts, and manage the actions of the different charts.
41  *
42  */

43
44 package com.icesoft.icefaces.samples.showcase.components.charts;
45
46
47 import java.awt.Color JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Arrays JavaDoc;
50 import java.util.List JavaDoc;
51
52 import com.icesoft.faces.component.outputchart.OutputChart;
53 import com.icesoft.faces.context.effects.Effect;
54 import com.icesoft.faces.context.effects.Highlight;
55
56 import javax.faces.event.ActionEvent;
57 import javax.faces.event.ValueChangeEvent;
58 import javax.faces.model.SelectItem;
59
60 /**
61  * The chart factory is responsible for returning a Chart fo rthe given
62  * OutputChart type.
63  *
64  * @author dnorthcott
65  * @version 1.5
66  */

67 public class ChartMediator {
68     
69     private static final List JavaDoc areaPaints =
70         new ArrayList JavaDoc(Arrays.asList(new Color JavaDoc[]{new Color JavaDoc(26, 86, 138),
71                                                 new Color JavaDoc(76, 126, 167),
72                                                 new Color JavaDoc(148, 179, 203)}));
73     
74     //List of charts that the user can seleect from the drop down menu
75
private static SelectItem[] chartList = new SelectItem[]{
76         new SelectItem(OutputChart.AREA_CHART_TYPE),
77         new SelectItem(OutputChart.AREA_STACKED_CHART_TYPE),
78         new SelectItem(OutputChart.BAR_CHART_TYPE),
79         new SelectItem(OutputChart.BAR_CLUSTERED_CHART_TYPE),
80         new SelectItem(OutputChart.BAR_STACKED_CHART_TYPE),
81         new SelectItem(OutputChart.LINE_CHART_TYPE),
82         new SelectItem(OutputChart.POINT_CHART_TYPE),
83         new SelectItem(OutputChart.PIE2D_CHART_TYPE),
84         new SelectItem(OutputChart.PIE3D_CHART_TYPE),};
85     
86     
87     //boolean to determine if the chart is a 2D pie chart
88
private boolean pie = false;
89     
90     
91     
92     //boolean to determine if the chart is an axis chart
93
private boolean axis = true;
94     
95     //boolean to determine if the chart is a 3D pie chart
96
public boolean pie3D = false;
97     
98     //Flag to determine whether the type of chart was changed
99
private boolean chartChangedFlag = true;
100     
101     //Temporary holding string when the type of chart has been changed
102
private String JavaDoc wasChanged;
103     
104     public static final String JavaDoc DEFAULT_STRING =
105             "Click on the image map below to display a chart value: ";
106     
107     //Highlight effect on the text when the image is clicked
108
private Highlight effectOutputText = new Highlight("#ffff99");
109     
110     //sets the chart type to bar for default
111
private String JavaDoc chart = OutputChart.BAR_CHART_TYPE;
112     
113     public Chart chartObject = new Chart(chart);
114     
115     //sets the string returned when the chart is clicked to the default value
116
private String JavaDoc clickedValue = DEFAULT_STRING;
117     
118     // Used for bar and barclustered horizontal attribute
119
private boolean horizontal = false;
120     
121     // Used to show the orientation radio buttons
122
private boolean orientationChange = true;
123     
124     //Title for the x-axis
125
private String JavaDoc titleX = "Years";
126     
127     //Title for the y-axis
128
private String JavaDoc titleY = "Problems";
129     
130     
131     /*
132      * Returns the list of charts to choose from
133      * @return list of possible charts.
134      */

135     public SelectItem[] getChartList() {
136         return chartList;
137     }
138     
139     /*
140      * Sets the isAxis Boolean
141      *@param boolean isAxis
142      */

143     public boolean isAxis() {
144         return axis;
145     }
146     
147     /*
148      * Returns whether the graph is a 3D PieChart
149      *@return boolean
150      */

151     public boolean isPie3D() {
152         return pie3D;
153     }
154     
155     /*
156      * Sets the graph to a 3D pie
157      *@param boolean pie3D
158      */

159     public void setPie3D(boolean pie3D) {
160         this.pie3D = pie3D;
161     }
162     
163     /*
164      * Returns whether the graph is a pie chart(2d)
165      *@return boolean
166      */

167     public boolean isPie() {
168         return pie;
169     }
170     
171     /*
172      * Sets the graph to an axis type
173      *@param boolean axis
174      */

175     public void setAxis(boolean axis) {
176         this.axis = axis;
177     }
178     
179     /*
180      * Sets the graph to a 2dPie type
181      *@param boolean pie
182      */

183     public void setPie(boolean pie) {
184         this.pie = pie;
185     }
186     
187     
188     /*
189      * Returns the chart String
190      *@return String chart
191      */

192     public String JavaDoc getChart() {
193         
194         return chart;
195     }
196     
197     
198     /*
199      * Sets the chart String
200      * Determines what type of graph it is based on the parameter passed in
201      *@param String chart
202      */

203     public void setChart(String JavaDoc chart) {
204         
205         if (wasChanged != null) {
206             this.chart = wasChanged;
207             wasChanged = null;
208         } else {
209             this.chart = chart;
210         }
211         
212         chartObject.setType(chart);
213         
214         if (!chart.equals(OutputChart.PIE3D_CHART_TYPE)) {
215             pie3D = false;
216             
217         }
218         if (chart.equals(OutputChart.PIE3D_CHART_TYPE) ||
219                 chart.equals(OutputChart.PIE2D_CHART_TYPE)) {
220             axis = false;
221             
222             if (chart.equals(OutputChart.PIE3D_CHART_TYPE)) {
223                 pie3D = true;
224                 
225                 
226             }
227         } else {
228             axis = true;
229         }
230         
231     }
232     
233     /*
234      * Determines whether or not the application should render based on status change
235      *@return boolean
236      */

237     public boolean allCharts(OutputChart component) {
238         
239         if (chartChangedFlag) {
240             chartChangedFlag = false;
241             return true;
242         } else {
243             return chartChangedFlag;
244         }
245     }
246     
247     
248     /*
249      * Determines whether the chart was changed
250      * Sets the default_string if the area type charts are selected
251      *@param ValueChangeEvent event
252      */

253     public void chartChanged(ValueChangeEvent event) {
254         chartChangedFlag = true;
255         wasChanged = (String JavaDoc) event.getNewValue();
256         
257         if (event.getNewValue().equals(OutputChart.AREA_CHART_TYPE) ||
258                 event.getNewValue().equals(OutputChart.AREA_STACKED_CHART_TYPE)) {
259             setClickedValue(
260                     "A client side image map is not supported for Area charts " +
261                     "(clicking on the chart will not display any values)");
262         } else {
263             setClickedValue(DEFAULT_STRING);
264         }
265         //if it is a bar chart then show the orientation radio buttons
266
if(event.getNewValue().equals(OutputChart.BAR_CHART_TYPE) ||
267               event.getNewValue().equals(OutputChart.BAR_CLUSTERED_CHART_TYPE)){
268             orientationChange = true;
269             if(horizontal == true){
270                 //align labels to the proper axis
271
titleX = "Problems";
272                 titleY = "Years";
273             }
274         }
275         else {
276             orientationChange = false;
277             //align labels to the proper axis
278
titleX = "Years";
279             titleY = "Problems";
280         }
281             
282     }
283     
284     
285     /*
286      * Returns the clickedValue
287      *@return String clickedValue
288      */

289     public String JavaDoc getClickedValue() {
290         
291         return clickedValue;
292     }
293     
294     public Chart getChartObject(){
295         return chartObject;
296     }
297     /*
298      * Returns the text effect
299      *@return Effect EffectOutputText
300      */

301     public Effect getEffectOutputText() {
302         return effectOutputText;
303     }
304     
305     /*
306      * Sets the output text effect
307      *@param Effect effectOutputText
308      */

309     public void setEffectOutputText(Effect effectOutputText) {
310         this.effectOutputText = (Highlight) effectOutputText;
311     }
312     
313     /*
314      * Sets the clicked value
315      *@param String clickedValue
316      */

317     public void setClickedValue(String JavaDoc clickedValue) {
318         
319         this.clickedValue = clickedValue;
320     }
321     
322     /*
323      * When the image map has been clicked this method returns the axis label plus the value
324      *@param ActionEvent event
325      */

326     public void imageClicked(ActionEvent event) {
327         
328         if (event.getSource() instanceof OutputChart) {
329             OutputChart chart = (OutputChart) event.getSource();
330             if (chart.getClickedImageMapArea().getXAxisLabel() != null) {
331                 
332                 setClickedValue(DEFAULT_STRING +
333                         chart.getClickedImageMapArea()
334                         .getXAxisLabel() +
335                         " : " + chart.getClickedImageMapArea()
336                         .getValue());
337                 
338                 effectOutputText.setFired(false);
339             }
340             
341         }
342         
343     }
344     
345     public void pieAction(ActionEvent event) {
346         
347         if (event.getSource() instanceof OutputChart) {
348             OutputChart chart = (OutputChart) event.getSource();
349             if (chart.getClickedImageMapArea().getLengendLabel() != null) {
350                 setClickedValue(DEFAULT_STRING + chart
351                         .getClickedImageMapArea().getLengendLabel()
352                         + " : " +
353                         chart.getClickedImageMapArea().getValue());
354                 PieChartBean.setSalesForYear(
355                         chart.getClickedImageMapArea().getLengendLabel());
356                 effectOutputText.setFired(false);
357                 
358                 
359             }
360         }
361         
362     }
363     /**
364      * Sets the horizontal, true the chart will be displayed in a
365      * horizontal orientation, false the chart will be in a vertical
366      * orientation.
367      *
368      * @param horizontal true for horizontal, false for vertical.
369      */

370     public void setHorizontal(boolean horizontal){
371         this.horizontal = horizontal;
372     }
373     
374     /**
375      * Gets the horizontal boolean value for the charts orientation.
376      *
377      * @return horizontal boolean value for chart orientation.
378      */

379     public boolean getHorizontal(){
380         return horizontal;
381     }
382     
383     /**
384      * Gets the orientationChange value. True is for when it is a bar or
385      * barclustered chart. False is all other charts. Used to display the
386      * radio button group for changing chart orientation.
387      *
388      * @return orientationChange true for visible, false is not visible.
389      */

390     public boolean getOrientationChange() {
391         return orientationChange;
392     }
393     
394     /**
395      * Sets the orientationChange value, true, radio button group is visivble,
396      * false, the radio button group is not visible.
397      *
398      * @param orientationChange true for visible, false is not.
399      */

400     public void setOrientationChange(boolean orientationChange) {
401         this.orientationChange = orientationChange;
402     }
403
404     /**
405      * Method called when the orientation is change by the user.
406      *
407      * @param event contains new orientation value.
408      */

409     public void selectOrientation(ValueChangeEvent event){
410         String JavaDoc temp = titleX;
411         if(event.getNewValue().equals("true")){
412             //set the orientation and reverse the axis labels
413
horizontal = true;
414             titleX = titleY;
415             titleY = temp;
416         }
417         else{
418             //set the orientation and reverse the axis labels
419
horizontal = false;
420             titleX = titleY;
421             titleY = temp;
422         }
423         //render the chart
424
chartChangedFlag = true;
425     }
426     
427     /**
428      * Gets the title of the x-axis.
429      *
430      * @return titleX name for the x-axis.
431      */

432     public String JavaDoc getTitleX(){
433         return titleX;
434     }
435     
436     /**
437      * Sets the title of the x-axis.
438      *
439      * @param titleX new title of the x-axis.
440      */

441     public void setTitleX(String JavaDoc titleX){
442         this.titleX = titleX;
443     }
444     
445     /**
446      * Gets the title of the y-axis.
447      *
448      * @return titleY name for the y-axis.
449      */

450     public String JavaDoc getTitleY(){
451         return titleY;
452     }
453     
454     /**
455      * Sets the title of the y-axis.
456      *
457      * @param titleY new title of the y-axis.
458      */

459     public void setTitleY(String JavaDoc titleY){
460         this.titleY = titleY;
461     }
462     
463     public List JavaDoc getAreaPaints() {
464         return areaPaints;
465     }
466 }
467
Popular Tags