svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / report / LayoutReportActionImpl.java @ 1616
History | View | Annotate | Download (22.8 KB)
1 | 1531 | jjdelcerro | package org.gvsig.app.project.documents.layout.report; |
---|---|---|---|
2 | |||
3 | 1615 | fdiaz | import java.awt.Color; |
4 | 1532 | jjdelcerro | import java.awt.Dimension; |
5 | 1531 | jjdelcerro | import java.awt.event.ActionEvent; |
6 | 1594 | fdiaz | import java.awt.geom.Point2D; |
7 | 1532 | jjdelcerro | import java.io.File; |
8 | import java.io.InputStream; |
||
9 | 1594 | fdiaz | import java.util.ArrayList; |
10 | import java.util.List; |
||
11 | 1600 | fdiaz | import java.util.Objects; |
12 | 1531 | jjdelcerro | import javax.json.JsonObject; |
13 | 1532 | jjdelcerro | import javax.swing.JComponent; |
14 | 1600 | fdiaz | import javax.swing.JOptionPane; |
15 | import javax.swing.SwingUtilities; |
||
16 | 1532 | jjdelcerro | import org.apache.commons.io.IOUtils; |
17 | 1594 | fdiaz | import org.apache.commons.lang3.StringUtils; |
18 | 1600 | fdiaz | import org.cresques.cts.ICoordTrans; |
19 | 1531 | jjdelcerro | import org.gvsig.andami.ui.mdiManager.IWindow; |
20 | import org.gvsig.app.ApplicationLocator; |
||
21 | import org.gvsig.app.ApplicationManager; |
||
22 | 1532 | jjdelcerro | import org.gvsig.app.project.documents.Document; |
23 | import org.gvsig.app.project.documents.layout.LayoutContext; |
||
24 | import org.gvsig.app.project.documents.layout.fframes.FFrameView; |
||
25 | import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
||
26 | import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
||
27 | 1531 | jjdelcerro | import static org.gvsig.app.project.documents.layout.report.LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME; |
28 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
29 | import org.gvsig.app.project.documents.view.gui.IView; |
||
30 | 1534 | jjdelcerro | import org.gvsig.desktopopen.DesktopOpen; |
31 | 1551 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
32 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
33 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
34 | import org.gvsig.expressionevaluator.MutableSymbolTable; |
||
35 | 1594 | fdiaz | import org.gvsig.fmap.dal.DALLocator; |
36 | import org.gvsig.fmap.dal.exception.DataException; |
||
37 | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
||
38 | 1531 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
39 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
40 | 1551 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
41 | 1531 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSet; |
42 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
43 | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
||
44 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
45 | import org.gvsig.fmap.dal.swing.report.AbstractReportAction; |
||
46 | import org.gvsig.fmap.dal.swing.report.ReportActionFactory; |
||
47 | 1594 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
48 | import org.gvsig.fmap.geom.GeometryLocator; |
||
49 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
50 | 1532 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
51 | import org.gvsig.fmap.mapcontext.MapContext; |
||
52 | 1594 | fdiaz | import org.gvsig.fmap.mapcontext.ViewPort; |
53 | 1537 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
54 | 1531 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
55 | 1601 | fdiaz | import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
56 | 1532 | jjdelcerro | import org.gvsig.pdf.lib.api.PDFDocument; |
57 | import org.gvsig.pdf.lib.api.PDFLocator; |
||
58 | import org.gvsig.pdf.lib.api.PDFManager; |
||
59 | import org.gvsig.pdf.swing.api.PDFSwingLocator; |
||
60 | import org.gvsig.pdf.swing.api.PDFSwingManager; |
||
61 | import org.gvsig.pdf.swing.api.PDFViewer; |
||
62 | 1615 | fdiaz | import org.gvsig.symbology.SymbologyLocator; |
63 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.ISimpleTextSymbol; |
||
64 | 1532 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
65 | 1600 | fdiaz | import org.gvsig.tools.dispose.DisposeUtils; |
66 | import org.gvsig.tools.exception.BaseException; |
||
67 | import org.gvsig.tools.i18n.I18nManager; |
||
68 | 1594 | fdiaz | import org.gvsig.tools.locator.LocatorException; |
69 | 1532 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
70 | import org.gvsig.tools.persistence.PersistentState; |
||
71 | import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
||
72 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
73 | import org.gvsig.tools.swing.api.ToolsSwingUtils; |
||
74 | 1600 | fdiaz | import org.gvsig.tools.swing.api.task.JTaskStatus; |
75 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
76 | 1532 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
77 | 1600 | fdiaz | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
78 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
79 | 1534 | jjdelcerro | import org.gvsig.tools.util.ToolsUtilLocator; |
80 | 1531 | jjdelcerro | |
81 | /**
|
||
82 | *
|
||
83 | * @author jjdelcerro
|
||
84 | */
|
||
85 | @SuppressWarnings("UseSpecificCatch") |
||
86 | 1594 | fdiaz | public class LayoutReportActionImpl extends AbstractReportAction implements LayoutReportAction { |
87 | 1531 | jjdelcerro | |
88 | 1594 | fdiaz | public static LayoutReportActionImpl createFakeLayoutReportAction(Document doc) { |
89 | 1531 | jjdelcerro | ApplicationManager application = ApplicationLocator.getApplicationManager(); |
90 | DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
||
91 | Feature f = null;
|
||
92 | 1551 | jjdelcerro | FeatureStore theStore = null;
|
93 | 1531 | jjdelcerro | ReportActionFactory theFactory = dataSwingManager.getReportActionFactory(LAYOUT_REPORT_ACTION_NAME); |
94 | try {
|
||
95 | IWindow activeView = application.getActiveWindow(ViewDocument.class); |
||
96 | ViewDocument viewdoc = (ViewDocument) ((IView)activeView).getDocument(); |
||
97 | FLyrVect layer = viewdoc.getMapContext().getLayers().getFirstActiveVectorLayer(); |
||
98 | 1551 | jjdelcerro | theStore = layer.getFeatureStore(); |
99 | 1531 | jjdelcerro | if( theStore.isFeatureSelectionEmpty() ) {
|
100 | f = theStore.first(); |
||
101 | } else {
|
||
102 | f = theStore.getFeatureSelection().first(); |
||
103 | } |
||
104 | } catch(Throwable t) { |
||
105 | LOGGER.debug("Can't retrieve test feature",t);
|
||
106 | } |
||
107 | 1594 | fdiaz | LayoutReportActionImpl report = new LayoutReportActionImpl(theFactory, theStore, f);
|
108 | 1551 | jjdelcerro | report.getSymbolTable().setVar("$LAYOUT", doc);
|
109 | 1594 | fdiaz | report.getSymbolTable().setVar("$PAGE", 1); |
110 | report.getSymbolTable().setVar("$PAGES", 1); |
||
111 | 1551 | jjdelcerro | return report;
|
112 | 1531 | jjdelcerro | } |
113 | |||
114 | |||
115 | private final FeatureStore store; |
||
116 | 1601 | fdiaz | private FeatureQuery query;
|
117 | 1551 | jjdelcerro | private FeatureSet selection;
|
118 | 1531 | jjdelcerro | private Feature current_row;
|
119 | 1551 | jjdelcerro | private MutableSymbolTable symbolTable;
|
120 | 1601 | fdiaz | private FeatureSymbolTable fSymbolTable;
|
121 | 1594 | fdiaz | private Iterable<Feature> set; |
122 | private int featuresCount; |
||
123 | 1608 | fdiaz | private LayoutReportActionParameters parameters;
|
124 | 1531 | jjdelcerro | |
125 | 1551 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
126 | 1601 | fdiaz | private LayoutReportActionImpl(ReportActionFactory factory, FeatureStore store, JsonObject json) {
|
127 | super(factory, null); |
||
128 | this.store = store;
|
||
129 | this.selection = null; |
||
130 | this.query = null; |
||
131 | this.current_row = null; |
||
132 | |||
133 | 1608 | fdiaz | this.parameters = LayoutReportActionParameters.create();
|
134 | this.parameters.fromJson(json);
|
||
135 | 1601 | fdiaz | |
136 | 1608 | fdiaz | this.setReportName(this.parameters.getIdentifier()); |
137 | this.setReportLabel(this.parameters.getLabel()); |
||
138 | 1601 | fdiaz | } |
139 | |||
140 | @SuppressWarnings("OverridableMethodCallInConstructor") |
||
141 | 1594 | fdiaz | private LayoutReportActionImpl(ReportActionFactory factory, FeatureStore store, Feature feature) {
|
142 | 1551 | jjdelcerro | // Fake layoutReportAction constructor
|
143 | 1601 | fdiaz | this(
|
144 | factory, |
||
145 | store, |
||
146 | 1608 | fdiaz | LayoutReportActionParameters.create(store.getName()).toJson() |
147 | 1601 | fdiaz | ); |
148 | 1551 | jjdelcerro | try {
|
149 | this.selection = store.createFeatureSelection();
|
||
150 | ((FeatureSelection)this.selection).select(feature);
|
||
151 | } catch(Throwable t) { |
||
152 | LOGGER.debug("Can't initialize selection",t);
|
||
153 | } |
||
154 | this.query = store.createFeatureQuery(feature.createFilter());
|
||
155 | 1531 | jjdelcerro | this.current_row = feature;
|
156 | 1551 | jjdelcerro | |
157 | 1531 | jjdelcerro | } |
158 | |||
159 | 1551 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
160 | 1594 | fdiaz | public LayoutReportActionImpl(ReportActionFactory factory, FeatureStore store, FeatureQuery query, FeatureSet selection, final JsonObject json) { |
161 | 1601 | fdiaz | this(factory, store, json);
|
162 | 1594 | fdiaz | |
163 | 1531 | jjdelcerro | this.selection = selection;
|
164 | this.query = query;
|
||
165 | } |
||
166 | |||
167 | 1532 | jjdelcerro | public Feature row() {
|
168 | return this.current_row; |
||
169 | } |
||
170 | |||
171 | 1594 | fdiaz | private Iterable<Feature> features() { |
172 | if( this.set == null ) { |
||
173 | if(this.selection == null){ |
||
174 | try {
|
||
175 | FeatureSet fset = store.getFeatureSet(query); |
||
176 | this.featuresCount = fset.size();
|
||
177 | this.set = fset;
|
||
178 | } catch (DataException ex) {
|
||
179 | throw new RuntimeException("Can't create dynamic-text report.", ex); |
||
180 | } |
||
181 | } else {
|
||
182 | this.featuresCount = this.selection.size(); |
||
183 | if(this.selection instanceof FeatureSelection){ |
||
184 | this.set = this.store.getFeaturesIterable(((FeatureSelection)this.selection).referenceIterator()); |
||
185 | } else {
|
||
186 | this.set = this.selection; |
||
187 | 1600 | fdiaz | DisposeUtils.bind(this.set);
|
188 | 1594 | fdiaz | } |
189 | } |
||
190 | } |
||
191 | return this.set; |
||
192 | } |
||
193 | |||
194 | |||
195 | 1531 | jjdelcerro | @Override
|
196 | public void actionPerformed(ActionEvent e) { |
||
197 | 1600 | fdiaz | if (!this.isEnabled()) { |
198 | return;
|
||
199 | } |
||
200 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
201 | final WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
||
202 | final JTaskStatus taskStatusPanel = ToolsSwingLocator.getTaskStatusSwingManager().createJTaskStatus();
|
||
203 | final LayoutReportAction report = this; |
||
204 | 1594 | fdiaz | |
205 | 1600 | fdiaz | SimpleTaskStatus theStatus = this.getStatus();
|
206 | theStatus.setIndeterminate(); |
||
207 | taskStatusPanel.setShowRemoveTaskButton(false);
|
||
208 | taskStatusPanel.bind(theStatus); |
||
209 | ToolsSwingUtils.ensureRowsCols(taskStatusPanel, 4, 100, 5, 200); |
||
210 | |||
211 | windowManager.showWindow( |
||
212 | taskStatusPanel.asJComponent(), |
||
213 | i18n.getTranslation("_Creating_report")+" '" + this.getReportLabel() + "'...", |
||
214 | WindowManager.MODE.TOOL |
||
215 | ); |
||
216 | |||
217 | Thread task = new Thread(() -> { |
||
218 | 1594 | fdiaz | List<File> singlePdfFiles = new ArrayList(); |
219 | 1600 | fdiaz | Iterable<Feature> features = null; |
220 | try {
|
||
221 | String title = this.getReportLabel(); |
||
222 | String creator = "gvSIG-desktop"; |
||
223 | String subject = "Layout report"; |
||
224 | |||
225 | File pdfFile = ToolsLocator.getFoldersManager().getUniqueTemporaryFile("report.pdf"); |
||
226 | int pag = 0; |
||
227 | features = features(); |
||
228 | theStatus.setRangeOfValues(1, featuresCount);
|
||
229 | theStatus.message(i18n.getTranslation("_Building_pages"));
|
||
230 | for (Feature feature : features) {
|
||
231 | if( theStatus.isCancellationRequested() ) {
|
||
232 | theStatus.cancel(); |
||
233 | return;
|
||
234 | } |
||
235 | File f = printReport(feature, ++pag);
|
||
236 | singlePdfFiles.add(f); |
||
237 | theStatus.incrementCurrentValue(); |
||
238 | } |
||
239 | |||
240 | theStatus.message(i18n.getTranslation("_Building_pdf"));
|
||
241 | PDFManager pdfManager = PDFLocator.getPDFManager(); |
||
242 | pdfManager.merge(singlePdfFiles, pdfFile, title, creator, subject, theStatus); |
||
243 | |||
244 | 1608 | fdiaz | if (this.parameters.isUseInternalPdfViewer()) { |
245 | 1600 | fdiaz | this.showEmbedded(pdfFile);
|
246 | } else {
|
||
247 | this.showExternal(pdfFile);
|
||
248 | } |
||
249 | |||
250 | theStatus.terminate(); |
||
251 | SwingUtilities.invokeLater(() -> {
|
||
252 | taskStatusPanel.setVisible(false);
|
||
253 | }); |
||
254 | |||
255 | } catch (Throwable t) { |
||
256 | LOGGER.warn("Can't create layout report.", t);
|
||
257 | theStatus.abort(); |
||
258 | } finally {
|
||
259 | for (File singlePdfFile : singlePdfFiles) { |
||
260 | singlePdfFile.delete(); |
||
261 | } |
||
262 | DisposeUtils.disposeQuietly(features); |
||
263 | |||
264 | 1594 | fdiaz | } |
265 | 1600 | fdiaz | }, "Report_" + report.getReportName());
|
266 | task.start(); |
||
267 | |||
268 | 1594 | fdiaz | } |
269 | |||
270 | private File printReport(Feature feature, int page) throws Exception { |
||
271 | this.current_row = feature;
|
||
272 | 1608 | fdiaz | final LayoutPanel layout = this.getTemplateAsLayout(); |
273 | 1594 | fdiaz | final LayoutContext context = layout.getLayoutContext();
|
274 | final Document document = layout.getDocument(); |
||
275 | 1551 | jjdelcerro | |
276 | 1594 | fdiaz | this.getSymbolTable();
|
277 | fSymbolTable.setFeature(current_row); |
||
278 | this.symbolTable.setVar("$LAYOUT", document); |
||
279 | this.symbolTable.setVar("$PAGE", page); |
||
280 | this.symbolTable.setVar("$PAGES", this.featuresCount); |
||
281 | |||
282 | File pdfFile = null; |
||
283 | Object previous_report = document.getProperty("$REPORT"); |
||
284 | 1615 | fdiaz | I18nManager i18n = ToolsLocator.getI18nManager(); |
285 | 1594 | fdiaz | try {
|
286 | document.setProperty("$REPORT", this); |
||
287 | /*
|
||
288 | TODO: Aqui podriamos consultar en el json para ver si hacemos:
|
||
289 | x zoom al envelope de la geometria de current_row,
|
||
290 | x si le aplicamos un buffer antes a ese envelop,
|
||
291 | X si la seleccionamos...
|
||
292 | - asi como con que vista tenemos que hacerlo. ????
|
||
293 | */
|
||
294 | int count = 1; |
||
295 | MapContext mapContext = null;
|
||
296 | MapContext mapContextCandidate = null;
|
||
297 | for (IFFrame frame : context.getAllFFrames()) {
|
||
298 | if (frame instanceof FFrameView) { |
||
299 | 1601 | fdiaz | if (mapContextCandidate == null) { |
300 | 1594 | fdiaz | mapContextCandidate = ((FFrameView) frame).getMapContext(); |
301 | 1551 | jjdelcerro | } |
302 | 1594 | fdiaz | String frameTag = frame.getTag();
|
303 | 1601 | fdiaz | String countTag = "#" + count; |
304 | 1608 | fdiaz | if (StringUtils.equalsIgnoreCase(this.parameters.getViewTag(), frameTag) |
305 | || StringUtils.equalsIgnoreCase(this.parameters.getViewTag(), countTag)) {
|
||
306 | 1594 | fdiaz | mapContext = ((FFrameView) frame).getMapContext(); |
307 | break;
|
||
308 | } |
||
309 | 1532 | jjdelcerro | } |
310 | 1594 | fdiaz | } |
311 | 1601 | fdiaz | if (mapContext == null) { |
312 | 1594 | fdiaz | mapContext = mapContextCandidate; |
313 | }; |
||
314 | 1601 | fdiaz | |
315 | 1594 | fdiaz | if (mapContext != null) { |
316 | 1608 | fdiaz | mapContext.setPrintGraphicsLayer(this.parameters.isHighlightRecord());
|
317 | 1600 | fdiaz | ICoordTrans ct = null;
|
318 | for (FLayer layer : mapContext.getLayers().getLayers(null)) { |
||
319 | if (layer instanceof FLyrVect) { |
||
320 | FeatureStore layerStore = ((FLyrVect) layer).getFeatureStore(); |
||
321 | if (layerStore != null) { |
||
322 | if (layerStore.getParameters().isTheSameStore(this.store.getParameters())) { |
||
323 | ct = ((FLyrVect) layer).getCoordTrans(); |
||
324 | break;
|
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | } |
||
329 | 1601 | fdiaz | Geometry geom = this.current_row.getDefaultGeometry();
|
330 | if (geom != null) { |
||
331 | geom = geom.cloneGeometry(); |
||
332 | if (ct != null && ct.getPOrig().equals(geom.getProjection())) { |
||
333 | geom.reProject(ct); |
||
334 | } else if (!geom.getProjection().equals(mapContext.getProjection())) { |
||
335 | ct = geom.getProjection().getCT(mapContext.getProjection()); |
||
336 | geom.reProject(ct); |
||
337 | } |
||
338 | 1608 | fdiaz | if (this.parameters.isZoomToCurrentRow()) { |
339 | 1594 | fdiaz | |
340 | 1601 | fdiaz | Envelope envelope = geom |
341 | 1608 | fdiaz | .getEnvelope().getGeometry().buffer(this.parameters.getBufferSize()).getEnvelope();
|
342 | 1601 | fdiaz | mapContext.getViewPort().setEnvelope(envelope); |
343 | 1594 | fdiaz | |
344 | 1608 | fdiaz | } else if (this.parameters.isCenterToCurrentRow()) { |
345 | 1594 | fdiaz | |
346 | 1601 | fdiaz | Envelope envelope = geom |
347 | 1608 | fdiaz | .getEnvelope().getGeometry().buffer(this.parameters.getBufferSize()).getEnvelope();
|
348 | 1601 | fdiaz | center(mapContext, envelope); |
349 | 1594 | fdiaz | |
350 | 1601 | fdiaz | } else {
|
351 | //DO NOTHING ??
|
||
352 | } |
||
353 | 1608 | fdiaz | if(this.parameters.isHighlightRecord()){ |
354 | 1601 | fdiaz | GraphicLayer graphicsLayer = mapContext.getGraphicsLayer(); |
355 | graphicsLayer.clear(); |
||
356 | 1608 | fdiaz | int idSymbol = graphicsLayer.addSymbol(this.parameters.getHighlightSymbol(this.store)); |
357 | if(this.parameters.isApplySymbolToRecord()){ |
||
358 | //Do nothing, is the default behavior
|
||
359 | } |
||
360 | if(this.parameters.isApplySymbolToEverythingExceptRecord()){ |
||
361 | 1615 | fdiaz | Geometry viewPortGeom = mapContext.getViewPort().getAdjustedEnvelope().getGeometry(); |
362 | if(geom.isValid()) {
|
||
363 | geom = viewPortGeom.difference(geom); |
||
364 | } else {
|
||
365 | Geometry geom2 = geom.fix(); |
||
366 | if(geom2 != null){ |
||
367 | geom = viewPortGeom.difference(geom2); |
||
368 | } else {
|
||
369 | geom = viewPortGeom; |
||
370 | ISimpleTextSymbol textSymbol = SymbologyLocator.getSymbologyManager().createSimpleTextSymbol(); |
||
371 | textSymbol.setTextColor(Color.RED.darker());
|
||
372 | textSymbol.setHaloColor(Color.WHITE);
|
||
373 | textSymbol.setDrawWithHalo(true);
|
||
374 | |||
375 | int idPointSymbol = graphicsLayer.addSymbol(textSymbol);
|
||
376 | graphicsLayer.addGraphic("LayourReport", geom.centroid(), idPointSymbol, i18n.getTranslation("_Invalid_geometry")); |
||
377 | } |
||
378 | } |
||
379 | 1608 | fdiaz | } |
380 | 1615 | fdiaz | graphicsLayer.addGraphic("LayourReport", geom, idSymbol);
|
381 | 1601 | fdiaz | } |
382 | 1594 | fdiaz | } |
383 | } |
||
384 | 1551 | jjdelcerro | |
385 | 1594 | fdiaz | context.fullRefresh(); |
386 | layout.getLayoutControl().getLayoutDraw().initialize(); |
||
387 | 1551 | jjdelcerro | |
388 | 1594 | fdiaz | pdfFile = ToolsLocator.getFoldersManager().getUniqueTemporaryFile("report.pdf");
|
389 | layout.layoutToPDF(pdfFile); |
||
390 | 1615 | fdiaz | } catch (Throwable th) { |
391 | LOGGER.warn("Can't print report.", th);
|
||
392 | this.getStatus().message(i18n.getTranslation("_Cant_print_report")+"\n\n"+th.getMessage()); |
||
393 | throw th;
|
||
394 | 1594 | fdiaz | } finally {
|
395 | document.setProperty("$REPORT", previous_report);
|
||
396 | } |
||
397 | 1532 | jjdelcerro | |
398 | 1594 | fdiaz | return pdfFile;
|
399 | 1531 | jjdelcerro | } |
400 | |||
401 | 1594 | fdiaz | private void center(MapContext mapContext, Envelope envelope) throws LocatorException, CreateEnvelopeException { |
402 | ViewPort viewPort = mapContext.getViewPort(); |
||
403 | Envelope oldExtent = viewPort.getAdjustedEnvelope(); |
||
404 | double oldCenterX = oldExtent.getCenter(0); |
||
405 | double oldCenterY = oldExtent.getCenter(1); |
||
406 | double centerX = envelope.getCenter(0); |
||
407 | double centerY = envelope.getCenter(1); |
||
408 | Point2D.Double center = new Point2D.Double(centerX, centerY); |
||
409 | double movX = centerX - oldCenterX;
|
||
410 | double movY = centerY - oldCenterY;
|
||
411 | |||
412 | double minx = oldExtent.getMinimum(0) + movX; |
||
413 | double miny = oldExtent.getMinimum(1) + movY; |
||
414 | double maxX = oldExtent.getMaximum(0) + movX; |
||
415 | double maxY = oldExtent.getMaximum(1) + movY; |
||
416 | Envelope extent = GeometryLocator.getGeometryManager().createEnvelope( |
||
417 | minx, miny, |
||
418 | maxX, maxY, |
||
419 | Geometry.SUBTYPES.GEOM2D); |
||
420 | viewPort.setEnvelope(extent); |
||
421 | } |
||
422 | |||
423 | 1600 | fdiaz | private void showEmbedded(File pdfFile) { |
424 | if (!SwingUtilities.isEventDispatchThread()) { |
||
425 | SwingUtilities.invokeLater(() -> {
|
||
426 | showEmbedded(pdfFile); |
||
427 | }); |
||
428 | return;
|
||
429 | } |
||
430 | try {
|
||
431 | final WindowManager windowManager = ToolsSwingLocator.getWindowManager();
|
||
432 | final PDFSwingManager pdfSwingManager = PDFSwingLocator.getPDFSwingManager();
|
||
433 | final PDFManager pdfManager = PDFLocator.getPDFManager();
|
||
434 | |||
435 | final PDFDocument pdfdoc = pdfManager.createPDFDocument(pdfFile);
|
||
436 | PDFViewer viewer = pdfSwingManager.createPDFViewer(); |
||
437 | viewer.put(pdfdoc); |
||
438 | viewer.setMode(PDFViewer.MODE_LIGHT); |
||
439 | |||
440 | JComponent comp = viewer.asJComponent();
|
||
441 | Dimension size = ToolsSwingUtils.ensureRowsCols(comp.getPreferredSize(), 20, 100, 40, 150); |
||
442 | comp.setSize(size); |
||
443 | |||
444 | 1616 | fdiaz | String title = ToolsSwingUtils.makeTitle("Report",this.getLabel(), null); |
445 | 1600 | fdiaz | windowManager.showWindow( |
446 | comp, |
||
447 | title, |
||
448 | WindowManager.MODE.WINDOW |
||
449 | ); |
||
450 | } catch (Exception ex) { |
||
451 | LOGGER.warn("Can't show pdf file '" + Objects.toString(pdfFile) + "'", ex); |
||
452 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
453 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
454 | dialogs.messageDialog( |
||
455 | i18n.getTranslation("_Cant_show_pdf_file")+" (" + Objects.toString(pdfFile) + ")\n\n"+BaseException.getMessageStack(ex, 0), |
||
456 | "Creating report '" + this.getReportLabel() + "'", |
||
457 | JOptionPane.WARNING_MESSAGE
|
||
458 | ); |
||
459 | } |
||
460 | 1534 | jjdelcerro | } |
461 | |||
462 | private void showExternal(File pdfFile) { |
||
463 | 1600 | fdiaz | if (!SwingUtilities.isEventDispatchThread()) { |
464 | SwingUtilities.invokeLater(() -> {
|
||
465 | showExternal(pdfFile); |
||
466 | }); |
||
467 | return;
|
||
468 | } |
||
469 | 1534 | jjdelcerro | DesktopOpen desktop = ToolsUtilLocator.getToolsUtilManager().createDesktopOpen(); |
470 | desktop.open(pdfFile); |
||
471 | } |
||
472 | |||
473 | 1608 | fdiaz | private LayoutPanel getTemplateAsLayout() throws Exception { |
474 | ResourcesStorage.Resource resource = getTemplateAsResource(); |
||
475 | if(resource == null){ |
||
476 | 1532 | jjdelcerro | return null; |
477 | } |
||
478 | InputStream is = null; |
||
479 | try {
|
||
480 | is = resource.asInputStream(); |
||
481 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
482 | PersistentState persistentState = persistenceManager.loadState(is); |
||
483 | |||
484 | LayoutPanel layout = (LayoutPanel) persistenceManager.create(persistentState); |
||
485 | 1594 | fdiaz | |
486 | 1532 | jjdelcerro | return layout;
|
487 | } finally {
|
||
488 | IOUtils.closeQuietly(is); |
||
489 | } |
||
490 | 1531 | jjdelcerro | } |
491 | 1551 | jjdelcerro | |
492 | 1608 | fdiaz | @Override
|
493 | public ResourcesStorage.Resource getTemplateAsResource() {
|
||
494 | ResourcesStorage resources = store.getResourcesStorage(); |
||
495 | if (resources == null) { |
||
496 | return null; |
||
497 | } |
||
498 | String templateName = this.parameters.getTemplateName(); //.getString(LAYOUT_REPORT_TEMPLATE_NAME, LAYOUT_REPORT_DEFAULT_TEMPLATE_NAME); // "1.layout"... |
||
499 | ResourcesStorage.Resource resource = resources.getResource(templateName); |
||
500 | return resource;
|
||
501 | } |
||
502 | |||
503 | 1551 | jjdelcerro | public MutableSymbolTable getSymbolTable() {
|
504 | if( this.symbolTable == null ) { |
||
505 | this.symbolTable = ExpressionUtils.createSymbolTable();
|
||
506 | 1594 | fdiaz | fSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable(); |
507 | fSymbolTable.setFeature(current_row); |
||
508 | this.symbolTable.addSymbolTable(fSymbolTable);
|
||
509 | 1551 | jjdelcerro | this.symbolTable.setVar("$REPORT", this); |
510 | } |
||
511 | return this.symbolTable; |
||
512 | } |
||
513 | |||
514 | 1594 | fdiaz | @Override
|
515 | 1551 | jjdelcerro | public String evaluateDynamicText(String s) { |
516 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager(); |
||
517 | if( !manager.isDynamicText(s) ) {
|
||
518 | return s;
|
||
519 | } |
||
520 | String x = manager.evaluateDynamicText(
|
||
521 | this.getSymbolTable(),
|
||
522 | s, |
||
523 | null,
|
||
524 | this.store.getResourcesStorage()
|
||
525 | ); |
||
526 | return x;
|
||
527 | } |
||
528 | 1594 | fdiaz | |
529 | 1608 | fdiaz | public LayoutReportActionParameters getParameters() {
|
530 | return this.parameters; |
||
531 | 1594 | fdiaz | } |
532 | |||
533 | 1531 | jjdelcerro | } |