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