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 @ 4568
History | View | Annotate | Download (14.6 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.configurableactions.ConfigurableActionsMamager; |
45 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
46 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
47 |
import org.gvsig.json.Json; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.i18n.I18nManager; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
51 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
52 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
53 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
54 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
55 |
import org.gvsig.tools.util.ToolsUtilLocator; |
56 |
import org.gvsig.topology.lib.api.TopologyLocator; |
57 |
import org.gvsig.topology.lib.api.TopologyManager; |
58 |
import org.gvsig.topology.lib.api.TopologyPlan; |
59 |
import org.gvsig.topology.swing.api.JTopologyPlanProperties; |
60 |
import org.gvsig.topology.swing.api.JTopologyReport; |
61 |
import org.gvsig.topology.swing.api.TopologySwingLocator; |
62 |
import org.gvsig.topology.swing.api.TopologySwingManager; |
63 |
|
64 |
/**
|
65 |
*
|
66 |
* @author gvSIG Team
|
67 |
*/
|
68 |
@SuppressWarnings("UseSpecificCatch") |
69 |
public class TopologyExtension extends Extension { |
70 |
|
71 |
public static final String TOPOLOGYPLAN_FILE_EXTENSION = "gvtoplan"; |
72 |
|
73 |
@Override
|
74 |
public void initialize() { |
75 |
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void postInitialize() { |
80 |
AppTopologyServices services = new AppTopologyServices();
|
81 |
|
82 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
83 |
manager.setDefaultServices(services); |
84 |
|
85 |
TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager(); |
86 |
swingManager.setDefaultServices(services); |
87 |
|
88 |
IconThemeHelper.registerIcon("document", "document-view-tree-icon", this); |
89 |
IconThemeHelper.registerIcon("topology", "topology-tree-folder", this); |
90 |
IconThemeHelper.registerIcon("topology", "topology-tree-project", this); |
91 |
|
92 |
ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
93 |
cfgActionsManager.addConfigurableAction(JTopologyPlanProperties.CONFIGURABLE_PANEL_ID, new SaveTopologyPlan());
|
94 |
cfgActionsManager.addConfigurableAction(JTopologyPlanProperties.CONFIGURABLE_PANEL_ID, new LoadTopologyPlan());
|
95 |
} |
96 |
|
97 |
|
98 |
@Override
|
99 |
public void execute(String action) { |
100 |
if( StringUtils.equalsIgnoreCase("tools-topology-create-or-modify", action) ) { |
101 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
102 |
ApplicationManager application = ApplicationLocator.getManager(); |
103 |
|
104 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
105 |
TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager(); |
106 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
107 |
|
108 |
final JTopologyPlanProperties panel = swingManager.createJTopologyPlan();
|
109 |
|
110 |
TopologyPlan plan = getCurrentTopologyPlan(); |
111 |
if( plan == null ) { |
112 |
plan = manager.createTopologyPlan(); |
113 |
} |
114 |
panel.put(plan); |
115 |
|
116 |
final Dialog dlg = winManager.createDialog( |
117 |
panel.asJComponent(), |
118 |
i18n.getTranslation("_Topology_plan"),
|
119 |
null,
|
120 |
WindowManager_v2.BUTTONS_APPLY_OK_CANCEL |
121 |
); |
122 |
dlg.setButtonLabel(WindowManager_v2.BUTTON_APPLY, i18n.getTranslation("_Execute"));
|
123 |
dlg.addActionListener((ActionEvent e) -> {
|
124 |
TopologyPlan plan1; |
125 |
switch(dlg.getAction()) {
|
126 |
case WindowManager_v2.BUTTON_OK:
|
127 |
plan1 = panel.fetch(null);
|
128 |
setCurrentTopologyPlan(plan1); |
129 |
break;
|
130 |
case WindowManager_v2.BUTTON_APPLY:
|
131 |
plan1 = panel.fetch(null);
|
132 |
setCurrentTopologyPlan(plan1); |
133 |
JTopologyReport jreport = swingManager.createJTopologyReport(plan1); |
134 |
jreport.put(plan1); |
135 |
winManager.showWindow( |
136 |
jreport.asJComponent(), |
137 |
i18n.getTranslation("_Topology_plan_error_inspector") + " ("+plan1.getName()+")", |
138 |
WindowManager.MODE.TOOL |
139 |
); |
140 |
Thread th = new Thread(() -> { |
141 |
plan1.execute(); |
142 |
}, "TopologyPlan-"+ plan1.getName());
|
143 |
th.start(); |
144 |
break;
|
145 |
} |
146 |
}); |
147 |
dlg.show(WindowManager.MODE.WINDOW); |
148 |
|
149 |
} else if( StringUtils.equalsIgnoreCase("tools-topology-execute", action) ) { |
150 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
151 |
final TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager();
|
152 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
153 |
|
154 |
|
155 |
final TopologyPlan plan = getCurrentTopologyPlan();
|
156 |
if( plan == null ) { |
157 |
// TODO: Mensaje de crear plan
|
158 |
return;
|
159 |
} |
160 |
JTopologyReport panel = swingManager.createJTopologyReport(plan); |
161 |
panel.put(plan); |
162 |
winManager.showWindow( |
163 |
panel.asJComponent(), |
164 |
i18n.getTranslation("_Topology_plan_error_inspector") + " ("+plan.getName()+")", |
165 |
WindowManager.MODE.TOOL |
166 |
); |
167 |
|
168 |
Thread th = new Thread(() -> { |
169 |
plan.execute(); |
170 |
}, "TopologyPlan-"+ plan.getName());
|
171 |
th.start(); |
172 |
} |
173 |
} |
174 |
|
175 |
|
176 |
private static String getCurrentTopologyPlanAsJson() { |
177 |
ApplicationManager application = ApplicationLocator.getManager(); |
178 |
// final ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
179 |
// if( view==null ) {
|
180 |
// return null;
|
181 |
// }
|
182 |
// String jsonPlan = (String) view.getProperty("TopologyPlan");
|
183 |
Project project = application.getCurrentProject(); |
184 |
String jsonPlan = (String) project.getProperty("TopologyPlan"); |
185 |
return jsonPlan;
|
186 |
} |
187 |
|
188 |
private static boolean setCurrentTopologyPlan(String jsonPlan) { |
189 |
ApplicationManager application = ApplicationLocator.getManager(); |
190 |
// final ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
|
191 |
// if( view==null ) {
|
192 |
// return false;
|
193 |
// }
|
194 |
// view.setProperty("TopologyPlan", jsonPlan);
|
195 |
Project project = application.getCurrentProject(); |
196 |
project.setProperty("TopologyPlan", jsonPlan);
|
197 |
return true; |
198 |
} |
199 |
|
200 |
public static TopologyPlan getCurrentTopologyPlan() { |
201 |
String jsonPlan_s = getCurrentTopologyPlanAsJson();
|
202 |
if( StringUtils.isBlank(jsonPlan_s) ) {
|
203 |
return null; |
204 |
} |
205 |
try {
|
206 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
207 |
TopologyPlan plan = manager.createTopologyPlan(); |
208 |
plan.fromJson(Json.createObject(jsonPlan_s)); |
209 |
return plan;
|
210 |
} catch (Exception ex) { |
211 |
throw new RuntimeException("Can't retrieve topology plan from json.",ex); |
212 |
} |
213 |
} |
214 |
|
215 |
public static boolean setCurrentTopologyPlan(TopologyPlan plan) { |
216 |
return setCurrentTopologyPlan(plan.toJson().toString());
|
217 |
} |
218 |
|
219 |
@Override
|
220 |
public boolean isEnabled() { |
221 |
// ApplicationManager application = ApplicationLocator.getManager();
|
222 |
// Document view = application.getActiveDocument(ViewManager.TYPENAME);
|
223 |
// return view!=null;
|
224 |
return true; |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public boolean isVisible() { |
229 |
return true; |
230 |
} |
231 |
|
232 |
private static class SaveTopologyPlan extends AbstractAction { |
233 |
|
234 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
235 |
public SaveTopologyPlan() {
|
236 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
237 |
|
238 |
this.putValue(NAME, i18n.getTranslation("_Save_topology_plan")); |
239 |
this.putValue(ACTION_COMMAND_KEY, "SaveTopologyPLanToFile"); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void actionPerformed(ActionEvent e) { |
244 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
245 |
|
246 |
JTopologyPlanProperties panel = (JTopologyPlanProperties) e.getSource(); |
247 |
TopologyPlan plan = panel.fetch(null);
|
248 |
|
249 |
// _Select_topology_plan_file
|
250 |
// Select topology plan file
|
251 |
// Seleccionar archivo de plan de topologĂa
|
252 |
|
253 |
FileDialogChooserManager fileDislogs = ToolsUtilLocator.getFileDialogChooserManager(); |
254 |
FileDialogChooser dialog = fileDislogs.create("TopologyPlan");
|
255 |
dialog.setCurrentDirectory(null);
|
256 |
dialog.setDialogTitle(i18n.getTranslation("_Select_topology_plan_file"));
|
257 |
dialog.setMultiSelectionEnabled(false);
|
258 |
dialog.addChoosableFileFilter(new FileFilter() { |
259 |
@Override
|
260 |
public boolean accept(File f) { |
261 |
return f.getName().endsWith(","+TOPOLOGYPLAN_FILE_EXTENSION); |
262 |
} |
263 |
|
264 |
@Override
|
265 |
public String getDescription() { |
266 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
267 |
return i18n.getTranslation("_Topology_plan"); |
268 |
} |
269 |
}); |
270 |
int n = dialog.showSaveDialog(panel.asJComponent());
|
271 |
if( n == JFileChooser.APPROVE_OPTION ) { |
272 |
File file = dialog.getSelectedFile();
|
273 |
if( FilenameUtils.indexOfExtension(file.getName())<0 ) { |
274 |
file = new File(file.getAbsolutePath()+"."+TOPOLOGYPLAN_FILE_EXTENSION); |
275 |
} |
276 |
try {
|
277 |
String fcontents = plan.toJson().toString();
|
278 |
FileUtils.write(file, fcontents, StandardCharsets.UTF_8); |
279 |
} catch (IOException ex) { |
280 |
logger.warn("Can't save topology plan",ex);
|
281 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
282 |
dialogs.messageDialog( |
283 |
i18n.getTranslation("_Cant_save_topology_plan"),
|
284 |
i18n.getTranslation("_Topology_plan"),
|
285 |
JOptionPane.WARNING_MESSAGE
|
286 |
); |
287 |
} |
288 |
} |
289 |
} |
290 |
} |
291 |
|
292 |
private static class LoadTopologyPlan extends AbstractAction { |
293 |
|
294 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
295 |
public LoadTopologyPlan() {
|
296 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
297 |
|
298 |
this.putValue(NAME, i18n.getTranslation("_Load_topology_plan")); |
299 |
this.putValue(ACTION_COMMAND_KEY, "LoadTopologyPLanFromFile"); |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void actionPerformed(ActionEvent e) { |
304 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
305 |
|
306 |
JTopologyPlanProperties panel = (JTopologyPlanProperties) e.getSource(); |
307 |
|
308 |
FileDialogChooserManager fileDislogs = ToolsUtilLocator.getFileDialogChooserManager(); |
309 |
FileDialogChooser dialog = fileDislogs.create("TopologyPlan");
|
310 |
dialog.setCurrentDirectory(null);
|
311 |
dialog.setDialogTitle(i18n.getTranslation("_Select_topology_plan_file"));
|
312 |
dialog.setMultiSelectionEnabled(false);
|
313 |
dialog.addChoosableFileFilter(new FileFilter() { |
314 |
@Override
|
315 |
public boolean accept(File f) { |
316 |
if( f.isDirectory() ) {
|
317 |
return true; |
318 |
} |
319 |
return FilenameUtils.isExtension(f.getName(), TOPOLOGYPLAN_FILE_EXTENSION);
|
320 |
} |
321 |
|
322 |
@Override
|
323 |
public String getDescription() { |
324 |
return "Topology plan ("+TOPOLOGYPLAN_FILE_EXTENSION+")"; |
325 |
} |
326 |
}); |
327 |
int n = dialog.showOpenDialog(panel.asJComponent());
|
328 |
if( n == JFileChooser.APPROVE_OPTION ) { |
329 |
File file = dialog.getSelectedFile();
|
330 |
try {
|
331 |
String fcontents = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
332 |
TopologyManager manager = TopologyLocator.getTopologyManager(); |
333 |
TopologyPlan plan = manager.createTopologyPlan(); |
334 |
plan.fromJson(Json.createObject(fcontents)); |
335 |
panel.put(plan); |
336 |
} catch (IOException ex) { |
337 |
logger.warn("Can't load topology plan",ex);
|
338 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
339 |
dialogs.messageDialog( |
340 |
i18n.getTranslation("_Cant_load_topology_plan"),
|
341 |
i18n.getTranslation("_Topology_plan"),
|
342 |
JOptionPane.WARNING_MESSAGE
|
343 |
); |
344 |
} |
345 |
} |
346 |
} |
347 |
} |
348 |
} |