Statistics
| Revision:

gvsig-raster / org.gvsig.raster.reproject / trunk / org.gvsig.raster.reproject / org.gvsig.raster.reproject.app / org.gvsig.raster.reproject.app.reprojectclient / src / main / java / org / gvsig / raster / reproject / app / ReprojectListener.java @ 1898

History | View | Annotate | Download (7.54 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
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
* as published by the Free Software Foundation; either version 2
9
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* 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
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.reproject.app;
23

    
24
import java.awt.Component;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.io.File;
28
import java.util.HashMap;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.cresques.cts.IProjection;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.ui.mdiManager.IWindow;
35
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.FLayers;
38
import org.gvsig.i18n.Messages;
39
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
40
import org.gvsig.raster.algorithm.process.IProcessActions;
41
import org.gvsig.raster.algorithm.process.ProcessException;
42
import org.gvsig.raster.algorithm.process.RasterProcess;
43
import org.gvsig.raster.fmap.layers.FLyrRaster;
44
import org.gvsig.raster.reproject.algorithm.ReprojectProcess;
45
import org.gvsig.raster.reproject.algorithm.swing.api.RasterReprojectionPanel;
46
import org.gvsig.raster.reproject.algorithm.swing.api.ReprojectionPanelDataModel;
47
import org.gvsig.raster.tools.algorithm.swing.reproject.RasterReprojectPanel;
48
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
49
import org.gvsig.raster.tools.app.basic.raster.bean.endinfo.EndInfoDialog;
50
import org.gvsig.raster.util.RasterNotLoadException;
51

    
52

    
53
/**
54
 * Actions for the reprojection panel
55
 * 
56
 * @author Nacho Brodin (nachobrodin@gmail.com)
57
 */
58
public class ReprojectListener implements ActionListener, IProcessActions {
59
        private RasterReprojectionPanel            reprojectPanel   = null;
60
        private FLayer                             lyr              = null;
61
        private ReprojectionPanelDataModel         dataModel        = null;
62
        private ReprojectWindow                    window           = null;
63

    
64
        public ReprojectListener(FLayer lyr, ReprojectionPanelDataModel dataModel, ReprojectWindow window, RasterReprojectionPanel reprojectPanel) {
65
                this.lyr = lyr;
66
                this.dataModel = dataModel;
67
                this.window = window;
68
                this.reprojectPanel = reprojectPanel;
69
        }
70

    
71
        public void actionPerformed(ActionEvent e) {
72
                if(reprojectPanel.getObjectSelected(e.getSource()) == RasterReprojectPanel.BUTTON_ACCEPT) {
73
                        double cellsize = dataModel.getCellSize();
74
                        int w = dataModel.getSize()[0];
75
                        int h = dataModel.getSize()[1];
76
                        IProjection srcPrj = dataModel.getSrcProjection();
77
                        IProjection dstPrj = dataModel.getDstProjection();
78
                        
79
                        if(cellsize <= 0 || w <= 0 || h <= 0) {
80
                                messageBoxError("wrong_size");
81
                                return;
82
                        }
83
                        
84
                        if(srcPrj == null || dstPrj == null) {
85
                                messageBoxError("wrong_projection");
86
                                return;
87
                        }
88
                        
89
                        RasterProcess process;
90
                        try {
91
                                process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("RasterReprojectionProcess");
92
                        } catch (ProcessException e1) {
93
                                messageBoxError("error_creating_algorithm");
94
                                return;
95
                        }
96
                        
97
                        String path = getPath();
98
                        if(path == null)
99
                                return;
100
                        
101
                        process.setActions(this);
102
                        process.addParam(ReprojectProcess.RASTER_STORE, ((FLyrRaster)lyr).getDataStore());
103
                        process.addParam(ReprojectProcess.SIZEX, w);
104
                        process.addParam(ReprojectProcess.SIZEY, h);
105
                        process.addParam(ReprojectProcess.PATH, path);
106
                        process.addParam(ReprojectProcess.SRC_PROJECTION, dataModel.getSrcProjection());
107
                        process.addParam(ReprojectProcess.DST_PROJECTION, dataModel.getDstProjection());
108
                        process.addParam(ReprojectProcess.CELLSIZE, dataModel.getCellSize());
109
                        process.addParam(ReprojectProcess.INTERPOLATION, dataModel.getInterpolationMethodSelected());
110
                        process.start();
111
                        
112
                }
113
                if(reprojectPanel.getObjectSelected(e.getSource()) == RasterReprojectPanel.BUTTON_CANCEL) {
114
                        if(window != null) {
115
                                PluginServices.getMDIManager().closeWindow(window);
116
                                window = null;
117
                        }
118
                }
119
        }
120
        
121
        /**
122
         * Gets the path to the file
123
         * @return
124
         */
125
        private String getPath() {
126
                String file = reprojectPanel.getFileSelected();
127
                if(file == null || file.equals("") || !file.matches("[a-zA-Z0-9_]*")) {
128
                        messageBoxError(
129
                                        Messages.getText("file_name_not_valid") + 
130
                                        "\n" + file + "\n " + 
131
                                        Messages.getText("valid_characters") + 
132
                                        " a-zA-Z0-9_");
133
                        return null;
134
                }
135
                
136
                if(!file.endsWith(".tif"))
137
                        file += ".tif";
138
                String dir = reprojectPanel.getDirectorySelected();
139
                if(!new File(dir).exists()) {
140
                        messageBoxError("directory_does_not_exists");
141
                        return null;
142
                }
143
                
144
                if(new File(dir + File.separator + file).exists()) {
145
                        if(!RasterToolsUtil.messageBoxYesOrNot(
146
                                        Messages.getText("file_exists") + 
147
                                        "\n " + (dir + File.separator + file) + "\n " + 
148
                                        Messages.getText("overwrite"), null))
149
                        return null;
150
                }
151
                return (dir + File.separator + file);
152
        }
153
        
154
        /**
155
         * Shows an error dialog with a text and a accept button 
156
         * @param msg Message to show in the dialog
157
         * @param parentWindow Parent window
158
         */
159
        public static void messageBoxError(String msg) {
160
                String string = Messages.getText("accept");
161
                Object[] options = {string};
162
                JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
163
                                        "<html>" + Messages.getText(msg).replaceAll("\n", "<br>") + "</html>",
164
                                        Messages.getText("confirmacion"),
165
                                        JOptionPane.OK_OPTION,
166
                                        JOptionPane.ERROR_MESSAGE,
167
                                        null,
168
                                        options,
169
                                        string);
170
        }
171

    
172
        @SuppressWarnings("unchecked")
173
        public void end(Object params) {
174
                if(window != null) {
175
                        PluginServices.getMDIManager().closeWindow(window);
176
                        window = null;
177
                }
178
                if(params instanceof HashMap<?, ?>) {
179
                        HashMap<String, Object>  map = (HashMap<String, Object>) params;
180
                        String fName = (String)map.get(ReprojectProcess.FILENAME);
181
                        long milis = (Long)map.get(ReprojectProcess.TIME);
182
                        processFinalize(fName, milis);
183
                        EndInfoDialog.show(fName, milis);
184
                }
185

    
186
        }
187
        
188
        /**
189
         * Acciones que se realizan al finalizar de crear los recortes de imagen.
190
         * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
191
         */
192
        private void processFinalize(String fileName, long milis) {
193
                if (!new File(fileName).exists())
194
                        return;
195

    
196
                String viewName = getViewName();
197
                if(viewName != null) {
198
                        if (RasterToolsUtil.messageBoxYesOrNot("cargar_toc", this)) {
199
                                try {
200
                                        RasterToolsUtil.loadLayer(viewName, fileName, null);
201
                                } catch (RasterNotLoadException e) {
202
                                        messageBoxError("error_load_layer");
203
                                }
204
                        }
205
                }
206
        }
207
        
208
        /**
209
         * Gets the view 
210
         * @return
211
         */
212
        private String getViewName() {
213
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
214
                for (int i = 0; i < w.length; i++) {
215
                        if(w[i] instanceof AbstractViewPanel) {
216
                                FLayers lyrs = ((AbstractViewPanel)w[i]).getMapControl().getMapContext().getLayers();
217
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
218
                                        FLayer lyr = lyrs.getLayer(j);
219
                                        if(this.lyr == lyr) {
220
                                                return PluginServices.getMDIManager().getWindowInfo((AbstractViewPanel) w[i]).getTitle();
221
                                        }
222
                                }
223
                        }
224
                }
225
                return null;
226
        }
227

    
228
        public void interrupted() {
229
        }
230
}