| 
001 /*002  * Copyright 2008-2015 the original author or authors.
 003  *
 004  * Licensed under the Apache License, Version 2.0 (the "License");
 005  * you may not use this file except in compliance with the License.
 006  * You may obtain a copy of the License at
 007  *
 008  *     http://www.apache.org/licenses/LICENSE-2.0
 009  *
 010  * Unless required by applicable law or agreed to in writing, software
 011  * distributed under the License is distributed on an "AS IS" BASIS,
 012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 013  * See the License for the specific language governing permissions and
 014  * limitations under the License.
 015  */
 016 package griffon.swing.editors;
 017
 018 import griffon.core.editors.AbstractPropertyEditor;
 019 import griffon.metadata.PropertyEditorFor;
 020
 021 import javax.imageio.ImageIO;
 022 import javax.imageio.stream.ImageInputStream;
 023 import java.awt.image.BufferedImage;
 024 import java.io.File;
 025 import java.io.IOException;
 026 import java.io.InputStream;
 027 import java.net.MalformedURLException;
 028 import java.net.URI;
 029 import java.net.URL;
 030
 031 import static griffon.util.GriffonNameUtils.isBlank;
 032
 033 /**
 034  * @author Andres Almiray
 035  * @since 2.0.0
 036  */
 037 @PropertyEditorFor(BufferedImage.class)
 038 public class BufferedImagePropertyEditor extends AbstractPropertyEditor {
 039     protected void setValueInternal(Object value) {
 040         if (null == value) {
 041             super.setValueInternal(null);
 042         } else if (value instanceof CharSequence) {
 043             handleAsString(String.valueOf(value));
 044         } else if (value instanceof File) {
 045             handleAsFile((File) value);
 046         } else if (value instanceof URL) {
 047             handleAsURL((URL) value);
 048         } else if (value instanceof URI) {
 049             handleAsURI((URI) value);
 050         } else if (value instanceof InputStream) {
 051             handleAsInputStream((InputStream) value);
 052         } else if (value instanceof ImageInputStream) {
 053             handleAsImageInputStream((ImageInputStream) value);
 054         } else if (value instanceof BufferedImage) {
 055             super.setValueInternal(value);
 056         } else {
 057             throw illegalValue(value, BufferedImage.class);
 058         }
 059     }
 060
 061     private void handleAsString(String str) {
 062         if (isBlank(str)) {
 063             super.setValueInternal(null);
 064         } else {
 065             handleAsURL(getClass().getClassLoader().getResource(str));
 066         }
 067     }
 068
 069     private void handleAsFile(File file) {
 070         try {
 071             super.setValueInternal(ImageIO.read(file));
 072         } catch (IOException e) {
 073             throw illegalValue(file, BufferedImage.class, e);
 074         }
 075     }
 076
 077     private void handleAsURL(URL url) {
 078         try {
 079             super.setValueInternal(ImageIO.read(url));
 080         } catch (IOException e) {
 081             throw illegalValue(url, BufferedImage.class, e);
 082         }
 083     }
 084
 085     private void handleAsURI(URI uri) {
 086         try {
 087             handleAsURL(uri.toURL());
 088         } catch (MalformedURLException e) {
 089             throw illegalValue(uri, BufferedImage.class, e);
 090         }
 091     }
 092
 093     private void handleAsInputStream(InputStream stream) {
 094         try {
 095             super.setValueInternal(ImageIO.read(stream));
 096         } catch (IOException e) {
 097             throw illegalValue(stream, BufferedImage.class, e);
 098         }
 099     }
 100
 101     private void handleAsImageInputStream(ImageInputStream stream) {
 102         try {
 103             super.setValueInternal(ImageIO.read(stream));
 104         } catch (IOException e) {
 105             throw illegalValue(stream, BufferedImage.class, e);
 106         }
 107     }
 108 }
 |