svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / actions / ReportStoreAction.java @ 47049
History | View | Annotate | Download (8.95 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.actions; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.io.InputStream; |
5 |
import java.net.URL; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Collections; |
8 |
import java.util.LinkedHashMap; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import javax.json.Json; |
12 |
import javax.json.JsonObject; |
13 |
import javax.swing.AbstractAction; |
14 |
import javax.swing.Action; |
15 |
import javax.swing.JComponent; |
16 |
import javax.swing.JMenuItem; |
17 |
import javax.swing.JPopupMenu; |
18 |
import org.apache.commons.io.IOUtils; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
21 |
import org.apache.commons.lang3.tuple.Pair; |
22 |
import org.gvsig.fmap.dal.DataStore; |
23 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
24 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
25 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
26 |
import org.gvsig.tools.ToolsLocator; |
27 |
import org.gvsig.tools.i18n.I18nManager; |
28 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
29 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
30 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
31 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
import org.gvsig.fmap.dal.swing.DALActionFactory; |
35 |
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
36 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
37 |
import org.gvsig.fmap.dal.swing.report.ReportActionFactory; |
38 |
import org.gvsig.fmap.dal.swing.report.ReportActionFactory.ReportCustomActionFactory; |
39 |
import org.gvsig.tools.dispose.DisposeUtils; |
40 |
|
41 |
/**
|
42 |
*
|
43 |
* @author jjdelcerro
|
44 |
*/
|
45 |
@SuppressWarnings("UseSpecificCatch") |
46 |
public class ReportStoreAction |
47 |
extends AbstractAction { |
48 |
|
49 |
public static String ACTION_NAME = "Reports"; |
50 |
|
51 |
public static class ReportStoreActionFactory implements DALActionFactory { |
52 |
|
53 |
private final Map<String,Pair<ReportCustomActionFactory,Boolean>> customActions; |
54 |
|
55 |
public ReportStoreActionFactory() {
|
56 |
this.customActions = new LinkedHashMap<>(); |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public String getName() { |
61 |
return ACTION_NAME;
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public Action createAction(DALActionContext context) { |
66 |
return new ReportStoreAction(this, context); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public boolean isApplicable(Object... args) { |
71 |
return true; |
72 |
} |
73 |
|
74 |
public void registerCustomAction(ReportCustomActionFactory factory, boolean insertSeparator) { |
75 |
this.customActions.put(factory.getName(), new ImmutablePair<>(factory,insertSeparator)); |
76 |
} |
77 |
|
78 |
public Map<String, Pair<ReportCustomActionFactory, Boolean>> getCustomActions() { |
79 |
return this.customActions; |
80 |
} |
81 |
|
82 |
public static void selfRegister() { |
83 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
84 |
dalSwingManager.registerStoreAction(new ReportStoreActionFactory());
|
85 |
String[][] iconNames = new String[][]{ |
86 |
new String[]{"Report", "report-select-reports"} |
87 |
}; |
88 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
89 |
for (String[] icon : iconNames) { |
90 |
URL url = ReportStoreAction.class.getResource("images/" + icon[1] + ".png"); |
91 |
theme.registerDefault("Report", icon[0], icon[1], null, url); |
92 |
} |
93 |
} |
94 |
|
95 |
} |
96 |
|
97 |
private static final Logger LOGGER = LoggerFactory.getLogger(ReportStoreAction.class); |
98 |
|
99 |
private final DALActionContext context; |
100 |
private final ReportStoreActionFactory factory; |
101 |
|
102 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
103 |
public ReportStoreAction(ReportStoreActionFactory factory, DALActionContext context) {
|
104 |
this.factory = factory;
|
105 |
this.context = context;
|
106 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
107 |
this.putValue(
|
108 |
Action.ACTION_COMMAND_KEY,
|
109 |
ACTION_NAME |
110 |
); |
111 |
this.putValue(
|
112 |
Action.SHORT_DESCRIPTION,
|
113 |
i18n.getTranslation("_Reports")
|
114 |
); |
115 |
this.putValue(
|
116 |
Action.SMALL_ICON,
|
117 |
ToolsSwingLocator.getIconThemeManager().getCurrent().get("report-select-reports")
|
118 |
); |
119 |
} |
120 |
|
121 |
private List<JsonObject> getReports(FeatureStore store) { |
122 |
List<JsonObject> reports = new ArrayList<>(); |
123 |
ResourcesStorage resources = null;
|
124 |
try {
|
125 |
resources = store.getResourcesStorage(); |
126 |
if (resources != null) { |
127 |
List<Resource> reportsResources = resources.getResources("report"); |
128 |
if (reportsResources != null && !reportsResources.isEmpty()) { |
129 |
for (Resource resource : reportsResources) {
|
130 |
InputStream is = null; |
131 |
try {
|
132 |
is = resource.asInputStream(); |
133 |
JsonObject json = Json.createReader(is).readObject(); |
134 |
reports.add(json); |
135 |
} catch (Exception ex) { |
136 |
LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex); |
137 |
} finally {
|
138 |
IOUtils.closeQuietly(is); |
139 |
} |
140 |
} |
141 |
} |
142 |
} |
143 |
} finally {
|
144 |
DisposeUtils.disposeQuietly(resources); |
145 |
} |
146 |
if (reports.isEmpty()) {
|
147 |
return null; |
148 |
} |
149 |
return reports;
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void actionPerformed(ActionEvent e) { |
154 |
try {
|
155 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
156 |
// this.setEnabled(false);
|
157 |
DataStore store = this.context.getStore();
|
158 |
if (!(store instanceof FeatureStore)) { |
159 |
return;
|
160 |
} |
161 |
List<ReportAction> actions = new ArrayList<>(); |
162 |
List<JsonObject> reports = this.getReports((FeatureStore) store); |
163 |
if (reports != null && !reports.isEmpty()) { |
164 |
for (JsonObject json : reports) {
|
165 |
ReportAction action = null;
|
166 |
for (ReportActionFactory theFactory : dataSwingManager.getReportActionFactories()) {
|
167 |
if( theFactory.isApplicable(json,this.context, e)) { |
168 |
action = theFactory.createReportAction( |
169 |
(FeatureStore) store, |
170 |
this.context.getQuery(),
|
171 |
this.context.getSelecteds(),
|
172 |
json |
173 |
); |
174 |
} |
175 |
} |
176 |
if( action != null ) { |
177 |
String label = (String) action.getValue(Action.NAME); |
178 |
if( StringUtils.isBlank(label) ) {
|
179 |
action.putValue(Action.NAME, action.getLabel());
|
180 |
} |
181 |
actions.add(action); |
182 |
} |
183 |
} |
184 |
} |
185 |
Collections.sort(
|
186 |
actions, |
187 |
(ReportAction o1, ReportAction o2) -> StringUtils.compare(o1.getLabel(), o2.getLabel()) |
188 |
); |
189 |
JPopupMenu popup = new JPopupMenu(); |
190 |
for (ReportAction action : actions) {
|
191 |
popup.add(new JMenuItem(action)); |
192 |
} |
193 |
for (Pair<ReportCustomActionFactory, Boolean> customAction : this.factory.getCustomActions().values()) { |
194 |
Action action = customAction.getLeft().createCustomAction(e, context);
|
195 |
if( action !=null ) { |
196 |
if( customAction.getRight() && popup.getComponentCount()>0 ) { |
197 |
popup.addSeparator(); |
198 |
} |
199 |
popup.add(action); |
200 |
} |
201 |
} |
202 |
JComponent button = this.context.getActionButton(ACTION_NAME); |
203 |
popup.show(button, 0, button.getHeight());
|
204 |
} catch (Exception ex) { |
205 |
LOGGER.warn("Can't show form", ex);
|
206 |
} finally {
|
207 |
// this.setEnabled(true);
|
208 |
} |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public boolean isEnabled() { |
213 |
try {
|
214 |
DataStore store = this.context.getStore();
|
215 |
if (!(store instanceof FeatureStore)) { |
216 |
return false; |
217 |
} |
218 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
219 |
List<JsonObject> reports = this.getReports((FeatureStore) store); |
220 |
if (reports != null && !reports.isEmpty()) { |
221 |
for (JsonObject json : reports) {
|
222 |
for (ReportActionFactory thefactory : dataSwingManager.getReportActionFactories()) {
|
223 |
if( thefactory.isApplicable(json,this.context)) { |
224 |
return true; |
225 |
} |
226 |
} |
227 |
} |
228 |
} |
229 |
} catch (Exception ex) { |
230 |
LOGGER.warn("Can't check isEnabled", ex);
|
231 |
} |
232 |
return false; |
233 |
} |
234 |
|
235 |
} |