From abd5aad20138e53e8b02ea8aa6da165812e18a26 Mon Sep 17 00:00:00 2001
From: GIRARDOT Raphael <raphael.girardot@synchrotron-soleil.fr>
Date: Fri, 21 Feb 2025 08:29:20 +0100
Subject: [PATCH] Better button orientation managament

---
 .../project/swing/border/FoldableBorder.java  | 49 +++++++++++++++++--
 1 file changed, 45 insertions(+), 4 deletions(-)

diff --git a/SwingUtilities/src/main/java/fr/soleil/lib/project/swing/border/FoldableBorder.java b/SwingUtilities/src/main/java/fr/soleil/lib/project/swing/border/FoldableBorder.java
index e787ffc..e2b748b 100644
--- a/SwingUtilities/src/main/java/fr/soleil/lib/project/swing/border/FoldableBorder.java
+++ b/SwingUtilities/src/main/java/fr/soleil/lib/project/swing/border/FoldableBorder.java
@@ -43,7 +43,7 @@ import fr.soleil.lib.project.swing.ArrowButton;
  */
 public class FoldableBorder extends ColoredLineTitledBorder implements MouseListener, MouseMotionListener {
 
-    private static final long serialVersionUID = -167592519370626180L;
+    private static final long serialVersionUID = 5793692903373431423L;
 
     protected static final Dimension ZERO_SIZE = new Dimension(0, 0);
 
@@ -168,6 +168,7 @@ public class FoldableBorder extends ColoredLineTitledBorder implements MouseList
                 case LEADING:
                 case TRAILING:
                     this.buttonPosition = buttonPosition;
+                    updateButtonOrientation(isFolded());
                     repaintLastComponent();
                     break;
             }
@@ -454,16 +455,42 @@ public class FoldableBorder extends ColoredLineTitledBorder implements MouseList
         return buttonBounds;
     }
 
+    protected void updateButtonOrientation(boolean folded) {
+        int orientation, ref;
+        if (folded) {
+            ref = getJustification(getLastComponent());
+            if (ref == CENTER) {
+                ref = getButtonPosition();
+            }
+            if (ref == LEFT || ref == LEADING) {
+                orientation = ArrowButton.RIGHT;
+            } else {
+                orientation = ArrowButton.LEFT;
+            }
+        } else {
+            ref = getPosition();
+            if (ref == ABOVE_BOTTOM || ref == BOTTOM || ref == BELOW_BOTTOM) {
+                orientation = ArrowButton.UP;
+            } else {
+                orientation = ArrowButton.DOWN;
+            }
+        }
+        if (arrowButton != null) {
+            arrowButton.setOrientation(orientation);
+        }
+    }
+
     public boolean isFolded() {
-        return arrowButton.getOrientation() == ArrowButton.LEFT;
+        return (arrowButton != null) && (arrowButton.getOrientation() == ArrowButton.LEFT
+                || arrowButton.getOrientation() == ArrowButton.RIGHT);
     }
 
     public void setFolded(boolean folded) {
         boolean formerFolded = isFolded();
         if (formerFolded != folded) {
-            arrowButton.setOrientation(folded ? ArrowButton.LEFT : ArrowButton.DOWN);
-            repaintLastComponent();
+            updateButtonOrientation(folded);
             changeSupport.firePropertyChange(BORDER_FOLDED, formerFolded, folded);
+            repaintLastComponent();
         }
     }
 
@@ -513,6 +540,20 @@ public class FoldableBorder extends ColoredLineTitledBorder implements MouseList
         }
     }
 
+    @Override
+    public void setTitlePosition(int titlePosition) {
+        super.setTitlePosition(titlePosition);
+        updateButtonOrientation(isFolded());
+        repaintLastComponent();
+    }
+
+    @Override
+    public void setTitleJustification(int titleJustification) {
+        super.setTitleJustification(titleJustification);
+        updateButtonOrientation(isFolded());
+        repaintLastComponent();
+    }
+
     @Override
     public void mouseClicked(MouseEvent e) {
         if (isButtonEvent(e) && arrowButton.isEnabled()) {
-- 
GitLab