1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jhunters.pool.ui;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24
25 import javafx.print.PageLayout;
26 import javafx.print.PageOrientation;
27 import javafx.print.Paper;
28 import javafx.print.Printer;
29 import javafx.print.PrinterJob;
30 import javafx.scene.layout.StackPane;
31 import javafx.scene.web.WebEngine;
32 import javafx.scene.web.WebView;
33 import net.sourceforge.jhunters.pool.PoolException;
34 import net.sourceforge.jhunters.pool.data.Competition;
35 import net.sourceforge.jhunters.pool.ui.CompetitionPanel.ViewSelect;
36
37
38
39
40 public class ReportPanel
41 extends StackPane {
42
43
44
45 protected interface TableSource {
46
47
48
49
50 void buildReport(final ReportBuilder pBuilder);
51
52
53
54
55
56 void createWeb(final ReportBuilder pBuilder);
57 }
58
59
60
61
62 private final AppControl theControl;
63
64
65
66
67 private final WebEngine theEngine;
68
69
70
71
72 private final ReportBuilder theReportBuilder;
73
74
75
76
77 private final ReportBuilder theWebBuilder;
78
79
80
81
82
83
84 protected ReportPanel(final AppControl pControl) throws PoolException {
85
86 theControl = pControl;
87
88
89 WebView myView = new WebView();
90 theEngine = myView.getEngine();
91
92
93 theReportBuilder = new ReportBuilder(false);
94 theWebBuilder = new ReportBuilder(true);
95
96
97 getChildren().add(myView);
98 }
99
100
101
102
103
104
105 protected void printReport(final TableSource pSource,
106 final PageOrientation pOrientation) {
107
108 try {
109
110 theReportBuilder.resetDocument();
111
112
113 pSource.buildReport(theReportBuilder);
114
115
116 String myReport = theReportBuilder.formatReport();
117
118
119 theEngine.loadContent(myReport);
120
121
122 PrinterJob job = PrinterJob.createPrinterJob();
123 if ((job != null)
124 && job.showPrintDialog(theControl.getStage())) {
125
126 Printer myPrinter = job.getPrinter();
127
128
129 PageLayout myLayout = myPrinter.createPageLayout(Paper.A4, pOrientation, Printer.MarginType.DEFAULT);
130 job.getJobSettings().setPageLayout(myLayout);
131
132
133 theEngine.print(job);
134 job.endJob();
135 }
136
137
138 } catch (PoolException e) {
139 theControl.reportException("Failed to print report", e);
140 }
141 }
142
143
144
145
146
147
148 protected void sourceReport(final TableSource pSource,
149 final File pFile) {
150
151 try {
152
153 theReportBuilder.resetDocument();
154
155
156 pSource.buildReport(theReportBuilder);
157
158
159 String myReport = theReportBuilder.formatReport();
160
161
162 createFileFromString(pFile, myReport);
163
164
165 } catch (PoolException e) {
166 theControl.reportException("Failed to print report", e);
167 }
168 }
169
170
171
172
173
174
175 protected void createWeb(final CompetitionPanel pMaster,
176 final Competition pCompetition) {
177
178 try {
179
180 Path myDir = Paths.get(System.getProperty(AppControl.PROP_USERHOME), "newHtml");
181
182
183 theWebBuilder.setTheTitle(pCompetition.getName());
184
185
186 for (ViewSelect myView : ViewSelect.values()) {
187
188 TableSource mySource = pMaster.getSource(myView);
189
190
191 if (mySource != null) {
192 theWebBuilder.newSection(myView.toString());
193 mySource.createWeb(theWebBuilder);
194 }
195 }
196
197
198 String myReport = theWebBuilder.formatReport();
199
200
201 theWebBuilder.copyWebResources(myDir);
202
203
204 Path myFile = myDir.resolve("competition.html");
205 createFileFromString(myFile.toFile(), myReport);
206
207
208 } catch (PoolException e) {
209 theControl.reportException("Failed to build webSite", e);
210 }
211 }
212
213
214
215
216
217
218
219 protected void createFileFromString(final File pFile,
220 final String pSource) throws PoolException {
221
222 try (PrintStream myOutFile = new PrintStream(pFile)) {
223
224 myOutFile.println(pSource);
225
226
227 } catch (IOException e) {
228 throw new PoolException("Failed to build output file", e);
229 }
230 }
231 }