svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / FileResource.java @ 20744
History | View | Annotate | Download (3.13 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.data.datastores.vectorial.file; |
32 |
|
33 |
import java.io.File; |
34 |
|
35 |
import org.gvsig.data.Resource; |
36 |
import org.gvsig.data.exception.DataException; |
37 |
import org.gvsig.data.exception.InitializeException; |
38 |
import org.gvsig.data.exception.OpenException; |
39 |
import org.gvsig.data.exception.ResourceChangedException; |
40 |
|
41 |
/**
|
42 |
* @author jmvivo
|
43 |
*
|
44 |
*/
|
45 |
public abstract class FileResource extends Resource { |
46 |
|
47 |
protected IFileStoreParameters params;
|
48 |
protected File file; |
49 |
private long fileLastModified=0; |
50 |
private boolean editing=false; |
51 |
|
52 |
public FileResource(IFileStoreParameters params){
|
53 |
this.params=params;
|
54 |
this.file = params.getFile();
|
55 |
} |
56 |
|
57 |
public void setFile(File aFile) throws InitializeException{ |
58 |
if (this.getRefencesCount() > 0){ |
59 |
throw new InitializeException("Resource in use",this.description()); |
60 |
} |
61 |
this.file=aFile;
|
62 |
} |
63 |
|
64 |
public IFileStoreParameters getParameters(){
|
65 |
return this.params; |
66 |
} |
67 |
|
68 |
/* (non-Javadoc)
|
69 |
* @see org.gvsig.data.Resource#description()
|
70 |
*/
|
71 |
public String description() { |
72 |
if (this.getFile()!= null){ |
73 |
return this.getName() +" File resource: " + this.getFile().getAbsolutePath(); |
74 |
} else{
|
75 |
return this.getName() +" File resource: {No file}"; |
76 |
} |
77 |
} |
78 |
|
79 |
public File getFile(){ |
80 |
return this.file; |
81 |
} |
82 |
|
83 |
protected String generateKey(){ |
84 |
return this.getName()+":"+this.getFile().getAbsolutePath(); |
85 |
} |
86 |
|
87 |
/* (non-Javadoc)
|
88 |
* @see org.gvsig.data.Resource#doChanged()
|
89 |
*/
|
90 |
protected void doChanged() { |
91 |
this.updateLastModified();
|
92 |
} |
93 |
|
94 |
protected void updateLastModified(){ |
95 |
this.fileLastModified = this.file.lastModified(); |
96 |
} |
97 |
|
98 |
protected void checkChanged() throws ResourceChangedException{ |
99 |
if (this.isEditing()){ |
100 |
return;
|
101 |
} |
102 |
if (this.fileLastModified == 0){ |
103 |
return;
|
104 |
} |
105 |
if (this.fileLastModified < this.file.lastModified()){ |
106 |
this.changed();
|
107 |
throw new ResourceChangedException(this.description()); |
108 |
} |
109 |
} |
110 |
|
111 |
/* (non-Javadoc)
|
112 |
* @see org.gvsig.data.Resource#doDispose()
|
113 |
*/
|
114 |
protected void doDispose() throws DataException{ |
115 |
this.file = null; |
116 |
} |
117 |
|
118 |
public boolean isEditable(){ |
119 |
return this.getFile().canWrite(); |
120 |
} |
121 |
|
122 |
protected void editing(){ |
123 |
this.editing=true; |
124 |
} |
125 |
|
126 |
protected boolean isEditing(){ |
127 |
return this.editing; |
128 |
} |
129 |
|
130 |
protected void stopEditing(){ |
131 |
this.editing=false; |
132 |
} |
133 |
|
134 |
} |
135 |
|