KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > chart > ChartPage


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

15 package org.apache.tapestry.workbench.chart;
16
17 import java.awt.Color JavaDoc;
18 import java.awt.Paint JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.hivemind.HiveMind;
23 import org.apache.tapestry.IAsset;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.event.PageBeginRenderListener;
26 import org.apache.tapestry.event.PageEvent;
27 import org.apache.tapestry.html.BasePage;
28 import org.apache.tapestry.valid.IValidationDelegate;
29 import org.jCharts.Chart;
30 import org.jCharts.chartData.ChartDataException;
31 import org.jCharts.chartData.PieChartDataSet;
32 import org.jCharts.nonAxisChart.PieChart2D;
33 import org.jCharts.properties.ChartProperties;
34 import org.jCharts.properties.LegendProperties;
35 import org.jCharts.properties.PieChart2DProperties;
36 import org.jCharts.test.TestDataGenerator;
37
38 /**
39  * Demonstrates more complex form handling (including loops and dynamic addition/deletion of rows)
40  * as well as dynamic image generation using JCharts.
41  *
42  * @author Howard Lewis Ship, Luis Neves
43  * @since 1.0.10
44  */

45
46 public abstract class ChartPage extends BasePage implements IChartProvider, PageBeginRenderListener
47 {
48
49     /**
50      * Invokes {@link #getPlotValues()}, which ensures that (on the very first request cycle), the
51      * persistent values property is set <em>before</em> the page recorder is locked.
52      */

53
54     public void pageBeginRender(PageEvent event)
55     {
56         if (getPlotValues() == null)
57         {
58             List JavaDoc plotValues = new ArrayList JavaDoc();
59
60             plotValues.add(new PlotValue("Fred", 10));
61             plotValues.add(new PlotValue("Barney", 15));
62             plotValues.add(new PlotValue("Dino", 7));
63
64             setPlotValues(plotValues);
65         }
66     }
67
68     public abstract List JavaDoc getPlotValues();
69
70     public abstract void setPlotValues(List JavaDoc plotValues);
71
72     public abstract PlotValue getPlotValue();
73
74     public abstract List JavaDoc getRemoveValues();
75
76     public abstract void setRemoveValues(List JavaDoc removeValues);
77
78     /**
79      * Invoked during the render; always returns false.
80      */

81
82     public boolean isMarkedForDeletion()
83     {
84         return false;
85     }
86
87     /**
88      * Invoked by the deleted checkbox (for each plotValue). If true, the the current plotValue is
89      * added to the list of plotValues to remove (though the actual removing is done inside
90      * {@link #delete(IRequestCycle)}, after the loop.
91      */

92
93     public void setMarkedForDeletion(boolean value)
94     {
95         if (value)
96         {
97             List JavaDoc removeValues = getRemoveValues();
98
99             if (removeValues == null)
100             {
101                 removeValues = new ArrayList JavaDoc();
102                 setRemoveValues(removeValues);
103             }
104
105             removeValues.add(getPlotValue());
106
107             // Deleting things screws up the validation delegate.
108
// That's because the errors are associated with the form name
109
// (not the component id), and deleting elements causes
110
// all the names to shift.
111

112             IValidationDelegate delegate = (IValidationDelegate) getBeans().getBean("delegate");
113
114             delegate.clear();
115         }
116     }
117
118     /**
119      * Listener method for the add button, adds an additional (blank) plot value.
120      */

121
122     public void add()
123     {
124         List JavaDoc plotValues = getPlotValues();
125
126         plotValues.add(new PlotValue());
127
128         setPlotValues(plotValues);
129     }
130
131     /**
132      * Listener method for the remove button, removes any checked plot values.
133      *
134      * @see #setMarkedForDeletion(boolean)
135      */

136
137     public void delete()
138     {
139         List JavaDoc removeValues = getRemoveValues();
140
141         if (removeValues != null)
142         {
143             List JavaDoc plotValues = getPlotValues();
144
145             plotValues.removeAll(removeValues);
146
147             setPlotValues(plotValues);
148         }
149     }
150
151     private IAsset chartImageAsset;
152
153     public IAsset getChartImageAsset()
154     {
155         return new ChartAsset(getRequestCycle(), this);
156     }
157
158     /**
159      * This method is invoked by the service (in a seperate request cycle from all the form handling
160      * stuff). The {@link #getChartImageAsset()}method provides an {@link IAsset}that is handled
161      * by the {@link ChartService}, and the asset encodes the identity of this page.
162      */

163
164     public Chart getChart()
165     {
166         LegendProperties legendProperties = new LegendProperties();
167         legendProperties.setNumColumns(2);
168         legendProperties.setPlacement(LegendProperties.RIGHT);
169         ChartProperties chartProperties = new ChartProperties();
170         chartProperties.setBackgroundPaint(Color.decode("#ffffcc"));
171
172         Chart result = new PieChart2D(getData(), legendProperties, chartProperties, 400, 350);
173
174         return result;
175     }
176
177     private PieChartDataSet getData()
178     {
179         List JavaDoc plotValues = getPlotValues();
180         int count = plotValues.size();
181         double[] data = new double[count];
182         String JavaDoc[] labels = new String JavaDoc[count];
183         PieChart2DProperties properties = new PieChart2DProperties();
184
185         for (int i = 0; i < count; i++)
186         {
187             PlotValue pv = (PlotValue) plotValues.get(i);
188
189             String JavaDoc name = pv.getName();
190
191             if (HiveMind.isBlank(name))
192                 name = "<New>";
193
194             data[i] = new Double JavaDoc(pv.getValue()).doubleValue();
195             labels[i] = new String JavaDoc(name);
196         }
197
198         Paint JavaDoc[] paints = TestDataGenerator.getRandomPaints(count);
199
200         try
201         {
202             return new PieChartDataSet("Pie Chart", data, labels, paints, properties);
203         }
204         catch (ChartDataException e)
205         {
206             return null;
207         }
208     }
209
210 }
Popular Tags