]> git.basschouten.com Git - openhab-addons.git/blob
8489fe9dc29736a58be0dbcb9be7bbdc58fe4858
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lametrictime.internal.api.impl;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19
20 import javax.activation.MimetypesFileTypeMap;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Implementation class for file icons.
28  *
29  * @author Gregory Moyer - Initial contribution
30  */
31 @NonNullByDefault
32 public class FileIcon extends AbstractDataIcon {
33     private final Logger logger = LoggerFactory.getLogger(FileIcon.class);
34
35     private final MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap();
36
37     private final Path path;
38
39     public FileIcon(File file) {
40         this(file.toPath());
41     }
42
43     public FileIcon(Path path) {
44         this.path = path;
45         mimeTypeMap.addMimeTypes("image/png png PNG");
46     }
47
48     @Override
49     protected void populateFields() {
50         setType(mimeTypeMap.getContentType(path.toFile()));
51         try {
52             setData(Files.readAllBytes(path));
53         } catch (IOException e) {
54             logger.warn("Failed reading icon content.", e);
55         }
56     }
57 }