Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / FileResource.java @ 23878

History | View | Annotate | Download (2.94 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

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.data.feature.file;
32

    
33
import java.io.File;
34

    
35
import org.gvsig.fmap.data.exceptions.DataException;
36
import org.gvsig.fmap.data.resource.exception.ResourceChangedException;
37
import org.gvsig.fmap.data.resource.spi.AbstractResource;
38

    
39
/**
40
 * @author jmvivo
41
 *
42
 */
43
public abstract class FileResource extends AbstractResource {
44

    
45
        protected File file;
46
        private long fileLastModified=0;
47
        private boolean editing=false;
48

    
49
        public FileResource(FileStoreParameters params) {
50
                super(params);
51
                this.file = params.getFile();
52
        }
53

    
54
        public void setFile(File aFile) throws ResourceInUseException {
55
                if (this.getRefencesCount() > 0){
56
                        throw new ResourceInUseException(this);
57
                }
58
                this.file=aFile;
59
        }
60

    
61

    
62
        /* (non-Javadoc)
63
         * @see org.gvsig.fmap.data.Resource#description()
64
         */
65
        public String getDescription() {
66
                if (this.getFile()!= null){
67
                        return this.getName() +" File resource: " + this.getFile().getAbsolutePath();
68
                } else{
69
                        return this.getName() +" File resource: {No file}";
70
                }
71
        }
72

    
73
        public File getFile(){
74
                return this.file;
75
        }
76

    
77
        protected String generateKey(){
78
                return this.getName()+":"+this.getFile().getAbsolutePath();
79
        }
80

    
81
        /* (non-Javadoc)
82
         * @see org.gvsig.fmap.data.Resource#doChanged()
83
         */
84
        protected void doChanged() {
85
                this.updateLastModified();
86
        }
87

    
88
        protected void updateLastModified(){
89
                this.fileLastModified = this.file.lastModified();
90
        }
91

    
92
        protected void checkChanged() throws ResourceChangedException{
93
                if (this.isEditing()){
94
                        return;
95
                }
96
                if (this.fileLastModified == 0){
97
                        return;
98
                }
99
                if (this.fileLastModified < this.file.lastModified()){
100
                        this.changed();
101
                        throw new ResourceChangedException(this);
102
                }
103
        }
104

    
105
        /* (non-Javadoc)
106
         * @see org.gvsig.fmap.data.Resource#doDispose()
107
         */
108
        protected void doDispose() throws DataException{
109
                this.file = null;
110
        }
111

    
112
        public boolean isEditable(){
113
                return this.getFile().canWrite();
114
        }
115

    
116
        protected void editing(){
117
                this.editing=true;
118
        }
119

    
120
        protected boolean isEditing(){
121
                return this.editing;
122
        }
123

    
124
        protected void stopEditing(){
125
                this.editing=false;
126
        }
127

    
128
}
129