Statistics
| Revision:

gvsig-raster / 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 @ 4181

History | View | Annotate | Download (4.48 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.net.URI;
28
import java.util.Map;
29

    
30
import javax.swing.JOptionPane;
31

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

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

    
43
/**
44
 * Mechanism to detect if a layer is in a folder with write permission. If the user does not have
45
 * permission to write, the application need a directory with this permission to write metadata files.
46
 *
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class PrepareLayerAskWritableDirectory implements PrepareDataStoreParameters, ActionListener {
50
        private DataStoreParameters         newParameters       = null;
51
        private DataStoreParameters         oldParameters        = null;
52
        private String                      selectedPath         = null;
53
        private boolean                     applyToAllLayers     = false;
54

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

    
61
                oldParameters = storeParameters;
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

    
71
                if(!isValidFolder(folder)) {
72
                        if(applyToAllLayers) {
73
                                setFolderToNewDataParameters(selectedPath);
74
                        } else {
75
                                WritableFolderPanel panel = new WritableFolderPanel(folder, this, layerName);
76
                                applyToAllLayers = panel.aplyAllFiles();
77
                        }
78
                } else {
79
                        return oldParameters;
80
                }
81

    
82
                return newParameters;
83
        }
84

    
85
        public void actionPerformed(ActionEvent e) {
86
                if(e.getID() == 0) { //Cambio de directorio
87
                        selectedPath = e.getActionCommand();
88
                }
89
                if(e.getID() == ButtonsPanel.BUTTON_ACCEPT) {
90
                        if(isValidFolder(selectedPath)) {
91
                                setFolderToNewDataParameters(selectedPath);
92
                        } else {
93
                                JOptionPane.showMessageDialog(null, Messages.getText("folder_not_writable"), "", JOptionPane.WARNING_MESSAGE);
94
                                newParameters = null;
95
                        }
96
                }
97

    
98
                if(e.getID() == ButtonsPanel.BUTTON_CANCEL) {
99
                        newParameters = null;
100
                }
101
        }
102

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

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

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

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

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

    
123
        public String getDescription() {
124
                return "Detects folders with write permission";
125
        }
126

    
127
        public String getName() {
128
                return "PrepareLayerAskWritableDirectory";
129
        }
130

    
131
        public Object create() {
132
                return this;
133
        }
134

    
135
        public Object create(Object[] args) {
136
                return this;
137
        }
138

    
139
        @SuppressWarnings("unchecked")
140
        public Object create(Map args) {
141
                return this;
142
        }
143

    
144
        public void interrupted() {
145
        }
146
}