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 / editroutecalibration / EditRouteCalibrationExtension.java @ 69
History | View | Annotate | Download (15.9 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.editroutecalibration; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.event.ComponentEvent; |
29 |
import java.awt.event.ComponentListener; |
30 |
import java.text.MessageFormat; |
31 |
import java.util.List; |
32 |
|
33 |
import javax.swing.JOptionPane; |
34 |
|
35 |
import org.apache.commons.lang3.StringUtils; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.andami.IconThemeHelper; |
40 |
import org.gvsig.andami.plugins.Extension; |
41 |
import org.gvsig.app.ApplicationLocator; |
42 |
import org.gvsig.app.ApplicationManager; |
43 |
import org.gvsig.app.project.documents.view.ViewDocument; |
44 |
import org.gvsig.app.project.documents.view.gui.IView; |
45 |
import org.gvsig.fmap.dal.exception.DataException; |
46 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
47 |
import org.gvsig.fmap.dal.feature.Feature; |
48 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
49 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
50 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.gvsig.fmap.mapcontext.MapContext; |
53 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
54 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
55 |
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationAlgorithmParams; |
56 |
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationSelectIdRouteAlgorithmParams; |
57 |
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
58 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
59 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
60 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
61 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
62 |
import org.gvsig.lrs.swing.impl.JLrsEditRouteCalibrationController; |
63 |
import org.gvsig.lrs.swing.impl.JLrsEditRouteCalibrationSelectIdRouteController; |
64 |
import org.gvsig.lrs.swing.impl.JLrsUtils; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.dispose.DisposableIterator; |
67 |
import org.gvsig.tools.i18n.I18nManager; |
68 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
69 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
70 |
|
71 |
|
72 |
/**
|
73 |
* @author dmartinez
|
74 |
*
|
75 |
*/
|
76 |
public class EditRouteCalibrationExtension extends Extension implements ComponentListener { |
77 |
|
78 |
private static final Logger logger = LoggerFactory.getLogger(EditRouteCalibrationExtension.class); |
79 |
|
80 |
private JLrsEditRouteCalibrationSelectIdRouteController panelSelectId;
|
81 |
|
82 |
private JLrsEditRouteCalibrationController panelController;
|
83 |
|
84 |
|
85 |
|
86 |
/* (non-Javadoc)
|
87 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
88 |
*/
|
89 |
public void execute(String actionCommand) { |
90 |
if (StringUtils.equalsIgnoreCase(actionCommand, "edit-route-calibration")) { |
91 |
IView view = getActiveView(); |
92 |
final FLyrVect activeLayer;
|
93 |
if (view!=null){ |
94 |
activeLayer = getActiveLayer(view); |
95 |
}else{
|
96 |
activeLayer = null;
|
97 |
} |
98 |
|
99 |
final MapContext mapContext = view.getMapControl().getMapContext();
|
100 |
|
101 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
102 |
|
103 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
104 |
|
105 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
106 |
|
107 |
try {
|
108 |
panelSelectId = (JLrsEditRouteCalibrationSelectIdRouteController)manager.createJLrsEditRouteCalibrationSelectIdRouteAlgorithmParameters(activeLayer, null);
|
109 |
panelSelectId.addComponentListener(this);
|
110 |
} catch (LrsNeededParameterException e2) {
|
111 |
logger.error("Error creating panel", e2);
|
112 |
JOptionPane.showMessageDialog(
|
113 |
null,
|
114 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
115 |
.append(":").append(e2.getLocalizedMessage()),
|
116 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
117 |
return;
|
118 |
} |
119 |
panelSelectId.setVisibleAceptCancel(true);
|
120 |
panelSelectId.addActionListener(new ActionListener() { |
121 |
|
122 |
public void actionPerformed(ActionEvent e) { |
123 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
124 |
if (panel.isCanceled()) {
|
125 |
return;
|
126 |
} |
127 |
LrsEditRouteCalibrationSelectIdRouteAlgorithmParams params = null;
|
128 |
try {
|
129 |
params = (LrsEditRouteCalibrationSelectIdRouteAlgorithmParams)panel.getParams(); |
130 |
} catch (LrsGettingParametersException e1) {
|
131 |
logger.error("Error getting parameters", e1);
|
132 |
JOptionPane.showMessageDialog(
|
133 |
null,
|
134 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
135 |
.append(":").append(e1.getLocalizedMessage()),
|
136 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
137 |
return;
|
138 |
} catch (LrsNeededParameterException e1) {
|
139 |
logger.warn("Error getting parameters", e1);
|
140 |
JOptionPane.showMessageDialog(
|
141 |
null,
|
142 |
i18nManager.getTranslation(e1.getMessage()), |
143 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
144 |
return;
|
145 |
} |
146 |
|
147 |
try {
|
148 |
panelController = (JLrsEditRouteCalibrationController)manager.createJLrsEditRouteCalibrationAlgorithmParameters(activeLayer, mapContext, params.getIdRouteField(), null);
|
149 |
panelController.addComponentListener(EditRouteCalibrationExtension.this); |
150 |
} catch (Exception e2) { |
151 |
logger.error("Error creating panel", e2);
|
152 |
JOptionPane.showMessageDialog(
|
153 |
null,
|
154 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
155 |
.append(":").append(e2.getLocalizedMessage()),
|
156 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
157 |
return;
|
158 |
} |
159 |
panelController.setVisibleAceptCancel(true);
|
160 |
panelController.addActionListener(new ActionListener() { |
161 |
public void actionPerformed(ActionEvent e) { |
162 |
JLrsEditRouteCalibrationController panel = (JLrsEditRouteCalibrationController) e.getSource(); |
163 |
if (panel.isCanceled()) {
|
164 |
return;
|
165 |
} |
166 |
|
167 |
if (panel.hasWarnings()){
|
168 |
boolean acceptedWarnings=showWarnings(panel.getWarnings());
|
169 |
if (acceptedWarnings){
|
170 |
panel.close(); |
171 |
}else{
|
172 |
return;
|
173 |
} |
174 |
} |
175 |
|
176 |
final LrsEditRouteCalibrationAlgorithmParams params;
|
177 |
try {
|
178 |
params = (LrsEditRouteCalibrationAlgorithmParams)panel.getParams(); |
179 |
} catch (LrsGettingParametersException e1) {
|
180 |
logger.error("Error getting parameters", e1);
|
181 |
JOptionPane.showMessageDialog(
|
182 |
null,
|
183 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
184 |
.append(":").append(e1.getLocalizedMessage()),
|
185 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
186 |
return;
|
187 |
} catch (LrsNeededParameterException e1) {
|
188 |
logger.warn("Error getting parameters", e1);
|
189 |
JOptionPane.showMessageDialog(
|
190 |
null,
|
191 |
i18nManager.getTranslation(e1.getMessage()), |
192 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
193 |
return;
|
194 |
} |
195 |
|
196 |
if (params!=null){ |
197 |
saveLayer(activeLayer,params); |
198 |
} |
199 |
|
200 |
panel.close(); |
201 |
} |
202 |
|
203 |
}); |
204 |
winManager.showWindow(panelController.asJComponent(), i18nManager.getTranslation("edit_route_calibration"),
|
205 |
WindowManager.MODE.WINDOW); |
206 |
} |
207 |
}); |
208 |
winManager.showWindow(panelSelectId.asJComponent(), i18nManager.getTranslation("edit_route_calibration"),
|
209 |
WindowManager.MODE.WINDOW); |
210 |
} |
211 |
} |
212 |
|
213 |
/* (non-Javadoc)
|
214 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
215 |
*/
|
216 |
public void initialize() { |
217 |
registerIcons(); |
218 |
} |
219 |
|
220 |
/* (non-Javadoc)
|
221 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
222 |
*/
|
223 |
public boolean isEnabled() { |
224 |
IView view = getActiveView(); |
225 |
if (view!=null){ |
226 |
FLyrVect activeLayer = getActiveLayer(view); |
227 |
if (panelSelectId == null && panelController == null && activeLayer!=null && activeLayer.isEditing()){ |
228 |
return JLrsUtils.isMlayerWithNonGeomField(activeLayer);
|
229 |
} |
230 |
} |
231 |
return false; |
232 |
} |
233 |
|
234 |
/* (non-Javadoc)
|
235 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
236 |
*/
|
237 |
public boolean isVisible() { |
238 |
ApplicationManager application = ApplicationLocator.getManager(); |
239 |
|
240 |
return application.getActiveComponent(ViewDocument.class) != null; |
241 |
} |
242 |
|
243 |
private void registerIcons() { |
244 |
IconThemeHelper.registerIcon("lrs", "pk_cian", this); |
245 |
} |
246 |
|
247 |
|
248 |
private IView getActiveView() {
|
249 |
ApplicationManager application = ApplicationLocator.getManager(); |
250 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
251 |
return view;
|
252 |
} |
253 |
|
254 |
private FLyrVect getActiveLayer(IView vista) {
|
255 |
if (vista != null) { |
256 |
ViewDocument viewDocument = vista.getViewDocument(); |
257 |
FLayer[] actives =
|
258 |
viewDocument.getMapContext().getLayers().getActives(); |
259 |
|
260 |
if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
261 |
return (FLyrVect) actives[0]; |
262 |
} |
263 |
} |
264 |
return null; |
265 |
} |
266 |
|
267 |
private boolean showWarnings(List<String> warnings){ |
268 |
final int MAXWARNINGSSHOWED=15; |
269 |
|
270 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
271 |
logger.debug("Warnings found accepting edit route panel");
|
272 |
StringBuilder messageBuilder= new StringBuilder().append(i18nManager.getTranslation("warnings_in_edit_route")); |
273 |
String newLine=System.getProperty("line.separator"); |
274 |
messageBuilder.append(newLine); |
275 |
for (int i=0;(i<=MAXWARNINGSSHOWED&&i<warnings.size());i++){ |
276 |
if(i<MAXWARNINGSSHOWED){
|
277 |
String warning=warnings.get(i);
|
278 |
messageBuilder.append(newLine); |
279 |
messageBuilder.append(warning); |
280 |
} |
281 |
if (i==MAXWARNINGSSHOWED&&warnings.size()>MAXWARNINGSSHOWED){
|
282 |
messageBuilder.append(newLine); |
283 |
int warningsNotShowed=warnings.size()-MAXWARNINGSSHOWED;
|
284 |
messageBuilder.append(MessageFormat.format(
|
285 |
i18nManager.getTranslation("more_warnings_found"), warningsNotShowed));
|
286 |
} |
287 |
} |
288 |
messageBuilder.append(newLine); |
289 |
messageBuilder.append(newLine).append(i18nManager.getTranslation("accept_warnings_question"));
|
290 |
int result =JOptionPane.showConfirmDialog( |
291 |
null,
|
292 |
messageBuilder, |
293 |
i18nManager.getTranslation("warnings_dialog"), JOptionPane.YES_NO_OPTION); |
294 |
if (result==JOptionPane.YES_OPTION)return true; |
295 |
else return false; |
296 |
} |
297 |
|
298 |
private void saveLayer(FLyrVect activeLayer,LrsEditRouteCalibrationAlgorithmParams params){ |
299 |
FeatureAttributeDescriptor fieldRoute=params.getIdRouteField(); |
300 |
String routeName=params.getSelectedRouteName();
|
301 |
Geometry modifiedGeometry=params.getModifiedGeometry(); |
302 |
|
303 |
FeatureStore featureStore=activeLayer.getFeatureStore(); |
304 |
if (!featureStore.isEditing()){
|
305 |
throw new IllegalArgumentException(); |
306 |
} |
307 |
DisposableIterator it=null;
|
308 |
EditableFeature editableFeature=null;
|
309 |
try {
|
310 |
FeatureSet selectedFeatures=featureStore.getFeatureSet(); |
311 |
it=selectedFeatures.fastIterator(); |
312 |
while (it.hasNext()&&editableFeature==null) { |
313 |
Feature feature = (Feature) it.next(); |
314 |
if (feature.get(fieldRoute.getName()) instanceof String && |
315 |
routeName.equals((String)feature.get(fieldRoute.getName()))){
|
316 |
editableFeature=feature.getEditable(); |
317 |
} |
318 |
} |
319 |
if (editableFeature==null){ |
320 |
throw new IllegalArgumentException(); |
321 |
}else{
|
322 |
editableFeature.setDefaultGeometry(modifiedGeometry); |
323 |
featureStore.update(editableFeature); |
324 |
} |
325 |
|
326 |
} catch (DataException e) {
|
327 |
logger.error("Error saving modified layer",e);
|
328 |
}finally{
|
329 |
if (it!=null){ |
330 |
it.dispose(); |
331 |
} |
332 |
} |
333 |
} |
334 |
|
335 |
/* (non-Javadoc)
|
336 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
337 |
*/
|
338 |
public void componentResized(ComponentEvent e) { |
339 |
// TODO Auto-generated method stub
|
340 |
|
341 |
} |
342 |
|
343 |
/* (non-Javadoc)
|
344 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
345 |
*/
|
346 |
public void componentMoved(ComponentEvent e) { |
347 |
// TODO Auto-generated method stub
|
348 |
|
349 |
} |
350 |
|
351 |
/* (non-Javadoc)
|
352 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
353 |
*/
|
354 |
public void componentShown(ComponentEvent e) { |
355 |
// TODO Auto-generated method stub
|
356 |
|
357 |
} |
358 |
|
359 |
/* (non-Javadoc)
|
360 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
361 |
*/
|
362 |
public void componentHidden(ComponentEvent e) { |
363 |
Component component = e.getComponent();
|
364 |
if (component == panelSelectId) {
|
365 |
panelSelectId = null;
|
366 |
} else if (component == panelController) { |
367 |
panelController = null;
|
368 |
} else {
|
369 |
if (panelSelectId != null && panelSelectId.getParent() == component) { |
370 |
panelSelectId = null;
|
371 |
} else if (panelController != null && panelController.getParent() == component) { |
372 |
panelController.close(); |
373 |
panelController = null;
|
374 |
|
375 |
} |
376 |
} |
377 |
} |
378 |
} |