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

org.nuiton.jaxx.runtime.init.UIInitializerContext Maven / Gradle / Ivy

There is a newer version: 3.1.5
Show newest version
package org.nuiton.jaxx.runtime.init;

/*-
 * #%L
 * JAXX :: Runtime Spi
 * %%
 * Copyright (C) 2008 - 2020 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.ultreia.java4all.util.SingletonSupplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.jaxx.runtime.JAXXObject;

import javax.swing.JComponent;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Context to collect components at init time.
 * 

* Created on 09/12/2020. * * @author Tony Chemit - [email protected] * @since 3.0 */ public class UIInitializerContext { private static final Logger log = LogManager.getLogger(UIInitializerContext.class); private final U ui; private final String prefix; private final Class[] types; private final ImmutableSet.Builder doNotBlockComponentIds; private final ImmutableSet.Builder> componentsToKeep; private final ArrayListMultimap focusComponents = ArrayListMultimap.create(); private final ImmutableList.Builder dependencies; private final SingletonSupplier, Object>> typedComponents; private Init initState; public static ArrayListMultimap, Object> components(JAXXObject ui) { ArrayListMultimap, Object> result = ArrayListMultimap.create(); ui.get$objectMap().values().forEach((v) -> result.put(v.getClass(), v)); return result; } public static Stream onComponents(Class type, ArrayListMultimap, Object> components) { return loadComponents(type, components).stream(); } @SuppressWarnings("unchecked") public static List loadComponents(Class type, ArrayListMultimap, Object> components) { ImmutableList.Builder result = ImmutableList.builder(); Set, Collection>> entries = components.asMap().entrySet(); entries.stream().filter(k -> type.isAssignableFrom(k.getKey())).forEach(k -> result.addAll((Collection) k.getValue())); return result.build(); } public UIInitializerContext(U ui, Class... types) { this.ui = Objects.requireNonNull(ui); this.prefix = "[" + ui.getClass().getSimpleName() + "] "; this.types = types; this.doNotBlockComponentIds = ImmutableSet.builder(); this.dependencies = ImmutableList.builder(); this.componentsToKeep = ImmutableSet.builder(); this.typedComponents = SingletonSupplier.of(this::init); } /** * Created on 09/12/2020. * * @author Tony Chemit - [email protected] * @since 8.0.1 */ public enum Init { FIRST_PASS, SECOND_PASS } public U getUi() { return ui; } public ArrayListMultimap, Object> init() { ArrayListMultimap, Object> typedComponents = ArrayListMultimap.create(); ui.get$objectMap().forEach((k, v) -> { if (v == null) { //FIXME This probably means a JAXX bug? log.warn(String.format("%s Null JAXX object with id: %s", prefix, k)); return; } typedComponents.put(v.getClass(), v); }); return typedComponents; } public String getPrefix() { return prefix; } public void registerDependencies(Object... dependencies) { this.dependencies.addAll(Arrays.asList(dependencies)); for (Object dep : dependencies) { ui.setContextValue(dep); } } public void addDoNotBlockComponentId(String editor) { doNotBlockComponentIds.add(editor); } public ImmutableSet getDoNotBlockComponentIds() { return doNotBlockComponentIds.build(); } public ImmutableList getDependencies() { return dependencies.build(); } public ArrayListMultimap getFocusComponents() { return focusComponents; } public ImmutableSet> getComponentsToKeep() { return componentsToKeep.build(); } public void addFocusComponents(String name, Collection collection) { focusComponents.putAll(name, collection); } public void addFocusComponent(String name, JComponent editor) { focusComponents.put(name, editor); } public UIInitializerContext startFirstPass() { log.info(String.format("%sInit widgets - first pass", prefix)); this.initState = Init.FIRST_PASS; return this; } public UIInitializerContext startSecondPass() { log.info(String.format("%sInit widgets - second pass", prefix)); this.initState = Init.SECOND_PASS; return this; } public UIInitializerContext onComponents(Class componentType, boolean keep, Consumer consumer) { if (keep) { componentsToKeep.add(componentType); } getComponents(componentType).forEach(consumer); return this; } public UIInitializerContext onComponents(Class componentType, Consumer consumer) { return onComponents(componentType, false, consumer); } public UIInitializerContext onSubComponents(Class componentType, boolean keep, Consumer consumer) { Set> subComponentsTypes = getSubComponentsTypes(componentType); if (keep) { componentsToKeep.addAll(subComponentsTypes); } getSubComponents(componentType, subComponentsTypes).forEach(consumer); return this; } public UIInitializerContext onSubComponents(Class componentType, Consumer consumer) { return onSubComponents(componentType, false, consumer); } public void checkFirstPass() { check(Init.FIRST_PASS); } public void checkSecondPass() { check(Init.SECOND_PASS); } public void check(Init initState) { if (!Objects.equals(initState, this.initState)) { throw new IllegalStateException("Must be in " + this + " but still on " + initState); } } @SuppressWarnings("unchecked") public Stream getComponents(Class componentType) { return (Stream) typedComponents.get().get(componentType).stream(); } @SuppressWarnings("unchecked") public Stream getSubComponents(Class componentType, Set> componentTypes) { ArrayListMultimap, Object> multimap = typedComponents.get(); return (Stream) multimap.asMap().entrySet().stream().filter(e -> componentTypes.contains(e.getKey())).flatMap(e -> e.getValue().stream()).distinct(); } public Set> getSubComponentsTypes(Class componentType) { ArrayListMultimap, Object> multimap = typedComponents.get(); return multimap.asMap().keySet().stream().filter(componentType::isAssignableFrom).collect(Collectors.toSet()); } public UIInitializerContext apply(Runnable consumer) { consumer.run(); return this; } public ArrayListMultimap, Object> getKeptComponents() { ArrayListMultimap, Object> result = ArrayListMultimap.create(); getComponentsToKeep().forEach(type -> result.putAll(type, typedComponents.get().get(type))); return result; } }