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

org.panteleyev.fx.WindowManager Maven / Gradle / Ivy

The newest version!
/*
 Copyright © 2020-2021 Petr Panteleyev 
 SPDX-License-Identifier: BSD-2-Clause
 */
package org.panteleyev.fx;

import javafx.collections.ObservableList;
import javafx.stage.Window;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * This class implements window manager to manipulate active {@link Controller controllers} opened by the application.
 */
public final class WindowManager {
    private final Supplier> windowListSupplier;

    private WindowManager(Supplier> windowListSupplier) {
        this.windowListSupplier = windowListSupplier;
    }

    public static WindowManager newInstance() {
        return new WindowManager(Window::getWindows);
    }

    static WindowManager newInstance(Supplier> windowListSupplier) {
        return new WindowManager(windowListSupplier);
    }

    /**
     * Finds first active controller of the specified class.
     *
     * @param ctrlClass controller class
     * @return controller
     */
    public Optional find(Class ctrlClass) {
        return getControllerStream(ctrlClass)
                .findFirst();
    }

    /**
     * Returns all active controllers.
     *
     * @return list of controllers
     */
    public List getControllers() {
        return getControllerStream().toList();
    }

    /**
     * Returns first active controller of the specified class that meets the criteria checked by enumerator.
     *
     * @param ctrlClass  controller class
     * @param enumerator enumerator
     * @return controller
     */
    public Optional find(Class ctrlClass, Predicate enumerator) {
        return getControllerStream(ctrlClass)
                .filter(enumerator)
                .findFirst();
    }

    /**
     * Returns all active controllers as stream.
     *
     * @return stream of controllers
     */
    public Stream getControllerStream() {
        return windowListSupplier.get().stream()
                .map(Window::getScene)
                .filter(w -> w.getUserData() != null && Controller.class.isAssignableFrom(w.getUserData().getClass()))
                .map(window -> (Controller) window.getUserData());
    }

    /**
     * Returns all active controllers of the specified class as stream.
     *
     * @param ctrlClass controller class
     * @return stream of controllers
     */
    public Stream getControllerStream(Class ctrlClass) {
        return windowListSupplier.get().stream()
                .map(Window::getScene)
                .filter(w -> w.getUserData() != null && ctrlClass.equals(w.getUserData().getClass()))
                .map(window -> (Controller) window.getUserData());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy