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.imageio / src / main / java / org / gvsig / fmap / dal / file / imageio / ImageIOStoreProvider.java @ 43868
History | View | Annotate | Download (7.04 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.imageio; |
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 |
|
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import org.gvsig.fmap.dal.DataServerExplorer; |
35 |
import org.gvsig.fmap.dal.DataStoreNotification; |
36 |
import org.gvsig.fmap.dal.DataStoreParameters; |
37 |
import org.gvsig.fmap.dal.FileHelper; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.exception.InitializeException; |
40 |
import org.gvsig.fmap.dal.exception.OpenException; |
41 |
import org.gvsig.fmap.dal.exception.ReadException; |
42 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
43 |
import org.gvsig.fmap.dal.raster.spi.AbstractAwtRasterStoreProvider; |
44 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
45 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
46 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
47 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
50 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
51 |
import org.gvsig.tools.ToolsLocator; |
52 |
import org.gvsig.tools.exception.BaseException; |
53 |
import org.gvsig.tools.task.SimpleTaskStatus; |
54 |
|
55 |
|
56 |
public class ImageIOStoreProvider |
57 |
extends
|
58 |
AbstractAwtRasterStoreProvider |
59 |
implements
|
60 |
ResourceConsumer |
61 |
{ |
62 |
|
63 |
protected String storeProviderName; |
64 |
protected String storeProviderDescription; |
65 |
|
66 |
protected ResourceProvider resource;
|
67 |
private BufferedImage image; |
68 |
|
69 |
/**
|
70 |
* @param storeProviderName
|
71 |
* @param storeProviderDescription
|
72 |
* @param params
|
73 |
* @param storeServices
|
74 |
* @throws InitializeException
|
75 |
*/
|
76 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
77 |
public ImageIOStoreProvider(
|
78 |
String storeProviderName,
|
79 |
String storeProviderDescription,
|
80 |
ImageIOStoreProviderParameters params, |
81 |
DataStoreProviderServices storeServices |
82 |
) throws InitializeException {
|
83 |
super(
|
84 |
params, |
85 |
storeServices, |
86 |
FileHelper.newMetadataContainer(storeProviderName) |
87 |
); |
88 |
this.storeProviderName = storeProviderName;
|
89 |
this.storeProviderDescription = storeProviderDescription;
|
90 |
this.init(params, storeServices);
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getProviderName() { |
95 |
return this.storeProviderName; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
protected void init(DataStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException { |
100 |
super.init(params, storeServices);
|
101 |
|
102 |
File file = getParameters().getFile();
|
103 |
if (file == null) { |
104 |
throw new InitializeException( |
105 |
new NullPointerException(this.storeProviderName +" file is null")); |
106 |
} |
107 |
|
108 |
resource = this.createResource(
|
109 |
FileResource.NAME, |
110 |
new Object[] { file.getAbsolutePath() } |
111 |
); |
112 |
|
113 |
resource.addConsumer(this);
|
114 |
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public ResourceProvider getResource() {
|
119 |
return this.resource; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public Object getSourceId() { |
124 |
return this.getParameters().getFile(); |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public String getName() { |
129 |
String name = this.getParameters().getFile().getName(); |
130 |
return FilenameUtils.getBaseName(name);
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public String getFullName() { |
135 |
return this.getParameters().getFile().getAbsolutePath(); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public boolean closeResourceRequested(ResourceProvider resource) { |
140 |
return true; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void resourceChanged(ResourceProvider resource) { |
145 |
this.getStoreServices().notifyChange(
|
146 |
DataStoreNotification.RESOURCE_CHANGED, |
147 |
resource); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
protected void doDispose() throws BaseException { |
152 |
super.doDispose();
|
153 |
resource.removeConsumer(this);
|
154 |
resource = null;
|
155 |
image = null;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public ImageIOStoreProviderParameters getParameters() {
|
160 |
return (ImageIOStoreProviderParameters) super.getParameters(); |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void open() throws OpenException { |
165 |
if (this.isOpen()) { |
166 |
return;
|
167 |
} |
168 |
SimpleTaskStatus taskStatus = null;
|
169 |
try {
|
170 |
taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(this.getName());
|
171 |
taskStatus.setIndeterminate(); |
172 |
taskStatus.setAutoremove(true);
|
173 |
taskStatus.message("Loading "+this.getName()+"..."); |
174 |
taskStatus.add(); |
175 |
image = ImageIO.read((File) getResource().getParameters().getDynValue(FileResource.NAME)); |
176 |
open(image.getRaster()); |
177 |
|
178 |
} catch (Exception e) { |
179 |
if( taskStatus!=null ) { |
180 |
taskStatus.abort(); |
181 |
taskStatus = null;
|
182 |
} |
183 |
try {
|
184 |
throw new OpenException(resource.getName(), e); |
185 |
} catch (AccessResourceException e1) {
|
186 |
throw new OpenException(getProviderName(), e); |
187 |
} |
188 |
} finally {
|
189 |
if( taskStatus!=null ) { |
190 |
taskStatus.terminate(); |
191 |
taskStatus = null;
|
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public DataServerExplorer getExplorer() throws ReadException, ValidateDataParametersException { |
198 |
DataManager manager = DALLocator.getDataManager(); |
199 |
FilesystemServerExplorerParameters params; |
200 |
try {
|
201 |
params = (FilesystemServerExplorerParameters) manager |
202 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
203 |
params.setRoot(this.getParameters().getFile().getParent());
|
204 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
205 |
} catch (DataException | ValidateDataParametersException e) {
|
206 |
throw new ReadException(this.getProviderName(), e); |
207 |
} |
208 |
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* @return BufferedImage
|
213 |
*/
|
214 |
public BufferedImage getImage() { |
215 |
autoOpen(); |
216 |
return this.image; |
217 |
} |
218 |
|
219 |
|
220 |
} |