001: /*
002: * PagesFrame.java
003: *
004: * Created on April 20, 2007, 1:37 AM
005: */
006:
007: package be.savat.components;
008:
009: import it.businesslogic.ireport.util.Misc;
010: import java.awt.Color;
011: import java.awt.Graphics;
012: import java.awt.Image;
013: import java.awt.Point;
014: import java.awt.image.BufferedImage;
015: import javax.swing.JFrame;
016: import net.sf.jasperreports.engine.JRExporterParameter;
017: import net.sf.jasperreports.engine.JasperPrint;
018: import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
019: import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;
020:
021: /**
022: *
023: * @author gtoffoli
024: */
025: public class PagesFrame extends javax.swing.JFrame {
026:
027: private JasperPrint jrPrint = null;
028: private JRGraphics2DExporter exporter = null;
029: private Point pageSize = new Point(350, 472);
030: JBookPanel turnerNewsPaper = null;
031:
032: /** Creates new form PagesFrame */
033: public PagesFrame(JasperPrint jrPrint) {
034: initAll(jrPrint);
035:
036: Misc.centerFrame(this );
037: }
038:
039: public void initAll(JasperPrint jrPrint) {
040:
041: initComponents();
042: this .jrPrint = jrPrint;
043: try {
044: exporter = new JRGraphics2DExporter();
045: } catch (Exception ex) {
046: }
047:
048: setTitle("JBookPanel from Pieter-Jan Savat!!! Great job!");
049:
050: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
051: setSize(800, 600);
052:
053: turnerNewsPaper = new JBookPanel() {
054:
055: protected Image loadPage(int index) {
056:
057: BufferedImage img = new BufferedImage(getPageSize().x,
058: getPageSize().y, BufferedImage.TYPE_INT_RGB);
059: Graphics gfx = img.getGraphics();
060: //gfx.setColor(Color.WHITE);
061: //gfx.fillRect(0, 0, img.getWidth(), img.getHeight());
062: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
063: getJrPrint());
064: exporter.setParameter(JRExporterParameter.PAGE_INDEX,
065: new Integer(index));
066: exporter.setParameter(
067: JRGraphics2DExporterParameter.GRAPHICS_2D, gfx);
068: exporter.setParameter(
069: JRGraphics2DExporterParameter.ZOOM_RATIO,
070: new Float((float) getPageSize().x
071: / (float) getJrPrint().getPageWidth()));
072:
073: try {
074: exporter.exportReport();
075: } catch (Exception ex) {
076: }
077: return img;
078: }
079: };
080:
081: turnerNewsPaper.setMargins(30, 40);
082: turnerNewsPaper.setBackground(new Color(157, 185, 235));
083: turnerNewsPaper.setLeftPageIndex(-1);
084: getContentPane().add(turnerNewsPaper,
085: java.awt.BorderLayout.CENTER);
086:
087: updateViewerSize();
088:
089: }
090:
091: public JasperPrint getJrPrint() {
092: return jrPrint;
093: }
094:
095: void updateViewerSize() {
096: System.out.println("Updating page size: " + getPageSize());
097: System.out.flush();
098:
099: int imageWidth = jrPrint.getPageWidth();
100: int imageHeight = jrPrint.getPageHeight();
101:
102: int width = getContentPane().getWidth() / 2 - 40;
103: int height = getContentPane().getHeight() - 60;
104:
105: if (imageWidth <= width && imageHeight <= height) {
106: getPageSize().x = width;
107: getPageSize().y = height;
108: } else if (width > 0 && height > 0)// Resize based on minor x/WIDTH... e y/HEIGHT
109: {
110: if ((double) ((double) imageWidth / (double) width) > (double) ((double) imageHeight / (double) height)) {
111: getPageSize().x = width;
112: getPageSize().y = Math.min((imageHeight * width)
113: / imageWidth, height);
114:
115: } else {
116: getPageSize().x = Math.min((imageWidth * height)
117: / imageHeight, width);
118: getPageSize().y = height;
119:
120: }
121: }
122:
123: int index = -1;
124: index = turnerNewsPaper.getLeftPageIndex();
125: //getContentPane().removeAll();
126:
127: turnerNewsPaper.setPages("", "", "",
128: jrPrint.getPages().size() - 1, getPageSize().x,
129: getPageSize().y);
130:
131: turnerNewsPaper.setLeftPageIndex(index);
132:
133: System.out.println("New size: " + getPageSize());
134: System.out.flush();
135: }
136:
137: public void setJrPrint(JasperPrint jrPrint) {
138: this .jrPrint = jrPrint;
139: }
140:
141: public JRGraphics2DExporter getExporter() {
142: return exporter;
143: }
144:
145: public void setExporter(JRGraphics2DExporter exporter) {
146: this .exporter = exporter;
147: }
148:
149: public Point getPageSize() {
150: return pageSize;
151: }
152:
153: public void setPageSize(Point pageSize) {
154: this .pageSize = pageSize;
155: }
156:
157: /** This method is called from within the constructor to
158: * initialize the form.
159: * WARNING: Do NOT modify this code. The content of this method is
160: * always regenerated by the Form Editor.
161: */
162: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
163: private void initComponents() {
164:
165: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
166: addComponentListener(new java.awt.event.ComponentAdapter() {
167: public void componentResized(
168: java.awt.event.ComponentEvent evt) {
169: formComponentResized(evt);
170: }
171: });
172:
173: pack();
174: }// </editor-fold>//GEN-END:initComponents
175:
176: private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
177:
178: updateViewerSize();
179: }//GEN-LAST:event_formComponentResized
180:
181: // Variables declaration - do not modify//GEN-BEGIN:variables
182: // End of variables declaration//GEN-END:variables
183:
184: }
|