KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > library > objects > TotalObjectDialog


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  * TotalObjectDialog.java
28  *
29  * Created on 21 settembre 2004, 20.34
30  *
31  */

32
33 package it.businesslogic.ireport.gui.library.objects;
34 import it.businesslogic.ireport.gui.library.CustomExpression;
35 import it.businesslogic.ireport.gui.*;
36 import it.businesslogic.ireport.*;
37 import javax.swing.tree.*;
38 import it.businesslogic.ireport.util.I18n;
39
40 /**
41  *
42  * @author Administrator
43  */

44 public class TotalObjectDialog extends javax.swing.JDialog JavaDoc {
45     
46     int dialogResult = 0;
47     DefaultMutableTreeNode fieldsNode = null;
48     DefaultMutableTreeNode variablesNode = null;
49     DefaultMutableTreeNode parametersNode = null;
50     
51     private boolean onlyFields = false;
52     
53     private JReportFrame jrf = null;
54     private Object JavaDoc selectedObject = null;
55     /** Creates new form TotalObjectDialog */
56     public TotalObjectDialog(java.awt.Frame JavaDoc parent, boolean modal) {
57         super(parent, modal);
58         initComponents();
59         
60         this.setSize(400, 450);
61       
62         this.setModal(true);
63         
64         
65         org.syntax.jedit.SyntaxDocument sd = new org.syntax.jedit.SyntaxDocument();
66         sd.setTokenMarker(new org.syntax.jedit.tokenmarker.JavaTokenMarker() );
67       
68         this.jRTextExpressionAreaDefaultExpression.setDocument( sd );
69       
70         jTree1.setCellRenderer( new it.businesslogic.ireport.gui.library.LibraryTreeCellRenderer());
71         updateAllTree();
72
73         this.dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
74         
75         it.businesslogic.ireport.util.Misc.centerFrame(this);
76         
77         javax.swing.KeyStroke JavaDoc escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
78         javax.swing.Action JavaDoc escapeAction = new javax.swing.AbstractAction JavaDoc() {
79             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
80                 jButtonCancelActionPerformed(e);
81             }
82         };
83        
84         getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
85         getRootPane().getActionMap().put("ESCAPE", escapeAction);
86
87         applyI18n();
88         //to make the default button ...
89
this.getRootPane().setDefaultButton(this.jButtonOK);
90     }
91     
92     
93     public void updateAllTree()
94     {
95                 DefaultMutableTreeNode root = new DefaultMutableTreeNode("Libs");
96         
97         DefaultTreeModel dtm = new DefaultTreeModel(root);
98         
99         jTree1.setModel( dtm );
100         jTree1.setDragEnabled(true);
101         jTree1.setRootVisible( false );
102         
103         jTree1.expandPath( new TreePath(root));
104
105         fieldsNode = new DefaultMutableTreeNode( it.businesslogic.ireport.util.I18n.getString("gui.library.fields","Fields"),true);
106         root.add( fieldsNode );
107         
108         if (!isOnlyFields())
109         {
110             variablesNode = new DefaultMutableTreeNode( it.businesslogic.ireport.util.I18n.getString("gui.library.variables","Variables"),true);
111             root.add( variablesNode );
112         
113             parametersNode = new DefaultMutableTreeNode( it.businesslogic.ireport.util.I18n.getString("gui.library.parameters","Parameters"),true);
114             root.add( parametersNode );
115             root.add( new DefaultMutableTreeNode( new it.businesslogic.ireport.gui.library.CustomExpression( it.businesslogic.ireport.util.I18n.getString("gui.library.customexpression","Custom expression")), true));
116         }
117         jLabelTitle.setText( it.businesslogic.ireport.util.I18n.getString("gui.library.totalobject.title","Select object to sum") );
118         
119     }
120     
121     
122     
123     /** This method is called from within the constructor to
124      * initialize the form.
125      * WARNING: Do NOT modify this code. The content of this method is
126      * always regenerated by the Form Editor.
127      */

128     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
129
private void initComponents() {
130         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
131
132         jLabelTitle = new javax.swing.JLabel JavaDoc();
133         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
134         jTree1 = new javax.swing.JTree JavaDoc();
135         jRTextExpressionAreaDefaultExpression = new it.businesslogic.ireport.gui.JRTextExpressionArea();
136         jPanel1 = new javax.swing.JPanel JavaDoc();
137         jPanel2 = new javax.swing.JPanel JavaDoc();
138         jButtonOK = new javax.swing.JButton JavaDoc();
139         jButtonCancel = new javax.swing.JButton JavaDoc();
140
141         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
142
143         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
144         jLabelTitle.setText("Select object to sum");
145         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
146         gridBagConstraints.gridx = 0;
147         gridBagConstraints.gridy = 0;
148         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
149         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
150         gridBagConstraints.weightx = 1.0;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 0, 4);
152         getContentPane().add(jLabelTitle, gridBagConstraints);
153
154         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(81, 180));
155         jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc() {
156             public void valueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {
157                 jTree1ValueChanged(evt);
158             }
159         });
160
161         jScrollPane1.setViewportView(jTree1);
162
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridx = 0;
165         gridBagConstraints.gridy = 1;
166         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
167         gridBagConstraints.weightx = 1.0;
168         gridBagConstraints.weighty = 1.0;
169         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 0, 4);
170         getContentPane().add(jScrollPane1, gridBagConstraints);
171
172         jRTextExpressionAreaDefaultExpression.setBorder(javax.swing.BorderFactory.createEtchedBorder());
173         jRTextExpressionAreaDefaultExpression.setElectricScroll(0);
174         jRTextExpressionAreaDefaultExpression.setMinimumSize(new java.awt.Dimension JavaDoc(0, 60));
175         jRTextExpressionAreaDefaultExpression.setPreferredSize(new java.awt.Dimension JavaDoc(310, 60));
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridx = 0;
178         gridBagConstraints.gridy = 2;
179         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
180         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
181         gridBagConstraints.weighty = 0.2;
182         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
183         getContentPane().add(jRTextExpressionAreaDefaultExpression, gridBagConstraints);
184
185         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
186
187         jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(10, 30));
188         jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(10, 30));
189         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190         gridBagConstraints.gridx = 0;
191         gridBagConstraints.gridy = 0;
192         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
193         gridBagConstraints.weightx = 1.0;
194         jPanel1.add(jPanel2, gridBagConstraints);
195
196         jButtonOK.setText("OK");
197         jButtonOK.addActionListener(new java.awt.event.ActionListener JavaDoc() {
198             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
199                 jButtonOKActionPerformed(evt);
200             }
201         });
202
203         jPanel1.add(jButtonOK, new java.awt.GridBagConstraints JavaDoc());
204
205         jButtonCancel.setText("Cancel");
206         jButtonCancel.addActionListener(new java.awt.event.ActionListener JavaDoc() {
207             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
208                 jButtonCancelActionPerformed(evt);
209             }
210         });
211
212         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 0);
214         jPanel1.add(jButtonCancel, gridBagConstraints);
215
216         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
217         gridBagConstraints.gridx = 0;
218         gridBagConstraints.gridy = 3;
219         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
220         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 4, 4);
221         getContentPane().add(jPanel1, gridBagConstraints);
222
223         pack();
224     }// </editor-fold>//GEN-END:initComponents
225

226     private void jButtonCancelActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
227
this.dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
228         this.setVisible(false);
229         dispose(); // TODO add your handling code here:
230
}//GEN-LAST:event_jButtonCancelActionPerformed
231

232     private void jButtonOKActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonOKActionPerformed
233

234         if (selectedObject == null)
235         {
236             javax.swing.JOptionPane.showMessageDialog(this,it.businesslogic.ireport.util.I18n.getString("gui.library.totalobject.selectanobject","Select an object to sum first"));
237             return;
238         }
239         
240         this.dialogResult = javax.swing.JOptionPane.OK_OPTION;
241         this.setVisible(false);
242         dispose();
243         
244         
245     }//GEN-LAST:event_jButtonOKActionPerformed
246

247     private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jTree1ValueChanged
248
// TODO add your handling code here:
249

250         selectedObject = null;
251         
252         if (jTree1.getLastSelectedPathComponent() != null)
253         {
254             DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
255             Object JavaDoc obj = dmtn.getUserObject();
256             if (!dmtn.isLeaf())
257             {
258                 return;
259             }
260             
261             if (obj instanceof CustomExpression)
262             {
263                 //jRTextExpressionAreaDefaultExpression.setText( obj+"");
264
jRTextExpressionAreaDefaultExpression.setEditable( true );
265                 jRTextExpressionAreaDefaultExpression.setBackground(java.awt.Color.WHITE);
266                 jRTextExpressionAreaDefaultExpression.setOpaque(true);
267             }
268             else
269             {
270                 if (obj instanceof JRParameter) jRTextExpressionAreaDefaultExpression.setText("$P{" + obj + "}");
271                 if (obj instanceof JRVariable) jRTextExpressionAreaDefaultExpression.setText("$V{" + obj + "}");
272                 if (obj instanceof JRField) jRTextExpressionAreaDefaultExpression.setText("$F{" + obj + "}");
273                 jRTextExpressionAreaDefaultExpression.setEditable(false);
274                 
275                 jRTextExpressionAreaDefaultExpression.setBackground(java.awt.Color.GRAY);
276                 jRTextExpressionAreaDefaultExpression.setOpaque(true);
277             }
278             selectedObject = obj;
279             
280         }
281         
282     }//GEN-LAST:event_jTree1ValueChanged
283

284     /**
285      * @param args the command line arguments
286      */

287     public static void main(String JavaDoc args[]) {
288         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
289             public void run() {
290                 new TotalObjectDialog(new javax.swing.JFrame JavaDoc(), true).setVisible(true);
291             }
292         });
293     }
294
295     /**
296      * Getter for property jrf.
297      * @return Value of property jrf.
298      */

299     public JReportFrame getJrf() {
300
301         return this.jrf;
302     }
303
304     /**
305      * Setter for property jrf.
306      * @param jrf New value of property jrf.
307      */

308     public void setJrf(JReportFrame jrf) {
309
310         this.jrf = jrf;
311         jRTextExpressionAreaDefaultExpression.getTokenMarker().setKeywordLookup(
312         jrf.getReport().getKeywordLookup());
313         
314         this.fieldsNode.removeAllChildren();
315        this.parametersNode.removeAllChildren();
316        this.variablesNode.removeAllChildren();
317        
318        
319        if (jrf == null) {
320            
321            jTree1.updateUI();
322            return;
323        }
324        
325        java.util.Enumeration JavaDoc e = jrf.getReport().getFields().elements();
326        while (e.hasMoreElements())
327        {
328              fieldsNode.add(new DefaultMutableTreeNode(e.nextElement()));
329        }
330        
331        if (!isOnlyFields())
332        {
333             e = jrf.getReport().getParameters().elements();
334            while (e.hasMoreElements())
335            {
336                  parametersNode.add(new DefaultMutableTreeNode(e.nextElement()));
337            }
338
339             e = jrf.getReport().getVariables().elements();
340            while (e.hasMoreElements())
341            {
342                  variablesNode.add(new DefaultMutableTreeNode(e.nextElement()));
343            }
344        }
345         
346        jTree1.updateUI();
347     }
348
349     /**
350      * Getter for property dialogResult.
351      * @return Value of property dialogResult.
352      */

353     public int getDialogResult() {
354
355         return this.dialogResult;
356     }
357
358     /**
359      * Setter for property dialogResult.
360      * @param dialogResult New value of property dialogResult.
361      */

362     public void setDialogResult(int dialogResult) {
363
364         this.dialogResult = dialogResult;
365     }
366
367     /**
368      * Getter for property selectedObject.
369      * @return Value of property selectedObject.
370      */

371     public Object JavaDoc getSelectedObject() {
372
373         return this.selectedObject;
374     }
375
376     /**
377      * Setter for property selectedObject.
378      * @param selectedObject New value of property selectedObject.
379      */

380     public void setSelectedObject(Object JavaDoc selectedObject) {
381
382         this.selectedObject = selectedObject;
383     }
384
385     public boolean isOnlyFields() {
386         return onlyFields;
387     }
388
389     public void setOnlyFields(boolean onlyFields) {
390         this.onlyFields = onlyFields;
391         updateAllTree();
392     }
393     
394     // Variables declaration - do not modify//GEN-BEGIN:variables
395
private javax.swing.JButton JavaDoc jButtonCancel;
396     private javax.swing.JButton JavaDoc jButtonOK;
397     private javax.swing.JLabel JavaDoc jLabelTitle;
398     private javax.swing.JPanel JavaDoc jPanel1;
399     private javax.swing.JPanel JavaDoc jPanel2;
400     private it.businesslogic.ireport.gui.JRTextExpressionArea jRTextExpressionAreaDefaultExpression;
401     private javax.swing.JScrollPane JavaDoc jScrollPane1;
402     private javax.swing.JTree JavaDoc jTree1;
403     // End of variables declaration//GEN-END:variables
404

405     public void applyI18n(){
406                 // Start autogenerated code ----------------------
407
jButtonCancel.setText(I18n.getString("totalObjectDialog.buttonCancel","Cancel"));
408                 jButtonOK.setText(I18n.getString("totalObjectDialog.buttonOK","OK"));
409                 // End autogenerated code ----------------------
410
}
411 }
412
Popular Tags