Revision 4181 org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.app.basic/src/main/java/org/gvsig/raster/tools/app/basic/raster/gui/wizard/PrepareLayerAskWritableDirectory.java

View differences:

PrepareLayerAskWritableDirectory.java
2 2
*
3 3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4 4
* of the Valencian Government (CIT)
5
* 
5
*
6 6
* This program is free software; you can redistribute it and/or
7 7
* modify it under the terms of the GNU General Public License
8 8
* as published by the Free Software Foundation; either version 2
9 9
* of the License, or (at your option) any later version.
10
* 
10
*
11 11
* This program is distributed in the hope that it will be useful,
12 12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
* GNU General Public License for more details.
15
* 
15
*
16 16
* You should have received a copy of the GNU General Public License
17 17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19
* MA  02110-1301, USA.
20
* 
20
*
21 21
*/
22 22
package org.gvsig.raster.tools.app.basic.raster.gui.wizard;
23 23

  
24 24
import java.awt.event.ActionEvent;
25 25
import java.awt.event.ActionListener;
26 26
import java.io.File;
27
import java.net.URI;
27 28
import java.util.Map;
28 29

  
29 30
import javax.swing.JOptionPane;
30 31

  
32
import org.apache.commons.lang3.StringUtils;
33

  
31 34
import org.gvsig.app.prepareAction.PrepareContext;
32 35
import org.gvsig.app.prepareAction.PrepareDataStoreParameters;
33 36
import org.gvsig.fmap.dal.DataStoreParameters;
......
40 43
/**
41 44
 * Mechanism to detect if a layer is in a folder with write permission. If the user does not have
42 45
 * permission to write, the application need a directory with this permission to write metadata files.
43
 * 
46
 *
44 47
 * @author Nacho Brodin (nachobrodin@gmail.com)
45 48
 */
46 49
public class PrepareLayerAskWritableDirectory implements PrepareDataStoreParameters, ActionListener {
......
48 51
	private DataStoreParameters         oldParameters        = null;
49 52
	private String                      selectedPath         = null;
50 53
	private boolean                     applyToAllLayers     = false;
51
	
54

  
52 55
	public DataStoreParameters prepare(DataStoreParameters storeParameters,
53 56
			PrepareContext context) throws BaseException {
54 57
		if(!(storeParameters instanceof RasterDataParameters) ||
55 58
			((RasterDataParameters)storeParameters).isSourceTiled())
56 59
			return storeParameters;
57
		
60

  
58 61
		oldParameters = storeParameters;
59
		String uri = ((RasterDataParameters)storeParameters).getURI();
60
		
61
		String folder = uri;
62
		String layerName = uri;
63
		if(uri.contains(File.separator)) {
64
			folder = getFolder(uri) ;
65
			layerName = getLayerName(uri);
66
		} else {
67
			folder = System.getProperty("user.home");
68
		}
69
		
62
		URI uri = ((RasterDataParameters)storeParameters).getURI();
63

  
64
		File file = new File(uri);
65
		String folder = file.getParent();
66
        String layerName = file.getName();
67
        if (StringUtils.isEmpty(folder)) {
68
            folder = System.getProperty("user.home");
69
        }
70

  
70 71
		if(!isValidFolder(folder)) {
71 72
			if(applyToAllLayers) {
72 73
				setFolderToNewDataParameters(selectedPath);
......
77 78
		} else {
78 79
			return oldParameters;
79 80
		}
80
		
81

  
81 82
		return newParameters;
82 83
	}
83
	
84
	private String getFolder(String uri) {
85
		return uri.substring(0, uri.lastIndexOf(File.separator));
86
	}
87
	
88
	private String getLayerName(String uri) {
89
		int lastSeparator = uri.lastIndexOf(File.separator);
90
		return uri.substring(lastSeparator + 1, uri.length());
91
	}
92
	
84

  
93 85
	public void actionPerformed(ActionEvent e) {
94 86
		if(e.getID() == 0) { //Cambio de directorio
95 87
			selectedPath = e.getActionCommand();
......
102 94
				newParameters = null;
103 95
			}
104 96
		}
105
		
97

  
106 98
		if(e.getID() == ButtonsPanel.BUTTON_CANCEL) {
107 99
			newParameters = null;
108 100
		}
109 101
	}
110
	
102

  
111 103
	private void setFolderToNewDataParameters(String path) {
112 104
		newParameters = oldParameters;
113 105
		newParameters.setDynValue(RasterDataParameters.FIELD_RMF_FOLDER, path);
114 106
	}
115
	
107

  
116 108
	private boolean isValidFolder(String folder) {
117 109
		File path = new File(folder);
118 110
		return (path.exists() && path.isDirectory() && path.canWrite());
119 111
	}
120
	
112

  
121 113
	public void post(DataStoreParameters storeParameters, PrepareContext context) {
122 114
		applyToAllLayers = false;
123 115
	}
124
	
116

  
125 117
	public void pre(DataStoreParameters storeParameters, PrepareContext context) {
126 118
	}
127
	
119

  
128 120
	public void end(Object param) {
129 121
	}
130 122

  

Also available in: Unified diff