IkonResolver.java
01 /*
02  * Copyright 2015-2018 Andres Almiray
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
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 package org.kordamp.ikonli.javafx;
17 
18 import javafx.scene.text.Font;
19 import org.kordamp.ikonli.IkonHandler;
20 
21 import java.util.LinkedHashSet;
22 import java.util.ServiceLoader;
23 import java.util.Set;
24 
25 import static java.util.Objects.requireNonNull;
26 
27 /**
28  @author Andres Almiray
29  */
30 public class IkonResolver {
31     private static final IkonResolver INSTANCE;
32     private static final Set<IkonHandler> HANDLERS = new LinkedHashSet<>();
33 
34     static {
35         INSTANCE = new IkonResolver();
36 
37         ClassLoader classLoader = IkonResolver.class.getClassLoader();
38         ServiceLoader<IkonHandler> loader = ServiceLoader.load(IkonHandler.class, classLoader);
39         for (IkonHandler handler : loader) {
40             HANDLERS.add(handler);
41             handler.setFont(Font.loadFont(classLoader.getResource(handler.getFontResourcePath()).toExternalForm()16));
42         }
43     }
44 
45     private IkonResolver() {
46 
47     }
48 
49     public static IkonResolver getInstance() {
50         return INSTANCE;
51     }
52 
53     public IkonHandler resolveIkonHandler(String value) {
54         requireNonNull(value, "Ikon description must not be null");
55         for (IkonHandler handler : HANDLERS) {
56             if (handler.supports(value)) {
57                 return handler;
58             }
59         }
60         throw new UnsupportedOperationException("Cannot resolve '" + value + "'");
61     }
62 }