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 / ConfigureReportFromLayout.java @ 1600
History | View | Annotate | Download (19.2 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.awt.event.ActionListener; |
11 |
import java.io.InputStream; |
12 |
import java.nio.charset.StandardCharsets; |
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.JComponent; |
18 |
import javax.swing.JOptionPane; |
19 |
import javax.swing.SwingUtilities; |
20 |
import javax.swing.event.DocumentEvent; |
21 |
import javax.swing.event.DocumentListener; |
22 |
import javax.swing.event.TreeSelectionEvent; |
23 |
import org.apache.commons.io.IOUtils; |
24 |
import org.apache.commons.lang3.StringUtils; |
25 |
import org.gvsig.app.ApplicationLocator; |
26 |
import org.gvsig.app.project.documents.Document; |
27 |
import org.gvsig.app.project.documents.layout.LayoutDocument; |
28 |
import org.gvsig.app.project.documents.layout.fframes.FFrameView; |
29 |
import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
30 |
import org.gvsig.app.project.documents.layout.report.LayoutReportAction; |
31 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionFactory; |
32 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionImpl; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
35 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
36 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
37 |
import org.gvsig.fmap.dal.swing.storesrepository.StoresRepositoryController; |
38 |
import org.gvsig.json.Json; |
39 |
import org.gvsig.json.JsonObjectBuilder; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.dispose.DisposeUtils; |
42 |
import org.gvsig.tools.i18n.I18nManager; |
43 |
import org.gvsig.tools.persistence.PersistenceManager; |
44 |
import org.gvsig.tools.persistence.PersistentState; |
45 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
46 |
import org.gvsig.tools.swing.api.ListElement; |
47 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
50 |
import org.gvsig.tools.swing.api.documentfilters.PositiveDoubleDocumentFilter; |
51 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
52 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
53 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
54 |
import org.gvsig.tools.util.LabeledValue; |
55 |
import org.gvsig.tools.util.LabeledValueImpl; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
*
|
61 |
* @author fdiaz
|
62 |
*/
|
63 |
public class ConfigureReportFromLayout extends ConfigureReportFromLayoutPanelView { |
64 |
|
65 |
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureReportFromLayout.class); |
66 |
|
67 |
private StoresRepositoryController treeSourceController;
|
68 |
private FeatureStore source;
|
69 |
private Dialog dialog; |
70 |
|
71 |
public ConfigureReportFromLayout() {
|
72 |
this.initComponents();
|
73 |
|
74 |
} |
75 |
|
76 |
private void initComponents() { |
77 |
|
78 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
79 |
translate(); |
80 |
|
81 |
fillCboLayout(); |
82 |
|
83 |
this.cboLayout.addActionListener(new ActionListener() { |
84 |
@Override
|
85 |
public void actionPerformed(ActionEvent e) { |
86 |
doChangeLayout(); |
87 |
} |
88 |
}); |
89 |
|
90 |
this.treeSourceController = dataSwingManager.createStoresRepositoryController(this.treeSource); |
91 |
this.treeSource.addTreeSelectionListener((TreeSelectionEvent e) -> { |
92 |
doChangeSource(); |
93 |
doUpdateComponents(); |
94 |
}); |
95 |
|
96 |
this.cboReport.addActionListener((ActionEvent e) -> { |
97 |
doChangeReport(); |
98 |
}); |
99 |
|
100 |
this.txtIdentifier.getDocument().addDocumentListener(new DocumentListener() { |
101 |
@Override
|
102 |
public void insertUpdate(DocumentEvent e) { |
103 |
doUpdateComponents(); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void removeUpdate(DocumentEvent e) { |
108 |
doUpdateComponents(); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void changedUpdate(DocumentEvent e) { |
113 |
doUpdateComponents(); |
114 |
} |
115 |
}); |
116 |
|
117 |
this.chkZoomToCurrentRow.addActionListener(new ActionListener() { |
118 |
@Override
|
119 |
public void actionPerformed(ActionEvent e) { |
120 |
doUpdateComponents(); |
121 |
} |
122 |
}); |
123 |
|
124 |
PositiveDoubleDocumentFilter.install(txtBufferSize); |
125 |
|
126 |
Dimension screensize = ApplicationLocator.getApplicationManager().getUIManager().getInternalSize();
|
127 |
ToolsSwingUtils.ensureHeightWitdh( |
128 |
this,
|
129 |
Math.max(3*(screensize.height/4), ToolsSwingUtils.rows2px(80)), |
130 |
Math.max(2*(screensize.width/4), ToolsSwingUtils.cols2px(35)), |
131 |
7*(screensize.height/8), |
132 |
3*(screensize.width/4) |
133 |
); |
134 |
|
135 |
SwingUtilities.invokeLater(this::doUpdateComponents); |
136 |
|
137 |
} |
138 |
|
139 |
private void doChangeSource() { |
140 |
FeatureStore theSource = this.treeSourceController.getSelectedFeatureStore();
|
141 |
if( theSource==null ) { |
142 |
return;
|
143 |
} |
144 |
if(this.source != null){ |
145 |
DisposeUtils.disposeQuietly(this.source);
|
146 |
} |
147 |
this.source = theSource;
|
148 |
fillCboReport(); |
149 |
doUpdateComponents(); |
150 |
} |
151 |
|
152 |
private void doChangeLayout() { |
153 |
fillCboView(); |
154 |
doUpdateComponents(); |
155 |
} |
156 |
|
157 |
private void doUpdateComponents() { |
158 |
this.treeSourceController.setEnabled(true); |
159 |
|
160 |
this.cboReport.setEnabled(this.source != null); |
161 |
|
162 |
this.txtBufferSize.setEnabled(this.chkZoomToCurrentRow.isSelected()); |
163 |
|
164 |
if(this.dialog != null){ |
165 |
boolean enabled = (this.source != null) && (this.getLayoutDocument() != null) && StringUtils.isNotBlank(this.txtIdentifier.getText()); |
166 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, enabled);
|
167 |
} |
168 |
} |
169 |
|
170 |
private void fillCboReport() { |
171 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
172 |
DefaultComboBoxModel<LabeledValue<ReportAction>> model = new DefaultComboBoxModel(); |
173 |
if(this.source == null) { |
174 |
this.cboReport.setModel(model);
|
175 |
return;
|
176 |
} |
177 |
List<ReportAction> reports = dataSwingManager.getReportActions(source, (Predicate<JsonObject>) (JsonObject t) -> LayoutReportActionFactory.isJsonApplicable(t)); |
178 |
|
179 |
model.addElement(new LabeledValueImpl<>("", null)); |
180 |
for (ReportAction report : reports) {
|
181 |
model.addElement(new LabeledValueImpl<>(report.getReportLabel(), report));
|
182 |
} |
183 |
cboReport.setModel(model); |
184 |
|
185 |
} |
186 |
|
187 |
List<Document> getLayouts() { |
188 |
|
189 |
List<Document> layouts = ApplicationLocator.getProjectManager().getCurrentProject().getDocuments(LayoutDocument.class); |
190 |
|
191 |
return layouts;
|
192 |
|
193 |
} |
194 |
|
195 |
|
196 |
|
197 |
private void fillCboLayout() { |
198 |
DefaultComboBoxModel<LabeledValue<LayoutDocument>> model = new DefaultComboBoxModel(); |
199 |
List<Document> layouts = getLayouts(); |
200 |
|
201 |
for (Document layout : layouts) { |
202 |
if(layout instanceof LayoutDocument){ |
203 |
model.addElement(new LabeledValueImpl<>(layout.getName(), (LayoutDocument)layout));
|
204 |
} |
205 |
} |
206 |
cboLayout.setModel(model); |
207 |
fillCboView(); |
208 |
|
209 |
} |
210 |
|
211 |
private void fillCboView() { |
212 |
LayoutDocument layout = getLayoutDocument(); |
213 |
DefaultComboBoxModel<LabeledValue<String>> model = new DefaultComboBoxModel(); |
214 |
if (layout != null) { |
215 |
int n = 1; |
216 |
for (IFFrame frame : layout.getLayoutContext().getAllFFrames()) {
|
217 |
if (frame instanceof FFrameView) { |
218 |
String tag = frame.getTag();
|
219 |
if(StringUtils.startsWith(tag, "#") && !StringUtils.equals(tag, "#"+n)){ |
220 |
model.addElement(new LabeledValueImpl<>(tag+" (invalid tag)",tag)); |
221 |
} else {
|
222 |
if(StringUtils.isBlank(tag)){
|
223 |
tag = "#"+n;
|
224 |
} |
225 |
model.addElement(new LabeledValueImpl<>(tag,tag));
|
226 |
} |
227 |
n++; |
228 |
} |
229 |
} |
230 |
} |
231 |
this.cboView.setModel(model);
|
232 |
} |
233 |
|
234 |
private LayoutReportAction getReportAction(){
|
235 |
LabeledValue<LayoutReportAction> selected = (LabeledValue<LayoutReportAction>)cboReport.getSelectedItem(); |
236 |
if(selected == null){ |
237 |
return null; |
238 |
} |
239 |
return selected.getValue();
|
240 |
} |
241 |
|
242 |
private LayoutDocument getLayoutDocument(){
|
243 |
LabeledValue<LayoutDocument> selected = (LabeledValue<LayoutDocument>)cboLayout.getSelectedItem(); |
244 |
if(selected == null){ |
245 |
return null; |
246 |
} |
247 |
return selected.getValue();
|
248 |
} |
249 |
|
250 |
private String getViewTag(){ |
251 |
LabeledValue<String> selected = (LabeledValue<String>)cboView.getSelectedItem(); |
252 |
if(selected == null){ |
253 |
return null; |
254 |
} |
255 |
return selected.getValue();
|
256 |
} |
257 |
|
258 |
public void fetch(ConfigureReportFromLayoutParameters params){ |
259 |
params.setLayout(this.getLayoutDocument());
|
260 |
params.setSource(this.source);
|
261 |
params.setIdentifier(this.txtIdentifier.getText());
|
262 |
params.setLabel(this.txtLabel.getText());
|
263 |
|
264 |
params.setCenterToCurrentRow(this.chkCenterToCurrentRow.isSelected());
|
265 |
params.setZoomToCurrentRow(this.chkZoomToCurrentRow.isSelected());
|
266 |
double bufferSize = PositiveDoubleDocumentFilter.getValue(this.txtBufferSize); |
267 |
params.setBufferSize(bufferSize); |
268 |
// params.setSelectCurrentRow(this.chkSelectCurrentRow.isSelected());
|
269 |
params.setViewTag(this.getViewTag());
|
270 |
params.setUseIntervalPdfViewer(this.chkUseInternalPdfViewer.isSelected());
|
271 |
} |
272 |
|
273 |
private void translate() { |
274 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
275 |
manager.translate(lblLayout); |
276 |
manager.translate(lblSource); |
277 |
manager.translate(lblReport); |
278 |
manager.translate(lblIdentifier); |
279 |
manager.translate(lblLabel); |
280 |
manager.translate(lblViewSeparator); |
281 |
manager.translate(lblView); |
282 |
manager.translate(chkCenterToCurrentRow); |
283 |
manager.translate(chkZoomToCurrentRow); |
284 |
manager.translate(lblBufferSize); |
285 |
// manager.translate(chkSelectCurrentRow);
|
286 |
manager.translate(chkUseInternalPdfViewer); |
287 |
manager.translate(txtBufferSize); |
288 |
} |
289 |
|
290 |
public boolean doConfigureReportFromLayout(ConfigureReportFromLayoutParameters params) { |
291 |
LayoutDocument layout = params.getLayout(); |
292 |
FeatureStore source = params.getSource(); |
293 |
String identifier = params.getIdentifier();
|
294 |
String label = params.getLabel();
|
295 |
|
296 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
297 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
298 |
|
299 |
if(layout == null){ |
300 |
dialogs.messageDialog( |
301 |
i18n.getTranslation("_Layout_cant_be_null"),
|
302 |
null,
|
303 |
"_Configure_report_from_layout",
|
304 |
JOptionPane.WARNING_MESSAGE);
|
305 |
return false; |
306 |
} |
307 |
|
308 |
if(source == null){ |
309 |
dialogs.messageDialog( |
310 |
i18n.getTranslation("_Source_cant_be_null"),
|
311 |
null,
|
312 |
"_Configure_report_from_layout",
|
313 |
JOptionPane.WARNING_MESSAGE);
|
314 |
return false; |
315 |
} |
316 |
|
317 |
if(StringUtils.isBlank(identifier)){
|
318 |
dialogs.messageDialog( |
319 |
i18n.getTranslation("_Identifier_cant_be_null"),
|
320 |
null,
|
321 |
"_Configure_report_from_layout",
|
322 |
JOptionPane.WARNING_MESSAGE);
|
323 |
return false; |
324 |
} |
325 |
|
326 |
boolean centerToCurrentRow = params.isCenterToCurrentRow();
|
327 |
boolean zoomToCurrentRow = params.isZoomToCurrentRow();
|
328 |
double bufferSize = params.getBufferSize();
|
329 |
boolean selectCurrentRow = params.isSelectCurrentRow();
|
330 |
boolean useInternalPdfViewer = params.isUseIntervalPdfViewer();
|
331 |
String viewTag = params.getViewTag();
|
332 |
|
333 |
return doConfigureReportFromLayout(
|
334 |
source, |
335 |
layout, |
336 |
identifier, |
337 |
label, |
338 |
centerToCurrentRow, |
339 |
zoomToCurrentRow, |
340 |
bufferSize, |
341 |
selectCurrentRow, |
342 |
viewTag, |
343 |
useInternalPdfViewer); |
344 |
|
345 |
|
346 |
} |
347 |
|
348 |
public boolean doConfigureReportFromLayout( |
349 |
FeatureStore store, |
350 |
LayoutDocument layout, |
351 |
String name,
|
352 |
String label,
|
353 |
boolean centerToCurrentRow,
|
354 |
boolean zoomToCurrentRow,
|
355 |
double bufferSize,
|
356 |
boolean selectCurrentRow,
|
357 |
String viewTag,
|
358 |
boolean useInternalPdfViewer
|
359 |
) { |
360 |
ResourcesStorage.Resource resource = null;
|
361 |
ResourcesStorage.Resource resourceLayout = null;
|
362 |
boolean selfContained = layout.isPersistIndependent();
|
363 |
try {
|
364 |
ResourcesStorage resources = store.getResourcesStorage(); |
365 |
|
366 |
String resourceName = getResourceName(resources, name);
|
367 |
if(resourceName == null){ |
368 |
resourceName = getNextResourceName(resources, "report");
|
369 |
} |
370 |
|
371 |
String templateName = LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME;
|
372 |
if(StringUtils.contains(resourceName, resources.getSeparator())){
|
373 |
templateName = StringUtils.replaceIgnoreCase(resourceName, resources.getSeparator()+"report", resources.getSeparator()+LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME);
|
374 |
} |
375 |
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); |
376 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_NAME, name); |
377 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_TYPE, LayoutReportActionFactory.LAYOUT_REPORT_ACTION_NAME); |
378 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_LABEL, label); |
379 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_TEMPLATE_NAME, templateName); |
380 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_CENTER_TO_CURRENT_ROW, centerToCurrentRow); |
381 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_ZOOM_TO_CURRENT_ROW, zoomToCurrentRow); |
382 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_BUFFER_SIZE, bufferSize); |
383 |
// jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_SELECT_CURRENT_ROW, selectCurrentRow);
|
384 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_VIEW_TAG, viewTag); |
385 |
jsonBuilder.add(LayoutReportActionImpl.LAYOUT_REPORT_USE_INTERNAL_PDF_VIEWER, useInternalPdfViewer); |
386 |
|
387 |
resource = resources.getResource(resourceName); |
388 |
IOUtils.write(jsonBuilder.toString(), resource.asOutputStream(), StandardCharsets.UTF_8); |
389 |
|
390 |
resourceLayout = resources.getResource(templateName); |
391 |
PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
392 |
layout.setPersistIndependent(true);
|
393 |
PersistentState persistentState = persistenceManager.getState(layout.getMainWindow()); |
394 |
persistenceManager.saveState(persistentState, resourceLayout.asOutputStream()); |
395 |
((JComponent)dialog).setVisible(false); |
396 |
return true; |
397 |
} catch (Exception ex) { |
398 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
399 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
400 |
dialogs.messageDialog( |
401 |
i18n.getTranslation("_Cant_configure_report_from_layout"),
|
402 |
null,
|
403 |
"_Configure_report_from_layout",
|
404 |
JOptionPane.WARNING_MESSAGE);
|
405 |
return false; |
406 |
} finally {
|
407 |
layout.setPersistIndependent(selfContained); |
408 |
IOUtils.closeQuietly(resource); |
409 |
IOUtils.closeQuietly(resourceLayout); |
410 |
} |
411 |
|
412 |
} |
413 |
|
414 |
private String getNextResourceName(ResourcesStorage resources, String resourceName){ |
415 |
List<ResourcesStorage.Resource> reports = resources.getResources(resourceName);
|
416 |
if(reports == null || reports.isEmpty()){ |
417 |
return resourceName;
|
418 |
} |
419 |
int n = -1; |
420 |
for (ResourcesStorage.Resource report : reports) {
|
421 |
if(StringUtils.contains(report.getName(), resources.getSeparator())){
|
422 |
String[] s = StringUtils.split(report.getName(), resources.getSeparator()); |
423 |
n = Math.max(n, Integer.parseInt(s[0])); |
424 |
} else {
|
425 |
n=0;
|
426 |
} |
427 |
} |
428 |
|
429 |
if (n < 0) { |
430 |
return resourceName;
|
431 |
} |
432 |
|
433 |
n++; |
434 |
return String.valueOf(n) + resources.getSeparator() + resourceName; |
435 |
|
436 |
|
437 |
} |
438 |
|
439 |
private String getResourceName(ResourcesStorage resources, String reportName) { |
440 |
try {
|
441 |
if (resources != null) { |
442 |
List<ResourcesStorage.Resource> reportsResources = resources.getResources("report"); |
443 |
if (reportsResources != null && !reportsResources.isEmpty()) { |
444 |
for (ResourcesStorage.Resource resource : reportsResources) {
|
445 |
InputStream is = null; |
446 |
try {
|
447 |
is = resource.asInputStream(); |
448 |
JsonObject json = javax.json.Json.createReader(is).readObject(); |
449 |
String name = json.getString(LayoutReportActionImpl.LAYOUT_REPORT_NAME, null); |
450 |
if(StringUtils.equalsIgnoreCase(name, reportName)){
|
451 |
return resource.getName();
|
452 |
} |
453 |
} catch (Exception ex) { |
454 |
LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex); |
455 |
} finally {
|
456 |
IOUtils.closeQuietly(is); |
457 |
} |
458 |
DisposeUtils.disposeQuietly(resource); |
459 |
} |
460 |
} |
461 |
} |
462 |
} finally {
|
463 |
DisposeUtils.disposeQuietly(resources); |
464 |
} |
465 |
return null; |
466 |
} |
467 |
|
468 |
|
469 |
private void doChangeReport() { |
470 |
LayoutReportAction report = getReportAction(); |
471 |
if(report == null){ |
472 |
this.txtIdentifier.setText(""); |
473 |
this.txtLabel.setText(""); |
474 |
} else {
|
475 |
this.txtIdentifier.setText(report.getReportName());
|
476 |
this.txtLabel.setText(report.getReportLabel());
|
477 |
this.chkCenterToCurrentRow.setSelected(report.isCenterToCurrentRow());
|
478 |
this.chkZoomToCurrentRow.setSelected(report.isZoomToCurrentRow());
|
479 |
PositiveDoubleDocumentFilter.setValue(this.txtBufferSize,report.getBufferSize());
|
480 |
// this.chkSelectCurrentRow.setSelected(report.isSelectCurrentRow());
|
481 |
ListElement.setSelected(this.cboView, report.getViewTag());
|
482 |
this.chkUseInternalPdfViewer.setSelected(report.isUseInternalPdfViewer());
|
483 |
} |
484 |
doUpdateComponents(); |
485 |
} |
486 |
|
487 |
void setDialog(Dialog dialog) { |
488 |
this.dialog = dialog;
|
489 |
} |
490 |
|
491 |
|
492 |
|
493 |
} |