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