View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Print preview.
39   */
40  public class ReportPanel
41          extends StackPane {
42      /**
43       * TableSource interface.
44       */
45      protected interface TableSource {
46                  /**
47                   * Build report.
48                   * @param pBuilder the builder
49                   */
50                  void buildReport(final ReportBuilder pBuilder);
51  
52          /**
53           * Create Web Site.
54           * @param pBuilder the builder
55           */
56                  void createWeb(final ReportBuilder pBuilder);
57      }
58  
59      /**
60       * Application control.
61       */
62      private final AppControl theControl;
63  
64      /**
65       * The Web Engine.
66       */
67      private final WebEngine theEngine;
68  
69      /**
70       * The Print Report Builder.
71       */
72      private final ReportBuilder theReportBuilder;
73  
74      /**
75       * The Web Builder.
76       */
77      private final ReportBuilder theWebBuilder;
78  
79      /**
80       * Constructor.
81       * @param pControl the application control
82       * @throws PoolException on error
83       */
84      protected ReportPanel(final AppControl pControl) throws PoolException {
85          /* Store parameters */
86          theControl = pControl;
87  
88          /* Create a WebView and access the engine */
89          WebView myView = new WebView();
90          theEngine = myView.getEngine();
91  
92          /* Create the report builders */
93          theReportBuilder = new ReportBuilder(false);
94          theWebBuilder = new ReportBuilder(true);
95  
96          /* Add the view to the StackPane */
97          getChildren().add(myView);
98      }
99  
100     /**
101      * Build report.
102      * @param pSource the source viewTable
103      * @param pOrientation the report orientation
104      */
105     protected void printReport(final TableSource pSource,
106                                final PageOrientation pOrientation) {
107         /* Protect against exceptions */
108         try {
109             /* Reset the builder */
110             theReportBuilder.resetDocument();
111 
112             /* Build the report */
113             pSource.buildReport(theReportBuilder);
114 
115             /* Format the report */
116             String myReport = theReportBuilder.formatReport();
117 
118             /* Load the document into the WebView */
119             theEngine.loadContent(myReport);
120 
121             /* Prepare to print the webPage */
122             PrinterJob job = PrinterJob.createPrinterJob();
123             if ((job != null)
124                 && job.showPrintDialog(theControl.getStage())) {
125                 /* Access printer and determine orientation */
126                 Printer myPrinter = job.getPrinter();
127 
128                 /* Create page layout of correct type */
129                 PageLayout myLayout = myPrinter.createPageLayout(Paper.A4, pOrientation, Printer.MarginType.DEFAULT);
130                 job.getJobSettings().setPageLayout(myLayout);
131 
132                 /* Print the WebPage */
133                 theEngine.print(job);
134                 job.endJob();
135             }
136 
137             /* Handle exceptions */
138         } catch (PoolException e) {
139             theControl.reportException("Failed to print report", e);
140         }
141     }
142 
143     /**
144      * Source report.
145      * @param pSource the source viewTable
146      * @param pFile the file to write to
147      */
148     protected void sourceReport(final TableSource pSource,
149                                 final File pFile) {
150         /* Protect against exceptions */
151         try {
152             /* Reset the builder */
153             theReportBuilder.resetDocument();
154 
155             /* Build the report */
156             pSource.buildReport(theReportBuilder);
157 
158             /* Format the report and return */
159             String myReport = theReportBuilder.formatReport();
160 
161             /* Create the report */
162             createFileFromString(pFile, myReport);
163 
164             /* Handle exceptions */
165         } catch (PoolException e) {
166             theControl.reportException("Failed to print report", e);
167         }
168     }
169 
170     /**
171      * create WebSite.
172      * @param pMaster the master competition panel
173      * @param pCompetition the competition
174      */
175     protected void createWeb(final CompetitionPanel pMaster,
176                              final Competition pCompetition) {
177         /* Protect against exceptions */
178         try {
179             /* Obtain the directory for the output */
180             Path myDir = Paths.get(System.getProperty(AppControl.PROP_USERHOME), "newHtml");
181 
182             /* Set the title */
183             theWebBuilder.setTheTitle(pCompetition.getName());
184 
185             /* Loop through the views */
186             for (ViewSelect myView : ViewSelect.values()) {
187                 /* Obtain the source table */
188                 TableSource mySource = pMaster.getSource(myView);
189 
190                 /* Build the section if available */
191                 if (mySource != null) {
192                     theWebBuilder.newSection(myView.toString());
193                     mySource.createWeb(theWebBuilder);
194                 }
195             }
196 
197             /* Format the report and return */
198             String myReport = theWebBuilder.formatReport();
199 
200             /* Copy the various resources */
201             theWebBuilder.copyWebResources(myDir);
202 
203             /* Create the webPage */
204             Path myFile = myDir.resolve("competition.html");
205             createFileFromString(myFile.toFile(), myReport);
206 
207             /* Handle exceptions */
208         } catch (PoolException e) {
209             theControl.reportException("Failed to build webSite", e);
210         }
211     }
212 
213     /**
214      * create File From String.
215      * @param pFile the file to create
216      * @param pSource the source string
217      * @throws PoolException on error
218      */
219     protected void createFileFromString(final File pFile,
220                                         final String pSource) throws PoolException {
221         /* Protect against exceptions */
222         try (PrintStream myOutFile = new PrintStream(pFile)) {
223             /* Write the source string to the file */
224             myOutFile.println(pSource);
225 
226             /* Handle exceptions */
227         } catch (IOException e) {
228             throw new PoolException("Failed to build output file", e);
229         }
230     }
231 }