svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / extension / reportfromlayout / ExportReportToLayout.java @ 1594
History | View | Annotate | Download (14.1 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.app.extension.reportfromlayout; |
7 |
|
8 |
import java.awt.Dimension; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.io.File; |
11 |
import java.io.FileOutputStream; |
12 |
import java.io.InputStream; |
13 |
import java.util.List; |
14 |
import java.util.function.Predicate; |
15 |
import javax.json.JsonObject; |
16 |
import javax.swing.DefaultComboBoxModel; |
17 |
import javax.swing.JOptionPane; |
18 |
import javax.swing.SwingUtilities; |
19 |
import javax.swing.event.ChangeEvent; |
20 |
import javax.swing.event.TreeSelectionEvent; |
21 |
import org.apache.commons.io.IOUtils; |
22 |
import org.apache.commons.lang3.StringUtils; |
23 |
import org.gvsig.app.ApplicationLocator; |
24 |
import org.gvsig.app.project.documents.layout.LayoutManager; |
25 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionImpl; |
26 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionFactory; |
27 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
28 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
29 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
30 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
31 |
import org.gvsig.fmap.dal.swing.storesrepository.StoresRepositoryController; |
32 |
import org.gvsig.i18n.Messages; |
33 |
import org.gvsig.json.Json; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dispose.DisposeUtils; |
36 |
import org.gvsig.tools.i18n.I18nManager; |
37 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
38 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
39 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
40 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
41 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
42 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
43 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
44 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
45 |
import org.gvsig.tools.util.LabeledValue; |
46 |
import org.gvsig.tools.util.LabeledValueImpl; |
47 |
import org.gvsig.utils.GenericFileFilter; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
/**
|
52 |
*
|
53 |
* @author fdiaz
|
54 |
*/
|
55 |
public class ExportReportToLayout extends ExportReportToLayoutPanelView { |
56 |
|
57 |
private static final Logger LOGGER = LoggerFactory.getLogger(ExportReportToLayout.class); |
58 |
|
59 |
private StoresRepositoryController treeSourceController;
|
60 |
private FeatureStore source;
|
61 |
private Dialog dialog; |
62 |
private FilePickerController pickerFile;
|
63 |
|
64 |
public ExportReportToLayout() {
|
65 |
this.initComponents();
|
66 |
|
67 |
} |
68 |
|
69 |
private void initComponents() { |
70 |
|
71 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
72 |
translate(); |
73 |
|
74 |
// fillCboLayout();
|
75 |
|
76 |
this.treeSourceController = dataSwingManager.createStoresRepositoryController(this.treeSource); |
77 |
this.treeSource.addTreeSelectionListener((TreeSelectionEvent e) -> { |
78 |
doChangeSource(); |
79 |
doUpdateComponents(); |
80 |
}); |
81 |
|
82 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
83 |
this.pickerFile = toolsSwingManager.createFilePickerController(txtFile, btnFile);
|
84 |
|
85 |
GenericFileFilter layoutMainExtensionFilter = new GenericFileFilter(
|
86 |
LayoutManager.TEMPLATE_FILE_POINTEXT,
|
87 |
Messages.getText("_Layout_template") + " (*" |
88 |
+ LayoutManager.TEMPLATE_FILE_POINTEXT + ")"); |
89 |
// this.pickerFile.addChoosableFileFilter(layoutMainExtensionFilter);
|
90 |
this.pickerFile.setFileFilter(layoutMainExtensionFilter);
|
91 |
|
92 |
|
93 |
this.pickerFile.addChangeListener((ChangeEvent e) -> { |
94 |
doUpdateComponents(); |
95 |
}); |
96 |
|
97 |
|
98 |
this.cboReport.addActionListener((ActionEvent e) -> { |
99 |
doChangeReport(); |
100 |
}); |
101 |
|
102 |
Dimension screensize = ApplicationLocator.getApplicationManager().getUIManager().getInternalSize();
|
103 |
ToolsSwingUtils.ensureHeightWitdh( |
104 |
this,
|
105 |
Math.max(3*(screensize.height/4), ToolsSwingUtils.rows2px(80)), |
106 |
Math.max(2*(screensize.width/4), ToolsSwingUtils.cols2px(35)), |
107 |
7*(screensize.height/8), |
108 |
3*(screensize.width/4) |
109 |
); |
110 |
|
111 |
SwingUtilities.invokeLater(this::doUpdateComponents); |
112 |
|
113 |
} |
114 |
|
115 |
private void translate() { |
116 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
117 |
manager.translate(lblSource); |
118 |
manager.translate(lblReport); |
119 |
manager.translate(lblFile); |
120 |
} |
121 |
|
122 |
private void doChangeSource() { |
123 |
FeatureStore theSource = this.treeSourceController.getSelectedFeatureStore();
|
124 |
if( theSource==null ) { |
125 |
return;
|
126 |
} |
127 |
if(this.source != null){ |
128 |
DisposeUtils.disposeQuietly(this.source);
|
129 |
} |
130 |
this.source = theSource;
|
131 |
fillCboReport(); |
132 |
} |
133 |
|
134 |
private void doUpdateComponents() { |
135 |
this.treeSourceController.setEnabled(true); |
136 |
|
137 |
this.cboReport.setEnabled(this.source != null); |
138 |
|
139 |
if(this.dialog != null){ |
140 |
boolean enabled = (this.source != null) && (this.pickerFile.get() != null); |
141 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, enabled);
|
142 |
} |
143 |
} |
144 |
|
145 |
private void fillCboReport() { |
146 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
147 |
DefaultComboBoxModel<LabeledValue<ReportAction>> model = new DefaultComboBoxModel(); |
148 |
if(this.source == null) { |
149 |
this.cboReport.setModel(model);
|
150 |
return;
|
151 |
} |
152 |
List<ReportAction> reports = dataSwingManager.getReportActions(source, (Predicate<JsonObject>) (JsonObject t) -> LayoutReportActionFactory.isJsonApplicable(t)); |
153 |
|
154 |
for (ReportAction report : reports) {
|
155 |
model.addElement(new LabeledValueImpl<>(report.getReportLabel(), report));
|
156 |
} |
157 |
cboReport.setModel(model); |
158 |
|
159 |
} |
160 |
|
161 |
// List<Document> getLayouts() {
|
162 |
//
|
163 |
// List<Document> layouts = ApplicationLocator.getProjectManager().getCurrentProject().getDocuments(LayoutDocument.class);
|
164 |
//
|
165 |
// return layouts;
|
166 |
//
|
167 |
// }
|
168 |
|
169 |
|
170 |
|
171 |
// private void fillCboLayout() {
|
172 |
// DefaultComboBoxModel<LabeledValue<LayoutDocument>> model = new DefaultComboBoxModel();
|
173 |
// List<Document> layouts = getLayouts();
|
174 |
//
|
175 |
// if(layouts.size() > 1){
|
176 |
// Collections.sort(layouts, new CompareUtils.NullSafeComparator<Document>() {
|
177 |
// @Override
|
178 |
// public int safeCompare(Document o1, Document o2) {
|
179 |
// return StringUtils.compare(o1.getName(), o2.getName());
|
180 |
// }
|
181 |
// });
|
182 |
// }
|
183 |
//
|
184 |
// for (Document layout : layouts) {
|
185 |
// if(layout instanceof LayoutDocument){
|
186 |
// model.addElement(new LabeledValueImpl<>(layout.getName(), (LayoutDocument)layout));
|
187 |
// }
|
188 |
// }
|
189 |
// cboLayout.setModel(model);
|
190 |
//
|
191 |
// }
|
192 |
|
193 |
private ReportAction getReportAction(){
|
194 |
return (ReportAction) ((LabeledValue)cboReport.getSelectedItem()).getValue();
|
195 |
} |
196 |
|
197 |
// private LayoutDocument getLayoutDocument(){
|
198 |
// return (LayoutDocument) ((LabeledValue)cboLayout.getSelectedItem()).getValue();
|
199 |
// }
|
200 |
|
201 |
public void fetch(ExportReportToLayoutParameters params){ |
202 |
params.setSource(this.source);
|
203 |
params.setReportAction(this.getReportAction());
|
204 |
params.setFile(this.pickerFile.get());
|
205 |
} |
206 |
|
207 |
public boolean doExportReportToLayout(ExportReportToLayoutParameters params) { |
208 |
FeatureStore source = params.getSource(); |
209 |
ReportAction report = params.getReportAction(); |
210 |
File file = params.getFile();
|
211 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
212 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
213 |
|
214 |
|
215 |
if(report == null) { |
216 |
dialogs.messageDialog( |
217 |
i18n.getTranslation("_Report_cant_be_null"),
|
218 |
null,
|
219 |
"_Export_report_to_layout",
|
220 |
JOptionPane.WARNING_MESSAGE);
|
221 |
return false; |
222 |
} |
223 |
|
224 |
if(source == null){ |
225 |
dialogs.messageDialog( |
226 |
i18n.getTranslation("_Source_cant_be_null"),
|
227 |
null,
|
228 |
"_Export_report_to_layout",
|
229 |
JOptionPane.WARNING_MESSAGE);
|
230 |
return false; |
231 |
} |
232 |
|
233 |
if(file == null){ |
234 |
dialogs.messageDialog( |
235 |
i18n.getTranslation("_File_cant_be_null"),
|
236 |
null,
|
237 |
"_Export_report_to_layout",
|
238 |
JOptionPane.WARNING_MESSAGE);
|
239 |
return false; |
240 |
} |
241 |
|
242 |
return doExportReportToLayout(source, report, file);
|
243 |
|
244 |
|
245 |
} |
246 |
|
247 |
public boolean doExportReportToLayout(FeatureStore store, ReportAction report, File templateFile) { |
248 |
|
249 |
|
250 |
ResourcesStorage.Resource resourceReport = null;
|
251 |
ResourcesStorage.Resource resourceLayout = null;
|
252 |
FileOutputStream templateOs = null; |
253 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
254 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
255 |
try {
|
256 |
ResourcesStorage resources = store.getResourcesStorage(); |
257 |
|
258 |
String resourceReportName = getResourceName(resources, report.getReportName());
|
259 |
if (resourceReportName == null) { |
260 |
dialogs.messageDialog( |
261 |
i18n.getTranslation("_Cant_find_resource_XreportnameX",
|
262 |
new String[]{ |
263 |
resourceReportName, |
264 |
} |
265 |
), |
266 |
null,
|
267 |
i18n.getTranslation("_Export_report_to_layout"),
|
268 |
JOptionPane.WARNING_MESSAGE);
|
269 |
return false; |
270 |
} |
271 |
|
272 |
resourceReport = resources.getResource(resourceReportName); |
273 |
|
274 |
JsonObject jsonReport = Json.createObject(resourceReport.asInputStream()); |
275 |
|
276 |
String templateName = jsonReport.getString(LayoutReportActionImpl.LAYOUT_REPORT_TEMPLATE_NAME, LayoutReportActionImpl.LAYOUT_REPORT_DEFAULT_TEMPLATE_NAME);
|
277 |
|
278 |
if(StringUtils.isBlank(templateName)){
|
279 |
|
280 |
dialogs.messageDialog( |
281 |
i18n.getTranslation("_Template_name_is_blank"//, |
282 |
// new String[]{
|
283 |
// resourceReportName,
|
284 |
// }
|
285 |
), |
286 |
null,
|
287 |
i18n.getTranslation("_Export_report_to_layout"),
|
288 |
JOptionPane.WARNING_MESSAGE);
|
289 |
return false; |
290 |
} |
291 |
|
292 |
resourceLayout = resources.getResource(templateName); |
293 |
|
294 |
templateOs = new FileOutputStream(templateFile); |
295 |
IOUtils.copy(resourceLayout.asInputStream(), templateOs); |
296 |
|
297 |
return true; |
298 |
} catch (Exception ex) { |
299 |
dialogs.messageDialog( |
300 |
i18n.getTranslation("_Cant_export_report_to_layout"),
|
301 |
null,
|
302 |
i18n.getTranslation("_Export_report_to_layout"),
|
303 |
JOptionPane.WARNING_MESSAGE);
|
304 |
return false; |
305 |
} finally {
|
306 |
IOUtils.closeQuietly(templateOs); |
307 |
IOUtils.closeQuietly(resourceReport); |
308 |
IOUtils.closeQuietly(resourceLayout); |
309 |
} |
310 |
|
311 |
|
312 |
} |
313 |
|
314 |
// private String getNextResourceName(ResourcesStorage resources, String resourceName){
|
315 |
// List<ResourcesStorage.Resource> reports = resources.getResources(resourceName);
|
316 |
// if(reports == null || reports.isEmpty()){
|
317 |
// return resourceName;
|
318 |
// }
|
319 |
// int n = -1;
|
320 |
// for (ResourcesStorage.Resource report : reports) {
|
321 |
// if(StringUtils.contains(report.getName(), resources.getSeparator())){
|
322 |
// String[] s = StringUtils.split(report.getName(), resources.getSeparator());
|
323 |
// n = Math.max(n, Integer.parseInt(s[0]));
|
324 |
// } else {
|
325 |
// n=0;
|
326 |
// }
|
327 |
// }
|
328 |
//
|
329 |
// if (n < 0) {
|
330 |
// return resourceName;
|
331 |
// }
|
332 |
//
|
333 |
// n++;
|
334 |
// return String.valueOf(n) + resources.getSeparator() + resourceName;
|
335 |
//
|
336 |
//
|
337 |
// }
|
338 |
//
|
339 |
private String getResourceName(ResourcesStorage resources, String reportName) { |
340 |
try {
|
341 |
if (resources != null) { |
342 |
List<ResourcesStorage.Resource> reportsResources = resources.getResources("report"); |
343 |
if (reportsResources != null && !reportsResources.isEmpty()) { |
344 |
for (ResourcesStorage.Resource resource : reportsResources) {
|
345 |
InputStream is = null; |
346 |
try {
|
347 |
is = resource.asInputStream(); |
348 |
JsonObject json = javax.json.Json.createReader(is).readObject(); |
349 |
String name = json.getString(LayoutReportActionImpl.LAYOUT_REPORT_NAME, null); |
350 |
if(StringUtils.equalsIgnoreCase(name, reportName)){
|
351 |
return resource.getName();
|
352 |
} |
353 |
} catch (Exception ex) { |
354 |
LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex); |
355 |
} finally {
|
356 |
IOUtils.closeQuietly(is); |
357 |
} |
358 |
DisposeUtils.disposeQuietly(resource); |
359 |
} |
360 |
} |
361 |
} |
362 |
} finally {
|
363 |
DisposeUtils.disposeQuietly(resources); |
364 |
} |
365 |
return null; |
366 |
} |
367 |
|
368 |
|
369 |
private void doChangeReport() { |
370 |
ReportAction report = getReportAction(); |
371 |
this.pickerFile.set(null); |
372 |
// if(report == null){
|
373 |
// this.txtIdentifier.setText("");
|
374 |
// this.txtLabel.setText("");
|
375 |
// } else {
|
376 |
// this.txtIdentifier.setText(report.getReportName());
|
377 |
// this.txtLabel.setText(report.getReportLabel());
|
378 |
//
|
379 |
// }
|
380 |
} |
381 |
|
382 |
void setDialog(Dialog dialog) { |
383 |
this.dialog = dialog;
|
384 |
} |
385 |
|
386 |
|
387 |
|
388 |
} |