ViewSwitcher.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2023 Parisi Alessandro - alessandro.parisi406@gmail.com
  3. * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX)
  4. *
  5. * MaterialFX is free software: you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * MaterialFX 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.
  13. * See the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with MaterialFX. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package app.others.ui;
  19. import io.github.palexdev.mfxcore.utils.Memoizer;
  20. import javafx.scene.Node;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.function.Function;
  24. // TODO implement animated switching for fun?
  25. public class ViewSwitcher<T> {
  26. //================================================================================
  27. // Properties
  28. //================================================================================
  29. private final Map<T, Function<T, Node>> views = new LinkedHashMap<>();
  30. //================================================================================
  31. // Methods
  32. //================================================================================
  33. public ViewSwitcher<T> register(T id, Function<T, Node> sceneSupplier) {
  34. views.put(id, Memoizer.memoize(sceneSupplier));
  35. return this;
  36. }
  37. public ViewSwitcher<T> unregister(T id) {
  38. views.remove(id);
  39. return this;
  40. }
  41. public Node load(T id) {
  42. return views.get(id).apply(id);
  43. }
  44. public Map<T, Function<T, Node>> views() {
  45. return views;
  46. }
  47. }