Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / raster / gui / wizard / PrepareLayerAskWritableDirectory.java @ 2385

History | View | Annotate | Download (4.7 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.tools.app.basic.raster.gui.wizard;
23

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

    
29
import javax.swing.JOptionPane;
30

    
31
import org.gvsig.app.prepareAction.PrepareContext;
32
import org.gvsig.app.prepareAction.PrepareDataStoreParameters;
33
import org.gvsig.fmap.dal.DataStoreParameters;
34
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
35
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
36
import org.gvsig.i18n.Messages;
37
import org.gvsig.raster.mainplugin.beans.createlayer.WritableFolderPanel;
38
import org.gvsig.tools.exception.BaseException;
39

    
40
/**
41
 * Mechanism to detect if a layer is in a folder with write permission. If the user does not have
42
 * permission to write, the application need a directory with this permission to write metadata files.
43
 * 
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class PrepareLayerAskWritableDirectory implements PrepareDataStoreParameters, ActionListener {
47
        private DataStoreParameters         newParameters       = null;
48
        private DataStoreParameters         oldParameters        = null;
49
        private String                      selectedPath         = null;
50
        private boolean                     applyToAllLayers     = false;
51
        
52
        public DataStoreParameters prepare(DataStoreParameters storeParameters,
53
                        PrepareContext context) throws BaseException {
54
                if(!(storeParameters instanceof RasterDataParameters) ||
55
                        ((RasterDataParameters)storeParameters).isSourceTiled())
56
                        return storeParameters;
57
                
58
                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
                
70
                if(!isValidFolder(folder)) {
71
                        if(applyToAllLayers) {
72
                                setFolderToNewDataParameters(selectedPath);
73
                        } else {
74
                                WritableFolderPanel panel = new WritableFolderPanel(folder, this, layerName);
75
                                applyToAllLayers = panel.aplyAllFiles();
76
                        }
77
                } else {
78
                        return oldParameters;
79
                }
80
                
81
                return newParameters;
82
        }
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
        
93
        public void actionPerformed(ActionEvent e) {
94
                if(e.getID() == 0) { //Cambio de directorio
95
                        selectedPath = e.getActionCommand();
96
                }
97
                if(e.getID() == ButtonsPanel.BUTTON_ACCEPT) {
98
                        if(isValidFolder(selectedPath)) {
99
                                setFolderToNewDataParameters(selectedPath);
100
                        } else {
101
                                JOptionPane.showMessageDialog(null, Messages.getText("folder_not_writable"), "", JOptionPane.WARNING_MESSAGE);
102
                                newParameters = null;
103
                        }
104
                }
105
                
106
                if(e.getID() == ButtonsPanel.BUTTON_CANCEL) {
107
                        newParameters = null;
108
                }
109
        }
110
        
111
        private void setFolderToNewDataParameters(String path) {
112
                newParameters = oldParameters;
113
                newParameters.setDynValue(RasterDataParameters.FIELD_RMF_FOLDER, path);
114
        }
115
        
116
        private boolean isValidFolder(String folder) {
117
                File path = new File(folder);
118
                return (path.exists() && path.isDirectory() && path.canWrite());
119
        }
120
        
121
        public void post(DataStoreParameters storeParameters, PrepareContext context) {
122
                applyToAllLayers = false;
123
        }
124
        
125
        public void pre(DataStoreParameters storeParameters, PrepareContext context) {
126
        }
127
        
128
        public void end(Object param) {
129
        }
130

    
131
        public String getDescription() {
132
                return "Detects folders with write permission";
133
        }
134

    
135
        public String getName() {
136
                return "PrepareLayerAskWritableDirectory";
137
        }
138

    
139
        public Object create() {
140
                return this;
141
        }
142

    
143
        public Object create(Object[] args) {
144
                return this;
145
        }
146

    
147
        @SuppressWarnings("unchecked")
148
        public Object create(Map args) {
149
                return this;
150
        }
151

    
152
        public void interrupted() {
153
        }
154
}