svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.exportto.app / org.gvsig.exportto.app.mainplugin / src / main / java / org / gvsig / export / app / extension / ExportLayerExtension.java @ 45602
History | View | Annotate | Download (12.3 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.export.app.extension; |
25 |
|
26 |
import java.awt.Dimension; |
27 |
import javax.swing.JComponent; |
28 |
import javax.swing.JOptionPane; |
29 |
import javax.swing.SwingUtilities; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.cresques.cts.ICoordTrans; |
32 |
import org.cresques.cts.IProjection; |
33 |
|
34 |
import org.gvsig.andami.IconThemeHelper; |
35 |
import org.gvsig.andami.plugins.Extension; |
36 |
import org.gvsig.app.ApplicationLocator; |
37 |
import org.gvsig.app.ApplicationManager; |
38 |
import org.gvsig.app.project.ProjectManager; |
39 |
import org.gvsig.app.project.documents.view.ViewDocument; |
40 |
import org.gvsig.app.project.documents.view.ViewManager; |
41 |
import org.gvsig.export.ExportLocator; |
42 |
import org.gvsig.export.ExportManager; |
43 |
import org.gvsig.export.ExportProcess; |
44 |
import org.gvsig.export.Filter; |
45 |
import org.gvsig.export.spi.ExportServiceFactory; |
46 |
import org.gvsig.export.swing.ExportFinishListenerAdapter; |
47 |
import org.gvsig.export.swing.ExportSwingLocator; |
48 |
import org.gvsig.fmap.mapcontext.MapContext; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.dataTypes.DataTypes; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
56 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
57 |
import org.gvsig.tools.util.ArrayUtils; |
58 |
import org.gvsig.export.swing.ExportSwingManager; |
59 |
import org.gvsig.export.swing.JExportProcessPanel; |
60 |
import org.gvsig.fmap.dal.DALLocator; |
61 |
import org.gvsig.fmap.dal.DataManager; |
62 |
import org.gvsig.fmap.dal.DataStore; |
63 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
64 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
65 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
66 |
import org.gvsig.tools.dynobject.DynObject; |
67 |
import org.gvsig.tools.observer.Notification; |
68 |
import org.gvsig.tools.observer.Observable; |
69 |
import org.gvsig.tools.observer.Observer; |
70 |
|
71 |
|
72 |
/**
|
73 |
* @author gvSIG Team
|
74 |
* @version $Id$
|
75 |
*
|
76 |
*/
|
77 |
public class ExportLayerExtension extends Extension { |
78 |
|
79 |
@Override
|
80 |
public void initialize() { |
81 |
IconThemeHelper.registerIcon("action", "layer-export", this); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void postInitialize() { |
86 |
super.postInitialize();
|
87 |
ProjectManager projectManager = ApplicationLocator.getProjectManager(); |
88 |
ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME); |
89 |
viewManager.addTOCContextAction("layer-exportto");
|
90 |
registerInCatalog(); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public void execute(String actionCommand) { |
95 |
this.execute(actionCommand, null); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void execute(String command, Object[] args) { |
100 |
if( !"exportto".equalsIgnoreCase(command) ) { |
101 |
return;
|
102 |
} |
103 |
Object layer = ArrayUtils.get(args, 0); |
104 |
if( layer instanceof FLyrVect ) { |
105 |
WindowManager.MODE mode = WindowManager.MODE.fromInteger( |
106 |
(Integer)ArrayUtils.get(args, 1,DataTypes.INT, 0) |
107 |
); |
108 |
FLyrVect layervect = (FLyrVect) layer; |
109 |
MapContext mapContext = layervect.getMapContext(); |
110 |
this.showExport(layervect, mapContext.getProjection(), mapContext, mode);
|
111 |
return;
|
112 |
} |
113 |
|
114 |
ApplicationManager application = ApplicationLocator.getManager(); |
115 |
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
116 |
if ( view != null ) { |
117 |
MapContext mapContext = view.getMapContext(); |
118 |
FLayers layers = mapContext.getLayers(); |
119 |
FLayer[] actives = layers.getActives();
|
120 |
try {
|
121 |
if ( actives.length == 1 ) { |
122 |
if( actives[0] instanceof FLyrVect ) { |
123 |
showExport( |
124 |
(FLyrVect) actives[0],
|
125 |
mapContext.getProjection(), |
126 |
mapContext, |
127 |
WindowManager.MODE.WINDOW |
128 |
); |
129 |
} |
130 |
} else {
|
131 |
for (FLayer theLayer : actives) {
|
132 |
if (layer instanceof FLyrVect) { |
133 |
showExport( |
134 |
(FLyrVect) theLayer, |
135 |
mapContext.getProjection(), |
136 |
mapContext, |
137 |
WindowManager.MODE.DIALOG |
138 |
); |
139 |
} |
140 |
} |
141 |
} |
142 |
} catch (Exception e) { |
143 |
logger.warn("Can't run export wizard.", e);
|
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
public void showExport( |
149 |
FLyrVect layer, |
150 |
IProjection contextProjetion, |
151 |
final MapContext mapContext,
|
152 |
WindowManager.MODE mode |
153 |
) { |
154 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
155 |
ExportManager manager = ExportLocator.getManager(); |
156 |
ExportSwingManager swingManager = ExportSwingLocator.getSwingManager(); |
157 |
|
158 |
final ExportProcess process = manager.createProcess();
|
159 |
process.setContext(mapContext); |
160 |
process.setSourceFeatureStore(layer.getFeatureStore()); |
161 |
process.setContextProjection(contextProjetion); |
162 |
process.setSourceProjection(layer.getProjection()); |
163 |
ICoordTrans ct = layer.getCoordTrans(); |
164 |
if( ct==null ) { |
165 |
process.setSourceTransformation(null);
|
166 |
} else {
|
167 |
process.setSourceProjection(ct.getPOrig()); |
168 |
process.setSourceTransformation(ct); |
169 |
} |
170 |
JExportProcessPanel panel = swingManager.createJExportProcessPanel( |
171 |
process, |
172 |
new Filter<ExportServiceFactory>() { |
173 |
@Override
|
174 |
public boolean apply(ExportServiceFactory factory) { |
175 |
return factory.hasVectorialSupport();
|
176 |
} |
177 |
} |
178 |
); |
179 |
panel.addFinishListener(new ExportFinishListenerAdapter() {
|
180 |
@Override
|
181 |
public void finished(JExportProcessPanel exportPanel) { |
182 |
doAddlayer(process, mapContext); |
183 |
} |
184 |
}); |
185 |
JComponent component = panel.asJComponent();
|
186 |
component.setPreferredSize(new Dimension(component.getPreferredSize().width, 530)); |
187 |
|
188 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
189 |
winmgr.showWindow( |
190 |
panel.asJComponent(), |
191 |
i18n.getTranslation("_Export_to"),
|
192 |
mode |
193 |
); |
194 |
} |
195 |
|
196 |
private void doAddlayer(final ExportProcess process, final MapContext mapContext) { |
197 |
if (!SwingUtilities.isEventDispatchThread()) { |
198 |
SwingUtilities.invokeLater(new Runnable() { |
199 |
@Override
|
200 |
public void run() { |
201 |
doAddlayer(process, mapContext); |
202 |
} |
203 |
}); |
204 |
return;
|
205 |
} |
206 |
|
207 |
ApplicationManager application = ApplicationLocator.getManager(); |
208 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
209 |
|
210 |
if (process.getTargetOpenStoreParameters()==null) { |
211 |
return;
|
212 |
} |
213 |
|
214 |
int n = application.confirmDialog(
|
215 |
i18n.getTranslation("_Add_new_layer_to_current_view"),
|
216 |
i18n.getTranslation("_Export"),
|
217 |
JOptionPane.YES_NO_OPTION,
|
218 |
JOptionPane.QUESTION_MESSAGE,
|
219 |
"AddExportedLayerToView"
|
220 |
); |
221 |
if( n != JOptionPane.YES_OPTION ) { |
222 |
return;
|
223 |
} |
224 |
try {
|
225 |
DataManager dataManager = DALLocator.getDataManager(); |
226 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
227 |
|
228 |
for (OpenDataStoreParameters params : process.getTargetOpenStoreParameters()) {
|
229 |
DataStore store = dataManager.openStore(params.getDataStoreName(), params); |
230 |
FLayer layer = mapContextManager.createLayer(store.getName(), store); |
231 |
mapContext.getLayers().addLayer(layer); |
232 |
} |
233 |
application.messageDialog( |
234 |
i18n.getTranslation("_Layer_added_to_view"),
|
235 |
null,
|
236 |
i18n.getTranslation("_Export"),
|
237 |
JOptionPane.QUESTION_MESSAGE,
|
238 |
"ExportedLayerAddedToView"
|
239 |
); |
240 |
} catch (Exception ex) { |
241 |
logger.warn("Can't add exported layer to view",ex);
|
242 |
application.messageDialog( |
243 |
i18n.getTranslation("_Cant_add_exported_layer_to_view"),
|
244 |
null,
|
245 |
i18n.getTranslation("_Export"),
|
246 |
JOptionPane.WARNING_MESSAGE,
|
247 |
"CantAddExportedLayerToView"
|
248 |
); |
249 |
} |
250 |
} |
251 |
|
252 |
@Override
|
253 |
public boolean isEnabled() { |
254 |
ApplicationManager application = ApplicationLocator.getManager(); |
255 |
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
256 |
if ( view != null ) { |
257 |
return view.getMapContext().hasActiveVectorLayers();
|
258 |
} |
259 |
return false; |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public boolean isVisible() { |
264 |
ApplicationManager application = ApplicationLocator.getManager(); |
265 |
ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
266 |
return view != null; |
267 |
} |
268 |
|
269 |
private void registerInCatalog() { |
270 |
final ApplicationManager application = ApplicationLocator.getApplicationManager();
|
271 |
application.addPropertiesObserver(new Observer() { |
272 |
@Override
|
273 |
public void update(Observable observable, Object notification) { |
274 |
if( !(notification instanceof Notification) ) { |
275 |
return;
|
276 |
} |
277 |
String propertyName = (String) ((Notification)notification).getValue(0); |
278 |
if( !StringUtils.equalsIgnoreCase(propertyName, "Catalog.Manager") ) { |
279 |
return;
|
280 |
} |
281 |
DynObject catalogManager = (DynObject) application.getProperty("Catalog.Manager");
|
282 |
if (catalogManager == null) { |
283 |
return;
|
284 |
} |
285 |
try {
|
286 |
String[] catalogNodes = new String[]{ |
287 |
"STORES_REPOSITORY_TABLE",
|
288 |
"DATABASE_TABLE",
|
289 |
"FOLDERS_FILE",
|
290 |
"BOOKMARKS_BOOKMARK"
|
291 |
}; |
292 |
for (String catalogNode : catalogNodes) { |
293 |
Object action = catalogManager.invokeDynMethod(
|
294 |
"getAction",
|
295 |
new Object[]{ |
296 |
ExportStoreFromCatalogAction.NAME, |
297 |
catalogNode |
298 |
} |
299 |
); |
300 |
if (action == null) { |
301 |
catalogManager.invokeDynMethod( |
302 |
"addAction",
|
303 |
new Object[]{ |
304 |
ExportStoreFromCatalogAction.NAME, |
305 |
catalogNode, |
306 |
new ExportStoreFromCatalogAction()
|
307 |
} |
308 |
); |
309 |
} |
310 |
} |
311 |
} catch (Exception ex) { |
312 |
logger.warn("Can't add action to catalog", ex);
|
313 |
} |
314 |
} |
315 |
}); |
316 |
} |
317 |
|
318 |
|
319 |
} |