001: package vicazh.hyperpool.stream;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.awt.print.*;
006: import java.io.*;
007: import java.text.*;
008: import java.util.*;
009: import javax.swing.*;
010: import javax.swing.table.*;
011: import org.jfree.layout.*;
012: import org.jfree.report.*;
013: import org.jfree.report.event.*;
014: import org.jfree.report.modules.gui.base.*;
015: import org.jfree.report.modules.output.pageable.base.*;
016: import org.jfree.report.modules.output.pageable.pdf.*;
017: import org.jfree.report.modules.parser.base.*;
018: import vicazh.hyperpool.*;
019: import vicazh.hyperpool.Start;
020: import java.util.logging.*;
021:
022: /**
023: * The graphic report service
024: *
025: * @author Victor Zhigunov
026: * @version 0.4.0
027: */
028: abstract public class IReportService extends IPeriodService implements
029: ReportServiceMBean {
030: private JButton buttonCreate;
031:
032: private JButton buttonPrint;
033:
034: private JButton buttonExport;
035:
036: private Object[] names;
037:
038: private Container container;
039:
040: private DefaultComboBoxModel model;
041:
042: private JComboBox boxzoom;
043:
044: private JComboBox boxpage;
045:
046: private RepaginationListener listener;
047:
048: private Component component;
049:
050: private JFileChooser chooser;
051:
052: private Component parent;
053:
054: private JDialog dialogReplace;
055:
056: private JOptionPane optionPaneReplace;
057:
058: private IReportModel reportModel;
059:
060: /**
061: * @param interfaceClass
062: * interface class
063: * @param name
064: * service name
065: * @param buttonClear
066: * clear button
067: * @param dialogClear
068: * clear dialog
069: * @param buttonStart
070: * start date range button
071: * @param buttonEnd
072: * end date range button
073: * @param box
074: * time period value combo box
075: * @param dialog
076: * date select dialog
077: * @param b1
078: * period radio button
079: * @param b2
080: * range radio button
081: * @param buttonCreate
082: * create button
083: * @param buttonPrint
084: * print button
085: * @param buttonExport
086: * export button
087: * @param names
088: * columns names
089: * @param container
090: * report container
091: * @param model
092: * type model
093: * @param boxzoom
094: * zoom combo box
095: * @param boxpage
096: * page combo box
097: * @param listener
098: * repagination listener
099: * @param component
100: * repagination component
101: * @param chooser
102: * file chooser
103: * @param parent
104: * parent component
105: * @param dialogReplace
106: * replace dialog
107: * @param reportModel
108: * report model
109: */
110: public IReportService(Class<?> interfaceClass, String name,
111: JButton buttonClear, JDialog dialogClear,
112: JButton buttonStart, JButton buttonEnd, JComboBox box,
113: JDialog dialog, AbstractButton b1, AbstractButton b2,
114: JButton buttonCreate, JButton buttonPrint,
115: JButton buttonExport, Object[] names, Container container,
116: DefaultComboBoxModel model, JComboBox boxzoom,
117: JComboBox boxpage, RepaginationListener listener,
118: Component component, JFileChooser chooser,
119: Component parent, JDialog dialogReplace,
120: IReportModel reportModel) {
121: super (interfaceClass, name, buttonClear, dialogClear,
122: buttonStart, buttonEnd, box, dialog, b1, b2);
123: this .buttonCreate = buttonCreate;
124: buttonCreate.addActionListener(new ActionListener() {
125: public void actionPerformed(ActionEvent e) {
126: hold();
127: IReportService.this .container
128: .add(IReportService.this .component);
129: IReportService.this .container.validate();
130: IReportService.this .container.repaint();
131: new Thread() {
132: public void run() {
133: try {
134: IReportType t = (IReportType) IReportService.this .model
135: .getSelectedItem();
136: report = ReportGenerator.getInstance()
137: .parseReport(t.url);
138: File[] f = listFiles(pc);
139: if (f == null) {
140: SwingUtilities
141: .invokeLater(new Runnable() {
142: public void run() {
143: IReportService.this .container
144: .removeAll();
145: IReportService.this .container
146: .validate();
147: IReportService.this .container
148: .repaint();
149: IReportService.this
150: .set();
151: }
152: });
153: return;
154: }
155: Map<Object, Object> map = new TreeMap<Object, Object>();
156: String delimiter = getDelimiter();
157: for (int i = 0; i < f.length; i++) {
158: BufferedReader r = new BufferedReader(
159: new InputStreamReader(
160: new ByteArrayInputStream(
161: get(f[i]))));
162: String line;
163: while ((line = r.readLine()) != null) {
164: int j1;
165: int j2 = line.indexOf(delimiter);
166: Date d = DateFormat
167: .getDateTimeInstance(
168: DateFormat.DEFAULT,
169: DateFormat.DEFAULT,
170: getLocale()).parse(
171: line.substring(0,
172: j2));
173: if (!pc.check(d))
174: continue;
175: if (t.detail != null) {
176: j1 = j2 + delimiter.length();
177: j2 = line
178: .indexOf(delimiter, j1);
179: if (t.detail.compareTo(Boolean
180: .valueOf(line
181: .substring(j1,
182: j2))) == -1)
183: continue;
184: }
185: Vector<Object> v = new Vector<Object>();
186: v.add(d);
187: for (int j = 0; j < IReportService.this .reportModel.count - 1; j++) {
188: j1 = j2 + delimiter.length();
189: j2 = line
190: .indexOf(delimiter, j1);
191: v
192: .add(IReportService.this .reportModel
193: .get(
194: j,
195: line
196: .substring(
197: j1,
198: j2)));
199: }
200: v
201: .add(IReportService.this .reportModel
202: .get(
203: IReportService.this .reportModel.count - 1,
204: line
205: .substring(j2
206: + delimiter
207: .length())));
208: map.put(d.getTime() + line, v);
209: }
210: r.close();
211: }
212: if (map.size() == 0) {
213: SwingUtilities
214: .invokeLater(new Runnable() {
215: public void run() {
216: IReportService.this .container
217: .removeAll();
218: IReportService.this .container
219: .validate();
220: IReportService.this .container
221: .repaint();
222: IReportService.this
223: .set();
224: }
225: });
226: return;
227: }
228: Vector<Object> v = new Vector<Object>();
229: Collections.addAll(v,
230: IReportService.this .names);
231: report
232: .setData(new DefaultTableModel(
233: new Vector<Object>(map
234: .values()), v));
235: report
236: .getReportConfiguration()
237: .setConfigProperty(
238: "org.jfree.report.modules.output.pageable.pdf.Encoding",
239: System
240: .getProperty("file.encoding"));
241: rp = new ReportPane(report);
242: rp
243: .setZoomFactor(((IReportZoom) IReportService.this .boxzoom
244: .getSelectedItem()).zoom);
245: rp
246: .addRepaginationListener(IReportService.this .listener);
247: rp.repaginate();
248: } catch (Exception e) {
249: Start.logger.log(Level.SEVERE, e
250: .getMessage(), e);
251: SwingUtilities.invokeLater(new Runnable() {
252: public void run() {
253: IReportService.this .container
254: .removeAll();
255: IReportService.this .container
256: .validate();
257: IReportService.this .container
258: .repaint();
259: IReportService.this .set();
260: }
261: });
262: return;
263: }
264: SwingUtilities.invokeLater(new Runnable() {
265: public void run() {
266: JPanel p = new JPanel(
267: new CenterLayout());
268: p.add(rp);
269: sp = new JScrollPane();
270: sp.setViewportView(p);
271: IReportService.this .container
272: .removeAll();
273: IReportService.this .container.add(sp);
274: IReportService.this .container
275: .validate();
276: IReportService.this .container.repaint();
277: IReportService.this .set();
278: IReportService.this .boxzoom
279: .setEnabled(true);
280: IReportService.this .buttonPrint
281: .setEnabled(true);
282: IReportService.this .buttonExport
283: .setEnabled(true);
284: IReportService.this .boxpage
285: .removeAllItems();
286: for (int i = 1; i <= rp
287: .getNumberOfPages(); i++)
288: IReportService.this .boxpage
289: .addItem(new Integer(i));
290: IReportService.this .boxpage
291: .setEnabled(true);
292: }
293: });
294: }
295: }.start();
296: }
297: });
298: this .buttonPrint = buttonPrint;
299: buttonPrint.addActionListener(new ActionListener() {
300: public void actionPerformed(ActionEvent e) {
301: PrinterJob printJob = PrinterJob.getPrinterJob();
302: printJob.setPageable(rp);
303: if (printJob.printDialog())
304: try {
305: printJob.print();
306: } catch (Exception ex) {
307: Start.logger.log(Level.SEVERE, ex.getMessage(),
308: ex);
309: }
310: }
311: });
312: this .buttonExport = buttonExport;
313: buttonExport.addActionListener(new ActionListener() {
314: public void actionPerformed(ActionEvent e) {
315: if (IReportService.this .chooser
316: .showSaveDialog(IReportService.this .parent) != JFileChooser.APPROVE_OPTION)
317: return;
318: File f = IReportService.this .chooser.getSelectedFile();
319: String end = '.' + ((IFileFilter) IReportService.this .chooser
320: .getFileFilter()).extension;
321: if (!f.getName().endsWith(end))
322: f = new File(f.getAbsolutePath() + end);
323: if (f.exists()) {
324: IReportService.this .dialogReplace
325: .setLocationRelativeTo(IReportService.this .dialogReplace
326: .getOwner());
327: IReportService.this .dialogReplace.setVisible(true);
328: Object value = optionPaneReplace.getValue();
329: if (value == JOptionPane.UNINITIALIZED_VALUE)
330: return;
331: optionPaneReplace
332: .setValue(JOptionPane.UNINITIALIZED_VALUE);
333: if (((Integer) value).intValue() != JOptionPane.YES_OPTION)
334: return;
335: }
336: try {
337: final OutputStream out = new BufferedOutputStream(
338: new FileOutputStream(f));
339: hold();
340: IReportService.this .container
341: .add(IReportService.this .component);
342: IReportService.this .container.validate();
343: IReportService.this .container.repaint();
344: new Thread() {
345: public void run() {
346: try {
347: PDFOutputTarget target = new PDFOutputTarget(
348: out);
349: target.open();
350: PageableReportProcessor proc = new PageableReportProcessor(
351: report);
352: proc
353: .addRepaginationListener(IReportService.this .listener);
354: proc.setOutputTarget(target);
355: proc.processReport();
356: target.close();
357: } catch (Exception e) {
358: Start.logger.log(Level.SEVERE, e
359: .getMessage(), e);
360: }
361: SwingUtilities.invokeLater(new Runnable() {
362: public void run() {
363: JPanel p = new JPanel(
364: new CenterLayout());
365: p.add(rp);
366: sp = new JScrollPane();
367: sp.setViewportView(p);
368: IReportService.this .container
369: .removeAll();
370: IReportService.this .container
371: .add(sp);
372: IReportService.this .container
373: .validate();
374: IReportService.this .container
375: .repaint();
376: IReportService.this .set();
377: IReportService.this .boxzoom
378: .setEnabled(true);
379: IReportService.this .buttonPrint
380: .setEnabled(true);
381: IReportService.this .buttonExport
382: .setEnabled(true);
383: IReportService.this .boxpage
384: .setEnabled(true);
385: }
386: });
387: }
388: }.start();
389: } catch (FileNotFoundException ex) {
390: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
391: }
392: }
393: });
394: this .names = names;
395: this .container = container;
396: this .model = model;
397: this .boxzoom = boxzoom;
398: boxzoom.addActionListener(new ActionListener() {
399: public void actionPerformed(ActionEvent e) {
400: rp
401: .setZoomFactor(((IReportZoom) IReportService.this .boxzoom
402: .getSelectedItem()).zoom);
403: JPanel p = new JPanel(new CenterLayout());
404: p.add(rp);
405: sp.setViewportView(p);
406: }
407: });
408: this .boxpage = boxpage;
409: boxpage.addActionListener(new ActionListener() {
410: public void actionPerformed(ActionEvent e) {
411: Integer page = (Integer) IReportService.this .boxpage
412: .getSelectedItem();
413: if (page != null)
414: rp.setPageNumber(page.intValue());
415: }
416: });
417: this .listener = listener;
418: this .component = component;
419: this .chooser = chooser;
420: this .parent = parent;
421: this .dialogReplace = dialogReplace;
422: optionPaneReplace = (JOptionPane) dialogReplace
423: .getContentPane();
424: optionPaneReplace.addPropertyChangeListener(this );
425: this .reportModel = reportModel;
426: boxzoom.setEnabled(false);
427: buttonPrint.setEnabled(false);
428: buttonExport.setEnabled(false);
429: boxpage.setEnabled(false);
430: }
431:
432: public String getDelimiter() {
433: return ((ReportServiceMBean) melement).getDelimiter();
434: }
435:
436: private JScrollPane sp;
437:
438: private ReportPane rp;
439:
440: protected void hold() {
441: super .hold();
442: container.removeAll();
443: boxzoom.setEnabled(false);
444: buttonPrint.setEnabled(false);
445: buttonExport.setEnabled(false);
446: boxpage.setEnabled(false);
447: buttonCreate.setEnabled(false);
448: }
449:
450: private JFreeReport report;
451:
452: public void updateUI() {
453: SwingUtilities.updateComponentTreeUI(component);
454: SwingUtilities.updateComponentTreeUI(dialogReplace);
455: SwingUtilities.updateComponentTreeUI(chooser);
456: super .updateUI();
457: }
458:
459: protected void set() {
460: super .set();
461: buttonCreate.setEnabled(isDirExists);
462: }
463:
464: public File[] listFiles(PeriodControl pc) {
465: return ((ReportServiceMBean) melement).listFiles(pc);
466: }
467:
468: public Locale getLocale() {
469: return ((ReportServiceMBean) melement).getLocale();
470: }
471: }
|