Statistics
| Revision:

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 @ 44395

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