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 / ImageIOFileSystemServerProvider.java @ 43867
History | View | Annotate | Download (6.03 KB)
1 | 43861 | jjdelcerro | /* 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 | 43867 | jjdelcerro | import java.awt.image.BufferedImage; |
26 | 43861 | jjdelcerro | import java.io.File; |
27 | 43867 | jjdelcerro | import java.io.FileOutputStream; |
28 | import java.io.IOException; |
||
29 | import java.lang.reflect.InvocationTargetException; |
||
30 | import java.lang.reflect.Method; |
||
31 | import java.util.logging.Level; |
||
32 | import java.util.logging.Logger; |
||
33 | import javax.imageio.IIOImage; |
||
34 | import javax.imageio.ImageIO; |
||
35 | import javax.imageio.ImageWriteParam; |
||
36 | import javax.imageio.ImageWriter; |
||
37 | 43861 | jjdelcerro | |
38 | import org.apache.commons.io.FilenameUtils; |
||
39 | 43867 | jjdelcerro | import org.apache.commons.io.IOUtils; |
40 | 43861 | jjdelcerro | |
41 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
42 | 43867 | jjdelcerro | import org.gvsig.fmap.dal.NewDataStoreParameters; |
43 | import org.gvsig.fmap.dal.exception.CreateException; |
||
44 | import org.gvsig.fmap.dal.exception.DataException; |
||
45 | import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters; |
||
46 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
47 | 43861 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
48 | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
||
49 | 43867 | jjdelcerro | import org.gvsig.raster.lib.buffer.api.Buffer; |
50 | import org.gvsig.tools.ToolsLocator; |
||
51 | import org.gvsig.tools.dataTypes.CoercionException; |
||
52 | import org.gvsig.tools.dataTypes.DataTypes; |
||
53 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
54 | import org.gvsig.tools.dynobject.DynField; |
||
55 | import org.gvsig.tools.dynobject.DynField_v2; |
||
56 | 43861 | jjdelcerro | |
57 | public class ImageIOFileSystemServerProvider extends AbstractFilesystemServerExplorerProvider { |
||
58 | |||
59 | protected FilesystemServerExplorerProviderServices serverExplorer;
|
||
60 | |||
61 | protected String[] fileExtensions; |
||
62 | 43867 | jjdelcerro | protected boolean canCreate; |
63 | 43861 | jjdelcerro | |
64 | public ImageIOFileSystemServerProvider(
|
||
65 | String name,
|
||
66 | String description,
|
||
67 | String[] fileExtensions |
||
68 | ) { |
||
69 | 43867 | jjdelcerro | this(name, description, fileExtensions, false); |
70 | } |
||
71 | |||
72 | public ImageIOFileSystemServerProvider(
|
||
73 | String name,
|
||
74 | String description,
|
||
75 | String[] fileExtensions, |
||
76 | boolean canCreate
|
||
77 | ) { |
||
78 | 43861 | jjdelcerro | super(name, description);
|
79 | this.fileExtensions = fileExtensions;
|
||
80 | 43867 | jjdelcerro | this.canCreate = canCreate;
|
81 | 43861 | jjdelcerro | } |
82 | |||
83 | @Override
|
||
84 | 43867 | jjdelcerro | public boolean canCreate() { |
85 | return canCreate;
|
||
86 | } |
||
87 | |||
88 | @Override
|
||
89 | public NewDataStoreParameters getCreateParameters() throws DataException { |
||
90 | if( !this.canCreate() ) { |
||
91 | throw new UnsupportedOperationException(); |
||
92 | } |
||
93 | return null ; // TODO: Falta la creacion de los parametros |
||
94 | } |
||
95 | |||
96 | @Override
|
||
97 | public void create(NewDataStoreParameters parameters, boolean overwrite) throws CreateException { |
||
98 | if( !this.canCreate() ) { |
||
99 | throw new UnsupportedOperationException(); |
||
100 | } |
||
101 | File output = ((FilesystemStoreParameters)parameters).getFile();
|
||
102 | if( output==null ) { |
||
103 | throw new IllegalArgumentException("Parameter 'file' can't be null."); |
||
104 | } |
||
105 | Buffer buffer = ((NewRasterStoreParameters)parameters).getBuffer();
|
||
106 | if( buffer==null ) { |
||
107 | throw new IllegalArgumentException("Parameter 'buffer' can't be null."); |
||
108 | } |
||
109 | BufferedImage image = buffer.getBufferedImage();
|
||
110 | try {
|
||
111 | ImageIO.write(image, this.getName(), output); |
||
112 | } catch (IOException ex) { |
||
113 | throw new CreateException(output.getAbsolutePath(), ex); |
||
114 | } |
||
115 | |||
116 | // DataTypesManager dtm = ToolsLocator.getDataTypesManager();
|
||
117 | // ImageWriter writer = getImageWriter();
|
||
118 | // ImageWriteParam writerParams = writer.getDefaultWriteParam();
|
||
119 | // Class<? extends ImageWriteParam> writerParamsClass = writerParams.getClass();
|
||
120 | // for( DynField field : parameters.getDynClass().getDynFields() ) {
|
||
121 | // DynField_v2 field2 = (DynField_v2) field;
|
||
122 | // try {
|
||
123 | // String methodName = (String) field2.getTags().get(
|
||
124 | // "ImageWriteParam",
|
||
125 | // DataTypes.STRING
|
||
126 | // );
|
||
127 | // if( methodName!=null ) {
|
||
128 | // Method setter = writerParamsClass.getMethod(
|
||
129 | // methodName,
|
||
130 | // dtm.getDefaultClass(field.getType())
|
||
131 | // );
|
||
132 | // setter.invoke(
|
||
133 | // writerParams,
|
||
134 | // parameters.getDynValue(field.getName())
|
||
135 | // );
|
||
136 | // }
|
||
137 | // } catch (Exception ex) {
|
||
138 | // }
|
||
139 | // }
|
||
140 | // FileOutputStream fos = null;
|
||
141 | // try {
|
||
142 | // fos = new FileOutputStream(output);
|
||
143 | // writer.setOutput(fos);
|
||
144 | // writer.write(null, new IIOImage(image, null, null), writerParams);
|
||
145 | // } catch (Exception ex) {
|
||
146 | // throw new CreateException(output.getAbsolutePath(), ex);
|
||
147 | // } finally {
|
||
148 | // IOUtils.closeQuietly(fos);
|
||
149 | // }
|
||
150 | |||
151 | } |
||
152 | |||
153 | // private ImageWriter getImageWriter() {
|
||
154 | // return null;
|
||
155 | // }
|
||
156 | |||
157 | @Override
|
||
158 | 43861 | jjdelcerro | public int getMode() { |
159 | return DataServerExplorer.MODE_RASTER;
|
||
160 | } |
||
161 | |||
162 | @Override
|
||
163 | public boolean accept(File pathname) { |
||
164 | return FilenameUtils.isExtension(pathname.getName().toLowerCase(), this.fileExtensions); |
||
165 | } |
||
166 | |||
167 | |||
168 | } |