JasperReportUI.java :  » Report » jmagallanes-1.0 » com » calipso » reportgenerator » userinterface » Java Open Source

Java Open Source » Report » jmagallanes 1.0 
jmagallanes 1.0 » com » calipso » reportgenerator » userinterface » JasperReportUI.java
package com.calipso.reportgenerator.userinterface;

import net.sf.jasperreports.view.JRViewer;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import com.calipso.reportgenerator.common.LanguageTraslator;
import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;

/**
 * Representa la interfaz del reporte estatico
 */

public class JasperReportUI extends JFrame implements ActionListener {

  private JasperPrint print;
  private JMenuItem menuItemPdf;
  private JMenuItem menuItemCvs;
  private JMenuItem menuItemExcel;
  private JMenuItem menuItemHtml;
  private JMenuItem menuItemXml;
  private JMenuItem menuItemExit;

  /**
   * Inicializa una instancia de <code>JasperReportUI</code>
   * @param jrViewer
   * @param print
   */
  public JasperReportUI(JRViewer jrViewer, JasperPrint print) {
    this.print = print;
    initialize(jrViewer);
  }

  /**
   * Inicializa la interfaz
   * @param jrViewer
   */
  private void initialize(JRViewer jrViewer) {
    setTitle(LanguageTraslator.traslate("331"));
    setJMenuBar(getFrameMenuBar());
    getContentPane().setLayout(new BorderLayout());
    JScrollPane pane = new JScrollPane(jrViewer);
    pane.setViewportView(jrViewer);
    getContentPane().add(pane, BorderLayout.CENTER);
  }

  private JMenuBar getFrameMenuBar() {
    JMenuBar menuBar = new JMenuBar();

    JMenu menuFile = new JMenu(com.calipso.reportgenerator.common.LanguageTraslator.traslate("301"));

    menuItemPdf = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("302"));
    menuItemPdf.addActionListener(this);
    menuFile.add(menuItemPdf);

    menuItemHtml = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("303"));
    menuItemHtml.addActionListener(this);
    menuFile.add(menuItemHtml);

    menuItemExcel = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("304"));
    menuItemExcel.addActionListener(this);
    menuFile.add(menuItemExcel);

    menuItemCvs = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("305"));
    menuItemCvs.addActionListener(this);
    menuFile.add(menuItemCvs);

    menuItemXml = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("306"));
    menuItemXml.addActionListener(this);
    menuFile.add(menuItemXml);

    menuFile.addSeparator();

    menuItemExit = new JMenuItem(com.calipso.reportgenerator.common.LanguageTraslator.traslate("144"));
    menuItemExit.addActionListener(this);
    menuFile.add(menuItemExit);

    menuBar.add(menuFile);

    return menuBar;
  }

  /**
   * Captura los eventos de la interfaz
   * @param ae
   */
  public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == menuItemExit) {
      dispose();
    } else {
      JFileChooser fileChooser = new JFileChooser();
      int result = fileChooser.showSaveDialog(this);
      if(result == JFileChooser.APPROVE_OPTION) {
        try {
          if(ae.getSource() == menuItemPdf) {
            JasperPrintManager. printReportToPdfFile(print, fileChooser.getSelectedFile().toString());
          } else if(ae.getSource() == menuItemHtml) {
            JasperExportManager.exportReportToHtmlFile(print, fileChooser.getSelectedFile().toString());
          } else if(ae.getSource() == menuItemExcel) {
            JRExporter exporter = new JRXlsExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileChooser.getSelectedFile().toString());
            exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
            exporter.exportReport();
          } else if(ae.getSource() == menuItemCvs) {
            JRCsvExporter exporter = new JRCsvExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileChooser.getSelectedFile().toString());
            exporter.exportReport();
          } else if(ae.getSource() == menuItemXml) {
            JasperPrintManager.printReportToXmlFile(print, fileChooser.getSelectedFile().toString(), true);
          }
        } catch(JRException e) {
          ShowExceptionMessageDialog.initExceptionDialogMessage(com.calipso.reportgenerator.common.LanguageTraslator.traslate("311"), e);
        }
      }
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.