Revision 1255

View differences:

org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.app/org.gvsig.raster.tools.app.reproject/src/main/java/org/gvsig/raster/tools/app/reproject/ReprojectListener.java
25 25
import java.awt.event.ActionEvent;
26 26
import java.awt.event.ActionListener;
27 27
import java.io.File;
28
import java.util.HashMap;
28 29

  
29 30
import javax.swing.JOptionPane;
30 31

  
......
90 91
				return;
91 92
			}
92 93
			
93
			String file = reprojectPanel.getFileSelected();
94
			File f = new File(file);
95
			if(!f.isFile())
96
				file = System.getProperty("java.io.tmpdir") + File.separator + file + ".tif";
97
			if(!file.endsWith(".tif"))
98
				file = file + ".tif";
94
			String path = getPath();
95
			if(path == null)
96
				return;
99 97
			
100 98
			process.setActions(this);
101 99
			process.addParam(ReprojectProcess.RASTER_STORE, ((FLyrRaster)lyr).getDataStore());
102 100
			process.addParam(ReprojectProcess.SIZEX, w);
103 101
			process.addParam(ReprojectProcess.SIZEY, h);
104
			process.addParam(ReprojectProcess.PATH, file);
102
			process.addParam(ReprojectProcess.PATH, path);
105 103
			process.addParam(ReprojectProcess.SRC_PROJECTION, dataModel.getSrcProjection());
106 104
			process.addParam(ReprojectProcess.DST_PROJECTION, dataModel.getDstProjection());
107 105
			process.addParam(ReprojectProcess.CELLSIZE, dataModel.getCellSize());
......
117 115
	}
118 116
	
119 117
	/**
118
	 * Gets the path to the file
119
	 * @return
120
	 */
121
	private String getPath() {
122
		String file = reprojectPanel.getFileSelected();
123
		if(file == null || file.equals("") || !file.matches("[a-zA-Z0-9_]*")) {
124
			messageBoxError(
125
					Messages.getText("file_name_not_valid") + 
126
					"\n" + file + "\n " + 
127
					Messages.getText("valid_characters") + 
128
					" a-zA-Z0-9_");
129
			return null;
130
		}
131
		
132
		if(!file.endsWith(".tif"))
133
			file += ".tif";
134
		String dir = reprojectPanel.getDirectorySelected();
135
		if(!new File(dir).exists()) {
136
			messageBoxError("directory_does_not_exists");
137
			return null;
138
		}
139
		
140
		if(new File(dir + File.separator + file).exists()) {
141
			if(!RasterToolsUtil.messageBoxYesOrNot(
142
					Messages.getText("file_exists") + 
143
					"\n " + (dir + File.separator + file) + "\n " + 
144
					Messages.getText("overwrite"), null))
145
			return null;
146
		}
147
		return (dir + File.separator + file);
148
	}
149
	
150
	/**
120 151
	 * Shows an error dialog with a text and a accept button 
121 152
	 * @param msg Message to show in the dialog
122 153
	 * @param parentWindow Parent window
......
134 165
					string);
135 166
	}
136 167

  
168
	/*
169
	 * (non-Javadoc)
170
	 * @see org.gvsig.raster.tools.algorithm.base.process.IProcessActions#end(java.lang.Object)
171
	 */
172
	@SuppressWarnings("unchecked")
137 173
	public void end(Object params) {
138 174
		if(window != null) {
139 175
			PluginServices.getMDIManager().closeWindow(window);
140 176
			window = null;
141 177
		}
142
		if(	params instanceof Object[] &&
143
				((Object[])params).length == 2 &&
144
				((Object[])params)[0] instanceof String &&
145
				((Object[])params)[1] instanceof Long) {
146

  
147
			String fName = (String)((Object[])params)[0];
148
			long milis = ((Long)((Object[])params)[1]).longValue();
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);
149 182
			processFinalize(fName, milis);
150 183
			EndInfoDialog.show(fName, milis);
151 184
		}
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.app/org.gvsig.raster.tools.app.reproject/src/main/java/org/gvsig/raster/tools/app/reproject/ReprojectTocMenuEntry.java
21 21
*/
22 22
package org.gvsig.raster.tools.app.reproject;
23 23

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

  
29 24
import javax.swing.Icon;
30
import javax.swing.JOptionPane;
31 25

  
32
import org.cresques.cts.IProjection;
33 26
import org.gvsig.andami.IconThemeHelper;
34 27
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
37 28
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction;
38 29
import org.gvsig.app.project.documents.view.toc.ITocItem;
39
import org.gvsig.fmap.dal.coverage.exception.ProcessException;
40 30
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.FLayers;
42 31
import org.gvsig.i18n.Messages;
43 32
import org.gvsig.raster.fmap.layers.FLyrRaster;
44 33
import org.gvsig.raster.fmap.layers.IRasterLayerActions;
45 34
import org.gvsig.raster.swing.newlayer.FileNameManagement;
46
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary;
47
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions;
48
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess;
49
import org.gvsig.raster.tools.algorithm.reproject.ReprojectProcess;
50 35
import org.gvsig.raster.tools.algorithm.swing.AlgorithmSwingLocator;
51 36
import org.gvsig.raster.tools.algorithm.swing.reproject.RasterReprojectPanel;
52 37
import org.gvsig.raster.tools.algorithm.swing.reproject.ReprojectPanelDataModel;
53
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
54
import org.gvsig.raster.tools.app.basic.raster.bean.endinfo.EndInfoDialog;
55 38
import org.gvsig.raster.tools.app.basic.raster.gui.IGenericToolBarMenuItem;
56
import org.gvsig.raster.util.RasterNotLoadException;
57 39

  
58 40

  
59 41
/**
60 42
 * @author Nacho Brodin (nachobrodin@gmail.com)
61 43
 */
62 44
public class ReprojectTocMenuEntry extends AbstractTocContextMenuAction 
63
	implements IGenericToolBarMenuItem, ActionListener, IProcessActions {
45
	implements IGenericToolBarMenuItem {
64 46
	static private ReprojectTocMenuEntry    singleton        = null;
65
	private RasterReprojectPanel            reprojectPanel   = null;
66
	private FLayer                          lyr              = null;
67
	private ReprojectPanelDataModel         dataModel        = null;
68
	private ReprojectWindow                 window           = null;
47
	private ReprojectListener               listener         = null;
69 48

  
70 49
	/*
71 50
	 *  (non-Javadoc)
......
138 117
	 */
139 118
	public void execute(ITocItem item, FLayer[] selectedItems) {
140 119
		if (selectedItems.length == 1) {
141
			lyr = selectedItems[0];
120
			FLayer lyr = selectedItems[0];
142 121
			if (lyr instanceof FLyrRaster) {
143 122
				FileNameManagement fileNameManagement = new FileNameManagementImpl();
144
				dataModel = new ReprojectPanelDataModelImpl((FLyrRaster)lyr);
145
				reprojectPanel = AlgorithmSwingLocator.getSwingManager().createRasterReprojectPanel(fileNameManagement, dataModel);
146
				reprojectPanel.addButtonsListener(this);
123
				ReprojectPanelDataModel dataModel = new ReprojectPanelDataModelImpl((FLyrRaster)lyr);
124
				RasterReprojectPanel reprojectPanel = AlgorithmSwingLocator.getSwingManager().createRasterReprojectPanel(fileNameManagement, dataModel);
147 125
				
148
				window = new ReprojectWindow(reprojectPanel.getComponent(), Messages.getText("reprojection"), 360, 460);
126
				ReprojectWindow window = new ReprojectWindow(reprojectPanel.getComponent(), Messages.getText("reprojection"), 360, 460);
127
				listener = new ReprojectListener(lyr, dataModel, window, reprojectPanel);
128
				reprojectPanel.addButtonsListener(listener);
149 129
				PluginServices.getMDIManager().addCentredWindow(window);
150 130
			}
151 131
		}
......
159 139
		return IconThemeHelper.getImageIcon("tools-raster-reproject");
160 140
	}
161 141

  
162
	public void actionPerformed(ActionEvent e) {
163
		if(reprojectPanel.getObjectSelected(e.getSource()) == RasterReprojectPanel.BUTTON_ACCEPT) {
164
			double cellsize = dataModel.getCellSize();
165
			int w = dataModel.getSize()[0];
166
			int h = dataModel.getSize()[1];
167
			IProjection srcPrj = dataModel.getSrcProjection();
168
			IProjection dstPrj = dataModel.getDstProjection();
169
			
170
			if(cellsize <= 0 || w <= 0 || h <= 0) {
171
				messageBoxError("wrong_size");
172
				return;
173
			}
174
			
175
			if(srcPrj == null || dstPrj == null) {
176
				messageBoxError("wrong_projection");
177
				return;
178
			}
179
			
180
			RasterProcess process;
181
			try {
182
				process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("RasterReprojectionProcess");
183
			} catch (ProcessException e1) {
184
				messageBoxError("error_creating_algorithm");
185
				return;
186
			}
187
			
188
			String file = reprojectPanel.getFileSelected();
189
			File f = new File(file);
190
			if(!f.isFile())
191
				file = System.getProperty("java.io.tmpdir") + File.separator + file + ".tif";
192
			if(!file.endsWith(".tif"))
193
				file = file + ".tif";
194
			
195
			process.setActions(this);
196
			process.addParam(ReprojectProcess.RASTER_STORE, ((FLyrRaster)lyr).getDataStore());
197
			process.addParam(ReprojectProcess.SIZEX, w);
198
			process.addParam(ReprojectProcess.SIZEY, h);
199
			process.addParam(ReprojectProcess.PATH, file);
200
			process.addParam(ReprojectProcess.SRC_PROJECTION, dataModel.getSrcProjection());
201
			process.addParam(ReprojectProcess.DST_PROJECTION, dataModel.getDstProjection());
202
			process.addParam(ReprojectProcess.CELLSIZE, dataModel.getCellSize());
203
			process.start();
204
			
205
		}
206
		if(reprojectPanel.getObjectSelected(e.getSource()) == RasterReprojectPanel.BUTTON_CANCEL) {
207
			if(window != null) {
208
				PluginServices.getMDIManager().closeWindow(window);
209
				window = null;
210
			}
211
		}
212
	}
213
	
214
	/**
215
	 * Shows an error dialog with a text and a accept button 
216
	 * @param msg Message to show in the dialog
217
	 * @param parentWindow Parent window
218
	 */
219
	public static void messageBoxError(String msg) {
220
		String string = Messages.getText("accept");
221
		Object[] options = {string};
222
		JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
223
					"<html>" + Messages.getText(msg).replaceAll("\n", "<br>") + "</html>",
224
					Messages.getText("confirmacion"),
225
					JOptionPane.OK_OPTION,
226
					JOptionPane.ERROR_MESSAGE,
227
					null,
228
					options,
229
					string);
230
	}
231

  
232
	public void end(Object params) {
233
		if(window != null) {
234
			PluginServices.getMDIManager().closeWindow(window);
235
			window = null;
236
		}
237
		if(	params instanceof Object[] &&
238
				((Object[])params).length == 2 &&
239
				((Object[])params)[0] instanceof String &&
240
				((Object[])params)[1] instanceof Long) {
241

  
242
			String fName = (String)((Object[])params)[0];
243
			long milis = ((Long)((Object[])params)[1]).longValue();
244
			processFinalize(fName, milis);
245
			EndInfoDialog.show(fName, milis);
246
		}
247

  
248
	}
249
	
250
	/**
251
	 * Acciones que se realizan al finalizar de crear los recortes de imagen.
252
	 * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
253
	 */
254
	private void processFinalize(String fileName, long milis) {
255
		if (!new File(fileName).exists())
256
			return;
257

  
258
		String viewName = getViewName();
259
		if(viewName != null) {
260
			if (RasterToolsUtil.messageBoxYesOrNot("cargar_toc", this)) {
261
				try {
262
					RasterToolsUtil.loadLayer(viewName, fileName, null);
263
				} catch (RasterNotLoadException e) {
264
					messageBoxError("error_load_layer");
265
				}
266
			}
267
		}
268
	}
269
	
270
	private String getViewName() {
271
		IWindow[] w = PluginServices.getMDIManager().getAllWindows();
272
		for (int i = 0; i < w.length; i++) {
273
			if(w[i] instanceof AbstractViewPanel) {
274
				FLayers lyrs = ((AbstractViewPanel)w[i]).getMapControl().getMapContext().getLayers();
275
				for (int j = 0; j < lyrs.getLayersCount(); j++) {
276
					FLayer lyr = lyrs.getLayer(j);
277
					if(this.lyr == lyr) {
278
						return PluginServices.getMDIManager().getWindowInfo((AbstractViewPanel) w[i]).getTitle();
279
					}
280
				}
281
			}
282
		}
283
		return null;
284
	}
285

  
286 142
	public void interrupted() {
287 143
		// TODO Auto-generated method stub
288 144
		
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.app/org.gvsig.raster.tools.app.reproject/src/main/resources/text.properties
2 2
reprojection=Reproyecci?n
3 3
wrong_size=Tama?o incorrecto en la imagen de salida
4 4
wrong_projection=Proyecci?n incorrecta
5
error_creating_algorithm=Error construyendo el objeto algoritmo
5
error_creating_algorithm=Error construyendo el objeto algoritmo
6
directory_does_not_exists=El directorio de destino no existe
7
file_name_not_valid=Nombre de fichero no v?lido
8
file_exists=El fichero de salida existe
9
overwrite=Quieres sobreescribirlo?
10
valid_characters=Caracteres validos
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.app/org.gvsig.raster.tools.app.reproject/src/main/resources/text_en.properties
2 2
reprojection=Reprojection
3 3
wrong_size=Wrong size in output image
4 4
wrong_projection=Wrong projection
5
error_creating_algorithm=Error building the algorithm object
5
error_creating_algorithm=Error building the algorithm object
6
directory_does_not_exists=The destination directory does not exists
7
file_name_not_valid=File name not valid
8
file_exists=The output file exists
9
overwrite=Do you want overwrite it?
10
valid_characters=Valid characters
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.swing/org.gvsig.raster.tools.algorithm.swing.impl/src/main/java/org/gvsig/raster/tools/algorithm/swing/impl/reproject/RasterReprojectPanelImpl.java
313 313
	public String getFileSelected() {
314 314
		return getCreateNewLayerPanel().getFileSelected();
315 315
	}
316
	
317
	public String getDirectorySelected() {
318
		return getCreateNewLayerPanel().getDirectorySelected();
319
	}
316 320
}
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.swing/org.gvsig.raster.tools.algorithm.swing.api/src/main/java/org/gvsig/raster/tools/algorithm/swing/reproject/RasterReprojectPanel.java
52 52
	
53 53
	public String getFileSelected();
54 54
	
55
	public String getDirectorySelected();
56
	
55 57
	public JComponent getComponent();
56 58
}
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.base/src/main/java/org/gvsig/raster/tools/algorithm/base/RasterBaseAlgorithmLibrary.java
36 36
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
37 37
 */
38 38
public class RasterBaseAlgorithmLibrary extends AbstractLibrary {
39
	public static final String         REGISTER_PROCESS_LABEL      = "RasterProcess";
40
	public static final String         REGISTER_PARAMETERS_LABEL   = "RasterProcessParameters";
39
	public static final String         REGISTER_PROCESS_LABEL             = "RasterProcess";
40
	public static final String         REGISTER_INPUT_PARAMETERS_LABEL    = "RasterProcessInputParameters";
41
	public static final String         REGISTER_OUTPUT_PARAMETERS_LABEL   = "RasterProcessOutputParameters";
41 42
	
42 43
	/**
43 44
	 * Registers a raster process
......
50 51
	}
51 52
	
52 53
	/**
54
	 *  Registers input parameters of a raster process
55
	 */
56
	@SuppressWarnings("unchecked")
57
	public static String registerInputParameter(String parameterLabel, Class parameterClass) {
58
		return registerParameter(parameterLabel, parameterClass, REGISTER_INPUT_PARAMETERS_LABEL);
59
	}
60
	
61
	/**
62
	 *  Registers output parameters of a raster process
63
	 */
64
	@SuppressWarnings("unchecked")
65
	public static String registerOutputParameter(String parameterLabel, Class parameterClass) {
66
		return registerParameter(parameterLabel, parameterClass, REGISTER_OUTPUT_PARAMETERS_LABEL);
67
	}
68
	
69
	/**
53 70
	 * Registers a raster process
54 71
	 */
55 72
	@SuppressWarnings("unchecked")
56
	public static String registerParameter(String parameterLabel, Class parameterClass) {
73
	private static String registerParameter(String parameterLabel, Class parameterClass, String type) {
57 74
		ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
58
		ExtensionPoint point = extensionPoints.get(RasterBaseAlgorithmLibrary.REGISTER_PARAMETERS_LABEL);
75
		ExtensionPoint point = extensionPoints.get(type);
59 76
		int i = 0;
60 77
		if(point.get(parameterLabel) == null) {
61 78
			point.append(parameterLabel, "", parameterClass);
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.base/src/main/java/org/gvsig/raster/tools/algorithm/base/process/RasterProcess.java
176 176
			}
177 177
			logger.warn(RasterLocator.getManager().getRasterUtils().getTrace(e));
178 178
			if(e.getMessage() != null && e.getMessage().compareTo("") != 0)
179
				messageBoxError(e.getMessage(), this);
179
				messageBoxError(e.getMessage(), null);
180 180
			else
181
				messageBoxError("error_processing", this);
181
				messageBoxError("error_processing", null);
182 182
			queueActions = null;
183 183
		} finally {
184 184
			taskEventManager.removeTask();
......
197 197
	 * Activa o desactiva el interfaz de progreso en el lanzamiento de la tarea como un thread.
198 198
	 * @param active true para activarlo o false para desactivarlo
199 199
	 */
200
	public void setProgressActive(boolean active){
200
	public void setProgressActive(boolean active) {
201 201
		this.progressActive = active;
202 202
	}
203 203
	
org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.reproject/src/main/java/org/gvsig/raster/tools/algorithm/reproject/ReprojectProcess.java
21 21
*/
22 22
package org.gvsig.raster.tools.algorithm.reproject;
23 23

  
24
import java.util.HashMap;
25

  
24 26
import javax.swing.SwingUtilities;
25 27

  
26 28
import org.cresques.cts.IProjection;
......
44 46
	public static String      SIZEX          = "SizeX";
45 47
	public static String      SIZEY          = "SizeY";
46 48
	public static String      CELLSIZE       = "CellSize";
49
	public static String      FILENAME       = "FileName";
50
	public static String      TIME           = "Time";
47 51
	
48 52
	private RasterDataStore   store          = null;
49 53
	private String            filename       = null;
......
56 60
	private double            cellSize       = 0;
57 61
	
58 62
	public static void registerParameters() {
59
		RASTER_STORE = RasterBaseAlgorithmLibrary.registerParameter(RASTER_STORE, RasterDataStore.class);
60
		PATH = RasterBaseAlgorithmLibrary.registerParameter(PATH, String.class);
61
		DST_PROJECTION = RasterBaseAlgorithmLibrary.registerParameter(DST_PROJECTION, IProjection.class);
62
		SRC_PROJECTION = RasterBaseAlgorithmLibrary.registerParameter(SRC_PROJECTION, IProjection.class);
63
		SIZEX = RasterBaseAlgorithmLibrary.registerParameter(SIZEX, Integer.class);
64
		SIZEY = RasterBaseAlgorithmLibrary.registerParameter(SIZEY, Integer.class);
65
		CELLSIZE = RasterBaseAlgorithmLibrary.registerParameter(CELLSIZE, Double.class);
63
		RASTER_STORE = RasterBaseAlgorithmLibrary.registerInputParameter(RASTER_STORE, RasterDataStore.class);
64
		PATH = RasterBaseAlgorithmLibrary.registerInputParameter(PATH, String.class);
65
		DST_PROJECTION = RasterBaseAlgorithmLibrary.registerInputParameter(DST_PROJECTION, IProjection.class);
66
		SRC_PROJECTION = RasterBaseAlgorithmLibrary.registerInputParameter(SRC_PROJECTION, IProjection.class);
67
		SIZEX = RasterBaseAlgorithmLibrary.registerInputParameter(SIZEX, Integer.class);
68
		SIZEY = RasterBaseAlgorithmLibrary.registerInputParameter(SIZEY, Integer.class);
69
		CELLSIZE = RasterBaseAlgorithmLibrary.registerInputParameter(CELLSIZE, Double.class);
70
		
71
		FILENAME = RasterBaseAlgorithmLibrary.registerOutputParameter(FILENAME, String.class);
72
		TIME = RasterBaseAlgorithmLibrary.registerOutputParameter(TIME, Long.class);
66 73
	}
67 74
	
68 75
	/*
......
103 110
			SwingUtilities.invokeLater(new Runnable() {
104 111
				public void run() {
105 112
					if (externalActions != null) {
106
						externalActions.end(new Object[]{filename, new Long(milis)});
113
						HashMap<String, Object> map = new HashMap<String, Object>();
114
						map.put(FILENAME, filename);
115
						map.put(TIME, new Long(milis));
116
						externalActions.end(map);
107 117
					}
108 118
				}
109 119
			});
......
119 129
	 * @see org.gvsig.raster.tools.app.basic.raster.process.RasterProcess#getResult()
120 130
	 */
121 131
	public Object getResult() {
122
		return new Object[]{filename, new Long(milis)};
132
		HashMap<String, Object> map = new HashMap<String, Object>();
133
		map.put(FILENAME, filename);
134
		map.put(TIME, new Long(milis));
135
		return map;
123 136
	}
124 137

  
125 138
	/*

Also available in: Unified diff