Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_proyeccion_al_vuelo / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / layerdatatype / ProcessEndActions.java @ 5472

History | View | Annotate | Download (6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2011-2012 Prodevelop S.L
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 */
21
package org.gvsig.raster.tools.app.basic.tool.layerdatatype;
22

    
23
import java.io.File;
24
import java.net.URI;
25
import java.net.URISyntaxException;
26
import java.util.HashMap;
27

    
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.ui.mdiManager.IWindow;
30
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.coverage.RasterLocator;
35
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
36
import org.gvsig.fmap.dal.coverage.util.ProviderServices;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
39
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.MapContextManager;
42
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontext.layers.FLayers;
45
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
46
import org.gvsig.raster.algorithm.process.IProcessActions;
47
import org.gvsig.raster.swing.RasterSwingLibrary;
48
import org.gvsig.raster.swing.RasterSwingLocator;
49
import org.gvsig.raster.tools.algorithm.layerdatatype.LayerDatatypeProcess;
50
import org.gvsig.raster.util.RasterNotLoadException;
51

    
52
/**
53
 * Actions when the process finalizes or is interrupted
54
 *
55
 * Nacho Brodin (nachobrodin@gmail.com)
56
 */
57
public class ProcessEndActions implements IProcessActions {
58
        private IWindow     window        = null;
59
        private FLayer      inputLyr      = null;
60

    
61
        /**
62
         * Constructor
63
         * @param window
64
         * @param lyr
65
         *        The input layer is only used to select the view where the new layer be loaded
66
         */
67
        public ProcessEndActions(IWindow window, FLayer lyr) {
68
                this.window = window;
69
                this.inputLyr = lyr;
70
        }
71

    
72
        @SuppressWarnings("unchecked")
73
        public void end(Object params) {
74
                if(window != null) {
75
                        PluginServices.getMDIManager().closeWindow(window);
76
                        window = null;
77
                }
78
                if(params instanceof HashMap<?, ?>) {
79
                        HashMap<String, Object>  map = (HashMap<String, Object>) params;
80
                        String fName = (String)map.get(LayerDatatypeProcess.FILENAME);
81
                        long milis = (Long)map.get(LayerDatatypeProcess.TIME);
82
                        processFinalize(fName, milis);
83
                        RasterSwingLocator.getSwingManager().showSummaryProcessDialog(fName, milis);
84
                }
85

    
86
        }
87

    
88
        /**
89
         * Acciones que se realizan al finalizar de crear los recortes de imagen.
90
         * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
91
         */
92
        private void processFinalize(String fileName, long milis) {
93
                if (!new File(fileName).exists())
94
                        return;
95

    
96
                if (RasterSwingLibrary.messageBoxYesOrNot("cargar_toc", this)) {
97
                        try {
98
                                loadLayer(fileName, fileName.substring(fileName.lastIndexOf(File.separator) + 1));
99
                        } catch (RasterNotLoadException e) {
100
                                RasterSwingLibrary.messageBoxError("error_load_layer", e);
101
                        }
102
                }
103
        }
104

    
105
        /**
106
         * Gets the view
107
         * @return
108
         */
109
        private AbstractViewPanel getView() {
110
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
111
                for (int i = 0; i < w.length; i++) {
112
                        if(w[i] instanceof AbstractViewPanel) {
113
                                FLayers lyrs = ((AbstractViewPanel)w[i]).getMapControl().getMapContext().getLayers();
114
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
115
                                        FLayer lyr = lyrs.getLayer(j);
116
                                        if(inputLyr == lyr) {
117
                                                return ((AbstractViewPanel) w[i]);
118
                                        }
119
                                }
120
                        }
121
                }
122
                return null;
123
        }
124

    
125
        @SuppressWarnings("deprecation")
126
        private void loadLayer(String fileName, String layerName) throws RasterNotLoadException {
127
                if(fileName ==  null)
128
                        return;
129

    
130
                //Seleccionamos la vista de gvSIG
131
                AbstractViewPanel theView = getView();
132

    
133
                theView.getMapControl().getMapContext().beginAtomicEvent();
134

    
135
                try {
136
                        DataManager dataManager = DALLocator.getDataManager();
137

    
138
                        ProviderServices provServ = RasterLocator.getManager().getProviderServices();
139
                        RasterDataParameters storeParameters = provServ.createParameters(fileName);
140
            File file = new File(fileName);
141
                        storeParameters.setURI(file.toURI());
142

    
143
                        MapContextManager mcm = MapContextLocator.getMapContextManager();
144

    
145
                        DataStore dataStore = null;
146
                        try {
147
                                dataStore = dataManager.createStore(storeParameters);
148
                        } catch (ValidateDataParametersException e) {
149
                                throw new RasterNotLoadException("Error al cargar la capa.");
150
                        } catch (InitializeException e) {
151
                                throw new RasterNotLoadException("Error al cargar la capa.");
152
                        } catch (ProviderNotRegisteredException e) {
153
                                throw new RasterNotLoadException("Error al cargar la capa.");
154
                        }
155

    
156
                        if(layerName == null) {
157
                                int endIndex = fileName.lastIndexOf(".");
158
                                if (endIndex < 0)
159
                                        endIndex = fileName.length();
160

    
161
                                layerName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex);
162
                        }
163

    
164
                        FLayer lyr = mcm.createLayer(layerName, dataStore);
165
                        theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
166

    
167
                } catch (LoadLayerException e) {
168
                        throw new RasterNotLoadException("Error al cargar la capa.", e);
169
                }
170
                theView.getMapControl().getMapContext().invalidate();
171
                theView.getMapControl().getMapContext().endAtomicEvent();
172
        }
173

    
174
        public void interrupted() {
175

    
176
    }
177

    
178
        public void updateProgress(int current, int total) {
179

    
180
        }
181
}