/**
 *  Copyright (c) 2015-2017 Angelo ZERR.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */
package org.eclipse.tm4e.ui.internal.themes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.tm4e.ui.themes.IThemeAssociation;

/**
 * Theme association registry.
 *
 */
public class ThemeAssociationRegistry extends BaseThemeAssociationRegistry {

	private final Map<String, BaseThemeAssociationRegistry> scopes;

	public ThemeAssociationRegistry() {
		scopes = new HashMap<>();
	}

	@Override
	public void register(IThemeAssociation association) {
		String scopeName = association.getScopeName();
		if (scopeName == null) {
			super.register(association);
		} else {
			BaseThemeAssociationRegistry registry = scopes.get(scopeName);
			if (registry == null) {
				registry = new BaseThemeAssociationRegistry();
				scopes.put(scopeName, registry);
			}
			registry.register(association);
		}
	}

	@Override
	public void unregister(IThemeAssociation association) {
		String scopeName = association.getScopeName();
		if (scopeName == null) {
			super.unregister(association);
		} else {
			BaseThemeAssociationRegistry registry = scopes.get(scopeName);
			if (registry != null) {
				registry.unregister(association);
			}
		}
	}

	public IThemeAssociation getThemeAssociationFor(String scopeName, String eclipseThemeId) {
		IThemeAssociation association = null;
		BaseThemeAssociationRegistry registry = scopes.get(scopeName);
		if (registry != null) {
			association = registry.getThemeAssociationFor(eclipseThemeId);
			if (association == null) {
				association = registry.getDefaultAssociation();
			}
		}
		if (association == null) {
			association = super.getThemeAssociationFor(eclipseThemeId);
		}
		return association != null ? association : getDefaultAssociation();
	}

	public IThemeAssociation[] getThemeAssociationsForScope(String scopeName) {
		BaseThemeAssociationRegistry registry = scopes.get(scopeName);
		if (registry != null) {
			return registry.getThemeAssociations().toArray(new IThemeAssociation[0]);
		}
		return getThemeAssociations(true);
	}
	
	@Override
	public List<IThemeAssociation> getThemeAssociations() {
		List<IThemeAssociation> allAssociations = new ArrayList<>(super.getThemeAssociations());
		for (BaseThemeAssociationRegistry registry : scopes.values()) {
			allAssociations.addAll(registry.getThemeAssociations());
		}
		return allAssociations;
	}

}

@@ -546,16 +546,53 @@
 il.Map;%0D
+%0Aimport java.util.stream.Collectors;%0D
 %0A%0D%0Aimpor
@@ -2427,131 +2427,1197 @@
 %0A%09%09%09
-return registry.getThemeAssociations().toArray(new IThemeAssociation%5B0%5D);%0D%0A%09%09%7D%0D%0A%09%09return getThemeAssociations(true);%0D%0A%09%7D%0D%0A%09
+// Get the user associations (from preferences)%0D%0A%09%09%09List%3CIThemeAssociation%3E userAssociations = registry.getThemeAssociations();%0D%0A%09%09%09// Get the default associations (from plugin)%0D%0A%09%09%09List%3CIThemeAssociation%3E defaultAssociations = getThemeAssociations().stream()%0D%0A%09%09%09%09%09.filter(theme -%3E theme.isDefault()).collect(Collectors.toList());%0D%0A%09%09%09// Add default association if user associations doesn't define it.%0D%0A%09%09%09for (IThemeAssociation defaultAssociation : defaultAssociations) %7B%0D%0A%09%09%09%09if (!(contains(userAssociations, defaultAssociation))) %7B%0D%0A%09%09%09%09%09userAssociations.add(defaultAssociation);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09return userAssociations.toArray(new IThemeAssociation%5B0%5D);%0D%0A%09%09%7D%0D%0A%09%09return getThemeAssociations(true);%0D%0A%09%7D%0D%0A%0D%0A%09private boolean contains(List%3CIThemeAssociation%3E userAssociations, IThemeAssociation defaultAssociation) %7B%0D%0A%09%09for (IThemeAssociation userAssociation : userAssociations) %7B%0D%0A%09%09%09if (defaultAssociation.getEclipseThemeId() == null) %7B%0D%0A%09%09%09%09if (userAssociation.getEclipseThemeId() == null) %7B%0D%0A%09%09%09%09%09return true;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09if (defaultAssociation.getEclipseThemeId().equals(userAssociation.getEclipseThemeId())) %7B%0D%0A%09%09%09%09%09return true;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return false;%0D%0A%09%7D%0D%0A
 %0D%0A%09@

Add default association if user associations doesn't define it. See https://github.com/eclipse/tm4e/issues/117 Signed-off-by: angelozerr <angelo.zerr@gmail.com>