[LIB-9] Separate chesshog-graphics module
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / AbstractImageLoader.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.img;
18
19 import java.awt.image.BufferedImage;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.nio.file.DirectoryStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.imageio.ImageIO;
29
30 import org.hedgecode.chess.img.fs.FileSystemDetector;
31 import org.hedgecode.chess.img.fs.FileSystemContractor;
32
33 /**
34  * Abstract common image loader with type state of saved resources.
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 public abstract class AbstractImageLoader implements ImageLoader {
39
40     private static final String IMAGES_DIR =
41             ImageConstants.RESOURCE_ROOT_DIR.concat(ImageConstants.RESOURCE_IMAGES_DIR);
42
43     private Type loadType;
44
45     private URI imageResourceUri;
46
47     public AbstractImageLoader() {
48         loadType = Type.STATELESS;
49         try {
50             if (FileSystemDetector.detectJar(getImageResourceUri())) {
51                 loadType = Type.STATEFUL;
52             }
53         } catch (ImageException ignored) {
54         }
55     }
56
57     protected abstract void clear();
58
59     @Override
60     public Type loadType() {
61         return loadType;
62     }
63
64     protected void setLoadType(Type loadType) {
65         if (Type.STATELESS.equals(loadType)) {
66             clear();
67         }
68         this.loadType = loadType;
69     }
70
71     protected URI getImageResourceUri() throws ImageException {
72         if (imageResourceUri == null) {
73             try {
74                 imageResourceUri = getClass().getResource(IMAGES_DIR).toURI();
75             } catch (URISyntaxException e) {
76                 throw new ImageException("image.unable.access.resource");
77             }
78         }
79         return imageResourceUri;
80     }
81
82     protected Map<String, BufferedImage> loadImages(String imagesPath, ImageFilter filter)
83             throws ImageException
84     {
85         Map<String, BufferedImage> images = new HashMap<>();
86         try (FileSystemContractor fsc = FileSystemDetector.detect(getImageResourceUri())) {
87             try (DirectoryStream<Path> ds =
88                          Files.newDirectoryStream(
89                                  fsc.getPath(imagesPath), filter
90                          )
91             ) {
92                 for (Path file : ds) {
93                     images.put(
94                             FilenameUtils.getBaseName(
95                                     file.getFileName().toString()
96                             ).toLowerCase(),
97                             ImageIO.read(
98                                     fsc.getResourceAsStream(file)
99                             )
100                     );
101                 }
102             }
103         } catch (Exception e) {
104             throw new ImageException("image.unable.access.resource", e.getMessage());
105         }
106         return images;
107     }
108
109 }