KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > BandsDialog


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  * BandsDialog.java
28  *
29  * Created on 10 maggio 2003, 9.51
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34
35 import javax.swing.table.*;
36 import javax.swing.*;
37 import javax.swing.event.*;
38 import it.businesslogic.ireport.*;
39 import it.businesslogic.ireport.util.*;
40
41 import java.util.*;
42 /**
43  *
44  * @author Administrator
45  */

46 public class BandsDialog extends javax.swing.JDialog JavaDoc {
47     
48     
49     private JReportFrame jReportFrame=null;
50     
51     private Band selectedBand = null;
52     
53     /** Creates new form BandsDialog */
54     public BandsDialog(java.awt.Frame JavaDoc parent, boolean modal) {
55         super(parent, modal);
56         initComponents();
57         applyI18n();
58         this.pack();
59          Misc.centerFrame(this);
60          jListBands.setModel(new DefaultListModel());
61         /*
62          
63          this.jRTextExpressionArea.getDocument().addDocumentListener( new javax.swing.event.DocumentListener() {
64             public void changedUpdate(javax.swing.event.DocumentEvent evt)
65             {
66                 jRTextExpressionAreaTextChanged();
67             }
68             public void insertUpdate(javax.swing.event.DocumentEvent evt) {
69                 jRTextExpressionAreaTextChanged();
70             }
71             public void removeUpdate(javax.swing.event.DocumentEvent evt) {
72                 jRTextExpressionAreaTextChanged();
73             }
74         });
75          *
76          */

77          
78          
79         javax.swing.KeyStroke JavaDoc escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
80         javax.swing.Action JavaDoc escapeAction = new javax.swing.AbstractAction JavaDoc() {
81             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
82                 setVisible(false);
83             }
84         };
85        
86         getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
87         getRootPane().getActionMap().put("ESCAPE", escapeAction);
88
89
90         //to make the default button ...
91
this.getRootPane().setDefaultButton(this.jButton1);
92          
93     }
94     
95     
96     private void jRTextExpressionAreaTextChanged()
97     {
98             
99     }
100     
101     public void jTableBandsListSelectionValueChanged(javax.swing.event.ListSelectionEvent JavaDoc e)
102     {
103        
104     }
105     
106      public void updateBands() {
107     
108          Object JavaDoc selected = getSelectedBand();
109          boolean isSelected = false;
110
111         ((DefaultListModel)jListBands.getModel()).removeAllElements();
112     
113         if (jReportFrame == null) return;
114         Enumeration e = jReportFrame.getReport().getBands().elements();
115         while (e.hasMoreElements())
116         {
117             it.businesslogic.ireport.Band band = (it.businesslogic.ireport.Band)e.nextElement();
118             ((DefaultListModel)jListBands.getModel()).addElement(band);
119             if (selected == band)
120             {
121                 jListBands.setSelectedValue(selected, true);
122                 isSelected = true;
123             }
124         }
125         
126         if (!isSelected && jReportFrame.getReport().getBands().size() > 0)
127         {
128             jListBands.setSelectedIndex(0);
129         }
130     }
131     /** This method is called from within the constructor to
132      * initialize the form.
133      * WARNING: Do NOT modify this code. The content of this method is
134      * always regenerated by the Form Editor.
135      */

136     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
137
private void initComponents() {
138         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
139
140         jPanelFields = new javax.swing.JPanel JavaDoc();
141         jPanelButtons = new javax.swing.JPanel JavaDoc();
142         jPanel2 = new javax.swing.JPanel JavaDoc();
143         jPanel3 = new javax.swing.JPanel JavaDoc();
144         jLabel1 = new javax.swing.JLabel JavaDoc();
145         jLabel2 = new javax.swing.JLabel JavaDoc();
146         jNumberFieldHeight = new it.businesslogic.ireport.gui.JNumberField();
147         jCheckBoxSplitAllowed = new javax.swing.JCheckBox JavaDoc();
148         jRTextExpressionArea = new it.businesslogic.ireport.gui.JRTextExpressionArea();
149         jButton1 = new javax.swing.JButton JavaDoc();
150         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
151         jListBands = new javax.swing.JList JavaDoc();
152
153         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
154             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
155                 closeDialog(evt);
156             }
157         });
158
159         jPanelFields.setLayout(new java.awt.BorderLayout JavaDoc(4, 4));
160
161         jPanelButtons.setLayout(new java.awt.BorderLayout JavaDoc());
162
163         jPanelButtons.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Band properties"));
164         jPanelButtons.setMinimumSize(new java.awt.Dimension JavaDoc(100, 10));
165         jPanelButtons.setPreferredSize(new java.awt.Dimension JavaDoc(300, 250));
166         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
167
168         jPanel3.setLayout(new java.awt.GridBagLayout JavaDoc());
169
170         jPanel3.setPreferredSize(new java.awt.Dimension JavaDoc(10, 55));
171         jLabel1.setText("Print When Expression");
172         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
173         gridBagConstraints.gridx = 0;
174         gridBagConstraints.gridy = 2;
175         gridBagConstraints.gridwidth = 2;
176         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
177         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 0, 3);
178         jPanel3.add(jLabel1, gridBagConstraints);
179
180         jLabel2.setText("Band height");
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 0;
183         gridBagConstraints.gridy = 0;
184         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
185         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 0, 3);
186         jPanel3.add(jLabel2, gridBagConstraints);
187
188         jNumberFieldHeight.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
189         jNumberFieldHeight.setText("0");
190         try {
191             jNumberFieldHeight.setDecimals(0);
192         } catch (java.beans.PropertyVetoException JavaDoc e1) {
193             e1.printStackTrace();
194         }
195         jNumberFieldHeight.setMinimumSize(new java.awt.Dimension JavaDoc(80, 19));
196         jNumberFieldHeight.setPreferredSize(new java.awt.Dimension JavaDoc(80, 19));
197         jNumberFieldHeight.addActionListener(new java.awt.event.ActionListener JavaDoc() {
198             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
199                 jNumberFieldHeightActionPerformed(evt);
200             }
201         });
202
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 0;
205         gridBagConstraints.gridy = 1;
206         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
207         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
208         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 3, 3, 3);
209         jPanel3.add(jNumberFieldHeight, gridBagConstraints);
210
211         jCheckBoxSplitAllowed.setSelected(true);
212         jCheckBoxSplitAllowed.setText("Split allowed");
213         jCheckBoxSplitAllowed.addActionListener(new java.awt.event.ActionListener JavaDoc() {
214             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
215                 jCheckBoxSplitAllowedActionPerformed(evt);
216             }
217         });
218
219         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
220         gridBagConstraints.gridx = 1;
221         gridBagConstraints.gridy = 0;
222         gridBagConstraints.gridheight = 2;
223         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
224         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
225         jPanel3.add(jCheckBoxSplitAllowed, gridBagConstraints);
226
227         jRTextExpressionArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
228         jRTextExpressionArea.setElectricScroll(0);
229         jRTextExpressionArea.setMinimumSize(new java.awt.Dimension JavaDoc(0, 0));
230         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
231         gridBagConstraints.gridx = 0;
232         gridBagConstraints.gridy = 3;
233         gridBagConstraints.gridwidth = 2;
234         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
235         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
236         gridBagConstraints.weightx = 1.0;
237         gridBagConstraints.weighty = 1.0;
238         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
239         jPanel3.add(jRTextExpressionArea, gridBagConstraints);
240
241         jButton1.setText("Apply");
242         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
243             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
244                 jButton1ActionPerformed(evt);
245             }
246         });
247
248         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
249         gridBagConstraints.gridx = 0;
250         gridBagConstraints.gridwidth = 2;
251         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
252         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
253         jPanel3.add(jButton1, gridBagConstraints);
254
255         jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);
256
257         jPanelButtons.add(jPanel2, java.awt.BorderLayout.CENTER);
258
259         jPanelFields.add(jPanelButtons, java.awt.BorderLayout.CENTER);
260
261         getContentPane().add(jPanelFields, java.awt.BorderLayout.CENTER);
262
263         jScrollPane1.setToolTipText("");
264         jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
265         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(150, 300));
266         jListBands.setBorder(javax.swing.BorderFactory.createEtchedBorder());
267         jListBands.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
268         jListBands.setToolTipText("List of bands");
269         jListBands.setMaximumSize(new java.awt.Dimension JavaDoc(32767, 32767));
270         jListBands.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
271             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
272                 jListBandsValueChanged(evt);
273             }
274         });
275
276         jScrollPane1.setViewportView(jListBands);
277
278         getContentPane().add(jScrollPane1, java.awt.BorderLayout.WEST);
279
280         pack();
281     }// </editor-fold>//GEN-END:initComponents
282

283     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
284

285         boolean changed = false;
286         
287         
288         Band b = getSelectedBand();
289         if (b == null) return;
290         int okHeight = b.getHeight();
291     // 1. we set band height, but first we adjust elements position
292
int delta = (int)jNumberFieldHeight.getValue() - b.getHeight();
293     if (delta != 0)
294     {
295                 changed = true;
296                 Report report = getJReportFrame().getReport();
297                 
298                 // Available space...
299
int available_height = report.getHeight() - report.getTopMargin()- report.getBottomMargin();
300                 
301                 if (!b.getName().equals("background") &&
302                     !( b.getName().equals("title") && getJReportFrame().getReport().isIsTitleNewPage()) &&
303                     !( b.getName().equals("summary") && getJReportFrame().getReport().isIsSummaryNewPage()))
304                 {
305                     for (int i=0; i<report.getBands().size(); ++i)
306                     {
307                         Band aBand = (Band)report.getBands().elementAt(i);
308
309                         if (!(aBand.getName().equals("background")) &&
310                             !(aBand.getName().equals("title") && getJReportFrame().getReport().isIsTitleNewPage()) &&
311                             !(aBand.getName().equals("summary") && getJReportFrame().getReport().isIsSummaryNewPage()) &&
312                             !(aBand == b))
313                         {
314                             available_height -= aBand.getHeight();
315                         }
316                     }
317                 }
318                 
319                 okHeight = Math.max(0, Math.min(available_height, (int)jNumberFieldHeight.getValue()));
320                                 
321                 delta = okHeight - b.getHeight();
322         // Redraw all under the min height...
323
int band_h = report.getBandYLocation(b);
324         b.setHeight( b.getHeight() + delta );
325         int edge_y = Math.min(report.getBandYLocation(b)+b.getHeight(),report.getBandYLocation(b)+b.getHeight()-delta);
326         Enumeration e = report.getElements().elements();
327         while (e.hasMoreElements())
328         {
329             ReportElement re = (ReportElement)e.nextElement();
330             if (re.position.y >= edge_y+10)
331             {
332                 // I should add the distance form the base of resized band, and my element...
333
int space_before_band = band_h - re.position.y - re.height;
334                 if (band_h <= report.getBandYLocation(re.getBand()) +10)
335                 space_before_band = 0;
336                 re.setPosition(new java.awt.Point JavaDoc(re.getPosition().x, re.getPosition().y+delta));
337                         }
338         }
339                 
340                 try {
341                     jNumberFieldHeight.setValue(b.getHeight());
342                 } catch (Exception JavaDoc ex){}
343                 //System.out.println("Repaint");
344
getJReportFrame().setIsDocDirty(true);
345                 getJReportFrame().updateScrollBars();
346                 getJReportFrame().getReportPanel().repaint();
347     }
348         
349         // Setting the new jCheckBoxSplitAllowed...
350
b = getSelectedBand();
351          if (b != null)
352          {
353              b.setSplitAllowed( jCheckBoxSplitAllowed.isSelected());
354              changed = true;
355          }
356          
357          if (!b.getPrintWhenExpression().equals(jRTextExpressionArea.getText() ))
358         {
359             getSelectedBand().setPrintWhenExpression( jRTextExpressionArea.getText() );
360             changed = true;
361         }
362          
363         if (changed )
364         {
365              getJReportFrame().getReport().incrementReportChanges();
366         }
367         
368     }//GEN-LAST:event_jButton1ActionPerformed
369

370     private void jCheckBoxSplitAllowedActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBoxSplitAllowedActionPerformed
371

372        
373         
374     }//GEN-LAST:event_jCheckBoxSplitAllowedActionPerformed
375

376     private void jNumberFieldHeightActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jNumberFieldHeightActionPerformed
377

378         
379         
380                                         
381     }//GEN-LAST:event_jNumberFieldHeightActionPerformed
382

383     private void jListBandsValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jListBandsValueChanged
384
//
385

386         // This solve a bug with the not reached focus event...
387
jNumberFieldHeightActionPerformed(null);
388         
389         
390         if (jListBands.getModel().getSize() == 0)
391         {
392              try {
393                 this.jNumberFieldHeight.setValue( 0 );
394             } catch (Exception JavaDoc ex) {}
395             this.jRTextExpressionArea.setText("");
396              jCheckBoxSplitAllowed.setSelected(false);
397             return;
398         }
399         
400         if (jListBands.getSelectedIndex() < 0)
401         {
402             jListBands.setSelectedIndex(0);
403             return;
404         }
405         
406         selectedBand = (Band)jListBands.getSelectedValue();
407         try {
408             this.jNumberFieldHeight.setValue( selectedBand.getHeight());
409         } catch (Exception JavaDoc ex) {}
410         this.jRTextExpressionArea.setText( selectedBand.getPrintWhenExpression());
411         this.jCheckBoxSplitAllowed.setSelected( selectedBand.isSplitAllowed() );
412     }//GEN-LAST:event_jListBandsValueChanged
413

414     /** Closes the dialog */
415     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
416
setVisible(false);
417         dispose();
418     }//GEN-LAST:event_closeDialog
419

420     /**
421      * @param args the command line arguments
422      */

423     public static void main(String JavaDoc args[]) {
424         new BandsDialog(new javax.swing.JFrame JavaDoc(), true).setVisible(true);
425     }
426     
427     /** Getter for property jReportFrame.
428      * @return Value of property jReportFrame.
429      *
430      */

431     public it.businesslogic.ireport.gui.JReportFrame getJReportFrame() {
432         return jReportFrame;
433     }
434     
435     /** Setter for property jReportFrame.
436      * @param jReportFrame New value of property jReportFrame.
437      *
438      */

439     public void setJReportFrame(it.businesslogic.ireport.gui.JReportFrame jReportFrame) {
440         this.jReportFrame = jReportFrame;
441         
442         // Update all...
443
if (jReportFrame == null)
444         {
445             setVisible(false);
446             return;
447         }
448         this.setTitle(jReportFrame.getReport().getName()+" bands...");
449         if (isVisible())
450         {
451             updateBands();
452         }
453     }
454     
455     // Variables declaration - do not modify//GEN-BEGIN:variables
456
private javax.swing.JButton JavaDoc jButton1;
457     private javax.swing.JCheckBox JavaDoc jCheckBoxSplitAllowed;
458     private javax.swing.JLabel JavaDoc jLabel1;
459     private javax.swing.JLabel JavaDoc jLabel2;
460     private javax.swing.JList JavaDoc jListBands;
461     private it.businesslogic.ireport.gui.JNumberField jNumberFieldHeight;
462     private javax.swing.JPanel JavaDoc jPanel2;
463     private javax.swing.JPanel JavaDoc jPanel3;
464     private javax.swing.JPanel JavaDoc jPanelButtons;
465     private javax.swing.JPanel JavaDoc jPanelFields;
466     private it.businesslogic.ireport.gui.JRTextExpressionArea jRTextExpressionArea;
467     private javax.swing.JScrollPane JavaDoc jScrollPane1;
468     // End of variables declaration//GEN-END:variables
469

470     public void setVisible(boolean visible)
471     {
472         if (visible == isVisible()) return;
473         super.setVisible(visible);
474         if (visible == true)
475         {
476             this.setJReportFrame(jReportFrame);
477         }
478     }
479     
480     public void setSelectedBand(String JavaDoc bandName )
481     {
482         for (int i=0; i<jListBands.getModel().getSize(); ++i)
483         {
484             Band b = (Band)jListBands.getModel().getElementAt( i );
485             if (b.getName().equals(bandName))
486             {
487                 jListBands.setSelectedIndex( i );
488                 return;
489             }
490         }
491     }
492
493     public Band getSelectedBand() {
494         return selectedBand;
495     }
496
497     public void setSelectedBand(Band selectedBand) {
498         this.selectedBand = selectedBand;
499     }
500     public void applyI18n(){
501                 // Start autogenerated code ----------------------
502
jCheckBoxSplitAllowed.setText(I18n.getString("bandsDialog.checkBoxSplitAllowed","Split allowed"));
503                 // End autogenerated code ----------------------
504
// Start autogenerated code ----------------------
505
jButton1.setText(I18n.getString("bandsDialog.button1","Apply"));
506                 jLabel1.setText(I18n.getString("bandsDialog.label1","Print When Expression"));
507                 jLabel2.setText(I18n.getString("bandsDialog.label2","Band height"));
508                 // End autogenerated code ----------------------
509

510                 ((javax.swing.border.TitledBorder JavaDoc)jPanelButtons.getBorder()).setTitle( I18n.getString("bandsDialog.panelBorder.BandProperties","Band properties") );
511     }
512 }
513
Popular Tags