Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / layerdatatype / ProcessEndActions.java @ 4181

History | View | Annotate | Download (6.12 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.raster.algorithm.process.IProcessActions;
46
import org.gvsig.raster.swing.RasterSwingLibrary;
47
import org.gvsig.raster.swing.RasterSwingLocator;
48
import org.gvsig.raster.tools.algorithm.layerdatatype.LayerDatatypeProcess;
49
import org.gvsig.raster.util.RasterNotLoadException;
50

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

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

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

    
85
        }
86

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

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

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

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

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

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

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

    
137
                        ProviderServices provServ = RasterLocator.getManager().getProviderServices();
138
                        RasterDataParameters storeParameters = provServ.createParameters(fileName);
139
                        URI uri;
140
            try {
141
                uri = new URI(fileName);
142
            } catch (URISyntaxException e1) {
143
                throw new RasterNotLoadException("Can't create URI from "+fileName, e1);
144
            }
145
                        storeParameters.setURI(uri);
146

    
147
                        MapContextManager mcm = MapContextLocator.getMapContextManager();
148

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

    
160
                        if(layerName == null) {
161
                                int endIndex = fileName.lastIndexOf(".");
162
                                if (endIndex < 0)
163
                                        endIndex = fileName.length();
164

    
165
                                layerName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex);
166
                        }
167

    
168
                        FLayer lyr = mcm.createLayer(layerName, dataStore);
169
                        theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
170

    
171
                } catch (LoadLayerException e) {
172
                        throw new RasterNotLoadException("Error al cargar la capa.", e);
173
                }
174
                theView.getMapControl().getMapContext().invalidate();
175
                theView.getMapControl().getMapContext().endAtomicEvent();
176
        }
177

    
178
        public void interrupted() {
179

    
180
    }
181

    
182
        public void updateProgress(int current, int total) {
183

    
184
        }
185
}