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 / createroute / CreateRouteExtension.java @ 23
History | View | Annotate | Download (10.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.createroute; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
|
28 |
import javax.swing.JOptionPane; |
29 |
|
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.bouncycastle.util.Store; |
32 |
import org.cresques.cts.IProjection; |
33 |
import org.gvsig.andami.IconThemeHelper; |
34 |
import org.gvsig.andami.plugins.Extension; |
35 |
import org.gvsig.app.ApplicationLocator; |
36 |
import org.gvsig.app.ApplicationManager; |
37 |
import org.gvsig.app.project.documents.view.ViewDocument; |
38 |
import org.gvsig.app.project.documents.view.gui.IView; |
39 |
import org.gvsig.fmap.dal.DALLocator; |
40 |
import org.gvsig.fmap.dal.DataManager; |
41 |
import org.gvsig.fmap.dal.DataStore; |
42 |
import org.gvsig.fmap.dal.DataStoreParameters; |
43 |
import org.gvsig.fmap.dal.exception.InitializeException; |
44 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
45 |
import org.gvsig.fmap.dal.exception.ReadException; |
46 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
47 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
48 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.type.GeometryType; |
51 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
52 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
53 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
55 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
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.LrsCreateRouteAlgorithmParams; |
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.JLrsCreateRouteParamsController; |
68 |
import org.gvsig.lrs.swing.impl.JLrsUtils; |
69 |
import org.gvsig.tools.ToolsLocator; |
70 |
import org.gvsig.tools.i18n.I18nManager; |
71 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
72 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
73 |
import org.gvsig.tools.task.SimpleTaskStatus; |
74 |
import org.slf4j.Logger; |
75 |
import org.slf4j.LoggerFactory; |
76 |
|
77 |
|
78 |
/**
|
79 |
* @author fdiaz
|
80 |
*
|
81 |
*/
|
82 |
public class CreateRouteExtension extends Extension { |
83 |
|
84 |
private static final Logger logger = LoggerFactory.getLogger(CreateRouteExtension.class); |
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, "create-routes")) { |
91 |
IView view = getActiveView(); |
92 |
FLayers layers = view.getMapControl().getMapContext().getLayers(); |
93 |
|
94 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
95 |
|
96 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
97 |
|
98 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
99 |
|
100 |
JLrsCreateRouteParamsController panel; |
101 |
try {
|
102 |
panel = (JLrsCreateRouteParamsController)manager.createJLrsCreateRouteAlgorithmParameters(layers, null);
|
103 |
} catch (LrsNeededParameterException e2) {
|
104 |
logger.error("Error creating panel", e2);
|
105 |
JOptionPane.showMessageDialog(
|
106 |
null,
|
107 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
108 |
.append(":").append(e2.getLocalizedMessage()),
|
109 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
110 |
return;
|
111 |
} |
112 |
panel.setVisibleAceptCancel(true);
|
113 |
panel.addActionListener(new ActionListener() { |
114 |
|
115 |
public void actionPerformed(ActionEvent e) { |
116 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
117 |
if (panel.isCanceled()) {
|
118 |
return;
|
119 |
} |
120 |
LrsCreateRouteAlgorithmParams params = null;
|
121 |
try {
|
122 |
params = (LrsCreateRouteAlgorithmParams)panel.getParams(); |
123 |
} catch (LrsGettingParametersException e1) {
|
124 |
logger.error("Error getting parameters", e1);
|
125 |
JOptionPane.showMessageDialog(
|
126 |
null,
|
127 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
128 |
.append(":").append(e1.getLocalizedMessage()),
|
129 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
130 |
return;
|
131 |
} catch (LrsNeededParameterException e1) {
|
132 |
logger.warn("Error getting parameters", e1);
|
133 |
JOptionPane.showMessageDialog(
|
134 |
null,
|
135 |
i18nManager.getTranslation(e1.getMessage()), |
136 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
137 |
return;
|
138 |
} |
139 |
LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
140 |
final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
|
141 |
final SHPNewStoreParameters shpParams=(SHPNewStoreParameters)params.getNewFeatureStoreParameters();
|
142 |
|
143 |
final SimpleTaskStatus taskStatus =
|
144 |
ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18nManager.getTranslation("progress"));
|
145 |
|
146 |
final JLrsProgressDialog progressDialog = manager.createJLrsProgressDialog(taskStatus);
|
147 |
|
148 |
winManager.showWindow(progressDialog.asJComponent(), i18nManager.getTranslation("create_routes"),
|
149 |
WindowManager.MODE.WINDOW); |
150 |
|
151 |
Thread task = new Thread(new Runnable() { |
152 |
|
153 |
public void run() { |
154 |
try {
|
155 |
algorithm.execute(taskStatus); |
156 |
int resultLoadLayer =JOptionPane.showConfirmDialog( |
157 |
null,
|
158 |
i18nManager.getTranslation("load_layer_question"),
|
159 |
i18nManager.getTranslation("load_layer_question_title"), JOptionPane.YES_NO_OPTION); |
160 |
if (resultLoadLayer==JOptionPane.YES_OPTION){ |
161 |
loadLayer(shpParams); |
162 |
} |
163 |
} catch (Exception e) { |
164 |
logger.error("Error creating routes", e);
|
165 |
String message = e.getMessage();
|
166 |
if (e.getCause() != null) { |
167 |
message = e.getCause().getMessage(); |
168 |
} |
169 |
JOptionPane.showMessageDialog(
|
170 |
progressDialog.asJComponent(), |
171 |
message, |
172 |
i18nManager.getTranslation("create_routes"),
|
173 |
JOptionPane.ERROR_MESSAGE
|
174 |
); |
175 |
} |
176 |
} |
177 |
}); |
178 |
task.start(); |
179 |
} |
180 |
}); |
181 |
winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("create_routes"),
|
182 |
WindowManager.MODE.WINDOW); |
183 |
} |
184 |
} |
185 |
|
186 |
/* (non-Javadoc)
|
187 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
188 |
*/
|
189 |
public void initialize() { |
190 |
registerIcons(); |
191 |
} |
192 |
|
193 |
/* (non-Javadoc)
|
194 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
195 |
*/
|
196 |
public boolean isEnabled() { |
197 |
IView view = getActiveView(); |
198 |
if (view!=null){ |
199 |
FLayers layers = view.getMapControl().getMapContext().getLayers(); |
200 |
return JLrsUtils.validateOneCurveLyrOneNonGeomField(layers);
|
201 |
}else return false; |
202 |
|
203 |
} |
204 |
|
205 |
/* (non-Javadoc)
|
206 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
207 |
*/
|
208 |
public boolean isVisible() { |
209 |
return true; |
210 |
} |
211 |
|
212 |
private void registerIcons() { |
213 |
IconThemeHelper.registerIcon("lrs", "pk_red", this); |
214 |
} |
215 |
|
216 |
|
217 |
private IView getActiveView() {
|
218 |
ApplicationManager application = ApplicationLocator.getManager(); |
219 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
220 |
return view;
|
221 |
} |
222 |
|
223 |
private void loadLayer(SHPNewStoreParameters shpParams){ |
224 |
if (shpParams!=null ){ |
225 |
String name=shpParams.getFile().getName();
|
226 |
if (name.contains(".shp")){ |
227 |
name=name.substring(0,name.lastIndexOf(".shp")); |
228 |
} |
229 |
MapContextManager mapContextManager =MapContextLocator.getMapContextManager(); |
230 |
|
231 |
FLyrVect newLayer=null;
|
232 |
try {
|
233 |
newLayer = (FLyrVect) mapContextManager.createLayer(name,shpParams); |
234 |
} catch (LoadLayerException e) {
|
235 |
logger.warn("Couldn't load layer",e);
|
236 |
} |
237 |
getActiveView().getMapControl().getMapContext().getLayers().addLayer(newLayer); |
238 |
} |
239 |
} |
240 |
|
241 |
} |