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 / generatedynamicsegmentation / GenerateDynamicSegmentationExtension.java @ 24
History | View | Annotate | Download (11.2 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.generatedynamicsegmentation; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
|
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
import org.gvsig.andami.IconThemeHelper; |
37 |
import org.gvsig.andami.plugins.Extension; |
38 |
import org.gvsig.app.ApplicationLocator; |
39 |
import org.gvsig.app.ApplicationManager; |
40 |
import org.gvsig.app.project.documents.Document; |
41 |
import org.gvsig.app.project.documents.table.TableDocument; |
42 |
import org.gvsig.app.project.documents.table.TableManager; |
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.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
47 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
48 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
49 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
51 |
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
52 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
53 |
import org.gvsig.lrs.lib.api.LrsAlgorithm; |
54 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
55 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
56 |
import org.gvsig.lrs.lib.api.LrsGenerateDynamicSegmentationAlgorithmParams; |
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.JLrsProgressDialog; |
61 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
62 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
63 |
import org.gvsig.lrs.swing.impl.JLrsGenerateDynamicSegmentationParamsController; |
64 |
import org.gvsig.lrs.swing.impl.JLrsUtils; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.i18n.I18nManager; |
67 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
69 |
import org.gvsig.tools.task.SimpleTaskStatus; |
70 |
|
71 |
|
72 |
/**
|
73 |
* @author dmartinez
|
74 |
*
|
75 |
*/
|
76 |
public class GenerateDynamicSegmentationExtension extends Extension { |
77 |
|
78 |
private static final Logger logger = LoggerFactory.getLogger(GenerateDynamicSegmentationExtension.class); |
79 |
|
80 |
/* (non-Javadoc)
|
81 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
82 |
*/
|
83 |
public void execute(String actionCommand) { |
84 |
if (StringUtils.equalsIgnoreCase(actionCommand, "generate-dynamic-segmentation")) { |
85 |
IView view = getActiveView(); |
86 |
FLayers layers = view.getMapControl().getMapContext().getLayers(); |
87 |
List<FeatureStore> tables=getTables();
|
88 |
|
89 |
|
90 |
final WindowManager winManager = ToolsSwingLocator.getWindowManager();
|
91 |
|
92 |
final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
|
93 |
|
94 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
95 |
|
96 |
JLrsGenerateDynamicSegmentationParamsController panel; |
97 |
try {
|
98 |
panel = (JLrsGenerateDynamicSegmentationParamsController)manager.createJLrsGenerateDynamicSegmentationAlgorithmParameters(layers,tables, null);
|
99 |
} catch (LrsNeededParameterException e2) {
|
100 |
logger.error("Error creating panel", e2);
|
101 |
JOptionPane.showMessageDialog(
|
102 |
null,
|
103 |
new StringBuilder().append(i18nManager.getTranslation("error_creating_panel")) |
104 |
.append(":").append(e2.getLocalizedMessage()),
|
105 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
106 |
return;
|
107 |
} |
108 |
panel.setVisibleAceptCancel(true);
|
109 |
panel.addActionListener(new ActionListener() { |
110 |
|
111 |
public void actionPerformed(ActionEvent e) { |
112 |
JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource(); |
113 |
if (panel.isCanceled()) {
|
114 |
return;
|
115 |
} |
116 |
LrsGenerateDynamicSegmentationAlgorithmParams params = null;
|
117 |
try {
|
118 |
params = (LrsGenerateDynamicSegmentationAlgorithmParams)panel.getParams(); |
119 |
} catch (LrsGettingParametersException e1) {
|
120 |
logger.error("Error getting parameters", e1);
|
121 |
JOptionPane.showMessageDialog(
|
122 |
null,
|
123 |
new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters")) |
124 |
.append(":").append(e1.getLocalizedMessage()),
|
125 |
i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE); |
126 |
return;
|
127 |
} catch (LrsNeededParameterException e1) {
|
128 |
logger.warn("Error getting parameters", e1);
|
129 |
JOptionPane.showMessageDialog(
|
130 |
null,
|
131 |
i18nManager.getTranslation(e1.getMessage()), |
132 |
i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); |
133 |
return;
|
134 |
} |
135 |
|
136 |
LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
137 |
final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
|
138 |
final SHPNewStoreParameters shpParams=(SHPNewStoreParameters)params.getNewFeatureStoreParameters();
|
139 |
|
140 |
final SimpleTaskStatus taskStatus =
|
141 |
ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18nManager.getTranslation("progress"));
|
142 |
|
143 |
final JLrsProgressDialog progressDialog = manager.createJLrsProgressDialog(taskStatus);
|
144 |
|
145 |
winManager.showWindow(progressDialog.asJComponent(), i18nManager.getTranslation("generate_dynamic_segmentation"),
|
146 |
WindowManager.MODE.WINDOW); |
147 |
|
148 |
Thread task = new Thread(new Runnable() { |
149 |
|
150 |
public void run() { |
151 |
try {
|
152 |
algorithm.execute(taskStatus); |
153 |
int resultLoadLayer =JOptionPane.showConfirmDialog( |
154 |
null,
|
155 |
i18nManager.getTranslation("load_layer_question"),
|
156 |
i18nManager.getTranslation("load_layer_question_title"), JOptionPane.YES_NO_OPTION); |
157 |
if (resultLoadLayer==JOptionPane.YES_OPTION){ |
158 |
loadLayer(shpParams); |
159 |
} |
160 |
|
161 |
} catch (Exception e) { |
162 |
logger.error("Error generating dynamic segmentation ", e);
|
163 |
String message = e.getMessage();
|
164 |
if (e.getCause() != null) { |
165 |
message = e.getCause().getMessage(); |
166 |
} |
167 |
JOptionPane.showMessageDialog(
|
168 |
progressDialog.asJComponent(), |
169 |
message, |
170 |
i18nManager.getTranslation("generate_dynamic_segmentation"),
|
171 |
JOptionPane.ERROR_MESSAGE
|
172 |
); |
173 |
} |
174 |
} |
175 |
}); |
176 |
task.start(); |
177 |
|
178 |
} |
179 |
}); |
180 |
winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("generate_dynamic_segmentation"),
|
181 |
WindowManager.MODE.WINDOW); |
182 |
} |
183 |
} |
184 |
|
185 |
/* (non-Javadoc)
|
186 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
187 |
*/
|
188 |
public void initialize() { |
189 |
registerIcons(); |
190 |
} |
191 |
|
192 |
/* (non-Javadoc)
|
193 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
194 |
*/
|
195 |
public boolean isEnabled() { |
196 |
IView view = getActiveView(); |
197 |
if (view!=null){ |
198 |
ViewDocument viewDocument = view.getViewDocument(); |
199 |
LayerCollection layers = viewDocument.getMapContext().getLayers(); |
200 |
if (JLrsUtils.validateMLayerWithNonGeomField(layers)){
|
201 |
List<FeatureStore> tables=getTables();
|
202 |
if (tables!=null&&tables.size()>0){ |
203 |
return JLrsUtils.validateStoreWithFieldsAndOneNumeric(tables);
|
204 |
} |
205 |
} |
206 |
} |
207 |
return false; |
208 |
} |
209 |
|
210 |
/* (non-Javadoc)
|
211 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
212 |
*/
|
213 |
public boolean isVisible() { |
214 |
return true; |
215 |
} |
216 |
|
217 |
private void registerIcons() { |
218 |
IconThemeHelper.registerIcon("lrs", "pk_yellow", this); |
219 |
} |
220 |
|
221 |
private List<FeatureStore> getTables(){ |
222 |
ApplicationManager application = ApplicationLocator.getManager(); |
223 |
List<Document> documents=application.getCurrentProject().getDocuments(TableManager.TYPENAME); |
224 |
List<FeatureStore> tables=new ArrayList<FeatureStore>(); |
225 |
if (documents!=null){ |
226 |
for (int i = 0; i < documents.size(); i++) { |
227 |
TableDocument tableDocument=(TableDocument)documents.get(i); |
228 |
tables.add(tableDocument.getStore()); |
229 |
} |
230 |
} |
231 |
return tables;
|
232 |
} |
233 |
|
234 |
private IView getActiveView() {
|
235 |
ApplicationManager application = ApplicationLocator.getManager(); |
236 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
237 |
return view;
|
238 |
} |
239 |
|
240 |
private void loadLayer(SHPNewStoreParameters shpParams){ |
241 |
if (shpParams!=null ){ |
242 |
String name=shpParams.getFile().getName();
|
243 |
if (name.contains(".shp")){ |
244 |
name=name.substring(0,name.lastIndexOf(".shp")); |
245 |
} |
246 |
MapContextManager mapContextManager =MapContextLocator.getMapContextManager(); |
247 |
|
248 |
FLyrVect newLayer=null;
|
249 |
try {
|
250 |
newLayer = (FLyrVect) mapContextManager.createLayer(name,shpParams); |
251 |
} catch (LoadLayerException e) {
|
252 |
logger.warn("Couldn't load layer",e);
|
253 |
} |
254 |
getActiveView().getMapControl().getMapContext().getLayers().addLayer(newLayer); |
255 |
} |
256 |
} |
257 |
|
258 |
} |