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

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