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 @ 1608
History | View | Annotate | Download (18.4 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.util.List; |
12 |
import java.util.function.Predicate; |
13 |
import javax.json.JsonObject; |
14 |
import javax.swing.DefaultComboBoxModel; |
15 |
import javax.swing.JComponent; |
16 |
import javax.swing.JOptionPane; |
17 |
import javax.swing.SwingUtilities; |
18 |
import javax.swing.event.ChangeEvent; |
19 |
import javax.swing.event.DocumentEvent; |
20 |
import javax.swing.event.DocumentListener; |
21 |
import javax.swing.event.TreeSelectionEvent; |
22 |
import org.apache.commons.lang3.StringUtils; |
23 |
import org.gvsig.app.ApplicationLocator; |
24 |
import org.gvsig.app.project.documents.Document; |
25 |
import org.gvsig.app.project.documents.layout.LayoutDocument; |
26 |
import org.gvsig.app.project.documents.layout.fframes.FFrameView; |
27 |
import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
28 |
import org.gvsig.app.project.documents.layout.report.LayoutReportAction; |
29 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionFactory; |
30 |
import org.gvsig.app.project.documents.layout.report.LayoutReportActionParameters; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
33 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
34 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
35 |
import org.gvsig.fmap.dal.swing.storesrepository.StoresRepositoryController; |
36 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
37 |
import org.gvsig.symbology.swing.SymbologySwingLocator; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dispose.DisposeUtils; |
40 |
import org.gvsig.tools.i18n.I18nManager; |
41 |
import org.gvsig.tools.swing.api.ListElement; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
44 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
45 |
import org.gvsig.tools.swing.api.documentfilters.PositiveDoubleDocumentFilter; |
46 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
47 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
48 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
49 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
50 |
import org.gvsig.tools.util.LabeledValue; |
51 |
import org.gvsig.tools.util.LabeledValueImpl; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
/**
|
56 |
*
|
57 |
* @author fdiaz
|
58 |
*/
|
59 |
public class ConfigureReportFromLayout extends ConfigureReportFromLayoutPanelView { |
60 |
|
61 |
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureReportFromLayout.class); |
62 |
private static final int TO_RECORD_VALUE = 1; |
63 |
private static final int TO_EVERYTIHNG_EXCEPT_RECORD_VALUE = 2; |
64 |
|
65 |
private StoresRepositoryController treeSourceController;
|
66 |
private FeatureStore source;
|
67 |
private Dialog dialog; |
68 |
private ISymbol highlightSymbol;
|
69 |
private PickerController<ISymbol> symbolPicker;
|
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((ActionEvent e) -> { |
118 |
doUpdateComponents(); |
119 |
}); |
120 |
|
121 |
this.chkHighlightRecord.addActionListener((ActionEvent e) -> { |
122 |
doUpdateComponents(); |
123 |
}); |
124 |
|
125 |
fillCboApplyTo(); |
126 |
|
127 |
this.symbolPicker = SymbologySwingLocator.getSwingManager().createSymbolPickerController(btnSelectSymbol, highlightSymbol);
|
128 |
this.symbolPicker.addChangeListener((ChangeEvent e) -> { |
129 |
this.highlightSymbol = this.symbolPicker.get(); |
130 |
}); |
131 |
|
132 |
PositiveDoubleDocumentFilter.install(txtBufferSize); |
133 |
|
134 |
Dimension screensize = ApplicationLocator.getApplicationManager().getUIManager().getInternalSize();
|
135 |
ToolsSwingUtils.ensureHeightWitdh( |
136 |
this,
|
137 |
Math.max(3*(screensize.height/4), ToolsSwingUtils.rows2px(80)), |
138 |
Math.max(2*(screensize.width/4), ToolsSwingUtils.cols2px(35)), |
139 |
7*(screensize.height/8), |
140 |
3*(screensize.width/4) |
141 |
); |
142 |
|
143 |
SwingUtilities.invokeLater(this::doUpdateComponents); |
144 |
|
145 |
} |
146 |
|
147 |
private void doChangeSource() { |
148 |
FeatureStore theSource = this.treeSourceController.getSelectedFeatureStore();
|
149 |
if( theSource==null ) { |
150 |
return;
|
151 |
} |
152 |
if(this.source != null){ |
153 |
DisposeUtils.disposeQuietly(this.source);
|
154 |
} |
155 |
this.source = theSource;
|
156 |
fillCboReport(); |
157 |
doUpdateComponents(); |
158 |
} |
159 |
|
160 |
private void doChangeLayout() { |
161 |
fillCboView(); |
162 |
doUpdateComponents(); |
163 |
} |
164 |
|
165 |
// private void doChangeApplySymbolTo() {
|
166 |
// Object item = cboApplySymbolTo.getSelectedItem();
|
167 |
// ((LabeledValue)item)
|
168 |
// doUpdateComponents();
|
169 |
// }
|
170 |
|
171 |
private void doUpdateComponents() { |
172 |
this.treeSourceController.setEnabled(true); |
173 |
|
174 |
this.cboReport.setEnabled(this.source != null); |
175 |
|
176 |
this.txtBufferSize.setEnabled(this.chkZoomToCurrentRow.isSelected()); |
177 |
|
178 |
this.btnSelectSymbol.setEnabled(this.chkHighlightRecord.isSelected()); |
179 |
this.cboApplySymbolTo.setEnabled(this.chkHighlightRecord.isSelected()); |
180 |
|
181 |
if(this.dialog != null){ |
182 |
boolean enabled = (this.source != null) && (this.getLayoutDocument() != null) && StringUtils.isNotBlank(this.txtIdentifier.getText()); |
183 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, enabled);
|
184 |
} |
185 |
} |
186 |
|
187 |
private void fillCboReport() { |
188 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
189 |
DefaultComboBoxModel<LabeledValue<ReportAction>> model = new DefaultComboBoxModel(); |
190 |
if(this.source == null) { |
191 |
this.cboReport.setModel(model);
|
192 |
return;
|
193 |
} |
194 |
List<ReportAction> reports = dataSwingManager.getReportActions(source, (Predicate<JsonObject>) (JsonObject t) -> LayoutReportActionFactory.isJsonApplicable(t)); |
195 |
|
196 |
model.addElement(new LabeledValueImpl<>("", null)); |
197 |
for (ReportAction report : reports) {
|
198 |
model.addElement(new LabeledValueImpl<>(report.getReportLabel(), report));
|
199 |
} |
200 |
cboReport.setModel(model); |
201 |
|
202 |
} |
203 |
|
204 |
List<Document> getLayouts() { |
205 |
|
206 |
List<Document> layouts = ApplicationLocator.getProjectManager().getCurrentProject().getDocuments(LayoutDocument.class); |
207 |
|
208 |
return layouts;
|
209 |
|
210 |
} |
211 |
|
212 |
|
213 |
|
214 |
private void fillCboLayout() { |
215 |
DefaultComboBoxModel<LabeledValue<LayoutDocument>> model = new DefaultComboBoxModel(); |
216 |
List<Document> layouts = getLayouts(); |
217 |
|
218 |
for (Document layout : layouts) { |
219 |
if(layout instanceof LayoutDocument){ |
220 |
model.addElement(new LabeledValueImpl<>(layout.getName(), (LayoutDocument)layout));
|
221 |
} |
222 |
} |
223 |
cboLayout.setModel(model); |
224 |
fillCboView(); |
225 |
|
226 |
} |
227 |
|
228 |
private void fillCboView() { |
229 |
LayoutDocument layout = getLayoutDocument(); |
230 |
DefaultComboBoxModel<LabeledValue<String>> model = new DefaultComboBoxModel(); |
231 |
if (layout != null) { |
232 |
int n = 1; |
233 |
for (IFFrame frame : layout.getLayoutContext().getAllFFrames()) {
|
234 |
if (frame instanceof FFrameView) { |
235 |
String tag = frame.getTag();
|
236 |
if(StringUtils.startsWith(tag, "#") && !StringUtils.equals(tag, "#"+n)){ |
237 |
model.addElement(new LabeledValueImpl<>(tag+" (invalid tag)",tag)); |
238 |
} else {
|
239 |
if(StringUtils.isBlank(tag)){
|
240 |
tag = "#"+n;
|
241 |
} |
242 |
model.addElement(new LabeledValueImpl<>(tag,tag));
|
243 |
} |
244 |
n++; |
245 |
} |
246 |
} |
247 |
} |
248 |
this.cboView.setModel(model);
|
249 |
} |
250 |
|
251 |
private void fillCboApplyTo() { |
252 |
DefaultComboBoxModel<LabeledValue<Integer>> model = new DefaultComboBoxModel(); |
253 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
254 |
|
255 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_to_record"), TO_RECORD_VALUE)); |
256 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_to_everything_except_record"), TO_EVERYTIHNG_EXCEPT_RECORD_VALUE)); |
257 |
|
258 |
this.cboApplySymbolTo.setModel(model);
|
259 |
} |
260 |
|
261 |
|
262 |
private LayoutReportAction getReportAction(){
|
263 |
LabeledValue<LayoutReportAction> selected = (LabeledValue<LayoutReportAction>)cboReport.getSelectedItem(); |
264 |
if(selected == null){ |
265 |
return null; |
266 |
} |
267 |
return selected.getValue();
|
268 |
} |
269 |
|
270 |
public LayoutDocument getLayoutDocument(){
|
271 |
LabeledValue<LayoutDocument> selected = (LabeledValue<LayoutDocument>)cboLayout.getSelectedItem(); |
272 |
if(selected == null){ |
273 |
return null; |
274 |
} |
275 |
return selected.getValue();
|
276 |
} |
277 |
|
278 |
private String getViewTag(){ |
279 |
LabeledValue<String> selected = (LabeledValue<String>)cboView.getSelectedItem(); |
280 |
if(selected == null){ |
281 |
return null; |
282 |
} |
283 |
return selected.getValue();
|
284 |
} |
285 |
|
286 |
public void fetch(LayoutReportActionParameters params){ |
287 |
// params.setLayout(this.getLayoutDocument());
|
288 |
// params.setSource(this.source);
|
289 |
params.setIdentifier(this.txtIdentifier.getText());
|
290 |
params.setLabel(this.txtLabel.getText());
|
291 |
|
292 |
params.setCenterToCurrentRow(this.chkCenterToCurrentRow.isSelected());
|
293 |
params.setZoomToCurrentRow(this.chkZoomToCurrentRow.isSelected());
|
294 |
double bufferSize = PositiveDoubleDocumentFilter.getValue(this.txtBufferSize); |
295 |
params.setBufferSize(bufferSize); |
296 |
params.setViewTag(this.getViewTag());
|
297 |
params.setUseInternalPdfViewer(this.chkUseInternalPdfViewer.isSelected());
|
298 |
params.setHighlightRecord(this.chkHighlightRecord.isSelected());
|
299 |
params.setHighlightSymbol(this.highlightSymbol);
|
300 |
Object applyToItem = this.cboApplySymbolTo.getSelectedItem(); |
301 |
if(applyToItem == null){ |
302 |
params.setApplySymbolToRecord(false);
|
303 |
params.setApplySymbolToEverythingExceptRecord(false);
|
304 |
} else {
|
305 |
switch ((int)((LabeledValue)applyToItem).getValue()) { |
306 |
case TO_RECORD_VALUE:
|
307 |
params.setApplySymbolToRecord(true);
|
308 |
params.setApplySymbolToEverythingExceptRecord(false);
|
309 |
break;
|
310 |
case TO_EVERYTIHNG_EXCEPT_RECORD_VALUE:
|
311 |
params.setApplySymbolToRecord(false);
|
312 |
params.setApplySymbolToEverythingExceptRecord(true);
|
313 |
break;
|
314 |
default:
|
315 |
params.setApplySymbolToRecord(false);
|
316 |
params.setApplySymbolToEverythingExceptRecord(false);
|
317 |
} |
318 |
} |
319 |
} |
320 |
|
321 |
private void translate() { |
322 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
323 |
manager.translate(lblLayout); |
324 |
manager.translate(lblSource); |
325 |
manager.translate(lblReport); |
326 |
manager.translate(lblIdentifier); |
327 |
manager.translate(lblLabel); |
328 |
manager.translate(lblViewSeparator); |
329 |
manager.translate(lblView); |
330 |
manager.translate(chkCenterToCurrentRow); |
331 |
manager.translate(chkZoomToCurrentRow); |
332 |
manager.translate(lblBufferSize); |
333 |
manager.translate(chkUseInternalPdfViewer); |
334 |
manager.translate(txtBufferSize); |
335 |
manager.translate(chkHighlightRecord); |
336 |
manager.translate(btnSelectSymbol); |
337 |
manager.translate(lblAppySymbolTo); |
338 |
manager.translate(cboApplySymbolTo); |
339 |
} |
340 |
|
341 |
public boolean doConfigureReportFromLayout(LayoutReportActionParameters params, LayoutDocument layout, FeatureStore source) { |
342 |
// LayoutDocument layout = params.getLayout();
|
343 |
// FeatureStore source = params.getSource();
|
344 |
String identifier = params.getIdentifier();
|
345 |
String label = params.getLabel();
|
346 |
|
347 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
348 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
349 |
|
350 |
if(layout == null){ |
351 |
dialogs.messageDialog( |
352 |
i18n.getTranslation("_Layout_cant_be_null"),
|
353 |
null,
|
354 |
"_Configure_report_from_layout",
|
355 |
JOptionPane.WARNING_MESSAGE);
|
356 |
return false; |
357 |
} |
358 |
|
359 |
if(source == null){ |
360 |
dialogs.messageDialog( |
361 |
i18n.getTranslation("_Source_cant_be_null"),
|
362 |
null,
|
363 |
"_Configure_report_from_layout",
|
364 |
JOptionPane.WARNING_MESSAGE);
|
365 |
return false; |
366 |
} |
367 |
|
368 |
if(StringUtils.isBlank(identifier)){
|
369 |
dialogs.messageDialog( |
370 |
i18n.getTranslation("_Identifier_cant_be_null"),
|
371 |
null,
|
372 |
"_Configure_report_from_layout",
|
373 |
JOptionPane.WARNING_MESSAGE);
|
374 |
return false; |
375 |
} |
376 |
|
377 |
boolean centerToCurrentRow = params.isCenterToCurrentRow();
|
378 |
boolean zoomToCurrentRow = params.isZoomToCurrentRow();
|
379 |
double bufferSize = params.getBufferSize();
|
380 |
boolean highlightRecord = params.isHighlightRecord();
|
381 |
ISymbol highlightSymbol = params.getHighlightSymbol(source); |
382 |
boolean applySymbolToRecord = params.isApplySymbolToRecord();
|
383 |
boolean applySymbolToEverythingExceptRecord = params.isApplySymbolToEverythingExceptRecord();
|
384 |
boolean useInternalPdfViewer = params.isUseInternalPdfViewer();
|
385 |
String viewTag = params.getViewTag();
|
386 |
|
387 |
return doConfigureReportFromLayout(
|
388 |
source, |
389 |
layout, |
390 |
identifier, |
391 |
label, |
392 |
centerToCurrentRow, |
393 |
zoomToCurrentRow, |
394 |
bufferSize, |
395 |
highlightRecord, |
396 |
highlightSymbol, |
397 |
applySymbolToRecord, |
398 |
applySymbolToEverythingExceptRecord, |
399 |
viewTag, |
400 |
useInternalPdfViewer); |
401 |
|
402 |
|
403 |
} |
404 |
|
405 |
public boolean doConfigureReportFromLayout( |
406 |
FeatureStore store, |
407 |
LayoutDocument layout, |
408 |
String name,
|
409 |
String label,
|
410 |
boolean centerToCurrentRow,
|
411 |
boolean zoomToCurrentRow,
|
412 |
double bufferSize,
|
413 |
boolean highlightRecord,
|
414 |
ISymbol highlightSymbol, |
415 |
boolean applySymbolToRecord,
|
416 |
boolean applySymbolToEverythingExceptRecord,
|
417 |
String viewTag,
|
418 |
boolean useInternalPdfViewer
|
419 |
) { |
420 |
try {
|
421 |
LayoutReportActionParameters reportParameters = LayoutReportActionParameters.create(); |
422 |
reportParameters.setIdentifier(name); |
423 |
reportParameters.setLabel(label); |
424 |
reportParameters.setHighlightSymbol(highlightSymbol); |
425 |
reportParameters.setCenterToCurrentRow(centerToCurrentRow); |
426 |
reportParameters.setZoomToCurrentRow(zoomToCurrentRow); |
427 |
reportParameters.setBufferSize(bufferSize); |
428 |
reportParameters.setHighlightRecord(highlightRecord); |
429 |
reportParameters.setApplySymbolToRecord(applySymbolToRecord); |
430 |
reportParameters.setApplySymbolToEverythingExceptRecord(applySymbolToEverythingExceptRecord); |
431 |
reportParameters.setViewTag(viewTag); |
432 |
reportParameters.setUseInternalPdfViewer(useInternalPdfViewer); |
433 |
|
434 |
reportParameters.save(store, layout); |
435 |
((JComponent)dialog).setVisible(false); |
436 |
return true; |
437 |
} catch (Exception ex) { |
438 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
439 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
440 |
dialogs.messageDialog( |
441 |
i18n.getTranslation("_Cant_configure_report_from_layout"),
|
442 |
null,
|
443 |
"_Configure_report_from_layout",
|
444 |
JOptionPane.WARNING_MESSAGE);
|
445 |
return false; |
446 |
} |
447 |
} |
448 |
|
449 |
private void doChangeReport() { |
450 |
LayoutReportAction report = getReportAction(); |
451 |
if(report == null){ |
452 |
this.txtIdentifier.setText(""); |
453 |
this.txtLabel.setText(""); |
454 |
} else {
|
455 |
this.txtIdentifier.setText(report.getReportName());
|
456 |
this.txtLabel.setText(report.getReportLabel());
|
457 |
this.chkCenterToCurrentRow.setSelected(report.getParameters().isCenterToCurrentRow());
|
458 |
this.chkZoomToCurrentRow.setSelected(report.getParameters().isZoomToCurrentRow());
|
459 |
PositiveDoubleDocumentFilter.setValue(this.txtBufferSize,report.getParameters().getBufferSize());
|
460 |
this.chkHighlightRecord.setSelected(report.getParameters().isHighlightRecord());
|
461 |
this.highlightSymbol = report.getParameters().getHighlightSymbol(this.source); |
462 |
ListElement.setSelected(this.cboView, report.getParameters().getViewTag());
|
463 |
this.chkUseInternalPdfViewer.setSelected(report.getParameters().isUseInternalPdfViewer());
|
464 |
this.symbolPicker.set(this.highlightSymbol); |
465 |
} |
466 |
doUpdateComponents(); |
467 |
} |
468 |
|
469 |
void setDialog(Dialog dialog) { |
470 |
this.dialog = dialog;
|
471 |
} |
472 |
|
473 |
public FeatureStore getStore() {
|
474 |
return this.source; |
475 |
} |
476 |
|
477 |
|
478 |
|
479 |
} |