KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > chart > XYDatasetPanel


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * XYDatasetPanel.java
28  *
29  * Created on 15 agosto 2005, 17.55
30  *
31  */

32
33 package it.businesslogic.ireport.chart;
34 import it.businesslogic.ireport.SubDataset;
35 import java.util.*;
36 import it.businesslogic.ireport.util.I18n;
37 /**
38  *
39  * @author Administrator
40  */

41 public class XYDatasetPanel extends javax.swing.JPanel JavaDoc implements ChartDatasetPanel {
42     
43     private XYDataset xyDataset = null;
44     private SubDataset subDataset = null;
45     
46     /** Creates new form PieDatasetPanel */
47     public XYDatasetPanel() {
48         initComponents();
49         applyI18n();
50         jList1.setModel( new javax.swing.DefaultListModel JavaDoc());
51     }
52
53     public void setXYDataset(XYDataset xyDataset) {
54         this.xyDataset = xyDataset;
55         
56         jButtonModify.setEnabled( false );
57         jButtonModify.setEnabled( false );
58         javax.swing.DefaultListModel JavaDoc lm = (javax.swing.DefaultListModel JavaDoc)jList1.getModel();
59         
60         lm.removeAllElements();
61         
62         Vector v = xyDataset.getXYSeries();
63                         
64         for (int i=0; i< v.size(); ++i)
65         {
66             lm.addElement(v.elementAt(i) );
67         }
68     }
69
70     public XYDataset getXYDataset() {
71         return xyDataset;
72     }
73   
74     private void updateSeriesType()
75     {
76             for (int i=0; i<xyDataset.getXYSeries().size(); ++i)
77             {
78                 Object JavaDoc series = xyDataset.getXYSeries().elementAt(i);
79                 ((javax.swing.DefaultListModel JavaDoc)jList1.getModel()).removeElement(series);
80                  xyDataset.getXYSeries().removeElementAt(i);
81                  i--;
82             }
83             jList1.updateUI();
84     }
85     
86     /** This method is called from within the constructor to
87      * initialize the form.
88      * WARNING: Do NOT modify this code. The content of this method is
89      * always regenerated by the Form Editor.
90      */

91     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
92
private void initComponents() {
93         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
94
95         buttonGroup1 = new javax.swing.ButtonGroup JavaDoc();
96         jPopupMenuSeries = new javax.swing.JPopupMenu JavaDoc();
97         jMenuItemCopy = new javax.swing.JMenuItem JavaDoc();
98         jMenuItemPaste = new javax.swing.JMenuItem JavaDoc();
99         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
100         jList1 = new javax.swing.JList JavaDoc();
101         jPanel1 = new javax.swing.JPanel JavaDoc();
102         jButtonAdd = new javax.swing.JButton JavaDoc();
103         jButtonModify = new javax.swing.JButton JavaDoc();
104         jButtonRemove = new javax.swing.JButton JavaDoc();
105         jPanel2 = new javax.swing.JPanel JavaDoc();
106         jLabel1 = new javax.swing.JLabel JavaDoc();
107
108         jMenuItemCopy.setText("Copy series");
109         jMenuItemCopy.addActionListener(new java.awt.event.ActionListener JavaDoc() {
110             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
111                 jMenuItemCopyActionPerformed(evt);
112             }
113         });
114
115         jPopupMenuSeries.add(jMenuItemCopy);
116
117         jMenuItemPaste.setText("Paste series");
118         jMenuItemPaste.addActionListener(new java.awt.event.ActionListener JavaDoc() {
119             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
120                 jMenuItemPasteActionPerformed(evt);
121             }
122         });
123
124         jPopupMenuSeries.add(jMenuItemPaste);
125
126         setLayout(new java.awt.GridBagLayout JavaDoc());
127
128         jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
129             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
130                 jList1ValueChanged(evt);
131             }
132         });
133         jList1.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
134             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
135                 jList1MouseClicked(evt);
136             }
137         });
138
139         jScrollPane1.setViewportView(jList1);
140
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridy = 2;
143         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
144         gridBagConstraints.weightx = 1.0;
145         gridBagConstraints.weighty = 1.0;
146         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
147         add(jScrollPane1, gridBagConstraints);
148
149         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
150
151         jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(100, 0));
152         jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(100, 0));
153         jButtonAdd.setText("Add");
154         jButtonAdd.addActionListener(new java.awt.event.ActionListener JavaDoc() {
155             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
156                 jButtonAddActionPerformed(evt);
157             }
158         });
159
160         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
161         gridBagConstraints.gridy = 0;
162         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
163         gridBagConstraints.weightx = 1.0;
164         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 0, 4);
165         jPanel1.add(jButtonAdd, gridBagConstraints);
166
167         jButtonModify.setText("Modify");
168         jButtonModify.addActionListener(new java.awt.event.ActionListener JavaDoc() {
169             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
170                 jButtonModifyActionPerformed(evt);
171             }
172         });
173
174         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
175         gridBagConstraints.gridy = 1;
176         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177         gridBagConstraints.weightx = 1.0;
178         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 4);
179         jPanel1.add(jButtonModify, gridBagConstraints);
180
181         jButtonRemove.setText("Remove");
182         jButtonRemove.addActionListener(new java.awt.event.ActionListener JavaDoc() {
183             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
184                 jButtonRemoveActionPerformed(evt);
185             }
186         });
187
188         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
189         gridBagConstraints.gridy = 2;
190         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
191         gridBagConstraints.weightx = 1.0;
192         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 4);
193         jPanel1.add(jButtonRemove, gridBagConstraints);
194
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
196         gridBagConstraints.gridy = 99;
197         gridBagConstraints.weighty = 1.0;
198         jPanel1.add(jPanel2, gridBagConstraints);
199
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridy = 2;
202         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203         add(jPanel1, gridBagConstraints);
204
205         jLabel1.setText("XY series");
206         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
207         gridBagConstraints.gridy = 1;
208         gridBagConstraints.gridwidth = 2;
209         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
210         gridBagConstraints.weightx = 1.0;
211         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 0, 4);
212         add(jLabel1, gridBagConstraints);
213
214     }// </editor-fold>//GEN-END:initComponents
215

216     private void jList1MouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jList1MouseClicked
217
if (evt.getClickCount() == 1 && evt.getButton() == evt.BUTTON3)
218         {
219             jMenuItemCopy.setEnabled(jList1.getSelectedIndex() >= 0);
220             jMenuItemPaste.setEnabled( it.businesslogic.ireport.gui.MainFrame.getMainInstance().getChartSeriesClipBoard() != null &&
221                                        it.businesslogic.ireport.gui.MainFrame.getMainInstance().getChartSeriesClipBoard().size() > 0);
222             
223             jPopupMenuSeries.show(jList1, evt.getPoint().x, evt.getPoint().y);
224         }
225         else if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1)
226         {
227             jButtonModifyActionPerformed(null);
228         }
229     }//GEN-LAST:event_jList1MouseClicked
230

231     private void jMenuItemPasteActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemPasteActionPerformed
232
Vector v = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getChartSeriesClipBoard();
233         
234         if (v != null && v.size() > 0)
235         {
236             for (int i=0; i<v.size(); ++i)
237             {
238                     XYSeries cs = (XYSeries)v.elementAt(i);
239                     cs = cs.cloneMe();
240                     getXYDataset().getXYSeries().addElement(cs);
241                     ((javax.swing.DefaultListModel JavaDoc)jList1.getModel()).addElement(cs);
242             }
243             jList1.updateUI();
244         }
245     }//GEN-LAST:event_jMenuItemPasteActionPerformed
246

247     private void jMenuItemCopyActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemCopyActionPerformed
248

249         Object JavaDoc[] values = jList1.getSelectedValues();
250         Vector copy_c = new Vector();
251         for (int i=0; i<values.length; ++i)
252         {
253                   copy_c.add( ((XYSeries)values[i]).cloneMe() );
254         }
255         it.businesslogic.ireport.gui.MainFrame.getMainInstance().setChartSeriesClipBoard(copy_c);
256     }//GEN-LAST:event_jMenuItemCopyActionPerformed
257

258     private void jButtonModifyActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonModifyActionPerformed
259

260         if (jList1.getSelectedIndex() >= 0)
261         {
262            
263                 XYSeries cs = (XYSeries)jList1.getSelectedValue();
264                 XYSeriesDialog csd = new XYSeriesDialog(it.businesslogic.ireport.gui.MainFrame.getMainInstance() ,true);
265
266                 csd.setSeriesExpression( cs.getSeriesExpression() );
267                 csd.setXValueExpression( cs.getXValueExpression() );
268                 csd.setYValueExpression( cs.getYValueExpression() );
269                 csd.setLabelExpression( cs.getLabelExpression() );
270                 csd.setSectionItemHyperlink( cs.getSectionItemHyperlink() );
271
272                 csd.setSubDataset( this.getSubDataset() );
273                 csd.setVisible(true);
274
275                 if (csd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
276                 {
277                     cs.setSeriesExpression( csd.getSeriesExpression() );
278                     cs.setXValueExpression( csd.getXValueExpression() );
279                     cs.setYValueExpression( csd.getYValueExpression() );
280                     cs.setLabelExpression( csd.getLabelExpression() );
281                     cs.setSectionItemHyperlink( csd.getSectionItemHyperlink() );
282
283                     jList1.updateUI();
284                 }
285         }
286     }//GEN-LAST:event_jButtonModifyActionPerformed
287

288     private void jButtonAddActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonAddActionPerformed
289

290             XYSeriesDialog csd = new XYSeriesDialog(it.businesslogic.ireport.gui.MainFrame.getMainInstance() ,true);
291             csd.setSubDataset( this.getSubDataset() );
292             csd.setVisible(true);
293
294             if (csd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
295             {
296                 XYSeries cs = new XYSeries();
297                 cs.setSeriesExpression( csd.getSeriesExpression() );
298                 cs.setXValueExpression( csd.getXValueExpression() );
299                 cs.setYValueExpression( csd.getYValueExpression() );
300                 cs.setLabelExpression( csd.getLabelExpression() );
301                 cs.setSectionItemHyperlink( csd.getSectionItemHyperlink() );
302
303                 xyDataset.getXYSeries().addElement(cs);
304                 ((javax.swing.DefaultListModel JavaDoc)jList1.getModel()).addElement(cs);
305                 jList1.updateUI();
306             }
307         
308     }//GEN-LAST:event_jButtonAddActionPerformed
309

310     private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
311

312         while (jList1.getSelectedIndex() >= 0)
313         {
314             xyDataset.getXYSeries().remove( jList1.getSelectedValue() );
315             ((javax.swing.DefaultListModel JavaDoc)jList1.getModel()).removeElementAt(jList1.getSelectedIndex());
316         }
317         
318     }//GEN-LAST:event_jButtonRemoveActionPerformed
319

320     private void jList1ValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jList1ValueChanged
321

322         if (jList1.getSelectedIndex() >= 0)
323         {
324             jButtonModify.setEnabled( true );
325             jButtonModify.setEnabled( true );
326         }
327         else
328         {
329             jButtonModify.setEnabled( false );
330             jButtonModify.setEnabled( false );
331         }
332     }//GEN-LAST:event_jList1ValueChanged
333

334     
335     // Variables declaration - do not modify//GEN-BEGIN:variables
336
private javax.swing.ButtonGroup JavaDoc buttonGroup1;
337     private javax.swing.JButton JavaDoc jButtonAdd;
338     private javax.swing.JButton JavaDoc jButtonModify;
339     private javax.swing.JButton JavaDoc jButtonRemove;
340     private javax.swing.JLabel JavaDoc jLabel1;
341     private javax.swing.JList JavaDoc jList1;
342     private javax.swing.JMenuItem JavaDoc jMenuItemCopy;
343     private javax.swing.JMenuItem JavaDoc jMenuItemPaste;
344     private javax.swing.JPanel JavaDoc jPanel1;
345     private javax.swing.JPanel JavaDoc jPanel2;
346     private javax.swing.JPopupMenu JavaDoc jPopupMenuSeries;
347     private javax.swing.JScrollPane JavaDoc jScrollPane1;
348     // End of variables declaration//GEN-END:variables
349

350      public void applyI18n()
351     {
352                 // Start autogenerated code ----------------------
353
jMenuItemCopy.setText(I18n.getString("xYDatasetPanel.menuItemCopy","Copy series"));
354                 jMenuItemPaste.setText(I18n.getString("xYDatasetPanel.menuItemPaste","Paste series"));
355                 // End autogenerated code ----------------------
356
// Start autogenerated code ----------------------
357
jLabel1.setText(I18n.getString("xYDatasetPanel.label1","XY series"));
358                 // End autogenerated code ----------------------
359
jButtonAdd.setText(it.businesslogic.ireport.util.I18n.getString("charts.newseries", "Add series"));
360         jButtonModify.setText(it.businesslogic.ireport.util.I18n.getString("charts.modifyseries", "Modify series"));
361         jButtonRemove.setText(it.businesslogic.ireport.util.I18n.getString("charts.removeseries", "Remove series"));
362         this.updateUI();
363         
364     }
365
366     public SubDataset getSubDataset() {
367         return subDataset;
368     }
369
370     public void setSubDataset(SubDataset subDataset) {
371         this.subDataset = subDataset;
372     }
373
374      
375 }
376
Popular Tags