NodeUtils.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package io.github.palexdev.materialfx.utils;
  2. import javafx.geometry.HPos;
  3. import javafx.geometry.Insets;
  4. import javafx.geometry.VPos;
  5. import javafx.scene.layout.Background;
  6. import javafx.scene.layout.BackgroundFill;
  7. import javafx.scene.layout.Region;
  8. import javafx.scene.paint.Paint;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * Utility class which provides convenience methods for working with Nodes
  13. */
  14. public class NodeUtils {
  15. private NodeUtils() {
  16. }
  17. /**
  18. * Changes the background color of a {@code Region} to the desired one.
  19. * @param region The region to change the background color to
  20. * @param fill The desired color
  21. */
  22. public static void updateBackground(Region region, Paint fill) {
  23. final Background background = region.getBackground();
  24. if (background == null || background.getFills().isEmpty()) {
  25. return;
  26. }
  27. final List<BackgroundFill> fills = new ArrayList<>();
  28. for (BackgroundFill bf : background.getFills()) {
  29. fills.add(new BackgroundFill(fill, bf.getRadii(), bf.getInsets()));
  30. }
  31. region.setBackground(new Background(fills.toArray(BackgroundFill[]::new)));
  32. }
  33. /**
  34. * Changes the background color of a {@code Region} to the desired one and lets specify the background insets.
  35. * @param region The region to change the background color to
  36. * @param fill The desired color
  37. * @param backgroundInsets The background insets to use
  38. */
  39. public static void updateBackground(Region region, Paint fill, Insets backgroundInsets) {
  40. final Background background = region.getBackground();
  41. if (background == null || background.getFills().isEmpty()) {
  42. return;
  43. }
  44. final List<BackgroundFill> fills = new ArrayList<>();
  45. for (BackgroundFill bf : background.getFills()) {
  46. fills.add(new BackgroundFill(fill, bf.getRadii(), backgroundInsets));
  47. }
  48. region.setBackground(new Background(fills.toArray(BackgroundFill[]::new)));
  49. }
  50. /* The next two methods are copied from com.sun.javafx.scene.control.skin.Utils class
  51. * It's a private module, so to avoid adding exports and opens I copied them
  52. */
  53. public static double computeXOffset(double width, double contentWidth, HPos hpos) {
  54. switch (hpos) {
  55. case LEFT:
  56. return 0;
  57. case CENTER:
  58. return (width - contentWidth) / 2;
  59. case RIGHT:
  60. return width - contentWidth;
  61. }
  62. return 0;
  63. }
  64. public static double computeYOffset(double height, double contentHeight, VPos vpos) {
  65. switch (vpos) {
  66. case TOP:
  67. return 0;
  68. case CENTER:
  69. return (height - contentHeight) / 2;
  70. case BOTTOM:
  71. return height - contentHeight;
  72. default:
  73. return 0;
  74. }
  75. }
  76. }