Revision 47340 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

View differences:

ReportStoreAction.java
1 1
package org.gvsig.fmap.dal.swing.impl.actions;
2 2

  
3 3
import java.awt.event.ActionEvent;
4
import java.io.InputStream;
5 4
import java.net.URL;
6
import java.util.ArrayList;
7
import java.util.Collections;
8 5
import java.util.LinkedHashMap;
9 6
import java.util.List;
10 7
import java.util.Map;
11
import javax.json.Json;
12 8
import javax.json.JsonObject;
13 9
import javax.swing.AbstractAction;
14 10
import javax.swing.Action;
15 11
import javax.swing.JComponent;
16 12
import javax.swing.JMenuItem;
17 13
import javax.swing.JPopupMenu;
18
import org.apache.commons.collections4.CollectionUtils;
19
import org.apache.commons.io.IOUtils;
20
import org.apache.commons.lang3.StringUtils;
21 14
import org.apache.commons.lang3.tuple.ImmutablePair;
22 15
import org.apache.commons.lang3.tuple.Pair;
23 16
import org.gvsig.fmap.dal.DataStore;
24 17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.swing.DALActionFactory;
19
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext;
25 20
import org.gvsig.fmap.dal.swing.DALSwingLocator;
26 21
import org.gvsig.fmap.dal.swing.DataSwingManager;
22
import static org.gvsig.fmap.dal.swing.impl.actions.ReportUtils.getReportActions;
23
import static org.gvsig.fmap.dal.swing.impl.actions.ReportUtils.getReports;
24
import org.gvsig.fmap.dal.swing.report.ReportAction;
25
import org.gvsig.fmap.dal.swing.report.ReportActionFactory;
26
import org.gvsig.fmap.dal.swing.report.ReportActionFactory.ReportCustomActionFactory;
27 27
import org.gvsig.tools.ToolsLocator;
28 28
import org.gvsig.tools.i18n.I18nManager;
29 29
import org.gvsig.tools.swing.api.ToolsSwingLocator;
30 30
import org.gvsig.tools.swing.icontheme.IconTheme;
31
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
32
import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource;
33 31
import org.slf4j.Logger;
34 32
import org.slf4j.LoggerFactory;
35
import org.gvsig.fmap.dal.swing.DALActionFactory;
36
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext;
37
import org.gvsig.fmap.dal.swing.report.ReportAction;
38
import org.gvsig.fmap.dal.swing.report.ReportActionFactory;
39
import org.gvsig.fmap.dal.swing.report.ReportActionFactory.ReportCustomActionFactory;
40
import org.gvsig.tools.dispose.DisposeUtils;
41 33

  
42 34
/**
43 35
 *
......
119 111
        );
120 112
    }
121 113

  
122
    private List<JsonObject> getReports(FeatureStore store) {
123
        List<JsonObject> reports = new ArrayList<>();
124
        ResourcesStorage resources = null;
125
        try {
126
            resources = store.getResourcesStorage();
127
            if (resources != null) {
128
                List<Resource> reportsResources = resources.getResources("report");
129
                if (reportsResources != null && !reportsResources.isEmpty()) {
130
                    for (Resource resource : reportsResources) {
131
                        InputStream is = null;
132
                        try {
133
                            is = resource.asInputStream();
134
                            JsonObject json = Json.createReader(is).readObject();
135
                            reports.add(json);
136
                        } catch (Exception ex) {
137
                            LOGGER.warn("Can't load report form resource (" + resource.getURL() + ")", ex);
138
                        } finally {
139
                            IOUtils.closeQuietly(is);
140
                        }
141
                        DisposeUtils.disposeQuietly(resource);
142
                    }
143
                }
144
            }
145
        } finally {
146
            DisposeUtils.disposeQuietly(resources);
147
        }
148
        if (reports.isEmpty()) {
149
            return null;
150
        }
151
        return reports;
152
    }
153 114

  
154 115
    @Override
155 116
    public void actionPerformed(ActionEvent e) {
156 117
        try {
157
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
158 118
//            this.setEnabled(false);
159 119
            DataStore store = this.context.getStore();
160 120
            if (!(store instanceof FeatureStore)) {
161 121
                return;
162 122
            }
163
            List<ReportAction> actions = new ArrayList<>();
164
            List<JsonObject> reports = this.getReports((FeatureStore) store);
165
            if (reports != null && !reports.isEmpty()) {
166
                for (JsonObject json : reports) {
167
                    ReportAction action = null;
168
                    for (ReportActionFactory theFactory : dataSwingManager.getReportActionFactories()) {
169
                        if( theFactory.isApplicable(json,this.context, e)) {
170
                            action = theFactory.createReportAction(
171
                                    (FeatureStore) store,
172
                                    this.context.getQuery(),
173
                                    this.context.getSelecteds(),
174
                                    json
175
                            );
176
                        }
177
                    }
178
                    if( action != null ) {
179
                        String label = (String) action.getValue(Action.NAME);
180
                        if( StringUtils.isBlank(label) ) {
181
                            action.putValue(Action.NAME, action.getLabel());
182
                        }
183
                        actions.add(action);
184
                    }
185
                }
186
            }
187
            Collections.sort(
188
                    actions, 
189
                    (ReportAction o1, ReportAction o2) -> StringUtils.compare(o1.getLabel(), o2.getLabel())
190
            );
123
            List<ReportAction> actions = getReportActions(store, null, context, e);
191 124
            JPopupMenu popup = new JPopupMenu();
192 125
            for (ReportAction action : actions) {
193 126
                popup.add(new JMenuItem(action));
......
218 151
                return false;
219 152
            }
220 153
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
221
            List<JsonObject> reports = this.getReports((FeatureStore) store);
154
            List<JsonObject> reports = getReports((FeatureStore) store);
222 155
            if (reports != null && !reports.isEmpty()) {
223 156
                for (JsonObject json : reports) {
224 157
                    for (ReportActionFactory thefactory : dataSwingManager.getReportActionFactories()) {

Also available in: Unified diff