1 | /* |
---|
2 | Copyright 2006 by Sean Luke |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | /* |
---|
9 | * Created on Apr 17, 2005 12:29:01 PM |
---|
10 | * |
---|
11 | * By: spaus |
---|
12 | */ |
---|
13 | package ec.display.chart; |
---|
14 | |
---|
15 | import javax.swing.JPanel; |
---|
16 | |
---|
17 | import java.awt.BorderLayout; |
---|
18 | import java.awt.FileDialog; |
---|
19 | import java.awt.Frame; |
---|
20 | import java.awt.Graphics2D; |
---|
21 | import java.awt.geom.Rectangle2D; |
---|
22 | import java.io.File; |
---|
23 | import java.io.FileOutputStream; |
---|
24 | |
---|
25 | import javax.swing.Box; |
---|
26 | import javax.swing.BoxLayout; |
---|
27 | import javax.swing.JButton; |
---|
28 | |
---|
29 | import org.jfree.chart.ChartPanel; |
---|
30 | import org.jfree.chart.JFreeChart; |
---|
31 | |
---|
32 | import com.lowagie.text.Document; |
---|
33 | import com.lowagie.text.pdf.DefaultFontMapper; |
---|
34 | import com.lowagie.text.pdf.PdfContentByte; |
---|
35 | import com.lowagie.text.pdf.PdfTemplate; |
---|
36 | import com.lowagie.text.pdf.PdfWriter; |
---|
37 | |
---|
38 | import ec.display.StatisticsChartPane; |
---|
39 | /** |
---|
40 | * @author spaus |
---|
41 | */ |
---|
42 | public class StatisticsChartPaneTab |
---|
43 | extends JPanel |
---|
44 | { |
---|
45 | |
---|
46 | private JPanel jPanel = null; |
---|
47 | private JButton printButton = null; |
---|
48 | private JButton closeButton = null; |
---|
49 | private final ChartPanel chartPane; |
---|
50 | /** |
---|
51 | * |
---|
52 | */ |
---|
53 | public StatisticsChartPaneTab(ChartPanel chartPane) |
---|
54 | { |
---|
55 | super(); |
---|
56 | this.chartPane = chartPane; |
---|
57 | initialize(); |
---|
58 | this.add(chartPane,BorderLayout.CENTER); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * @param isDoubleBuffered |
---|
63 | */ |
---|
64 | public StatisticsChartPaneTab(ChartPanel chartPane, boolean isDoubleBuffered) |
---|
65 | { |
---|
66 | super(isDoubleBuffered); |
---|
67 | this.chartPane = chartPane; |
---|
68 | initialize(); |
---|
69 | this.add(chartPane,BorderLayout.CENTER); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * This method initializes this |
---|
74 | * |
---|
75 | * @return void |
---|
76 | */ |
---|
77 | private void initialize() |
---|
78 | { |
---|
79 | this.setLayout(new BorderLayout()); |
---|
80 | this.setSize(300,200); |
---|
81 | this.add(getJPanel(), java.awt.BorderLayout.SOUTH); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * This method initializes jPanel |
---|
86 | * |
---|
87 | * @return javax.swing.JPanel |
---|
88 | */ |
---|
89 | private JPanel getJPanel() |
---|
90 | { |
---|
91 | if (jPanel == null) |
---|
92 | { |
---|
93 | jPanel = new JPanel(); |
---|
94 | jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.X_AXIS)); |
---|
95 | jPanel.add(Box.createHorizontalGlue()); |
---|
96 | jPanel.add(getPrintButton(), null); |
---|
97 | jPanel.add(getCloseButton(), null); |
---|
98 | } |
---|
99 | return jPanel; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * This method initializes jButton |
---|
104 | * |
---|
105 | * @return javax.swing.JButton |
---|
106 | */ |
---|
107 | private JButton getPrintButton() |
---|
108 | { |
---|
109 | if (printButton == null) |
---|
110 | { |
---|
111 | printButton = new JButton(); |
---|
112 | printButton.setText("Export to PDF..."); |
---|
113 | final JFreeChart chart = chartPane.getChart(); |
---|
114 | printButton.addActionListener(new java.awt.event.ActionListener() |
---|
115 | { |
---|
116 | public void actionPerformed(java.awt.event.ActionEvent e) |
---|
117 | { |
---|
118 | try |
---|
119 | |
---|
120 | { |
---|
121 | int width = chartPane.getWidth(); |
---|
122 | int height = chartPane.getHeight(); |
---|
123 | |
---|
124 | FileDialog fileDialog = new FileDialog(new Frame(),"Export...",FileDialog.SAVE); |
---|
125 | fileDialog.setDirectory(System.getProperty("user.dir")); |
---|
126 | fileDialog.setFile("*.pdf"); |
---|
127 | fileDialog.setVisible(true); |
---|
128 | String fileName = fileDialog.getFile(); |
---|
129 | if ( fileName != null ) |
---|
130 | |
---|
131 | { |
---|
132 | if (!fileName.endsWith(".pdf")) |
---|
133 | { |
---|
134 | fileName = fileName+".pdf"; |
---|
135 | } |
---|
136 | File f = new File(fileDialog.getDirectory(), fileName); |
---|
137 | Document document = new Document(new com.lowagie.text.Rectangle(width,height)); |
---|
138 | PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f)); |
---|
139 | document.addAuthor("ECJ Console"); |
---|
140 | document.open(); |
---|
141 | PdfContentByte cb = writer.getDirectContent(); |
---|
142 | PdfTemplate tp = cb.createTemplate(width, height); |
---|
143 | Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); |
---|
144 | Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height); |
---|
145 | chart.draw(g2, rectangle2D); |
---|
146 | g2.dispose(); |
---|
147 | cb.addTemplate(tp, 0, 0); |
---|
148 | document.close(); |
---|
149 | } |
---|
150 | } |
---|
151 | catch( Exception ex ) |
---|
152 | |
---|
153 | { |
---|
154 | ex.printStackTrace(); |
---|
155 | } |
---|
156 | } |
---|
157 | }); |
---|
158 | } |
---|
159 | return printButton; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * This method initializes jButton1 |
---|
164 | * |
---|
165 | * @return javax.swing.JButton |
---|
166 | */ |
---|
167 | private JButton getCloseButton() |
---|
168 | { |
---|
169 | if (closeButton == null) |
---|
170 | { |
---|
171 | closeButton = new JButton(); |
---|
172 | closeButton.setText("Close"); |
---|
173 | final StatisticsChartPaneTab pane = this; |
---|
174 | closeButton.addActionListener(new java.awt.event.ActionListener() |
---|
175 | { |
---|
176 | public void actionPerformed(java.awt.event.ActionEvent e) |
---|
177 | { |
---|
178 | StatisticsChartPane parent = (StatisticsChartPane)pane.getParent(); |
---|
179 | parent.removeTabAt(parent.indexOfComponent(pane)); |
---|
180 | } |
---|
181 | }); |
---|
182 | } |
---|
183 | return closeButton; |
---|
184 | } |
---|
185 | } |
---|