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: * TextWizardForm.java
028: *
029: * Created on 30 luglio 2004, 16.34
030: *
031: */
032:
033: package it.businesslogic.ireport.plugin.textwizard;
034:
035: import it.businesslogic.ireport.connection.*;
036: import it.businesslogic.ireport.*;
037: import it.businesslogic.ireport.undo.*;
038: import it.businesslogic.ireport.gui.event.*;
039: import java.util.*;
040: import javax.swing.*;
041: import javax.swing.table.*;
042: import java.sql.*;
043: import it.businesslogic.ireport.util.I18n;
044:
045: /**
046: *
047: * @author Administrator
048: */
049: public class TextWizardForm extends javax.swing.JDialog {
050:
051: /** Creates new form TextWizardForm */
052: public TextWizardForm(java.awt.Frame parent, boolean modal) {
053: super (parent, modal);
054: initComponents();
055: applyI18n();
056:
057: javax.swing.SpinnerNumberModel smodel = new javax.swing.SpinnerNumberModel();
058: smodel.setMinimum(new Integer(0));
059: smodel.setMaximum(new Integer(1000000));
060: jSpinner1.setModel(smodel);
061: }
062:
063: /** This method is called from within the constructor to
064: * initialize the form.
065: * WARNING: Do NOT modify this code. The content of this method is
066: * always regenerated by the Form Editor.
067: */
068: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
069: private void initComponents() {
070: java.awt.GridBagConstraints gridBagConstraints;
071:
072: jPanel4 = new javax.swing.JPanel();
073: jPanel1 = new javax.swing.JPanel();
074: jLabel1 = new javax.swing.JLabel();
075: jSpinner1 = new javax.swing.JSpinner();
076: jPanel2 = new javax.swing.JPanel();
077: jSeparator1 = new javax.swing.JSeparator();
078: jPanel3 = new javax.swing.JPanel();
079: jButton1 = new javax.swing.JButton();
080: jButton2 = new javax.swing.JButton();
081: jPanel5 = new javax.swing.JPanel();
082: jScrollPane1 = new javax.swing.JScrollPane();
083: jTable1 = new javax.swing.JTable();
084:
085: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
086: jPanel4.setLayout(new java.awt.BorderLayout());
087:
088: jPanel1.setLayout(new java.awt.GridBagLayout());
089:
090: jPanel1.setPreferredSize(new java.awt.Dimension(10, 70));
091: jLabel1.setText("Max report width (chars): ");
092: gridBagConstraints = new java.awt.GridBagConstraints();
093: gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
094: jPanel1.add(jLabel1, gridBagConstraints);
095:
096: jSpinner1.setPreferredSize(new java.awt.Dimension(100, 20));
097: gridBagConstraints = new java.awt.GridBagConstraints();
098: gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
099: jPanel1.add(jSpinner1, gridBagConstraints);
100:
101: gridBagConstraints = new java.awt.GridBagConstraints();
102: gridBagConstraints.weightx = 1.0;
103: jPanel1.add(jPanel2, gridBagConstraints);
104:
105: gridBagConstraints = new java.awt.GridBagConstraints();
106: gridBagConstraints.gridx = 0;
107: gridBagConstraints.gridy = 1;
108: gridBagConstraints.gridwidth = 3;
109: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
110: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
111: jPanel1.add(jSeparator1, gridBagConstraints);
112:
113: jPanel3.setLayout(new java.awt.GridBagLayout());
114:
115: jButton1.setText("Check for fields widths");
116: jButton1.addActionListener(new java.awt.event.ActionListener() {
117: public void actionPerformed(java.awt.event.ActionEvent evt) {
118: jButton1ActionPerformed(evt);
119: }
120: });
121:
122: gridBagConstraints = new java.awt.GridBagConstraints();
123: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
124: jPanel3.add(jButton1, gridBagConstraints);
125:
126: jButton2.setText("Add elements");
127: jButton2.addActionListener(new java.awt.event.ActionListener() {
128: public void actionPerformed(java.awt.event.ActionEvent evt) {
129: jButton2ActionPerformed(evt);
130: }
131: });
132:
133: gridBagConstraints = new java.awt.GridBagConstraints();
134: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
135: jPanel3.add(jButton2, gridBagConstraints);
136:
137: gridBagConstraints = new java.awt.GridBagConstraints();
138: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
139: gridBagConstraints.weightx = 1.0;
140: jPanel3.add(jPanel5, gridBagConstraints);
141:
142: gridBagConstraints = new java.awt.GridBagConstraints();
143: gridBagConstraints.gridx = 0;
144: gridBagConstraints.gridy = 2;
145: gridBagConstraints.gridwidth = 3;
146: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
147: gridBagConstraints.weightx = 1.0;
148: gridBagConstraints.weighty = 1.0;
149: jPanel1.add(jPanel3, gridBagConstraints);
150:
151: jPanel4.add(jPanel1, java.awt.BorderLayout.NORTH);
152:
153: jTable1
154: .setModel(new javax.swing.table.DefaultTableModel(
155: new Object[][] {
156:
157: }, new String[] { "Column", "Display size",
158: "Cut to..." }) {
159: Class[] types = new Class[] {
160: java.lang.Object.class,
161: java.lang.Integer.class,
162: java.lang.Integer.class };
163: boolean[] canEdit = new boolean[] { false, false,
164: true };
165:
166: public Class getColumnClass(int columnIndex) {
167: return types[columnIndex];
168: }
169:
170: public boolean isCellEditable(int rowIndex,
171: int columnIndex) {
172: return canEdit[columnIndex];
173: }
174: });
175: jScrollPane1.setViewportView(jTable1);
176:
177: jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);
178:
179: getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);
180:
181: pack();
182: }
183:
184: // </editor-fold>//GEN-END:initComponents
185:
186: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
187:
188: // Per ogni campo creiamo etichetta nelle banda delle etichette e un adeguato textfield nella
189: // banda di dettaglio...
190:
191: // Rimuoviamo tutti i campi dalla banda delle colonne....
192: if (it.businesslogic.ireport.gui.MainFrame.getMainInstance()
193: .getActiveReportFrame() == null) {
194: javax.swing.JOptionPane.showMessageDialog(this , I18n
195: .getString("messages.textWizardForm.noReportFrame",
196: "No report frame selected!"));
197: return;
198: }
199:
200: int count_chars = 0;
201:
202: int[] selectedRows = jTable1.getSelectedRows();
203: for (int i = 0; i < jTable1.getSelectedRowCount(); ++i) {
204: count_chars += ((Integer) jTable1.getValueAt(
205: selectedRows[i], 2)).intValue();
206: }
207:
208: if (count_chars > ((Integer) jSpinner1.getValue()).intValue()) {
209: if (javax.swing.JOptionPane.OK_OPTION != javax.swing.JOptionPane
210: .showConfirmDialog(
211: this ,
212: I18n
213: .getString(
214: "messages.textWizardForm.outOfMargin",
215: "Fields will go out of margins. Continue anyway?"))) {
216: return;
217: }
218: }
219:
220: Vector elements = it.businesslogic.ireport.gui.MainFrame
221: .getMainInstance().getActiveReportFrame().getReport()
222: .getElements();
223: Enumeration enum_elements = elements.elements();
224: Vector elements_to_remove = new Vector();
225: while (enum_elements.hasMoreElements()) {
226: ReportElement element = (ReportElement) enum_elements
227: .nextElement();
228: if (element.getBand().getName().equalsIgnoreCase(
229: "columnHeader")
230: || element.getBand().getName().equalsIgnoreCase(
231: "detail")) {
232: elements_to_remove.add(element);
233: }
234: }
235:
236: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
237: .getActiveReportFrame().setSelectedElements(
238: elements_to_remove);
239: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
240: .getActiveReportFrame().deleteSelectedElements();
241:
242: int left_char = 0;
243: Band columnHeader = it.businesslogic.ireport.gui.MainFrame
244: .getMainInstance().getActiveReportFrame().getReport()
245: .getBandByName("columnHeader");
246: Band detail = it.businesslogic.ireport.gui.MainFrame
247: .getMainInstance().getActiveReportFrame().getReport()
248: .getBandByName("detail");
249: for (int i = 0; i < jTable1.getSelectedRowCount(); ++i) {
250: // Aggiungiamo un elemento di etichetta e uno di testo....
251: int cut_to = ((Integer) jTable1.getValueAt(selectedRows[i],
252: 2)).intValue();
253: StaticTextReportElement stre = new StaticTextReportElement(
254: left_char * 10 + 10, columnHeader
255: .getBandYLocation() + 10, 10 * cut_to, 20);
256: stre.setBand(columnHeader);
257: stre.setText(jTable1.getValueAt(selectedRows[i], 0) + "");
258: stre.setFontName("Monospaced");
259: stre.setFontSize(14);
260:
261: it.businesslogic.ireport.gui.MainFrame
262: .getMainInstance()
263: .getActiveReportFrame()
264: .fireReportListenerReportElementsChanged(
265: new ReportElementChangedEvent(
266: it.businesslogic.ireport.gui.MainFrame
267: .getMainInstance()
268: .getActiveReportFrame(),
269: stre,
270: ReportElementChangedEvent.ADDED));
271: it.businesslogic.ireport.gui.MainFrame
272: .getMainInstance()
273: .getActiveReportFrame()
274: .addUndoOperation(
275: new InsertElementOperation(
276: it.businesslogic.ireport.gui.MainFrame
277: .getMainInstance()
278: .getActiveReportFrame(),
279: stre));
280: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
281: .getActiveReportFrame().getReport().getElements()
282: .add(stre);
283:
284: TextFieldReportElement tfre = new TextFieldReportElement(
285: left_char * 10 + 10,
286: detail.getBandYLocation() + 10, 10 * cut_to, 20);
287: tfre.setBand(detail);
288: tfre.setFontName("Monospaced");
289: tfre.setFontSize(14);
290: String name = "$F{"
291: + jTable1.getValueAt(selectedRows[i], 0) + "}";
292:
293: // cerchiamo il campo e vediamo se e numerico...
294: Enumeration fields = it.businesslogic.ireport.gui.MainFrame
295: .getMainInstance().getActiveReportFrame()
296: .getReport().getFields().elements();
297:
298: boolean is_string = false;
299: String type = "";
300:
301: while (fields.hasMoreElements()) {
302: JRField field = (JRField) fields.nextElement();
303: if (field.getName().equalsIgnoreCase(
304: jTable1.getValueAt(selectedRows[i], 0) + "")) {
305: if (field.getClassType().equalsIgnoreCase(
306: "java.lang.String"))
307: is_string = true;
308: type = field.getClassType();
309: break;
310: }
311: }
312:
313: if (is_string) {
314: tfre.setText("( ((" + name + "!=null) && (" + name
315: + ".length() > " + cut_to + ")) ? " + name
316: + ".substring(0," + cut_to + ") : " + name
317: + ")");
318: } else {
319: tfre.setText(name);
320:
321: }
322: tfre.setClassExpression(type);
323:
324: it.businesslogic.ireport.gui.MainFrame
325: .getMainInstance()
326: .getActiveReportFrame()
327: .fireReportListenerReportElementsChanged(
328: new ReportElementChangedEvent(
329: it.businesslogic.ireport.gui.MainFrame
330: .getMainInstance()
331: .getActiveReportFrame(),
332: tfre,
333: ReportElementChangedEvent.ADDED));
334: it.businesslogic.ireport.gui.MainFrame
335: .getMainInstance()
336: .getActiveReportFrame()
337: .addUndoOperation(
338: new InsertElementOperation(
339: it.businesslogic.ireport.gui.MainFrame
340: .getMainInstance()
341: .getActiveReportFrame(),
342: tfre));
343: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
344: .getActiveReportFrame().getReport().getElements()
345: .add(tfre);
346:
347: left_char += cut_to + 1;
348: }
349: }//GEN-LAST:event_jButton2ActionPerformed
350:
351: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
352: // TODO add your handling code here:
353:
354: DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
355: dtm.setRowCount(0);
356:
357: if (it.businesslogic.ireport.gui.MainFrame.getMainInstance()
358: .getActiveReportFrame() == null) {
359: javax.swing.JOptionPane.showMessageDialog(this , I18n
360: .getString("messages.textWizardForm.noReportFrame",
361: "No report frame selected!"));
362: return;
363: }
364:
365: int chars = it.businesslogic.ireport.gui.MainFrame
366: .getMainInstance().getActiveReportFrame().getReport()
367: .getWidth() / 10;
368: ((javax.swing.SpinnerNumberModel) jSpinner1.getModel())
369: .setValue(new Integer(chars));
370:
371: IReportConnection conn = (IReportConnection) it.businesslogic.ireport.gui.MainFrame
372: .getMainInstance().getProperties().get(
373: "DefaultConnection");
374: if (!conn.isJDBCConnection()) {
375: javax.swing.JOptionPane
376: .showMessageDialog(
377: this ,
378: I18n
379: .getString(
380: "messages.textWizardForm.noActiveJDBC",
381: "The active connection is not of type JDBC. Activate a JDBC connection first."));
382: return;
383: }
384:
385: String query = it.businesslogic.ireport.gui.MainFrame
386: .getMainInstance().getActiveReportFrame().getReport()
387: .getQuery();
388:
389: if (query.toLowerCase().indexOf("where") >= 0) {
390: query = query.substring(0, query.toLowerCase().lastIndexOf(
391: "where"));
392: }
393:
394: //
395:
396: Connection con = null;
397: Statement stmt = null;
398: ResultSet rs = null;
399: ResultSetMetaData rsmd = null;
400: try {
401: con = conn.getConnection();
402: stmt = con.createStatement();
403: try {
404: stmt.setMaxRows(1);
405: } catch (Exception ex) {
406: }
407: rs = stmt.executeQuery(query);
408: rsmd = rs.getMetaData();
409:
410: for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
411: String name = rsmd.getColumnName(i);
412: TextColumn ct = new TextColumn(name, rsmd
413: .getColumnDisplaySize(i));
414:
415: Enumeration fields = it.businesslogic.ireport.gui.MainFrame
416: .getMainInstance().getActiveReportFrame()
417: .getReport().getFields().elements();
418: boolean found = false;
419: while (fields.hasMoreElements()) {
420: JRField field = (JRField) fields.nextElement();
421: if (field.getName().equalsIgnoreCase(name)) {
422: found = true;
423: }
424: }
425: if (found)
426: dtm.addRow(new Object[] { ct,
427: new Integer(ct.getSize()),
428: new Integer(ct.getSize()) });
429: }
430: } catch (Exception ex) {
431: javax.swing.JOptionPane.showMessageDialog(this , I18n
432: .getFormattedString(
433: "messages.textWizardForm.queryError",
434: "error during query execution: {0}",
435: new Object[] { ex.getMessage() }));
436: ex.printStackTrace();
437: } finally {
438:
439: try {
440: if (stmt != null)
441: stmt.close();
442: } catch (Exception ex) {
443: }
444: try {
445: if (con != null)
446: con.close();
447: } catch (Exception ex) {
448: }
449: }
450:
451: }//GEN-LAST:event_jButton1ActionPerformed
452:
453: /**
454: * @param args the command line arguments
455: */
456: public static void main(String args[]) {
457: new TextWizardForm(new javax.swing.JFrame(), true)
458: .setVisible(true);
459: }
460:
461: // Variables declaration - do not modify//GEN-BEGIN:variables
462: private javax.swing.JButton jButton1;
463: private javax.swing.JButton jButton2;
464: private javax.swing.JLabel jLabel1;
465: private javax.swing.JPanel jPanel1;
466: private javax.swing.JPanel jPanel2;
467: private javax.swing.JPanel jPanel3;
468: private javax.swing.JPanel jPanel4;
469: private javax.swing.JPanel jPanel5;
470: private javax.swing.JScrollPane jScrollPane1;
471: private javax.swing.JSeparator jSeparator1;
472: private javax.swing.JSpinner jSpinner1;
473: private javax.swing.JTable jTable1;
474:
475: // End of variables declaration//GEN-END:variables
476:
477: public void applyI18n() {
478: // Start autogenerated code ----------------------
479: jButton1.setText(I18n.getString("textWizardForm.button1",
480: "Check for fields widths"));
481: jButton2.setText(I18n.getString("textWizardForm.button2",
482: "Add elements"));
483: jLabel1.setText(I18n.getString("textWizardForm.label1",
484: "Max report width (chars): "));
485: // End autogenerated code ----------------------
486:
487: jTable1.getColumnModel().getColumn(0).setHeaderValue(
488: I18n.getString("textWizardForm.tablecolumn.column",
489: "Column"));
490: jTable1.getColumnModel().getColumn(1).setHeaderValue(
491: I18n.getString(
492: "textWizardForm.tablecolumn.displaySize",
493: "Display size"));
494: jTable1.getColumnModel().getColumn(2).setHeaderValue(
495: I18n.getString("textWizardForm.tablecolumn.cutTo",
496: "Cut to..."));
497: }
498: }
|