001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * 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, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.Cursor;
025: import java.awt.Dimension;
026: import java.awt.event.WindowEvent;
027: import java.awt.print.PageFormat;
028: import javax.swing.*;
029: import javax.swing.text.*;
030:
031: public class DataReportViewer extends javax.swing.JFrame implements
032: Runnable, java.awt.event.ActionListener,
033: java.awt.event.WindowListener, javax.swing.event.ChangeListener {
034: private ReportView reportView;
035: private JScrollPane jsP;
036: private String path;
037: private int maxHeight = 0;
038: private int maxWidth = 0;
039: private int counter = 1;
040: private int totalPage;
041: private javax.swing.JWindow export;
042: private javax.swing.JLabel label;
043: private java.awt.Toolkit objToolkit;
044: private Dimension dScSize;
045: private JButton next;
046: private JButton prev;
047: private JButton print;
048: private JSlider slider;
049: private XmlFileReader xmlFR;
050: private javax.swing.JProgressBar jwPrint;
051:
052: public DataReportViewer() {
053: this (null);
054: }
055:
056: public DataReportViewer(String path) {
057: super ();
058:
059: this .path = path;
060:
061: this .setBounds(0, 0, 1024, 768);
062: this .addWindowListener(this );
063:
064: reportView = new ReportView(null);
065: reportView.setBackground(java.awt.Color.white);
066:
067: jsP = new JScrollPane(reportView);
068:
069: objToolkit = java.awt.Toolkit.getDefaultToolkit();
070: dScSize = objToolkit.getScreenSize();
071:
072: export = new javax.swing.JWindow(this );
073: export.setBounds((dScSize.width / 2 - 100),
074: (dScSize.height / 2 - 10), 200, 20);
075:
076: label = new javax.swing.JLabel("Loading .....");
077: label.setFont(new java.awt.Font(null, java.awt.Font.BOLD, 15));
078: label.setBackground(java.awt.Color.white);
079: export.getContentPane().add(label);
080: export.setVisible(false);
081: }
082:
083: public void loadReport() throws Exception {
084: try {
085: javax.swing.JFileChooser chooser = null;
086: int returnVal = javax.swing.JFileChooser.CANCEL_OPTION;
087:
088: if (path == null) {
089: chooser = new javax.swing.JFileChooser();
090: XmlFileFilter filter = new XmlFileFilter();
091:
092: filter.addExtension("dpv");
093: filter.setDescription("DataReport exp.");
094:
095: chooser.setFileFilter(filter);
096: returnVal = chooser.showOpenDialog(this );
097: }
098:
099: if ((returnVal == javax.swing.JFileChooser.APPROVE_OPTION)
100: || (path != null)) {
101: export.setVisible(true);
102:
103: if (path == null) {
104: xmlFR = new XmlFileReader(chooser.getSelectedFile()
105: .getAbsolutePath(), false);
106: } else {
107: xmlFR = new XmlFileReader(path, false);
108: }
109:
110: totalPage = xmlFR.getTotalPage();
111:
112: java.util.Vector vObjF = xmlFR.getObjects("page.1");
113: java.util.Iterator iteratorObjectF = vObjF.iterator();
114:
115: while (iteratorObjectF.hasNext()) {
116: final Object obj = iteratorObjectF.next();
117:
118: if (obj instanceof javax.swing.text.JTextComponent) {
119: ((JTextComponent) obj).setEditable(false);
120: }
121:
122: reportView.add((JComponent) obj);
123: UpdateDimensionPreviewPanel((JComponent) obj);
124: }
125:
126: reportView.setPreferredSize(new Dimension(maxWidth,
127: maxHeight));
128:
129: Rule ruleHeader = new Rule(Rule.HORIZONTAL, true);
130: ruleHeader.setPreferredHeight(12);
131:
132: Rule ruleRow = new Rule(Rule.VERTICAL, true);
133: ruleRow.setPreferredWidth(12);
134:
135: jsP.setColumnHeaderView(ruleHeader);
136: jsP.setRowHeaderView(ruleRow);
137:
138: Corner cornerUR = new Corner();
139: Corner cornerUL = new Corner();
140: Corner cornerLL = new Corner();
141:
142: jsP.setCorner(JScrollPane.UPPER_LEFT_CORNER, cornerUL);
143: jsP.setCorner(JScrollPane.UPPER_RIGHT_CORNER, cornerUR);
144: jsP.setCorner(JScrollPane.LOWER_LEFT_CORNER, cornerLL);
145:
146: this .createButton();
147: this .getContentPane().add(jsP,
148: java.awt.BorderLayout.CENTER);
149:
150: export.setVisible(false);
151: this .setVisible(true);
152:
153: }
154: } catch (java.lang.OutOfMemoryError e) {
155: this .setVisible(false);
156: javax.swing.JOptionPane
157: .showMessageDialog(
158: null,
159: "Out of Memory error - Non enough memory - To reduce the dimensions of the report and to retry.");
160: System.exit(0);
161: }
162: }
163:
164: private void createButton() {
165: JPanel buttonPanel = new JPanel();
166: buttonPanel.setLayout(new javax.swing.BoxLayout(buttonPanel,
167: javax.swing.BoxLayout.X_AXIS));
168: ImageIcon right = new ImageIcon(JPreviewFrame.class
169: .getResource("img/next.png"));
170: next = new JButton(right);
171: next.setPreferredSize(new Dimension(80, 40));
172: next.setLocation(0, 0);
173: next.addActionListener(this );
174:
175: slider = new JSlider(JSlider.HORIZONTAL, 1, totalPage, 1);
176: slider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
177: slider.setPaintTicks(true);
178: slider.setMajorTickSpacing(1);
179: slider.setMinorTickSpacing(1);
180: slider.setPaintLabels(true);
181: slider.setSnapToTicks(true);
182: slider.setPreferredSize(new Dimension(1100, 40));
183: slider.addChangeListener(this );
184:
185: ImageIcon left = new ImageIcon(JPreviewFrame.class
186: .getResource("img/prev.png"));
187: prev = new JButton(left);
188: prev.setEnabled(false);
189: prev.setPreferredSize(new Dimension(80, 40));
190: prev.setLocation(101, 0);
191: prev.addActionListener(this );
192:
193: ImageIcon printers = new ImageIcon(JPreviewFrame.class
194: .getResource("img/printers.png"));
195: print = new JButton(printers);
196: print.setEnabled(true);
197: print.setPreferredSize(new Dimension(100, 40));
198: print.setLocation(202, 0);
199: print.addActionListener(this );
200:
201: if (totalPage <= 1) {
202: next.setEnabled(false);
203: }
204:
205: JPanel sliderPanel = new JPanel();
206: sliderPanel.setLayout(new javax.swing.BoxLayout(sliderPanel,
207: javax.swing.BoxLayout.X_AXIS));
208: sliderPanel.setPreferredSize(new Dimension(1000, 50));
209: sliderPanel.setBorder(javax.swing.BorderFactory
210: .createLoweredBevelBorder());
211: sliderPanel.add(slider);
212:
213: buttonPanel.add(sliderPanel);
214:
215: JPanel button = new JPanel();
216: button.setLayout(new java.awt.GridLayout(1, 4));
217: button.setPreferredSize(new Dimension(260, 50));
218: button.add(prev);
219: button.add(next);
220: button.add(print);
221:
222: buttonPanel.add(button);
223:
224: this .getContentPane().add(buttonPanel,
225: java.awt.BorderLayout.NORTH);
226: }
227:
228: public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
229: if (actionEvent.getSource().equals(next)) {
230: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
231: slider.setValue(slider.getValue() + 1);
232: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
233: }
234:
235: if (actionEvent.getSource().equals(prev)) {
236: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
237: slider.setValue(slider.getValue() - 1);
238: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
239: }
240:
241: if (actionEvent.getSource().equals(print)) {
242:
243: PrintThread printJPrew = new PrintThread(this , reportView,
244: totalPage);
245: jwPrint = printJPrew.getJProgressBarPrint();
246: new Thread(printJPrew).start();
247: }
248: }
249:
250: private void UpdateDimensionPreviewPanel(JComponent obj) {
251: if ((obj.getLocation().x + obj.getSize().width) > maxWidth) {
252: maxWidth = obj.getLocation().x + obj.getSize().width;
253: }
254:
255: if ((obj.getLocation().y + obj.getSize().height) > maxHeight) {
256: maxHeight = obj.getLocation().y + obj.getSize().height;
257: }
258: }
259:
260: private void PrintThis(java.awt.print.Printable prObj) {
261: SecurityManager security = System.getSecurityManager();
262:
263: if (security != null) {
264: try {
265: security.checkPrintJobAccess();
266: } catch (SecurityException se) {
267: System.out
268: .println("Security error trying to print data !!! :(");
269: }
270: } else {
271: //System.out.println( "No security manager !!! :(" );
272: java.awt.print.PrinterJob printJob = java.awt.print.PrinterJob
273: .getPrinterJob();
274: reportView.setPrintJob(printJob);
275: PageFormat pf = printJob.pageDialog(printJob.defaultPage());
276: printJob.setPrintable(prObj, pf);
277:
278: java.awt.print.Book book = new java.awt.print.Book();
279: book.append(reportView, pf, totalPage);
280: printJob.setPageable(book);
281:
282: if (printJob.printDialog()) {
283: try {
284: printJob.print();
285: } catch (Exception ex) {
286: ex.printStackTrace();
287: }
288: }
289: }
290: }
291:
292: public void stateChanged(javax.swing.event.ChangeEvent e) {
293: JSlider s1 = (JSlider) e.getSource();
294: int value = s1.getValue();
295:
296: java.util.Vector vObjF = xmlFR.getObjects("page." + value);
297: counter = value;
298:
299: java.util.Iterator iteratorObjectF = vObjF.iterator();
300: reportView.removeAll();
301:
302: while (iteratorObjectF.hasNext()) {
303: final Object obj = iteratorObjectF.next();
304: reportView.add((JComponent) obj);
305: UpdateDimensionPreviewPanel((JComponent) obj);
306: }
307:
308: if (value == 1) {
309: prev.setEnabled(false);
310:
311: if (totalPage > 1) {
312: next.setEnabled(true);
313: }
314:
315: } else if (value == totalPage) {
316: if (totalPage > 1) {
317: prev.setEnabled(true);
318: }
319:
320: next.setEnabled(false);
321:
322: } else {
323: prev.setEnabled(true);
324: next.setEnabled(true);
325: }
326:
327: reportView.setPreferredSize(new Dimension(maxWidth, maxHeight));
328: reportView.repaint();
329:
330: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
331: }
332:
333: public void windowClosing(WindowEvent windowEvent) {
334: xmlFR = null;
335: }
336:
337: public void windowOpened(WindowEvent windowEvent) {
338: }
339:
340: public void windowClosed(WindowEvent windowEvent) {
341: System.exit(0);
342: }
343:
344: public void windowDeiconified(WindowEvent windowEvent) {
345: }
346:
347: public void windowDeactivated(WindowEvent windowEvent) {
348: }
349:
350: public void windowActivated(WindowEvent windowEvent) {
351: }
352:
353: public void windowIconified(WindowEvent windowEvent) {
354: }
355:
356: public void run() {
357: try {
358: loadReport();
359:
360: } catch (java.lang.Exception e) {
361: export.setVisible(false);
362: javax.swing.JOptionPane.showMessageDialog(null, e);
363: }
364: }
365:
366: public boolean imageUpdate(java.awt.Image img, int infoflags,
367: int x, int y, int width, int height) {
368: boolean retValue;
369:
370: retValue = super .imageUpdate(img, infoflags, x, y, width,
371: height);
372: return retValue;
373: }
374:
375: class ReportView extends JPanel implements java.awt.print.Printable {
376: public int pagIndexPrint = 0;
377: private java.awt.print.PrinterJob printerJob;
378:
379: public ReportView(java.awt.LayoutManager layout) {
380: super (layout);
381: }
382:
383: public void setPrintJob(java.awt.print.PrinterJob prj) {
384: this .printerJob = prj;
385: }
386:
387: public int print(java.awt.Graphics g,
388: java.awt.print.PageFormat pf, int pageIndex)
389: throws java.awt.print.PrinterException {
390: reportView.removeAll();
391: java.util.Vector vObjF = xmlFR.getObjects("page."
392: + (pageIndex + 1));
393:
394: java.util.Iterator iteratorObjectF = vObjF.iterator();
395:
396: while (iteratorObjectF.hasNext()) {
397: final Object obj = iteratorObjectF.next();
398: reportView.add((JComponent) obj);
399: }
400:
401: reportView.repaint();
402:
403: DataReportViewer.this .jwPrint.setString("Pagina "
404: + (pageIndex + 1));
405: DataReportViewer.this .jwPrint.setValue(pageIndex + 1);
406:
407: java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
408:
409: disableDoubleBuffering(this );
410: g2.translate(pf.getImageableX(), pf.getImageableY());
411:
412: double scaleX = pf.getImageableWidth()
413: / DataReportViewer.this .maxWidth;
414: double scaleY = pf.getImageableHeight()
415: / DataReportViewer.this .maxHeight;
416:
417: if (scaleX < 1 || scaleY < 1) {
418: if (true) {
419: if (scaleX < scaleY) {
420: scaleY = scaleX;
421: } else {
422: scaleX = scaleY;
423: }
424: }
425: g2.scale(scaleX, scaleY);
426: }
427:
428: paint(g2);
429: enableDoubleBuffering(this );
430:
431: return PAGE_EXISTS;
432: }
433:
434: public void disableDoubleBuffering(java.awt.Component c) {
435: javax.swing.RepaintManager currentManager = javax.swing.RepaintManager
436: .currentManager(c);
437: currentManager.setDoubleBufferingEnabled(false);
438: }
439:
440: public void enableDoubleBuffering(java.awt.Component c) {
441: javax.swing.RepaintManager currentManager = javax.swing.RepaintManager
442: .currentManager(c);
443: currentManager.setDoubleBufferingEnabled(true);
444: }
445:
446: public boolean imageUpdate(java.awt.Image img, int infoflags,
447: int x, int y, int width, int height) {
448: boolean retValue;
449:
450: retValue = super.imageUpdate(img, infoflags, x, y, width,
451: height);
452: return retValue;
453: }
454: }
455: }
|