gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.app / org.gvsig.lrs.app.mainplugin / src / main / java / org / gvsig / lrs / app / showmeasures / ShowMeasuresExtension.java @ 11
History | View | Annotate | Download (11.4 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.lrs.app.showmeasures; |
24 |
|
25 |
import java.awt.Dimension; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
|
29 |
import javax.swing.JOptionPane; |
30 |
|
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
import org.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.andami.plugins.Extension; |
37 |
import org.gvsig.app.ApplicationLocator; |
38 |
import org.gvsig.app.ApplicationManager; |
39 |
import org.gvsig.app.project.documents.view.ViewDocument; |
40 |
import org.gvsig.app.project.documents.view.gui.IView; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.exception.ReadException; |
43 |
import org.gvsig.fmap.dal.feature.Feature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
45 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
46 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 |
import org.gvsig.fmap.dal.feature.FeatureType; |
48 |
import org.gvsig.fmap.geom.DataTypes; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.type.GeometryType; |
51 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
52 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
53 |
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
54 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
55 |
import org.gvsig.lrs.lib.api.LrsAlgorithm; |
56 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
57 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
58 |
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams; |
59 |
import org.gvsig.lrs.lib.api.LrsShowMeasuresAlgorithmParams; |
60 |
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
61 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
62 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
63 |
import org.gvsig.lrs.swing.api.JLrsProgressDialog; |
64 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
65 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
66 |
import org.gvsig.lrs.swing.impl.JLrsCalibrateRouteParamsController; |
67 |
import org.gvsig.lrs.swing.impl.JLrsShowMeasuresParamsController; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dispose.DisposeUtils; |
70 |
import org.gvsig.tools.exception.BaseException; |
71 |
import org.gvsig.tools.i18n.I18nManager; |
72 |
import org.gvsig.tools.observer.Observable; |
73 |
import org.gvsig.tools.observer.Observer; |
74 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
75 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
76 |
import org.gvsig.tools.task.SimpleTaskStatus; |
77 |
import org.gvsig.tools.visitor.VisitCanceledException; |
78 |
import org.gvsig.tools.visitor.Visitor; |
79 |
|
80 |
|
81 |
/**
|
82 |
* @author dmartinez
|
83 |
*
|
84 |
*/
|
85 |
public class ShowMeasuresExtension extends Extension { |
86 |
|
87 |
private static final Logger logger = LoggerFactory.getLogger(ShowMeasuresExtension.class); |
88 |
|
89 |
/* (non-Javadoc)
|
90 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
91 |
*/
|
92 |
public void execute(String actionCommand) { |
93 |
if (StringUtils.equalsIgnoreCase(actionCommand, "show-measures")) { |
94 |
IView view = getActiveView(); |
95 |
FLyrVect activeLayer = getActiveLayer(view); |
96 |
|
97 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
98 |
|
99 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
100 |
|
101 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
102 |
|
103 |
JLrsShowMeasuresParamsController panel; |
104 |
try {
|
105 |
panel = (JLrsShowMeasuresParamsController)manager.createJLrsShowMeasuresAlgorithmParameters(activeLayer, null);
|
106 |
} catch (LrsNeededParameterException e2) {
|
107 |
logger.error("Error creating panel", e2);
|
108 |
JOptionPane.showMessageDialog(
|
109 |
null,
|
110 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
111 |
.append(":").append(e2.getLocalizedMessage()),
|
112 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
113 |
return;
|
114 |
} |
115 |
panel.setVisibleAceptCancel(true);
|
116 |
panel.addActionListener(new ActionListener() { |
117 |
|
118 |
public void actionPerformed(ActionEvent e) { |
119 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
120 |
if (panel.isCanceled()) {
|
121 |
return;
|
122 |
} |
123 |
LrsShowMeasuresAlgorithmParams params = null;
|
124 |
try {
|
125 |
params = (LrsShowMeasuresAlgorithmParams)panel.getParams(); |
126 |
} catch (LrsGettingParametersException e1) {
|
127 |
logger.error("Error getting parameters", e1);
|
128 |
JOptionPane.showMessageDialog(
|
129 |
null,
|
130 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
131 |
.append(":").append(e1.getLocalizedMessage()),
|
132 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
133 |
return;
|
134 |
} catch (LrsNeededParameterException e1) {
|
135 |
logger.warn("Error getting parameters", e1);
|
136 |
JOptionPane.showMessageDialog(
|
137 |
null,
|
138 |
i18nManager.getTranslation(e1.getMessage()), |
139 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
140 |
return;
|
141 |
} |
142 |
LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
143 |
final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
|
144 |
final SimpleTaskStatus taskStatus =
|
145 |
ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18nManager.getTranslation("progress"));
|
146 |
|
147 |
final JLrsProgressDialog progressDialog = manager.createJLrsProgressDialog(taskStatus);
|
148 |
|
149 |
winManager.showWindow(progressDialog.asJComponent(), i18nManager.getTranslation("show_measures"),
|
150 |
WindowManager.MODE.WINDOW); |
151 |
|
152 |
Thread task = new Thread(new Runnable() { |
153 |
|
154 |
public void run() { |
155 |
try {
|
156 |
algorithm.execute(taskStatus); |
157 |
} catch (Exception e) { |
158 |
logger.error("Error showing measures", e);
|
159 |
String message = e.getMessage();
|
160 |
if (e.getCause() != null) { |
161 |
message = e.getCause().getMessage(); |
162 |
} |
163 |
// JOptionPane.showMessageDialog(progressDialog.asJComponent(), message);
|
164 |
JOptionPane.showMessageDialog(
|
165 |
progressDialog.asJComponent(), |
166 |
message, |
167 |
i18nManager.getTranslation("show_measures"),
|
168 |
JOptionPane.ERROR_MESSAGE
|
169 |
); |
170 |
} |
171 |
} |
172 |
}); |
173 |
task.start(); |
174 |
|
175 |
} |
176 |
}); |
177 |
winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("show_measures"),
|
178 |
WindowManager.MODE.WINDOW); |
179 |
} |
180 |
} |
181 |
|
182 |
/* (non-Javadoc)
|
183 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
184 |
*/
|
185 |
public void initialize() { |
186 |
registerIcons(); |
187 |
} |
188 |
|
189 |
/* (non-Javadoc)
|
190 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
191 |
*/
|
192 |
public boolean isEnabled() { |
193 |
IView view = getActiveView(); |
194 |
FLyrVect activeLayer = getActiveLayer(view); |
195 |
if (activeLayer != null) { |
196 |
return correctLayerFound(activeLayer);
|
197 |
} |
198 |
return false; |
199 |
} |
200 |
|
201 |
/* (non-Javadoc)
|
202 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
203 |
*/
|
204 |
public boolean isVisible() { |
205 |
return true; |
206 |
} |
207 |
|
208 |
private void registerIcons() { |
209 |
IconThemeHelper.registerIcon("lrs", "pk_green", this); |
210 |
} |
211 |
|
212 |
private IView getActiveView() {
|
213 |
ApplicationManager application = ApplicationLocator.getManager(); |
214 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
215 |
return view;
|
216 |
} |
217 |
|
218 |
private FLyrVect getActiveLayer(IView vista) {
|
219 |
if (vista != null) { |
220 |
ViewDocument viewDocument = vista.getViewDocument(); |
221 |
FLayer[] actives =
|
222 |
viewDocument.getMapContext().getLayers().getActives(); |
223 |
|
224 |
if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
225 |
return (FLyrVect) actives[0]; |
226 |
} |
227 |
} |
228 |
return null; |
229 |
} |
230 |
|
231 |
//TODO Extract validations to a common class
|
232 |
/**
|
233 |
* Tries to find one curve M-layer with selected Features
|
234 |
*
|
235 |
* @param FLayer
|
236 |
* @return
|
237 |
* @throws ReadException
|
238 |
*/
|
239 |
private static boolean correctLayerFound(FLayer layer) { |
240 |
boolean correctLayerFound = false; |
241 |
// Validates is FLyrVect
|
242 |
FLyrVect lyrVect = null;
|
243 |
if (layer instanceof FLyrVect) { |
244 |
lyrVect = (FLyrVect) layer; |
245 |
} else
|
246 |
return false; |
247 |
|
248 |
try {
|
249 |
if (lyrVect != null && (lyrVect.getGeometryType() |
250 |
.isTypeOf(Geometry.TYPES.CURVE) |
251 |
|| lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.MULTICURVE))) { |
252 |
//TODO M-geometry commented to test button
|
253 |
// if (lyrVect.getGeometryType()
|
254 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM2DM) ||
|
255 |
// lyrVect.getGeometryType()
|
256 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM3DM)) {
|
257 |
|
258 |
//At least a feature must be selected
|
259 |
return hasSelectedFeatures(lyrVect);
|
260 |
|
261 |
//}
|
262 |
} |
263 |
} catch (Exception e) { |
264 |
logger.error("Can't get linear vector layers", e);
|
265 |
} |
266 |
return correctLayerFound;
|
267 |
} |
268 |
|
269 |
private static boolean hasSelectedFeatures(FLyrVect layer) { |
270 |
FeatureStore featureStore=layer.getFeatureStore(); |
271 |
if(featureStore!=null){ |
272 |
try {
|
273 |
FeatureSelection selectedFeatures= featureStore.getFeatureSelection(); |
274 |
if ((selectedFeatures.getSize() >0)) { |
275 |
return true; |
276 |
} |
277 |
} catch (DataException e) {
|
278 |
logger.warn("Can't get selected feature", e);
|
279 |
} |
280 |
} |
281 |
return false; |
282 |
} |
283 |
|
284 |
} |