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 @ 47426
History | View | Annotate | Download (6.34 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.actions; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.net.URL; |
5 |
import java.util.LinkedHashMap; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import javax.json.JsonObject; |
9 |
import javax.swing.AbstractAction; |
10 |
import javax.swing.Action; |
11 |
import javax.swing.JComponent; |
12 |
import javax.swing.JMenuItem; |
13 |
import javax.swing.JPopupMenu; |
14 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
15 |
import org.apache.commons.lang3.tuple.Pair; |
16 |
import org.gvsig.fmap.dal.DataStore; |
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; |
20 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
21 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
22 |
import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME; |
23 |
import static org.gvsig.fmap.dal.swing.impl.actions.ReportUtils.getReportActions; |
24 |
import static org.gvsig.fmap.dal.swing.impl.actions.ReportUtils.getReports; |
25 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel; |
26 |
import org.gvsig.fmap.dal.swing.report.ReportAction; |
27 |
import org.gvsig.fmap.dal.swing.report.ReportActionFactory; |
28 |
import org.gvsig.fmap.dal.swing.report.ReportActionFactory.ReportCustomActionFactory; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.i18n.I18nManager; |
31 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
32 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
33 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
@SuppressWarnings("UseSpecificCatch") |
42 |
public class ReportStoreAction |
43 |
extends AbstractAction { |
44 |
|
45 |
public static String ACTION_NAME = "Reports"; |
46 |
|
47 |
public static class ReportStoreActionFactory implements DALActionFactory { |
48 |
|
49 |
private final Map<String,Pair<ReportCustomActionFactory,Boolean>> customActions; |
50 |
|
51 |
public ReportStoreActionFactory() {
|
52 |
this.customActions = new LinkedHashMap<>(); |
53 |
} |
54 |
|
55 |
@Override
|
56 |
public String getName() { |
57 |
return ACTION_NAME;
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public Action createAction(DALActionContext context) { |
62 |
return new ReportStoreAction(this, context); |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public boolean isApplicable(Object... args) { |
67 |
return true; |
68 |
} |
69 |
|
70 |
public void registerCustomAction(ReportCustomActionFactory factory, boolean insertSeparator) { |
71 |
this.customActions.put(factory.getName(), new ImmutablePair<>(factory,insertSeparator)); |
72 |
} |
73 |
|
74 |
public Map<String, Pair<ReportCustomActionFactory, Boolean>> getCustomActions() { |
75 |
return this.customActions; |
76 |
} |
77 |
|
78 |
public static void selfRegister() { |
79 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
80 |
dalSwingManager.registerStoreAction(new ReportStoreActionFactory());
|
81 |
|
82 |
ToolsSwingUtils.registerIcons( |
83 |
ReportStoreAction.class, |
84 |
"images",
|
85 |
LIBRARY_NAME, |
86 |
new String[]{"storeaction", "storeaction-reports-select"} |
87 |
); |
88 |
|
89 |
} |
90 |
|
91 |
} |
92 |
|
93 |
private static final Logger LOGGER = LoggerFactory.getLogger(ReportStoreAction.class); |
94 |
|
95 |
private final DALActionContext context; |
96 |
private final ReportStoreActionFactory factory; |
97 |
|
98 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
99 |
public ReportStoreAction(ReportStoreActionFactory factory, DALActionContext context) {
|
100 |
this.factory = factory;
|
101 |
this.context = context;
|
102 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
103 |
this.putValue(
|
104 |
Action.ACTION_COMMAND_KEY,
|
105 |
ACTION_NAME |
106 |
); |
107 |
this.putValue(
|
108 |
Action.SHORT_DESCRIPTION,
|
109 |
i18n.getTranslation("_Reports")
|
110 |
); |
111 |
this.putValue(
|
112 |
Action.SMALL_ICON,
|
113 |
ToolsSwingLocator.getIconThemeManager().getCurrent().get("storeaction-reports-select")
|
114 |
); |
115 |
} |
116 |
|
117 |
|
118 |
@Override
|
119 |
public void actionPerformed(ActionEvent e) { |
120 |
try {
|
121 |
// this.setEnabled(false);
|
122 |
DataStore store = this.context.getStore();
|
123 |
if (!(store instanceof FeatureStore)) { |
124 |
return;
|
125 |
} |
126 |
List<ReportAction> actions = getReportActions(store, null, context, e); |
127 |
JPopupMenu popup = new JPopupMenu(); |
128 |
for (ReportAction action : actions) {
|
129 |
popup.add(new JMenuItem(action)); |
130 |
} |
131 |
for (Pair<ReportCustomActionFactory, Boolean> customAction : this.factory.getCustomActions().values()) { |
132 |
Action action = customAction.getLeft().createCustomAction(e, context);
|
133 |
if( action !=null ) { |
134 |
if( customAction.getRight() && popup.getComponentCount()>0 ) { |
135 |
popup.addSeparator(); |
136 |
} |
137 |
popup.add(action); |
138 |
} |
139 |
} |
140 |
JComponent button = this.context.getActionButton(ACTION_NAME); |
141 |
popup.show(button, 0, button.getHeight());
|
142 |
} catch (Exception ex) { |
143 |
LOGGER.warn("Can't show form", ex);
|
144 |
} finally {
|
145 |
// this.setEnabled(true);
|
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public boolean isEnabled() { |
151 |
try {
|
152 |
DataStore store = this.context.getStore();
|
153 |
if (!(store instanceof FeatureStore)) { |
154 |
return false; |
155 |
} |
156 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
157 |
List<JsonObject> reports = getReports((FeatureStore) store);
|
158 |
if (reports != null && !reports.isEmpty()) { |
159 |
for (JsonObject json : reports) {
|
160 |
for (ReportActionFactory thefactory : dataSwingManager.getReportActionFactories()) {
|
161 |
if( thefactory.isApplicable(json,this.context)) { |
162 |
return true; |
163 |
} |
164 |
} |
165 |
} |
166 |
} |
167 |
Map<String, Pair<ReportCustomActionFactory, Boolean>> customActions = this.factory.getCustomActions(); |
168 |
return customActions!=null && !customActions.isEmpty(); |
169 |
} catch (Exception ex) { |
170 |
LOGGER.warn("Can't check isEnabled", ex);
|
171 |
} |
172 |
return false; |
173 |
} |
174 |
|
175 |
} |