KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > ChartUI


1 package com.calipso.reportgenerator.userinterface;
2
3 import com.calipso.reportgenerator.common.HeaderTableModel;
4 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.*;
8
9 import org.jfree.data.category.DefaultCategoryDataset;
10 import org.jfree.data.category.CategoryDataset;
11
12 /** Representa la interfaz grafica generada cuando se presiona sobre
13  * el boton Grafico de la interfaz principal.
14  */

15 public class ChartUI extends JFrame implements ItemListener, ActionListener, KeyListener{
16
17     private ChartCustomizer chartCustomizer;
18     private ReportGeneratorConfiguration conf;
19     private PivotTable pivotTable;
20     private JPanel northPanel;
21     private JPanel mainPanel;
22     private JRadioButton btRow;
23     private JRadioButton btCol;
24     private JComboBox cbChart;
25     private JButton btAceptar;
26     private JButton btCancelar;
27     private ChartData chartData;
28     private ButtonGroup btGroup;
29     private JButton btChartCustomizer;
30     private JPanel btsPanel;
31
32     /**
33      * Inicializa una instancia de ChartUI
34      * @param pivotTable contiene los componentes necesarios que utiliza esta clase
35      */

36     public ChartUI(PivotTable pivotTable, ReportGeneratorConfiguration conf) {
37         Image icon = conf.getImage("icon");
38         if(icon != null) {
39           setIconImage(icon);
40         }
41         this.conf = conf;
42         this.pivotTable = pivotTable;
43         initialize();
44         HeaderTableModel rowHeaderTableModel = (HeaderTableModel) pivotTable.getRowHeaderjTable().getModel();
45         HeaderTableModel columnHeaderTableModel = (HeaderTableModel) pivotTable.getColumnHeaderjTable().getModel();
46         chartData = new ChartData(pivotTable.getDatajTable(), rowHeaderTableModel, columnHeaderTableModel);
47         this.pack();
48         this.setResizable(false);
49         this.setTitle(com.calipso.reportgenerator.common.LanguageTraslator.traslate("238"));
50         java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
51         setLocation((screenSize.width - 250) / 2, (screenSize.height - 200) / 2);
52         this.setVisible(true);
53     }
54
55     /**
56      * Crea los componentes graficos
57      */

58     private void initialize(){
59         Container container = this.getContentPane();
60
61         createCboBox();
62
63         btRow = new JRadioButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("110"));
64         btRow.addActionListener(this);
65         btRow.setSelected(true);
66         btCol = new JRadioButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("111"));
67         btCol.addActionListener(this);
68         northPanel = new JPanel(new FlowLayout());
69         btGroup = new ButtonGroup();
70         btGroup.add(btRow);
71         btGroup.add(btCol);
72         northPanel.add(btRow);
73         northPanel.add(btCol);
74         northPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), com.calipso.reportgenerator.common.LanguageTraslator.traslate("240")));
75
76         mainPanel = new JPanel(new BorderLayout());
77         btChartCustomizer = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("239"));
78         btChartCustomizer.addActionListener(this);
79         btChartCustomizer.addKeyListener(this);
80         mainPanel.add(cbChart, BorderLayout.CENTER);
81         mainPanel.add(btChartCustomizer, BorderLayout.EAST);
82         mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), com.calipso.reportgenerator.common.LanguageTraslator.traslate("241")));
83
84         btAceptar = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("112"));
85         btAceptar.addActionListener(this);
86         btAceptar.setFocusable(true);
87         btAceptar.setFocusTraversalKeysEnabled(true);
88         btAceptar.addKeyListener(this);
89         btCancelar = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("113"));
90         btCancelar.addActionListener(this);
91         btCancelar.addKeyListener(this);
92         btsPanel = new JPanel(new FlowLayout());
93         btsPanel.add(btAceptar);
94         btsPanel.add(btCancelar);
95
96         container.add(northPanel, BorderLayout.NORTH);
97         container.add(mainPanel, BorderLayout.CENTER);
98         container.add(btsPanel, BorderLayout.SOUTH);
99     }
100
101     /**
102      * Crea un JComboBox dependiendo del rango seleccionado
103      * en la tabla de datos
104      */

105     private void createCboBox(){
106         cbChart = new JComboBox();
107         JTable table = pivotTable.getDatajTable();
108         int selectedColumnCount = table.getSelectedColumnCount();
109         int selectedRowCount = table.getSelectedRowCount();
110         if(selectedColumnCount > 1 && selectedRowCount > 1){
111             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("114"));
112             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("115"));
113             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("245"));
114             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("242"));
115             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("243"));
116             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("244"));
117         }else {
118             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("114"));
119             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("116"));
120             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("115"));
121             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("245"));
122             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("242"));
123             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("243"));
124             cbChart.addItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("244"));
125         }
126         cbChart.addItemListener(this);
127     }
128
129     /***
130      * Evento que se ejecuta al realizar una accion sobre el JComboBox
131      */

132     public void itemStateChanged(ItemEvent e) {
133     }
134
135     /**
136      * Ejecuta el chart seleccionado
137      * @param isRow determina si la ejecucion se realiza en base a filas o columnas
138      */

139     private void executeChart(boolean isRow){
140
141         String JavaDoc tittle = pivotTable.getReportResult().getReportSpec().getDescription();
142         JFrame frame = new JFrame();
143         Image icon = conf.getImage("icon");
144         if(icon != null) {
145           frame.setIconImage(icon);
146         }
147         frame.setSize(600, 500);
148         JScrollPane pane;
149         if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("114"))){
150             DefaultCategoryDataset dataset = chartData.getDatasetForChart(isRow);
151             if(chartCustomizer == null){
152               AreaChart areaChart = new AreaChart(dataset, tittle, Color.white, true, true, frame.getSize(), false);
153               pane = new JScrollPane(areaChart);
154               frame.getContentPane().add(pane);
155               frame.pack();
156               frame.setVisible(true);
157               return;
158             }
159             AreaChart areaChart = new AreaChart(dataset, tittle, chartCustomizer.getColor(),
160                                                 chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
161           pane = new JScrollPane(areaChart);
162           frame.getContentPane().add(pane);
163             frame.pack();
164             frame.setVisible(true);
165             return;
166         }
167         if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("116"))){
168             double chartDataArray[] = chartData.getChartDataArray();
169             String JavaDoc descripcion [] = chartData.getDescriptionForChart(null);
170             if(chartCustomizer == null){
171               PieChart pieChart = new PieChart(chartDataArray, descripcion, tittle, true, true);
172               pane = new JScrollPane(pieChart);
173               frame.getContentPane().add(pane);
174               frame.pack();
175               frame.setVisible(true);
176               return;
177             }
178             PieChart pieChart = new PieChart(chartDataArray, descripcion, tittle,
179                                              chartCustomizer.isLegend(), chartCustomizer.isTooltips());
180             pane = new JScrollPane(pieChart);
181             frame.getContentPane().add(pane);
182             frame.pack();
183             frame.setVisible(true);
184             return;
185         }if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("115"))){
186             CategoryDataset dataset = chartData.getDatasetForChart(isRow);
187             if(chartCustomizer == null){
188               HorizontalBarChart horBarChart = new HorizontalBarChart(dataset, tittle,
189                                                                       Color.white, true, true, frame.getSize(), false);
190               pane = new JScrollPane(horBarChart);
191               frame.getContentPane().add(pane);
192               frame.pack();
193               frame.setVisible(true);
194               return;
195             }
196             HorizontalBarChart horBarChart = new HorizontalBarChart(dataset, tittle,
197                                                                     chartCustomizer.getColor(), chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
198             pane = new JScrollPane(horBarChart);
199             frame.getContentPane().add(pane);
200             frame.pack();
201             frame.setVisible(true);
202             return;
203         }if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("242"))){
204             CategoryDataset dataset = chartData.getDatasetForChart(isRow);
205
206
207             if(chartCustomizer == null){
208               LineChart lineChart = new LineChart(dataset, tittle,
209                                                     Color.white, true, true, frame.getSize(), false);
210               pane = new JScrollPane(lineChart);
211               frame.getContentPane().add(pane);
212               frame.pack();
213               frame.setVisible(true);
214               return;
215             }
216             LineChart lineChart = new LineChart(dataset, tittle,
217                                                 chartCustomizer.getColor(), chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
218             pane = new JScrollPane(lineChart);
219             frame.getContentPane().add(pane);
220             frame.pack();
221             frame.setVisible(true);
222             return;
223         }if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("243"))){
224             CategoryDataset dataset = chartData.getDatasetForChart(isRow);
225             if(chartCustomizer == null){
226               VerticalBarChart verticalBarChart = new VerticalBarChart(dataset, tittle,
227                                                     Color.white, true, true, frame.getSize(), false);
228               pane = new JScrollPane(verticalBarChart);
229               frame.getContentPane().add(pane);
230               frame.pack();
231               frame.setVisible(true);
232               return;
233             }
234             VerticalBarChart verticalBarChart = new VerticalBarChart(dataset, tittle,
235                                                 chartCustomizer.getColor(), chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
236             pane = new JScrollPane(verticalBarChart);
237             frame.getContentPane().add(pane);
238             frame.pack();
239             frame.setVisible(true);
240             return;
241         }if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("244"))){
242             CategoryDataset dataset = chartData.getDatasetForChart(isRow);
243             if(chartCustomizer == null){
244               VerticalBarChart3D verticalBarChart3D = new VerticalBarChart3D(dataset, tittle,
245                                                     Color.white, true, true, frame.getSize(), false);
246               pane = new JScrollPane(verticalBarChart3D);
247               frame.getContentPane().add(pane);
248               frame.pack();
249               frame.setVisible(true);
250               return;
251             }
252             VerticalBarChart3D verticalBarChart3D = new VerticalBarChart3D(dataset, tittle,
253                                                 chartCustomizer.getColor(), chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
254             pane = new JScrollPane(verticalBarChart3D);
255             frame.getContentPane().add(pane);
256             frame.pack();
257             frame.setVisible(true);
258             return;
259         }if(cbChart.getSelectedItem().toString().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("245"))){
260             CategoryDataset dataset = chartData.getDatasetForChart(isRow);
261             if(chartCustomizer == null){
262               HorizontalBarChart3D horizontalBarChart3D = new HorizontalBarChart3D(dataset, tittle,
263                                                     Color.white, true, true, frame.getSize(), false);
264               pane = new JScrollPane(horizontalBarChart3D);
265               frame.getContentPane().add(pane);
266               frame.pack();
267               frame.setVisible(true);
268               return;
269             }
270             HorizontalBarChart3D horizontalBarChart3D = new HorizontalBarChart3D(dataset, tittle,
271                                                 chartCustomizer.getColor(), chartCustomizer.isLegend(), chartCustomizer.isTooltips(), frame.getSize(), chartCustomizer.isMultipleAxis());
272             pane = new JScrollPane(horizontalBarChart3D);
273             frame.getContentPane().add(pane);
274             frame.pack();
275             frame.setVisible(true);
276             return;
277         }
278     }
279
280     /**
281      * Se ejecuta al presionar un boton
282      * @param e representa el evento que se ejecuta
283      */

284     public void actionPerformed(ActionEvent e) {
285         if(e.getActionCommand().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("113"))){
286             this.dispose();
287             return;
288         }
289         if(e.getActionCommand().equals(com.calipso.reportgenerator.common.LanguageTraslator.traslate("112"))){
290             this.setVisible(false);
291             executeChart(getExecutionType());
292 // this.dispose();
293
return;
294         }
295         if(e.getSource() == btChartCustomizer){
296           chartCustomizer = new ChartCustomizer(this, true);
297           chartCustomizer.show();
298         }
299     }
300
301     /**
302      * Determina el tipo de ejecucion de chart, es decir, por filas o columnas
303      * @return boolean true si es por filas, false si es por columnas
304      */

305     private boolean getExecutionType(){
306         if(btRow.isSelected()){
307             return true;
308         }
309         return false;
310     }
311
312   /**
313    * Intercepta los eventos del teclado
314    * @param e
315    */

316   public void keyTyped(KeyEvent e) {
317   }
318
319   /**
320    * Intercepta los eventos del teclado y realiza
321    * la accion correspondiente.
322    * @param e
323    */

324   public void keyPressed(KeyEvent e) {
325     if(e.getSource() == btAceptar){
326       this.setVisible(false);
327       executeChart(getExecutionType());
328       return;
329     }else if(e.getSource() == btCancelar){
330       this.dispose();
331       return;
332     }else if(e.getSource() == btChartCustomizer){
333       chartCustomizer = new ChartCustomizer(this, true);
334       chartCustomizer.show();
335     }
336   }
337
338   /**
339    * Intercepta los eventos del teclado
340    * @param e
341    */

342   public void keyReleased(KeyEvent e) {
343   }
344 }
345
Popular Tags