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

History | View | Annotate | Download (5.88 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.util.HashMap;
25

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

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

    
58
        /**
59
         * Constructor
60
         * @param window
61
         * @param lyr
62
         *        The input layer is only used to select the view where the new layer be loaded
63
         */
64
        public ProcessEndActions(IWindow window, FLayer lyr) {
65
                this.window = window;
66
                this.inputLyr = lyr;
67
        }
68
        
69
        @SuppressWarnings("unchecked")
70
        public void end(Object params) {
71
                if(window != null) {
72
                        PluginServices.getMDIManager().closeWindow(window);
73
                        window = null;
74
                }
75
                if(params instanceof HashMap<?, ?>) {
76
                        HashMap<String, Object>  map = (HashMap<String, Object>) params;
77
                        String fName = (String)map.get(LayerDatatypeProcess.FILENAME);
78
                        long milis = (Long)map.get(LayerDatatypeProcess.TIME);
79
                        processFinalize(fName, milis);
80
                        RasterSwingLocator.getSwingManager().showSummaryProcessDialog(fName, milis);
81
                }
82

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

    
93
                if (RasterToolsUtil.messageBoxYesOrNot("cargar_toc", this)) {
94
                        try {
95
                                loadLayer(fileName, fileName.substring(fileName.lastIndexOf(File.separator) + 1));
96
                        } catch (RasterNotLoadException e) {
97
                                RasterToolsUtil.messageBoxError("error_load_layer", e);
98
                        }
99
                }
100
        }
101
        
102
        /**
103
         * Gets the view 
104
         * @return
105
         */
106
        private AbstractViewPanel getView() {
107
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
108
                for (int i = 0; i < w.length; i++) {
109
                        if(w[i] instanceof AbstractViewPanel) {
110
                                FLayers lyrs = ((AbstractViewPanel)w[i]).getMapControl().getMapContext().getLayers();
111
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
112
                                        FLayer lyr = lyrs.getLayer(j);
113
                                        if(inputLyr == lyr) {
114
                                                return ((AbstractViewPanel) w[i]);
115
                                        }
116
                                }
117
                        }
118
                }
119
                return null;
120
        }
121
        
122
        @SuppressWarnings("deprecation")
123
        private void loadLayer(String fileName, String layerName) throws RasterNotLoadException {
124
                if(fileName ==  null)
125
                        return;
126

    
127
                //Seleccionamos la vista de gvSIG
128
                AbstractViewPanel theView = getView();
129

    
130
                theView.getMapControl().getMapContext().beginAtomicEvent();
131

    
132
                try {
133
                        DataManager dataManager = DALLocator.getDataManager();
134
                        
135
                        ProviderServices provServ = RasterLocator.getManager().getProviderServices();
136
                        RasterDataParameters storeParameters = provServ.createParameters(fileName);
137
                        storeParameters.setURI(fileName);
138
                        
139
                        MapContextManager mcm = MapContextLocator.getMapContextManager();
140
                        
141
                        DataStore dataStore = null;
142
                        try {
143
                                dataStore = dataManager.createStore(storeParameters);
144
                        } catch (ValidateDataParametersException e) {
145
                                throw new RasterNotLoadException("Error al cargar la capa.");
146
                        } catch (InitializeException e) {
147
                                throw new RasterNotLoadException("Error al cargar la capa.");
148
                        } catch (ProviderNotRegisteredException e) {
149
                                throw new RasterNotLoadException("Error al cargar la capa.");
150
                        }
151
                        
152
                        if(layerName == null) {
153
                                int endIndex = fileName.lastIndexOf(".");
154
                                if (endIndex < 0)
155
                                        endIndex = fileName.length();
156
                                
157
                                layerName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex);
158
                        }
159
                        
160
                        FLayer lyr = mcm.createLayer(layerName, dataStore);
161
                        theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
162

    
163
                } catch (LoadLayerException e) {
164
                        throw new RasterNotLoadException("Error al cargar la capa.");
165
                } 
166
                theView.getMapControl().getMapContext().invalidate();
167
                theView.getMapControl().getMapContext().endAtomicEvent();
168
        }
169
        
170
        public void interrupted() {
171

    
172
    }
173
        
174
        public void updateProgress(int current, int total) {
175
                
176
        }
177
}