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 / calibrateroute / CalibrateRouteExtension.java @ 46
History | View | Annotate | Download (10.5 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.calibrateroute; |
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.FeatureAttributeDescriptor; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
47 |
import org.gvsig.fmap.geom.DataTypes; |
48 |
import org.gvsig.fmap.geom.Geometry; |
49 |
import org.gvsig.fmap.geom.type.GeometryType; |
50 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
51 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
52 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
53 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
55 |
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
56 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
57 |
import org.gvsig.lrs.lib.api.LrsAlgorithm; |
58 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
59 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
60 |
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams; |
61 |
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
62 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
63 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
64 |
import org.gvsig.lrs.swing.api.JLrsProgressDialog; |
65 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
66 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
67 |
import org.gvsig.lrs.swing.impl.JLrsCalibrateRouteParamsController; |
68 |
import org.gvsig.lrs.swing.impl.JLrsUtils; |
69 |
import org.gvsig.tools.ToolsLocator; |
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 |
|
78 |
|
79 |
/**
|
80 |
* @author fdiaz
|
81 |
*
|
82 |
*/
|
83 |
public class CalibrateRouteExtension extends Extension { |
84 |
|
85 |
private static final Logger logger = LoggerFactory.getLogger(CalibrateRouteExtension.class); |
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, "calibrate-routes")) { |
92 |
IView view = getActiveView(); |
93 |
FLayers layers = view.getMapControl().getMapContext().getLayers(); |
94 |
|
95 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
96 |
|
97 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
98 |
|
99 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
100 |
|
101 |
JLrsCalibrateRouteParamsController panel; |
102 |
try {
|
103 |
panel = (JLrsCalibrateRouteParamsController)manager.createJLrsCalibrateRouteAlgorithmParameters(layers, null);
|
104 |
} catch (LrsNeededParameterException e2) {
|
105 |
logger.error("Error creating panel", e2);
|
106 |
JOptionPane.showMessageDialog(
|
107 |
null,
|
108 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
109 |
.append(":").append(e2.getLocalizedMessage()),
|
110 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
111 |
return;
|
112 |
} |
113 |
panel.setVisibleAceptCancel(true);
|
114 |
panel.addActionListener(new ActionListener() { |
115 |
|
116 |
public void actionPerformed(ActionEvent e) { |
117 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
118 |
if (panel.isCanceled()) {
|
119 |
return;
|
120 |
} |
121 |
LrsCalibrateRouteAlgorithmParams params = null;
|
122 |
try {
|
123 |
params = (LrsCalibrateRouteAlgorithmParams)panel.getParams(); |
124 |
} catch (LrsGettingParametersException e1) {
|
125 |
logger.error("Error getting parameters", e1);
|
126 |
JOptionPane.showMessageDialog(
|
127 |
null,
|
128 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
129 |
.append(":").append(e1.getLocalizedMessage()),
|
130 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
131 |
return;
|
132 |
} catch (LrsNeededParameterException e1) {
|
133 |
logger.warn("Error getting parameters", e1);
|
134 |
JOptionPane.showMessageDialog(
|
135 |
null,
|
136 |
i18nManager.getTranslation(e1.getMessage()), |
137 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
138 |
return;
|
139 |
} |
140 |
LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
141 |
final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
|
142 |
final SHPNewStoreParameters shpParams=(SHPNewStoreParameters)params.getNewFeatureStoreParameters();
|
143 |
|
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("calibrate_routes"),
|
150 |
WindowManager.MODE.WINDOW); |
151 |
|
152 |
Thread task = new Thread(new Runnable() { |
153 |
|
154 |
public void run() { |
155 |
try {
|
156 |
algorithm.execute(taskStatus); |
157 |
int resultLoadLayer =JOptionPane.showConfirmDialog( |
158 |
null,
|
159 |
i18nManager.getTranslation("load_layer_question"),
|
160 |
i18nManager.getTranslation("load_layer_question_title"), JOptionPane.YES_NO_OPTION); |
161 |
if (resultLoadLayer==JOptionPane.YES_OPTION){ |
162 |
loadLayer(shpParams); |
163 |
} |
164 |
} catch (Exception e) { |
165 |
logger.error("Error calibrating route", e);
|
166 |
String message = e.getMessage();
|
167 |
if (e.getCause() != null) { |
168 |
message = e.getCause().getMessage(); |
169 |
} |
170 |
JOptionPane.showMessageDialog(
|
171 |
progressDialog.asJComponent(), |
172 |
message, |
173 |
i18nManager.getTranslation("calibrate_routes"),
|
174 |
JOptionPane.ERROR_MESSAGE
|
175 |
); |
176 |
} |
177 |
} |
178 |
}); |
179 |
task.start(); |
180 |
|
181 |
} |
182 |
}); |
183 |
winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("calibrate_routes"),
|
184 |
WindowManager.MODE.WINDOW); |
185 |
} |
186 |
} |
187 |
|
188 |
/* (non-Javadoc)
|
189 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
190 |
*/
|
191 |
public void initialize() { |
192 |
registerIcons(); |
193 |
} |
194 |
|
195 |
/* (non-Javadoc)
|
196 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
197 |
*/
|
198 |
public boolean isEnabled() { |
199 |
IView view = getActiveView(); |
200 |
//FLyrVect activeLayer = getActiveLayer(view);
|
201 |
if (view!=null){ |
202 |
ViewDocument viewDocument = view.getViewDocument(); |
203 |
FLayers layers = viewDocument.getMapContext().getLayers(); |
204 |
return JLrsUtils.validatedCurveAndPointLayer(layers);
|
205 |
} |
206 |
|
207 |
return false; |
208 |
|
209 |
} |
210 |
|
211 |
/* (non-Javadoc)
|
212 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
213 |
*/
|
214 |
public boolean isVisible() { |
215 |
return true; |
216 |
} |
217 |
|
218 |
private void registerIcons() { |
219 |
IconThemeHelper.registerIcon("lrs", "pk_blue", this); |
220 |
} |
221 |
|
222 |
|
223 |
private IView getActiveView() {
|
224 |
ApplicationManager application = ApplicationLocator.getManager(); |
225 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
226 |
return view;
|
227 |
} |
228 |
|
229 |
private void loadLayer(SHPNewStoreParameters shpParams){ |
230 |
if (shpParams!=null ){ |
231 |
String name=shpParams.getFile().getName();
|
232 |
if (name.contains(".shp")){ |
233 |
name=name.substring(0,name.lastIndexOf(".shp")); |
234 |
} |
235 |
MapContextManager mapContextManager =MapContextLocator.getMapContextManager(); |
236 |
|
237 |
FLyrVect newLayer=null;
|
238 |
try {
|
239 |
newLayer = (FLyrVect) mapContextManager.createLayer(name,shpParams); |
240 |
} catch (LoadLayerException e) {
|
241 |
logger.warn("Couldn't load layer",e);
|
242 |
} |
243 |
getActiveView().getMapControl().getMapContext().getLayers().addLayer(newLayer); |
244 |
} |
245 |
} |
246 |
|
247 |
} |