svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.jpg / src / main / java / org / gvsig / fmap / dal / file / jpg / JpgStoreProvider.java @ 43803
History | View | Annotate | Download (7.3 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.file.jpg; |
24 |
|
25 |
import java.awt.image.BufferedImage; |
26 |
import java.io.File; |
27 |
|
28 |
import javax.imageio.ImageIO; |
29 |
|
30 |
import org.apache.commons.io.FilenameUtils; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataServerExplorer; |
37 |
import org.gvsig.fmap.dal.DataStoreNotification; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.FileHelper; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.exception.InitializeException; |
42 |
import org.gvsig.fmap.dal.exception.OpenException; |
43 |
import org.gvsig.fmap.dal.exception.ReadException; |
44 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
45 |
import org.gvsig.fmap.dal.raster.spi.AbstractAwtRasterStoreProvider; |
46 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
47 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
48 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
49 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
52 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
53 |
import org.gvsig.metadata.MetadataLocator; |
54 |
import org.gvsig.metadata.MetadataManager; |
55 |
import org.gvsig.metadata.exceptions.MetadataException; |
56 |
import org.gvsig.tools.ToolsLocator; |
57 |
import org.gvsig.tools.dynobject.DynObject; |
58 |
import org.gvsig.tools.exception.BaseException; |
59 |
import org.gvsig.tools.task.TaskStatusManager; |
60 |
|
61 |
/**
|
62 |
* Provider for Jpg files
|
63 |
* @author fdiaz
|
64 |
*
|
65 |
*/
|
66 |
public class JpgStoreProvider extends AbstractAwtRasterStoreProvider implements ResourceConsumer{ |
67 |
|
68 |
private static final Logger logger = |
69 |
LoggerFactory.getLogger(JpgStoreProvider.class); |
70 |
|
71 |
public static String NAME = "JPG"; |
72 |
public static String DESCRIPTION = "JPG provider"; |
73 |
public static final String METADATA_DEFINITION_NAME = NAME; |
74 |
|
75 |
private ResourceProvider resource;
|
76 |
private BufferedImage image; |
77 |
|
78 |
protected static void registerMetadataDefinition() |
79 |
throws MetadataException {
|
80 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
81 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
82 |
manager.addDefinition(METADATA_DEFINITION_NAME, |
83 |
JpgStoreProviderParameters.class |
84 |
.getResourceAsStream(JpgStoreProvider.NAME+"Metadata.xml"),
|
85 |
JpgStoreProviderParameters.class.getClassLoader()); |
86 |
} |
87 |
} |
88 |
|
89 |
/**
|
90 |
* @param params
|
91 |
* @param storeServices
|
92 |
* @throws InitializeException
|
93 |
*/
|
94 |
public JpgStoreProvider(JpgStoreProviderParameters params,
|
95 |
DataStoreProviderServices storeServices) |
96 |
throws InitializeException {
|
97 |
super(
|
98 |
params, |
99 |
storeServices, |
100 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
101 |
); |
102 |
this.init(params, storeServices);
|
103 |
} |
104 |
|
105 |
protected JpgStoreProvider(JpgStoreProviderParameters params,
|
106 |
DataStoreProviderServices storeServices, DynObject metadata) |
107 |
throws InitializeException {
|
108 |
super(params, storeServices, metadata);
|
109 |
this.init(params, storeServices);
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public String getProviderName() { |
114 |
return NAME;
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
protected void init(DataStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException { |
119 |
super.init(params, storeServices);
|
120 |
|
121 |
File file = getParameters().getFile();
|
122 |
if (file == null) { |
123 |
throw new InitializeException( |
124 |
new NullPointerException(NAME +" file is null")); |
125 |
} |
126 |
|
127 |
resource = this.createResource(
|
128 |
FileResource.NAME, |
129 |
new Object[] { file.getAbsolutePath() } |
130 |
); |
131 |
|
132 |
resource.addConsumer(this);
|
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
@Override
|
138 |
public ResourceProvider getResource() {
|
139 |
return this.resource; |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public Object getSourceId() { |
144 |
return this.getParameters().getFile(); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public String getName() { |
149 |
String name = this.getParameters().getFile().getName(); |
150 |
return FilenameUtils.getBaseName(name);
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public String getFullName() { |
155 |
return this.getParameters().getFile().getAbsolutePath(); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public boolean closeResourceRequested(ResourceProvider resource) { |
160 |
return true; |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void resourceChanged(ResourceProvider resource) { |
165 |
this.getStoreServices().notifyChange(
|
166 |
DataStoreNotification.RESOURCE_CHANGED, |
167 |
resource); |
168 |
} |
169 |
|
170 |
@Override
|
171 |
protected void doDispose() throws BaseException { |
172 |
super.doDispose();
|
173 |
resource.removeConsumer(this);
|
174 |
} |
175 |
|
176 |
@Override
|
177 |
public JpgStoreProviderParameters getParameters() {
|
178 |
return (JpgStoreProviderParameters) super.getParameters(); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void open() throws OpenException { |
183 |
if (this.isOpen()) { |
184 |
return;
|
185 |
} |
186 |
try {
|
187 |
image = ImageIO.read((File) getResource().getParameters().getDynValue(FileResource.NAME)); |
188 |
open(image.getRaster()); |
189 |
|
190 |
} catch (Exception e) { |
191 |
try {
|
192 |
throw new OpenException(resource.getName(), e); |
193 |
} catch (AccessResourceException e1) {
|
194 |
throw new OpenException(getProviderName(), e); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public DataServerExplorer getExplorer() throws ReadException, ValidateDataParametersException { |
201 |
DataManager manager = DALLocator.getDataManager(); |
202 |
FilesystemServerExplorerParameters params; |
203 |
try {
|
204 |
params = (FilesystemServerExplorerParameters) manager |
205 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
206 |
params.setRoot(this.getParameters().getFile().getParent());
|
207 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
208 |
} catch (DataException e) {
|
209 |
throw new ReadException(this.getProviderName(), e); |
210 |
} catch (ValidateDataParametersException e) {
|
211 |
throw new ReadException(this.getProviderName(), e); |
212 |
} |
213 |
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* @return BufferedImage
|
218 |
* @throws OpenException
|
219 |
*/
|
220 |
public BufferedImage getImage() { |
221 |
autoOpen(); |
222 |
return this.image; |
223 |
} |
224 |
|
225 |
} |