001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * TotalObjectDialog.java
028: *
029: * Created on 21 settembre 2004, 20.34
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.library.objects;
034:
035: import it.businesslogic.ireport.gui.library.CustomExpression;
036: import it.businesslogic.ireport.gui.*;
037: import it.businesslogic.ireport.*;
038: import javax.swing.tree.*;
039: import it.businesslogic.ireport.util.I18n;
040:
041: /**
042: *
043: * @author Administrator
044: */
045: public class TotalObjectDialog extends javax.swing.JDialog {
046:
047: int dialogResult = 0;
048: DefaultMutableTreeNode fieldsNode = null;
049: DefaultMutableTreeNode variablesNode = null;
050: DefaultMutableTreeNode parametersNode = null;
051:
052: private boolean onlyFields = false;
053:
054: private JReportFrame jrf = null;
055: private Object selectedObject = null;
056:
057: /** Creates new form TotalObjectDialog */
058: public TotalObjectDialog(java.awt.Frame parent, boolean modal) {
059: super (parent, modal);
060: initComponents();
061:
062: this .setSize(400, 450);
063:
064: this .setModal(true);
065:
066: org.syntax.jedit.SyntaxDocument sd = new org.syntax.jedit.SyntaxDocument();
067: sd
068: .setTokenMarker(new org.syntax.jedit.tokenmarker.JavaTokenMarker());
069:
070: this .jRTextExpressionAreaDefaultExpression.setDocument(sd);
071:
072: jTree1
073: .setCellRenderer(new DocumentExpressionEditorTreeCellRenderer());
074: updateAllTree();
075:
076: this .dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
077:
078: it.businesslogic.ireport.util.Misc.centerFrame(this );
079:
080: javax.swing.KeyStroke escape = javax.swing.KeyStroke
081: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
082: false);
083: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
084: public void actionPerformed(java.awt.event.ActionEvent e) {
085: jButtonCancelActionPerformed(e);
086: }
087: };
088:
089: getRootPane().getInputMap(
090: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
091: escape, "ESCAPE");
092: getRootPane().getActionMap().put("ESCAPE", escapeAction);
093:
094: applyI18n();
095: //to make the default button ...
096: this .getRootPane().setDefaultButton(this .jButtonOK);
097: }
098:
099: public void updateAllTree() {
100: DefaultMutableTreeNode root = new DefaultMutableTreeNode("Libs");
101:
102: DefaultTreeModel dtm = new DefaultTreeModel(root);
103:
104: jTree1.setModel(dtm);
105: jTree1.setDragEnabled(true);
106: jTree1.setRootVisible(false);
107:
108: jTree1.expandPath(new TreePath(root));
109:
110: fieldsNode = new DefaultMutableTreeNode(
111: it.businesslogic.ireport.util.I18n.getString(
112: "gui.library.fields", "Fields"), true);
113: root.add(fieldsNode);
114:
115: if (!isOnlyFields()) {
116: variablesNode = new DefaultMutableTreeNode(
117: it.businesslogic.ireport.util.I18n.getString(
118: "gui.library.variables", "Variables"), true);
119: root.add(variablesNode);
120:
121: parametersNode = new DefaultMutableTreeNode(
122: it.businesslogic.ireport.util.I18n.getString(
123: "gui.library.parameters", "Parameters"),
124: true);
125: root.add(parametersNode);
126: root
127: .add(new DefaultMutableTreeNode(
128: new it.businesslogic.ireport.gui.library.CustomExpression(
129: it.businesslogic.ireport.util.I18n
130: .getString(
131: "gui.library.customexpression",
132: "Custom expression")),
133: true));
134: }
135: jLabelTitle.setText(it.businesslogic.ireport.util.I18n
136: .getString("gui.library.totalobject.title",
137: "Select object to sum"));
138:
139: }
140:
141: /** This method is called from within the constructor to
142: * initialize the form.
143: * WARNING: Do NOT modify this code. The content of this method is
144: * always regenerated by the Form Editor.
145: */
146: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
147: private void initComponents() {
148: java.awt.GridBagConstraints gridBagConstraints;
149:
150: jLabelTitle = new javax.swing.JLabel();
151: jScrollPane1 = new javax.swing.JScrollPane();
152: jTree1 = new javax.swing.JTree();
153: jRTextExpressionAreaDefaultExpression = new it.businesslogic.ireport.gui.JRTextExpressionArea();
154: jPanel1 = new javax.swing.JPanel();
155: jPanel2 = new javax.swing.JPanel();
156: jButtonOK = new javax.swing.JButton();
157: jButtonCancel = new javax.swing.JButton();
158:
159: getContentPane().setLayout(new java.awt.GridBagLayout());
160:
161: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
162: jLabelTitle.setText("Select object to sum");
163: gridBagConstraints = new java.awt.GridBagConstraints();
164: gridBagConstraints.gridx = 0;
165: gridBagConstraints.gridy = 0;
166: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
167: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
168: gridBagConstraints.weightx = 1.0;
169: gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
170: getContentPane().add(jLabelTitle, gridBagConstraints);
171:
172: jScrollPane1.setPreferredSize(new java.awt.Dimension(81, 180));
173: jTree1
174: .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
175: public void valueChanged(
176: javax.swing.event.TreeSelectionEvent evt) {
177: jTree1ValueChanged(evt);
178: }
179: });
180:
181: jScrollPane1.setViewportView(jTree1);
182:
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridx = 0;
185: gridBagConstraints.gridy = 1;
186: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187: gridBagConstraints.weightx = 1.0;
188: gridBagConstraints.weighty = 1.0;
189: gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
190: getContentPane().add(jScrollPane1, gridBagConstraints);
191:
192: jRTextExpressionAreaDefaultExpression
193: .setBorder(javax.swing.BorderFactory
194: .createEtchedBorder());
195: jRTextExpressionAreaDefaultExpression.setElectricScroll(0);
196: jRTextExpressionAreaDefaultExpression
197: .setMinimumSize(new java.awt.Dimension(0, 60));
198: jRTextExpressionAreaDefaultExpression
199: .setPreferredSize(new java.awt.Dimension(310, 60));
200: gridBagConstraints = new java.awt.GridBagConstraints();
201: gridBagConstraints.gridx = 0;
202: gridBagConstraints.gridy = 2;
203: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
204: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
205: gridBagConstraints.weighty = 0.2;
206: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
207: getContentPane().add(jRTextExpressionAreaDefaultExpression,
208: gridBagConstraints);
209:
210: jPanel1.setLayout(new java.awt.GridBagLayout());
211:
212: jPanel1.setMinimumSize(new java.awt.Dimension(10, 30));
213: jPanel1.setPreferredSize(new java.awt.Dimension(10, 30));
214: gridBagConstraints = new java.awt.GridBagConstraints();
215: gridBagConstraints.gridx = 0;
216: gridBagConstraints.gridy = 0;
217: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
218: gridBagConstraints.weightx = 1.0;
219: jPanel1.add(jPanel2, gridBagConstraints);
220:
221: jButtonOK.setText("OK");
222: jButtonOK
223: .addActionListener(new java.awt.event.ActionListener() {
224: public void actionPerformed(
225: java.awt.event.ActionEvent evt) {
226: jButtonOKActionPerformed(evt);
227: }
228: });
229:
230: jPanel1.add(jButtonOK, new java.awt.GridBagConstraints());
231:
232: jButtonCancel.setText("Cancel");
233: jButtonCancel
234: .addActionListener(new java.awt.event.ActionListener() {
235: public void actionPerformed(
236: java.awt.event.ActionEvent evt) {
237: jButtonCancelActionPerformed(evt);
238: }
239: });
240:
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
243: jPanel1.add(jButtonCancel, gridBagConstraints);
244:
245: gridBagConstraints = new java.awt.GridBagConstraints();
246: gridBagConstraints.gridx = 0;
247: gridBagConstraints.gridy = 3;
248: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
249: gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
250: getContentPane().add(jPanel1, gridBagConstraints);
251:
252: pack();
253: }// </editor-fold>//GEN-END:initComponents
254:
255: private void jButtonCancelActionPerformed(
256: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
257: this .dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
258: this .setVisible(false);
259: dispose(); // TODO add your handling code here:
260: }//GEN-LAST:event_jButtonCancelActionPerformed
261:
262: private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
263:
264: if (selectedObject == null) {
265: javax.swing.JOptionPane.showMessageDialog(this ,
266: it.businesslogic.ireport.util.I18n.getString(
267: "gui.library.totalobject.selectanobject",
268: "Select an object to sum first"));
269: return;
270: }
271:
272: this .dialogResult = javax.swing.JOptionPane.OK_OPTION;
273: this .setVisible(false);
274: dispose();
275:
276: }//GEN-LAST:event_jButtonOKActionPerformed
277:
278: private void jTree1ValueChanged(
279: javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
280: // TODO add your handling code here:
281:
282: selectedObject = null;
283:
284: if (jTree1.getLastSelectedPathComponent() != null) {
285: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) jTree1
286: .getLastSelectedPathComponent();
287: Object obj = dmtn.getUserObject();
288: if (!dmtn.isLeaf()) {
289: return;
290: }
291:
292: if (obj instanceof CustomExpression) {
293: //jRTextExpressionAreaDefaultExpression.setText( obj+"");
294: jRTextExpressionAreaDefaultExpression.setEditable(true);
295: jRTextExpressionAreaDefaultExpression
296: .setBackground(java.awt.Color.WHITE);
297: jRTextExpressionAreaDefaultExpression.setOpaque(true);
298: } else {
299: if (obj instanceof JRParameter)
300: jRTextExpressionAreaDefaultExpression.setText("$P{"
301: + obj + "}");
302: if (obj instanceof JRVariable)
303: jRTextExpressionAreaDefaultExpression.setText("$V{"
304: + obj + "}");
305: if (obj instanceof JRField)
306: jRTextExpressionAreaDefaultExpression.setText("$F{"
307: + obj + "}");
308: jRTextExpressionAreaDefaultExpression
309: .setEditable(false);
310:
311: jRTextExpressionAreaDefaultExpression
312: .setBackground(java.awt.Color.GRAY);
313: jRTextExpressionAreaDefaultExpression.setOpaque(true);
314: }
315: selectedObject = obj;
316:
317: }
318:
319: }//GEN-LAST:event_jTree1ValueChanged
320:
321: /**
322: * @param args the command line arguments
323: */
324: public static void main(String args[]) {
325: java.awt.EventQueue.invokeLater(new Runnable() {
326: public void run() {
327: new TotalObjectDialog(new javax.swing.JFrame(), true)
328: .setVisible(true);
329: }
330: });
331: }
332:
333: /**
334: * Getter for property jrf.
335: * @return Value of property jrf.
336: */
337: public JReportFrame getJrf() {
338:
339: return this .jrf;
340: }
341:
342: /**
343: * Setter for property jrf.
344: * @param jrf New value of property jrf.
345: */
346: public void setJrf(JReportFrame jrf) {
347:
348: this .jrf = jrf;
349: jRTextExpressionAreaDefaultExpression.getTokenMarker()
350: .setKeywordLookup(jrf.getReport().getKeywordLookup());
351:
352: this .fieldsNode.removeAllChildren();
353: this .parametersNode.removeAllChildren();
354: this .variablesNode.removeAllChildren();
355:
356: if (jrf == null) {
357:
358: jTree1.updateUI();
359: return;
360: }
361:
362: java.util.Enumeration e = jrf.getReport().getFields()
363: .elements();
364: while (e.hasMoreElements()) {
365: fieldsNode.add(new DefaultMutableTreeNode(e.nextElement()));
366: }
367:
368: if (!isOnlyFields()) {
369: e = jrf.getReport().getParameters().elements();
370: while (e.hasMoreElements()) {
371: parametersNode.add(new DefaultMutableTreeNode(e
372: .nextElement()));
373: }
374:
375: e = jrf.getReport().getVariables().elements();
376: while (e.hasMoreElements()) {
377: variablesNode.add(new DefaultMutableTreeNode(e
378: .nextElement()));
379: }
380: }
381:
382: jTree1.updateUI();
383: }
384:
385: /**
386: * Getter for property dialogResult.
387: * @return Value of property dialogResult.
388: */
389: public int getDialogResult() {
390:
391: return this .dialogResult;
392: }
393:
394: /**
395: * Setter for property dialogResult.
396: * @param dialogResult New value of property dialogResult.
397: */
398: public void setDialogResult(int dialogResult) {
399:
400: this .dialogResult = dialogResult;
401: }
402:
403: /**
404: * Getter for property selectedObject.
405: * @return Value of property selectedObject.
406: */
407: public Object getSelectedObject() {
408:
409: return this .selectedObject;
410: }
411:
412: /**
413: * Setter for property selectedObject.
414: * @param selectedObject New value of property selectedObject.
415: */
416: public void setSelectedObject(Object selectedObject) {
417:
418: this .selectedObject = selectedObject;
419: }
420:
421: public boolean isOnlyFields() {
422: return onlyFields;
423: }
424:
425: public void setOnlyFields(boolean onlyFields) {
426: this .onlyFields = onlyFields;
427: updateAllTree();
428: }
429:
430: // Variables declaration - do not modify//GEN-BEGIN:variables
431: private javax.swing.JButton jButtonCancel;
432: private javax.swing.JButton jButtonOK;
433: private javax.swing.JLabel jLabelTitle;
434: private javax.swing.JPanel jPanel1;
435: private javax.swing.JPanel jPanel2;
436: private it.businesslogic.ireport.gui.JRTextExpressionArea jRTextExpressionAreaDefaultExpression;
437: private javax.swing.JScrollPane jScrollPane1;
438: private javax.swing.JTree jTree1;
439:
440: // End of variables declaration//GEN-END:variables
441:
442: public void applyI18n() {
443: // Start autogenerated code ----------------------
444: jButtonCancel.setText(I18n.getString(
445: "totalObjectDialog.buttonCancel", "Cancel"));
446: jButtonOK.setText(I18n.getString("totalObjectDialog.buttonOK",
447: "OK"));
448: // End autogenerated code ----------------------
449: jButtonCancel.setMnemonic(I18n.getString(
450: "totalObjectDialog.buttonCancelMnemonic", "c")
451: .charAt(0));
452: jButtonOK.setMnemonic(I18n.getString(
453: "totalObjectDialog.buttonOKMnemonic", "o").charAt(0));
454: }
455: }
|