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 @ 43926
History | View | Annotate | Download (9.48 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40557 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 41494 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 40435 | jjdelcerro | * 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 | 41494 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40435 | jjdelcerro | *
|
21 | 40557 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | 43926 | jjdelcerro | package org.gvsig.export.app.extension; |
25 | 40435 | jjdelcerro | |
26 | 43926 | jjdelcerro | import java.awt.Dimension; |
27 | import javax.swing.JComponent; |
||
28 | import javax.swing.JOptionPane; |
||
29 | import org.cresques.cts.ICoordTrans; |
||
30 | 40435 | jjdelcerro | import org.cresques.cts.IProjection; |
31 | |||
32 | import org.gvsig.andami.IconThemeHelper; |
||
33 | import org.gvsig.andami.plugins.Extension; |
||
34 | import org.gvsig.app.ApplicationLocator; |
||
35 | import org.gvsig.app.ApplicationManager; |
||
36 | 41494 | jjdelcerro | import org.gvsig.app.project.ProjectManager; |
37 | 40435 | jjdelcerro | import org.gvsig.app.project.documents.view.ViewDocument; |
38 | import org.gvsig.app.project.documents.view.ViewManager; |
||
39 | 43926 | jjdelcerro | import org.gvsig.export.ExportLocator; |
40 | import org.gvsig.export.ExportManager; |
||
41 | import org.gvsig.export.ExportProcess; |
||
42 | import org.gvsig.export.Filter; |
||
43 | import org.gvsig.export.spi.ExportServiceFactory; |
||
44 | import org.gvsig.export.swing.ExportFinishListenerAdapter; |
||
45 | import org.gvsig.export.swing.ExportSwingLocator; |
||
46 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
47 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
48 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
49 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
50 | 41878 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
51 | 42615 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypes; |
52 | 41878 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
53 | 41494 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
54 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
55 | 42615 | jjdelcerro | import org.gvsig.tools.util.ArrayUtils; |
56 | 43926 | jjdelcerro | import org.gvsig.export.swing.ExportSwingManager; |
57 | import org.gvsig.export.swing.JExportProcessPanel; |
||
58 | import org.gvsig.fmap.dal.DALLocator; |
||
59 | import org.gvsig.fmap.dal.DataManager; |
||
60 | import org.gvsig.fmap.dal.DataStore; |
||
61 | import org.gvsig.fmap.dal.OpenDataStoreParameters; |
||
62 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
63 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
64 | 40435 | jjdelcerro | |
65 | 42615 | jjdelcerro | |
66 | 40435 | jjdelcerro | /**
|
67 | * @author gvSIG Team
|
||
68 | * @version $Id$
|
||
69 | 41494 | jjdelcerro | *
|
70 | 40435 | jjdelcerro | */
|
71 | 43926 | jjdelcerro | public class ExportLayerExtension extends Extension { |
72 | 40435 | jjdelcerro | |
73 | 42615 | jjdelcerro | @Override
|
74 | 40435 | jjdelcerro | public void initialize() { |
75 | IconThemeHelper.registerIcon("action", "layer-export", this); |
||
76 | 43926 | jjdelcerro | IconThemeHelper.registerIcon("picker", "picker-crs", this); |
77 | 40435 | jjdelcerro | } |
78 | |||
79 | @Override
|
||
80 | public void postInitialize() { |
||
81 | super.postInitialize();
|
||
82 | 41494 | jjdelcerro | ProjectManager projectManager = ApplicationLocator.getProjectManager(); |
83 | ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME); |
||
84 | viewManager.addTOCContextAction("layer-exportto");
|
||
85 | |||
86 | 40435 | jjdelcerro | } |
87 | |||
88 | 42615 | jjdelcerro | @Override
|
89 | 40435 | jjdelcerro | public void execute(String actionCommand) { |
90 | 42615 | jjdelcerro | this.execute(actionCommand, null); |
91 | } |
||
92 | |||
93 | @Override
|
||
94 | public void execute(String command, Object[] args) { |
||
95 | if( !"exportto".equalsIgnoreCase(command) ) { |
||
96 | return;
|
||
97 | } |
||
98 | Object layer = ArrayUtils.get(args, 0); |
||
99 | if( layer instanceof FLyrVect ) { |
||
100 | WindowManager.MODE mode = WindowManager.MODE.fromInteger( |
||
101 | (Integer)ArrayUtils.get(args, 1,DataTypes.INT, 0) |
||
102 | ); |
||
103 | FLyrVect layervect = (FLyrVect) layer; |
||
104 | MapContext mapContext = layervect.getMapContext(); |
||
105 | 43926 | jjdelcerro | this.showExport(layervect, mapContext.getProjection(), mapContext, mode);
|
106 | 42615 | jjdelcerro | return;
|
107 | } |
||
108 | |||
109 | 40435 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
110 | 42615 | jjdelcerro | ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
111 | 41494 | jjdelcerro | if ( view != null ) { |
112 | 40435 | jjdelcerro | MapContext mapContext = view.getMapContext(); |
113 | FLayers layers = mapContext.getLayers(); |
||
114 | FLayer[] actives = layers.getActives();
|
||
115 | try {
|
||
116 | 41494 | jjdelcerro | if ( actives.length == 1 ) { |
117 | 43926 | jjdelcerro | if( actives[0] instanceof FLyrVect ) { |
118 | showExport( |
||
119 | (FLyrVect) actives[0],
|
||
120 | mapContext.getProjection(), |
||
121 | mapContext, |
||
122 | WindowManager.MODE.WINDOW |
||
123 | ); |
||
124 | 40435 | jjdelcerro | } |
125 | 41494 | jjdelcerro | } else {
|
126 | 43926 | jjdelcerro | for (FLayer theLayer : actives) {
|
127 | if (layer instanceof FLyrVect) { |
||
128 | showExport( |
||
129 | (FLyrVect) theLayer, |
||
130 | mapContext.getProjection(), |
||
131 | mapContext, |
||
132 | WindowManager.MODE.DIALOG |
||
133 | ); |
||
134 | 41494 | jjdelcerro | } |
135 | } |
||
136 | 40435 | jjdelcerro | } |
137 | } catch (Exception e) { |
||
138 | 43926 | jjdelcerro | logger.warn("Can't run export wizard.", e);
|
139 | 40435 | jjdelcerro | } |
140 | } |
||
141 | } |
||
142 | |||
143 | 43926 | jjdelcerro | public void showExport( |
144 | FLyrVect layer, |
||
145 | IProjection contextProjetion, |
||
146 | final MapContext mapContext,
|
||
147 | WindowManager.MODE mode |
||
148 | ) { |
||
149 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
150 | ExportManager manager = ExportLocator.getManager(); |
||
151 | ExportSwingManager swingManager = ExportSwingLocator.getSwingManager(); |
||
152 | 40435 | jjdelcerro | |
153 | 43926 | jjdelcerro | final ExportProcess process = manager.createProcess();
|
154 | process.setSourceFeatureStore(layer.getFeatureStore()); |
||
155 | process.setContextProjection(contextProjetion); |
||
156 | process.setSourceProjection(layer.getProjection()); |
||
157 | ICoordTrans ct = layer.getCoordTrans(); |
||
158 | if( ct==null ) { |
||
159 | process.setSourceTransformation(null);
|
||
160 | } else {
|
||
161 | process.setSourceProjection(ct.getPOrig()); |
||
162 | process.setSourceTransformation(ct); |
||
163 | } |
||
164 | JExportProcessPanel panel = swingManager.createJExportProcessPanel( |
||
165 | process, |
||
166 | new Filter<ExportServiceFactory>() { |
||
167 | @Override
|
||
168 | public boolean apply(ExportServiceFactory factory) { |
||
169 | return factory.hasVectorialSupport();
|
||
170 | } |
||
171 | } |
||
172 | ); |
||
173 | panel.addFinishListener(new ExportFinishListenerAdapter() {
|
||
174 | @Override
|
||
175 | public void finished(JExportProcessPanel exportPanel) { |
||
176 | doAddlayer(process, mapContext); |
||
177 | } |
||
178 | }); |
||
179 | JComponent component = panel.asJComponent();
|
||
180 | component.setPreferredSize(new Dimension(component.getPreferredSize().width, 530)); |
||
181 | 41878 | jjdelcerro | |
182 | 41494 | jjdelcerro | WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
183 | 43926 | jjdelcerro | winmgr.showWindow( |
184 | panel.asJComponent(), |
||
185 | i18n.getTranslation("_Export_to"),
|
||
186 | mode |
||
187 | ); |
||
188 | } |
||
189 | 40435 | jjdelcerro | |
190 | 43926 | jjdelcerro | private void doAddlayer(ExportProcess process, MapContext mapContext) { |
191 | ApplicationManager application = ApplicationLocator.getManager(); |
||
192 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
193 | |||
194 | int n = application.confirmDialog(
|
||
195 | i18n.getTranslation("_Add_new_layer_to_current_view"),
|
||
196 | i18n.getTranslation("_Export"),
|
||
197 | JOptionPane.YES_NO_OPTION,
|
||
198 | JOptionPane.QUESTION_MESSAGE,
|
||
199 | "AddExportedLayerToView"
|
||
200 | ); |
||
201 | if( n != JOptionPane.YES_OPTION ) { |
||
202 | return;
|
||
203 | } |
||
204 | try {
|
||
205 | DataManager dataManager = DALLocator.getDataManager(); |
||
206 | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
||
207 | |||
208 | for (OpenDataStoreParameters params : process.getTargetOpenStoreParameters()) {
|
||
209 | DataStore store = dataManager.openStore(params.getDataStoreName(), params); |
||
210 | FLayer layer = mapContextManager.createLayer(store.getName(), store); |
||
211 | mapContext.getLayers().addLayer(layer); |
||
212 | } |
||
213 | application.messageDialog( |
||
214 | i18n.getTranslation("_Layer_added_to_view"),
|
||
215 | null,
|
||
216 | i18n.getTranslation("_Export"),
|
||
217 | JOptionPane.QUESTION_MESSAGE,
|
||
218 | "ExportedLayerAddedToView"
|
||
219 | ); |
||
220 | } catch (Exception ex) { |
||
221 | logger.warn("Can't add exported layer to view",ex);
|
||
222 | application.messageDialog( |
||
223 | i18n.getTranslation("_Cant_add_exported_layer_to_view"),
|
||
224 | null,
|
||
225 | i18n.getTranslation("_Export"),
|
||
226 | JOptionPane.WARNING_MESSAGE,
|
||
227 | "CantAddExportedLayerToView"
|
||
228 | ); |
||
229 | } |
||
230 | 40435 | jjdelcerro | } |
231 | |||
232 | 42615 | jjdelcerro | @Override
|
233 | 40435 | jjdelcerro | public boolean isEnabled() { |
234 | ApplicationManager application = ApplicationLocator.getManager(); |
||
235 | 42615 | jjdelcerro | ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
236 | 41494 | jjdelcerro | if ( view != null ) { |
237 | 43926 | jjdelcerro | return view.getMapContext().hasActiveVectorLayers();
|
238 | 40435 | jjdelcerro | } |
239 | return false; |
||
240 | } |
||
241 | |||
242 | 42615 | jjdelcerro | @Override
|
243 | 40435 | jjdelcerro | public boolean isVisible() { |
244 | 42615 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
245 | ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
246 | return view != null; |
||
247 | 40435 | jjdelcerro | } |
248 | 41494 | jjdelcerro | |
249 | 40435 | jjdelcerro | } |