All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.teamapps.icons.IconProvider Maven / Gradle / Ivy

There is a newer version: 0.9.195
Show newest version
/*-
 * ========================LICENSE_START=================================
 * TeamApps
 * ---
 * Copyright (C) 2014 - 2021 TeamApps.org
 * ---
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.teamapps.icons;

import org.teamapps.icons.spi.IconDecoder;
import org.teamapps.icons.spi.IconEncoder;
import org.teamapps.icons.spi.IconLoader;

import java.io.IOException;
import java.nio.file.Files;

public class IconProvider implements IconLoaderContext, IconDecoderContext {

	private final IconLibraryRegistry iconLibraryRegistry;
	private final IconCache iconCache;

	public IconProvider(IconLibraryRegistry iconLibraryRegistry) {
		this.iconLibraryRegistry = iconLibraryRegistry;
		try {
			this.iconCache = new IconCache(Files.createTempDirectory("teamapps-icon-cache").toFile());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public IconProvider(IconLibraryRegistry iconLibraryRegistry, IconCache iconCache) {
		this.iconLibraryRegistry = iconLibraryRegistry;
		this.iconCache = iconCache;
	}

	@Override
	public Icon decodeIcon(String qualifiedEncodedIcon) {
		String libraryName = getLibraryName(qualifiedEncodedIcon);
		String encodedIconString = qualifiedEncodedIcon.substring(libraryName.length() + 1);
		IconDecoder iconDecoder = iconLibraryRegistry.getIconDecoder(libraryName);
		return iconDecoder.decodeIcon(encodedIconString, this);
	}

	public IconResource loadIcon(String qualifiedEncodedIcon, int size) {
		if (iconCache != null) {
			IconResource cachedIcon = iconCache.getCachedIcon(qualifiedEncodedIcon, size);
			if (cachedIcon != null) {
				return cachedIcon;
			}
		}

		String libraryName = getLibraryName(qualifiedEncodedIcon);
		String encodedIconString = qualifiedEncodedIcon.substring(libraryName.length() + 1);
		IconDecoder iconDecoder = iconLibraryRegistry.getIconDecoder(libraryName);
		Icon icon = iconDecoder.decodeIcon(encodedIconString, this);

		IconResource iconResource = loadIconWithoutCaching(icon, size);
		if (iconResource == null) {
			return null;
		}

		if (iconCache != null) {
			iconCache.putIcon(qualifiedEncodedIcon, size, iconResource);
		}

		return iconResource;
	}

	@Override
	public IconResource loadIcon(Icon icon, int size) {
		return loadIconWithoutCaching(icon, size);
	}

	private IconResource loadIconWithoutCaching(Icon icon, int size) {
		String libraryName = getLibraryName(icon);
		IconLoader iconLoader = iconLibraryRegistry.getIconLoader(libraryName);
		IconResource iconResource = iconLoader.loadIcon(icon, size, this);
		if (iconResource == null) {
			return null;
		}

		if (iconResource.getIconType().isRasterImage() && iconResource.getSize() > 0 && size > 0 &&  iconResource.getSize() != size) {
			byte[] resizedIconBytes = new PngIconResizer().resizeIcon(iconResource.getBytes(), size);
			iconResource = new IconResource(resizedIconBytes, IconType.PNG, size);
		}
		return iconResource;
	}

	public , S> IconEncoder getIconEncoder(Class iconClass) {
		return iconLibraryRegistry.getIconEncoder(iconClass);
	}

	public , S> String getLibraryName(Icon icon) {
		return iconLibraryRegistry.getLibraryName(icon);
	}

	public , S> void registerIconLibrary(Class iconClass) {
		iconLibraryRegistry.registerIconLibrary(iconClass);
	}

	public , S> void registerIconLibrary(Class iconClass, String libraryName, IconEncoder iconEncoder, IconDecoder iconDecoder, IconLoader iconLoader, S defaultStyle) {
		iconLibraryRegistry.registerIconLibrary(iconClass, libraryName, iconEncoder, iconDecoder, iconLoader, defaultStyle);
	}

	public , S> S getDefaultStyle(Class iconClass) {
		return iconLibraryRegistry.getDefaultStyle(iconClass);
	}

	private String getLibraryName(String qualifiedEncodedIcon) {
		int firstDotIndex = qualifiedEncodedIcon.indexOf('.');
		return qualifiedEncodedIcon.substring(0, firstDotIndex);
	}
}