Statistics
| Revision:

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

History | View | Annotate | Download (5.86 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.DataStoreParameters;
33
import org.gvsig.fmap.dal.coverage.RasterLocator;
34
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
35
import org.gvsig.fmap.dal.coverage.util.ProviderServices;
36
import org.gvsig.fmap.dal.exception.InitializeException;
37
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
38
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
39
import org.gvsig.fmap.mapcontext.MapContextLocator;
40
import org.gvsig.fmap.mapcontext.MapContextManager;
41
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
42
import org.gvsig.fmap.mapcontext.layers.FLayer;
43
import org.gvsig.fmap.mapcontext.layers.FLayers;
44
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions;
45
import org.gvsig.raster.tools.algorithm.layerdatatype.LayerDatatypeProcess;
46
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
47
import org.gvsig.raster.tools.app.basic.raster.bean.endinfo.EndInfoDialog;
48
import org.gvsig.raster.util.RasterNotLoadException;
49

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

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

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

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

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

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

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

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

    
173
    }
174
}