|
| 1 | +/* OwlPlug |
| 2 | + * Copyright (C) 2021 Arthur <dropsnorz@gmail.com> |
| 3 | + * |
| 4 | + * This file is part of OwlPlug. |
| 5 | + * |
| 6 | + * OwlPlug is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License version 3 |
| 8 | + * as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * OwlPlug is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with OwlPlug. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.owlplug.plugin.components; |
| 20 | + |
| 21 | +import com.owlplug.plugin.model.IPlugin; |
| 22 | +import com.owlplug.plugin.model.Plugin; |
| 23 | +import com.owlplug.plugin.model.PluginComponent; |
| 24 | +import com.owlplug.plugin.model.PluginFormat; |
| 25 | +import com.owlplug.plugin.model.PluginType; |
| 26 | +import java.util.function.Predicate; |
| 27 | +import javafx.beans.binding.Bindings; |
| 28 | +import javafx.beans.property.ObjectProperty; |
| 29 | +import javafx.beans.property.SimpleObjectProperty; |
| 30 | +import javafx.collections.FXCollections; |
| 31 | +import javafx.collections.ObservableSet; |
| 32 | +import org.springframework.stereotype.Component; |
| 33 | + |
| 34 | +@Component |
| 35 | +public class PluginFilterState { |
| 36 | + |
| 37 | + private final ObservableSet<PluginFormat> selectedFormats = FXCollections.observableSet(); |
| 38 | + private final ObservableSet<PluginType> selectedTypes = FXCollections.observableSet(); |
| 39 | + private final ObservableSet<String> selectedManufacturers = FXCollections.observableSet(); |
| 40 | + private final ObservableSet<String> selectedCategories = FXCollections.observableSet(); |
| 41 | + private final ObjectProperty<Predicate<IPlugin>> predicate = new SimpleObjectProperty<>(); |
| 42 | + |
| 43 | + public PluginFilterState() { |
| 44 | + predicate.bind(Bindings.createObjectBinding(this::buildPredicate, |
| 45 | + selectedFormats, selectedTypes, selectedManufacturers, selectedCategories)); |
| 46 | + } |
| 47 | + |
| 48 | + private Predicate<IPlugin> buildPredicate() { |
| 49 | + if (selectedFormats.isEmpty() && selectedTypes.isEmpty() |
| 50 | + && selectedManufacturers.isEmpty() && selectedCategories.isEmpty()) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + return plugin -> { |
| 54 | + if (plugin instanceof PluginComponent component) { |
| 55 | + return matchesFormat(component.asPlugin().getFormat()) |
| 56 | + && matchesType(component.getType()) |
| 57 | + && matchesManufacturer(component.getManufacturerName()) |
| 58 | + && matchesCategory(component.getCategory()); |
| 59 | + } else { |
| 60 | + Plugin p = (Plugin) plugin; |
| 61 | + return matchesFormat(p.getFormat()) |
| 62 | + && (matchesType(p.getType()) |
| 63 | + || p.getComponents().stream().anyMatch(c -> matchesType(c.getType()))) |
| 64 | + && (matchesManufacturer(p.getManufacturerName()) |
| 65 | + || p.getComponents().stream().anyMatch(c -> matchesManufacturer(c.getManufacturerName()))) |
| 66 | + && (matchesCategory(p.getCategory()) |
| 67 | + || p.getComponents().stream().anyMatch(c -> matchesCategory(c.getCategory()))); |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + private boolean matchesFormat(PluginFormat format) { |
| 73 | + return selectedFormats.isEmpty() || selectedFormats.contains(format); |
| 74 | + } |
| 75 | + |
| 76 | + private boolean matchesType(PluginType type) { |
| 77 | + return selectedTypes.isEmpty() || selectedTypes.contains(type); |
| 78 | + } |
| 79 | + |
| 80 | + private boolean matchesManufacturer(String manufacturer) { |
| 81 | + return selectedManufacturers.isEmpty() || selectedManufacturers.contains(manufacturer); |
| 82 | + } |
| 83 | + |
| 84 | + private boolean matchesCategory(String category) { |
| 85 | + return selectedCategories.isEmpty() || selectedCategories.contains(category); |
| 86 | + } |
| 87 | + |
| 88 | + public ObservableSet<PluginFormat> getSelectedFormats() { |
| 89 | + return selectedFormats; |
| 90 | + } |
| 91 | + |
| 92 | + public ObservableSet<PluginType> getSelectedTypes() { |
| 93 | + return selectedTypes; |
| 94 | + } |
| 95 | + |
| 96 | + public ObservableSet<String> getSelectedManufacturers() { |
| 97 | + return selectedManufacturers; |
| 98 | + } |
| 99 | + |
| 100 | + public ObservableSet<String> getSelectedCategories() { |
| 101 | + return selectedCategories; |
| 102 | + } |
| 103 | + |
| 104 | + public ObjectProperty<Predicate<IPlugin>> predicateProperty() { |
| 105 | + return predicate; |
| 106 | + } |
| 107 | + |
| 108 | + public void clearAll() { |
| 109 | + selectedFormats.clear(); |
| 110 | + selectedTypes.clear(); |
| 111 | + selectedManufacturers.clear(); |
| 112 | + selectedCategories.clear(); |
| 113 | + } |
| 114 | +} |
0 commit comments