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 / GraphAction.java @ 47389
History | View | Annotate | Download (6.72 KB)
1 | 46052 | omartinez | package org.gvsig.fmap.dal.swing.impl.actions; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | import java.awt.event.ActionEvent; |
||
5 | import java.util.ArrayList; |
||
6 | import javax.swing.AbstractAction; |
||
7 | import javax.swing.Action; |
||
8 | import javax.swing.event.ListSelectionEvent; |
||
9 | import javax.swing.event.ListSelectionListener; |
||
10 | import org.gvsig.fmap.dal.DataStore; |
||
11 | import org.gvsig.fmap.dal.feature.Feature; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
13 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
14 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
15 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
16 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
17 | import org.gvsig.fmap.dal.swing.AbstractDALActionFactory; |
||
18 | 47389 | fdiaz | import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
19 | 46052 | omartinez | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
20 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
21 | import org.gvsig.tools.ToolsLocator; |
||
22 | import org.gvsig.tools.i18n.I18nManager; |
||
23 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
24 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
25 | import org.jfree.chart.ChartFactory; |
||
26 | import org.jfree.chart.ChartPanel; |
||
27 | import org.jfree.chart.JFreeChart; |
||
28 | import org.jfree.chart.plot.PlotOrientation; |
||
29 | import org.jfree.data.category.DefaultCategoryDataset; |
||
30 | 47389 | fdiaz | import org.slf4j.Logger; |
31 | import org.slf4j.LoggerFactory; |
||
32 | 46052 | omartinez | |
33 | /**
|
||
34 | *
|
||
35 | * @author jjdelcerro
|
||
36 | */
|
||
37 | @SuppressWarnings("UseSpecificCatch") |
||
38 | public class GraphAction |
||
39 | extends AbstractAction |
||
40 | implements ListSelectionListener |
||
41 | { |
||
42 | |||
43 | private class svDefaultCategoryDataset extends DefaultCategoryDataset { |
||
44 | |||
45 | public svDefaultCategoryDataset() {
|
||
46 | |||
47 | } |
||
48 | } |
||
49 | |||
50 | public static class GraphActionFactory extends AbstractDALActionFactory { |
||
51 | |||
52 | public static final String ACTION_NAME = "Graph"; |
||
53 | |||
54 | public GraphActionFactory() {
|
||
55 | super(ACTION_NAME);
|
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | public Action createAction(DALActionContext context) { |
||
60 | return new GraphAction(context); |
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public boolean isApplicable(Object... args) { |
||
65 | return false; // args[0] instanceof SearchActionContext; |
||
66 | } |
||
67 | 47049 | jjdelcerro | |
68 | public static void selfRegister() { |
||
69 | DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
||
70 | dalSwingManager.registerStoreAction(new GraphActionFactory());
|
||
71 | } |
||
72 | 46052 | omartinez | |
73 | } |
||
74 | |||
75 | |||
76 | private static final Logger LOGGER = LoggerFactory.getLogger(ShowFormAction.class); |
||
77 | |||
78 | private final DALActionContext context; |
||
79 | private boolean showFormSingleElement; |
||
80 | |||
81 | public GraphAction(DALActionContext context) {
|
||
82 | this.context = context;
|
||
83 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
84 | this.putValue(
|
||
85 | Action.ACTION_COMMAND_KEY,
|
||
86 | GraphActionFactory.ACTION_NAME |
||
87 | ); |
||
88 | this.putValue(
|
||
89 | Action.SHORT_DESCRIPTION,
|
||
90 | i18n.getTranslation("_Show_graph")
|
||
91 | ); |
||
92 | this.putValue(
|
||
93 | Action.SMALL_ICON,
|
||
94 | ToolsSwingLocator.getIconThemeManager().getCurrent().get("chart")
|
||
95 | ); |
||
96 | try {
|
||
97 | this.showFormSingleElement = true; |
||
98 | DataStore store = this.context.getStore();
|
||
99 | if( !(store instanceof FeatureStore) ) { |
||
100 | return;
|
||
101 | } |
||
102 | FeatureStore featureStore = (FeatureStore) store; |
||
103 | FeatureAttributeDescriptor[] pks = featureStore.getDefaultFeatureType().getPrimaryKey();
|
||
104 | if (pks == null || pks.length < 1) { |
||
105 | this.showFormSingleElement = false; |
||
106 | } |
||
107 | } catch (Exception ex) { |
||
108 | } |
||
109 | } |
||
110 | |||
111 | @Override
|
||
112 | public void actionPerformed(ActionEvent e) { |
||
113 | try {
|
||
114 | this.setEnabled(false); |
||
115 | DataStore store = this.context.getStore();
|
||
116 | if( !(store instanceof FeatureStore) ) { |
||
117 | return ;
|
||
118 | } |
||
119 | FeatureStore featureStore = (FeatureStore) store; |
||
120 | FeatureQuery query = this.context.getQuery();
|
||
121 | |||
122 | svDefaultCategoryDataset dataset = new svDefaultCategoryDataset();
|
||
123 | FeatureSet features = featureStore.getFeatureSet(query); |
||
124 | |||
125 | Iterable<FeatureQueryOrder.FeatureQueryOrderMember> orderMembers = query.getOrder().members();
|
||
126 | ArrayList<String> fields = new ArrayList<>(); |
||
127 | for (FeatureQueryOrder.FeatureQueryOrderMember orderMember : orderMembers) {
|
||
128 | fields.add(orderMember.getAttributeName()); |
||
129 | } |
||
130 | if (fields.size()<2) { |
||
131 | return;
|
||
132 | } |
||
133 | |||
134 | fields.add((String) this.context.getQuery().getAggregateFunctions().keySet().toArray()[0]); |
||
135 | |||
136 | String field1 = fields.get(0); |
||
137 | String field2 = fields.get(1); |
||
138 | String fieldValue = fields.get(2); |
||
139 | |||
140 | for (Feature feature : features) {
|
||
141 | Object value = feature.get(fieldValue);
|
||
142 | if (value instanceof Number) { |
||
143 | String valueField1;
|
||
144 | String valueField2;
|
||
145 | if(feature.getType().getAttributeDescriptor(field1)!=null) { |
||
146 | valueField1 = feature.getLabelOfValue(field1); |
||
147 | } else {
|
||
148 | valueField1 = feature.get(field1).toString(); |
||
149 | } |
||
150 | if(feature.getType().getAttributeDescriptor(field2)!=null) { |
||
151 | valueField2 = feature.getLabelOfValue(field2); |
||
152 | } else {
|
||
153 | valueField2 = feature.get(field1).toString(); |
||
154 | } |
||
155 | |||
156 | dataset.addValue((Number) value, valueField1, valueField2);
|
||
157 | } |
||
158 | } |
||
159 | |||
160 | JFreeChart barChart = ChartFactory.createBarChart( |
||
161 | field1 + " " + field2,
|
||
162 | field2, |
||
163 | fieldValue, |
||
164 | dataset, |
||
165 | PlotOrientation.VERTICAL, |
||
166 | true,
|
||
167 | true,
|
||
168 | false);
|
||
169 | ChartPanel chartPanel = new ChartPanel(barChart);
|
||
170 | chartPanel.setPreferredSize(new Dimension( 360 , 200 )); |
||
171 | 47389 | fdiaz | ToolsSwingLocator.getWindowManager().showWindow(chartPanel, "action", WindowManager.MODE.DIALOG);
|
172 | 46052 | omartinez | |
173 | |||
174 | } catch (Exception ex) { |
||
175 | LOGGER.warn("Can't show form", ex);
|
||
176 | } finally {
|
||
177 | this.setEnabled(true); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | @Override
|
||
182 | public void valueChanged(ListSelectionEvent e) { |
||
183 | if (e.getValueIsAdjusting()) {
|
||
184 | return;
|
||
185 | } |
||
186 | if (this.context.getSelectedsCount()== 0) { |
||
187 | this.setEnabled(true); |
||
188 | return;
|
||
189 | } |
||
190 | this.setEnabled(showFormSingleElement);
|
||
191 | } |
||
192 | |||
193 | } |