gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.app / org.gvsig.topology.app.mainplugin / src / main / java / org / gvsig / topology / app / mainplugin / TopologyExtension.java @ 9122
History | View | Annotate | Download (17.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.topology.app.mainplugin; |
25 |
|
26 |
import java.awt.event.ActionEvent; |
27 |
import java.io.File; |
28 |
import java.io.IOException; |
29 |
import java.nio.charset.StandardCharsets; |
30 |
import javax.swing.AbstractAction; |
31 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
32 |
import static javax.swing.Action.NAME; |
33 |
import javax.swing.JFileChooser; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.filechooser.FileFilter; |
36 |
import org.apache.commons.io.FileUtils; |
37 |
import org.apache.commons.io.FilenameUtils; |
38 |
import org.apache.commons.lang.StringUtils; |
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.Project; |
44 |
import org.gvsig.app.project.documents.view.ViewDocument; |
45 |
import org.gvsig.app.project.documents.view.ViewManager; |
46 |
import org.gvsig.configurableactions.ConfigurableActionsMamager; |
47 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
48 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
51 |
import org.gvsig.json.Json; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.i18n.I18nManager; |
54 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
55 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
56 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
57 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
58 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
59 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
60 |
import org.gvsig.tools.util.ToolsUtilLocator; |
61 |
import org.gvsig.topology.lib.api.TopologyLocator; |
62 |
import org.gvsig.topology.lib.api.TopologyManager; |
63 |
import org.gvsig.topology.lib.api.TopologyPlan; |
64 |
import org.gvsig.topology.swing.api.JTopologyPlanProperties; |
65 |
import org.gvsig.topology.swing.api.JTopologyReport; |
66 |
import org.gvsig.topology.swing.api.TopologySwingLocator; |
67 |
import org.gvsig.topology.swing.api.TopologySwingManager; |
68 |
|
69 |
/**
|
70 |
*
|
71 |
* @author gvSIG Team
|
72 |
*/
|
73 |
@SuppressWarnings("UseSpecificCatch") |
74 |
public class TopologyExtension extends Extension { |
75 |
|
76 |
public static final String TOPOLOGYPLAN_FILE_EXTENSION = "gvtoplan"; |
77 |
|
78 |
@Override
|
79 |
public void initialize() { |
80 |
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void postInitialize() { |
85 |
AppTopologyServices services = new AppTopologyServices();
|
86 |
|
87 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
88 |
manager.setDefaultServices(services); |
89 |
|
90 |
TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager(); |
91 |
swingManager.setDefaultServices(services); |
92 |
|
93 |
IconThemeHelper.registerIcon("topology", "topology-plan", "document-view-tree-icon", this); |
94 |
IconThemeHelper.registerIcon("topology", "topology-plan", "topology-tree-folder", this); |
95 |
IconThemeHelper.registerIcon("topology", "topology-plan", "topology-tree-project", this); |
96 |
IconThemeHelper.registerIcon("action", "tools-topology-checklayer", this); |
97 |
|
98 |
ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
99 |
cfgActionsManager.addConfigurableAction(JTopologyPlanProperties.CONFIGURABLE_PANEL_ID, new SaveTopologyPlan());
|
100 |
cfgActionsManager.addConfigurableAction(JTopologyPlanProperties.CONFIGURABLE_PANEL_ID, new LoadTopologyPlan());
|
101 |
} |
102 |
|
103 |
|
104 |
@Override
|
105 |
public void execute(String action) { |
106 |
if( StringUtils.equalsIgnoreCase("tools-topology-create-or-modify", action) ) { |
107 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
108 |
ApplicationManager application = ApplicationLocator.getManager(); |
109 |
|
110 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
111 |
TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager(); |
112 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
113 |
|
114 |
final JTopologyPlanProperties panel = swingManager.createJTopologyPlan();
|
115 |
|
116 |
TopologyPlan plan = getCurrentTopologyPlan(); |
117 |
if( plan == null ) { |
118 |
plan = manager.createTopologyPlan(); |
119 |
} |
120 |
panel.put(plan); |
121 |
|
122 |
final Dialog dlg = winManager.createDialog( |
123 |
panel.asJComponent(), |
124 |
i18n.getTranslation("_Topology_plan"),
|
125 |
null,
|
126 |
WindowManager_v2.BUTTONS_APPLY_OK_CANCEL |
127 |
); |
128 |
dlg.setButtonLabel(WindowManager_v2.BUTTON_APPLY, i18n.getTranslation("_Execute"));
|
129 |
dlg.addActionListener((ActionEvent e) -> {
|
130 |
TopologyPlan plan1; |
131 |
switch(dlg.getAction()) {
|
132 |
case WindowManager_v2.BUTTON_OK:
|
133 |
plan1 = panel.fetch(null);
|
134 |
setCurrentTopologyPlan(plan1); |
135 |
break;
|
136 |
case WindowManager_v2.BUTTON_APPLY:
|
137 |
plan1 = panel.fetch(null);
|
138 |
setCurrentTopologyPlan(plan1); |
139 |
JTopologyReport jreport = swingManager.createJTopologyReport(plan1); |
140 |
jreport.put(plan1); |
141 |
winManager.showWindow( |
142 |
jreport.asJComponent(), |
143 |
i18n.getTranslation("_Topology_plan_error_inspector") + " ("+plan1.getName()+")", |
144 |
WindowManager.MODE.TOOL |
145 |
); |
146 |
Thread th = new Thread(() -> { |
147 |
plan1.execute(); |
148 |
}, "TopologyPlan-"+ plan1.getName());
|
149 |
th.start(); |
150 |
break;
|
151 |
} |
152 |
}); |
153 |
dlg.show(WindowManager.MODE.WINDOW); |
154 |
|
155 |
} else if( StringUtils.equalsIgnoreCase("tools-topology-execute", action) ) { |
156 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
157 |
final TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager();
|
158 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
159 |
|
160 |
|
161 |
final TopologyPlan plan = getCurrentTopologyPlan();
|
162 |
if( plan == null ) { |
163 |
// TODO: Mensaje de crear plan
|
164 |
return;
|
165 |
} |
166 |
JTopologyReport panel = swingManager.createJTopologyReport(plan); |
167 |
panel.put(plan); |
168 |
winManager.showWindow( |
169 |
panel.asJComponent(), |
170 |
ToolsSwingUtils.makeTitle("_Topology_plan_error_inspector", plan.getName(), null), |
171 |
WindowManager.MODE.TOOL |
172 |
); |
173 |
|
174 |
Thread th = new Thread(() -> { |
175 |
plan.execute(); |
176 |
}, "TopologyPlan-"+ plan.getName());
|
177 |
th.start(); |
178 |
|
179 |
} else if( StringUtils.equalsIgnoreCase("tools-topology-checklayer", action) ) { |
180 |
checkCurrentLayer(); |
181 |
} |
182 |
} |
183 |
|
184 |
|
185 |
private static String getCurrentTopologyPlanAsJson() { |
186 |
ApplicationManager application = ApplicationLocator.getManager(); |
187 |
// final ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
188 |
// if( view==null ) {
|
189 |
// return null;
|
190 |
// }
|
191 |
// String jsonPlan = (String) view.getProperty("TopologyPlan");
|
192 |
Project project = application.getCurrentProject(); |
193 |
String jsonPlan = (String) project.getProperty("TopologyPlan"); |
194 |
return jsonPlan;
|
195 |
} |
196 |
|
197 |
private static boolean setCurrentTopologyPlan(String jsonPlan) { |
198 |
ApplicationManager application = ApplicationLocator.getManager(); |
199 |
// final ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
200 |
// if( view==null ) {
|
201 |
// return false;
|
202 |
// }
|
203 |
// view.setProperty("TopologyPlan", jsonPlan);
|
204 |
Project project = application.getCurrentProject(); |
205 |
project.setProperty("TopologyPlan", jsonPlan);
|
206 |
return true; |
207 |
} |
208 |
|
209 |
public static TopologyPlan getCurrentTopologyPlan() { |
210 |
String jsonPlan_s = getCurrentTopologyPlanAsJson();
|
211 |
if( StringUtils.isBlank(jsonPlan_s) ) {
|
212 |
return null; |
213 |
} |
214 |
try {
|
215 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
216 |
TopologyPlan plan = manager.createTopologyPlan(); |
217 |
plan.fromJson(Json.createObject(jsonPlan_s)); |
218 |
return plan;
|
219 |
} catch (Exception ex) { |
220 |
throw new RuntimeException("Can't retrieve topology plan from json.",ex); |
221 |
} |
222 |
} |
223 |
|
224 |
public static boolean setCurrentTopologyPlan(TopologyPlan plan) { |
225 |
return setCurrentTopologyPlan(plan.toJson().toString());
|
226 |
} |
227 |
|
228 |
@Override
|
229 |
public boolean isEnabled() { |
230 |
// ApplicationManager application = ApplicationLocator.getManager();
|
231 |
// Document view = application.getActiveDocument(ViewManager.TYPENAME);
|
232 |
// return view!=null;
|
233 |
return true; |
234 |
} |
235 |
|
236 |
@Override
|
237 |
public boolean isVisible() { |
238 |
return true; |
239 |
} |
240 |
|
241 |
private static class SaveTopologyPlan extends AbstractAction { |
242 |
|
243 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
244 |
public SaveTopologyPlan() {
|
245 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
246 |
|
247 |
this.putValue(NAME, i18n.getTranslation("_Save_topology_plan")); |
248 |
this.putValue(ACTION_COMMAND_KEY, "SaveTopologyPLanToFile"); |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public void actionPerformed(ActionEvent e) { |
253 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
254 |
|
255 |
JTopologyPlanProperties panel = (JTopologyPlanProperties) e.getSource(); |
256 |
TopologyPlan plan = panel.fetch(null);
|
257 |
|
258 |
// _Select_topology_plan_file
|
259 |
// Select topology plan file
|
260 |
// Seleccionar archivo de plan de topologĂa
|
261 |
|
262 |
FileDialogChooserManager fileDislogs = ToolsUtilLocator.getFileDialogChooserManager(); |
263 |
FileDialogChooser dialog = fileDislogs.create("TopologyPlan");
|
264 |
dialog.setCurrentDirectory(null);
|
265 |
dialog.setDialogTitle(i18n.getTranslation("_Select_topology_plan_file"));
|
266 |
dialog.setMultiSelectionEnabled(false);
|
267 |
dialog.addChoosableFileFilter(new FileFilter() { |
268 |
@Override
|
269 |
public boolean accept(File f) { |
270 |
return f.getName().endsWith(","+TOPOLOGYPLAN_FILE_EXTENSION); |
271 |
} |
272 |
|
273 |
@Override
|
274 |
public String getDescription() { |
275 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
276 |
return i18n.getTranslation("_Topology_plan"); |
277 |
} |
278 |
}); |
279 |
int n = dialog.showSaveDialog(panel.asJComponent());
|
280 |
if( n == JFileChooser.APPROVE_OPTION ) { |
281 |
File file = dialog.getSelectedFile();
|
282 |
if( FilenameUtils.indexOfExtension(file.getName())<0 ) { |
283 |
file = new File(file.getAbsolutePath()+"."+TOPOLOGYPLAN_FILE_EXTENSION); |
284 |
} |
285 |
try {
|
286 |
String fcontents = plan.toJson().toString();
|
287 |
FileUtils.write(file, fcontents, StandardCharsets.UTF_8); |
288 |
} catch (IOException ex) { |
289 |
logger.warn("Can't save topology plan",ex);
|
290 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
291 |
dialogs.messageDialog( |
292 |
i18n.getTranslation("_Cant_save_topology_plan"),
|
293 |
i18n.getTranslation("_Topology_plan"),
|
294 |
JOptionPane.WARNING_MESSAGE
|
295 |
); |
296 |
} |
297 |
} |
298 |
} |
299 |
} |
300 |
|
301 |
private static class LoadTopologyPlan extends AbstractAction { |
302 |
|
303 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
304 |
public LoadTopologyPlan() {
|
305 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
306 |
|
307 |
this.putValue(NAME, i18n.getTranslation("_Load_topology_plan")); |
308 |
this.putValue(ACTION_COMMAND_KEY, "LoadTopologyPLanFromFile"); |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public void actionPerformed(ActionEvent e) { |
313 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
314 |
|
315 |
JTopologyPlanProperties panel = (JTopologyPlanProperties) e.getSource(); |
316 |
|
317 |
FileDialogChooserManager fileDislogs = ToolsUtilLocator.getFileDialogChooserManager(); |
318 |
FileDialogChooser dialog = fileDislogs.create("TopologyPlan");
|
319 |
dialog.setCurrentDirectory(null);
|
320 |
dialog.setDialogTitle(i18n.getTranslation("_Select_topology_plan_file"));
|
321 |
dialog.setMultiSelectionEnabled(false);
|
322 |
dialog.addChoosableFileFilter(new FileFilter() { |
323 |
@Override
|
324 |
public boolean accept(File f) { |
325 |
if( f.isDirectory() ) {
|
326 |
return true; |
327 |
} |
328 |
return FilenameUtils.isExtension(f.getName(), TOPOLOGYPLAN_FILE_EXTENSION);
|
329 |
} |
330 |
|
331 |
@Override
|
332 |
public String getDescription() { |
333 |
return "Topology plan ("+TOPOLOGYPLAN_FILE_EXTENSION+")"; |
334 |
} |
335 |
}); |
336 |
int n = dialog.showOpenDialog(panel.asJComponent());
|
337 |
if( n == JFileChooser.APPROVE_OPTION ) { |
338 |
File file = dialog.getSelectedFile();
|
339 |
try {
|
340 |
String fcontents = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
341 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
342 |
TopologyPlan plan = manager.createTopologyPlan(); |
343 |
plan.fromJson(Json.createObject(fcontents)); |
344 |
panel.put(plan); |
345 |
} catch (IOException ex) { |
346 |
logger.warn("Can't load topology plan",ex);
|
347 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
348 |
dialogs.messageDialog( |
349 |
i18n.getTranslation("_Cant_load_topology_plan"),
|
350 |
i18n.getTranslation("_Topology_plan"),
|
351 |
JOptionPane.WARNING_MESSAGE
|
352 |
); |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
|
358 |
private void checkCurrentLayer() { |
359 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
360 |
|
361 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
362 |
if( viewdoc == null ) { |
363 |
application.messageDialog( |
364 |
"There must be an active layer in the active View.",
|
365 |
"Check geometries",
|
366 |
JOptionPane.WARNING_MESSAGE
|
367 |
); |
368 |
} |
369 |
FLayer[] activeLayers = viewdoc.getMapContext().getLayers().getActives();
|
370 |
if( activeLayers==null || activeLayers.length!=1 || !(activeLayers[0] instanceof VectorLayer) ) { |
371 |
application.messageDialog( |
372 |
"There must be an active vector layer in the active View.",
|
373 |
"Check geometries",
|
374 |
JOptionPane.WARNING_MESSAGE
|
375 |
); |
376 |
return;
|
377 |
} |
378 |
VectorLayer layer = (VectorLayer) activeLayers[0];
|
379 |
|
380 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
381 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
382 |
TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager(); |
383 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
384 |
|
385 |
AppTopologyServices services = new AppTopologyServices();
|
386 |
manager.setDefaultServices(services); |
387 |
swingManager.setDefaultServices(services); |
388 |
|
389 |
TopologyPlan plan = manager.createTopologyPlan(); |
390 |
plan.setName(i18n.getTranslation("_Validate_geometries"));
|
391 |
plan.addDataSet(layer.getName(), layer.getFeatureStore()); |
392 |
plan.addRule(TopologyManager.RULE_GEOMETRYISVALID_ID, layer.getName(), null, 0); |
393 |
plan.addRule(TopologyManager.RULE_GEOMETRYMUSTNOTBENULL_ID, layer.getName(), null, 0); |
394 |
|
395 |
JTopologyReport panel = swingManager.createJTopologyReport(plan); |
396 |
panel.put(plan); |
397 |
winManager.showWindow( |
398 |
panel.asJComponent(), |
399 |
ToolsSwingUtils.makeTitle("_Topology_plan_error_inspector", plan.getName(), layer.getName()),
|
400 |
WindowManager.MODE.TOOL |
401 |
); |
402 |
|
403 |
Thread th = new Thread(() -> { |
404 |
plan.execute(); |
405 |
}, "TopologyPlan-"+ plan.getName());
|
406 |
th.start(); |
407 |
} |
408 |
} |