Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / FileResource.java @ 23754

History | View | Annotate | Download (3.03 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.exceptions.InitializeException;
37
import org.gvsig.fmap.data.exceptions.ResourceChangedException;
38
import org.gvsig.fmap.data.impl.AbstractResource;
39

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

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

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

    
55
        public void setFile(File aFile) throws InitializeException{
56
                if (this.getRefencesCount() > 0){
57
                        throw new InitializeException("Resource in use",this.getDescription());
58
                }
59
                this.file=aFile;
60
        }
61

    
62

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

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

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

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

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

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

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

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

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

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

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

    
129
}
130