From 4bba3f18acb5a270f6a844bae7c6bf15fbff2b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levy=2EXie-=E8=A7=A3=E5=AE=89=E6=A3=AE?= Date: Wed, 25 Dec 2024 09:38:17 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-145112=20feat:=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=99=A8=E4=BA=A4=E4=BA=92=E5=8A=A8=E7=94=BB-=E6=8A=98?= =?UTF-8?q?=E5=8F=A0=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/foldablepane/UIExpandablePane.java | 84 +++++++++++++------ 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/foldablepane/UIExpandablePane.java b/designer-base/src/main/java/com/fr/design/foldablepane/UIExpandablePane.java index faae8d9f84..355c95ab6d 100644 --- a/designer-base/src/main/java/com/fr/design/foldablepane/UIExpandablePane.java +++ b/designer-base/src/main/java/com/fr/design/foldablepane/UIExpandablePane.java @@ -1,12 +1,12 @@ package com.fr.design.foldablepane; - +import com.formdev.flatlaf.util.Animator; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.fr.design.border.FineBorderFactory; import javax.swing.JPanel; import java.awt.BorderLayout; -import java.awt.Color; +import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -14,29 +14,29 @@ import static com.fine.swing.ui.layout.Layouts.cell; import static com.fine.swing.ui.layout.Layouts.column; import static com.fine.swing.ui.layout.Layouts.fix; - /** - * Created by MoMeak on 2017/7/5. + * 折叠面板 + * + * @author Levy.Xie + * @since 11.0 + * Created on 2024/11/15 */ public class UIExpandablePane extends JPanel { private static final long serialVersionUID = 1L; - private HeaderPane headerPanel; - private JPanel contentPanel; - private Color color = Color.black; - private String title; - private int headWidth; - private int headHeight; + private boolean showExpand = true; + private Animator animator; + private int fullHeight; + private final JPanel contentPanel; + private final String title; public JPanel getContentPanel() { return contentPanel; } - public UIExpandablePane(String title, int headWidth, int headHeight, JPanel contentPanel) { + public UIExpandablePane(String title, @Deprecated int headWidth, @Deprecated int headHeight, JPanel contentPanel) { super(); this.title = title; - this.headWidth = headWidth; - this.headHeight = headHeight; this.contentPanel = contentPanel; initComponents(false); } @@ -48,48 +48,80 @@ public class UIExpandablePane extends JPanel { public UIExpandablePane(String title, JPanel contentPanel, boolean withUnderline) { super(); this.title = title; - this.headHeight = headHeight; this.contentPanel = contentPanel; initComponents(withUnderline); } private void initComponents(boolean withUnderline) { this.setLayout(new BorderLayout()); - - headerPanel = new HeaderPane(title); + HeaderPane headerPanel = new HeaderPane(title); headerPanel.addMouseListener(new PanelAction()); setcontentPanelontentPanelBorder(); if (withUnderline) { this.add(column( - cell(headerPanel), cell(contentPanel).with(it -> it.setBorder(new ScaledEmptyBorder(0, 0, 10, 0))), + cell(headerPanel), + cell(contentPanel).with(it -> it.setBorder(new ScaledEmptyBorder(0, 0, 10, 0))), fix(1).with(it -> it.setBorder(FineBorderFactory.createDefaultUnderlineBorder()))) .getComponent()); } else { this.add(headerPanel, BorderLayout.NORTH); this.add(contentPanel, BorderLayout.CENTER); } - + initAnimation(); setOpaque(false); } + /** + * 组件竖向折叠显示动画 + */ + private void initAnimation() { + int width = contentPanel.getWidth(); + boolean sizeSet = contentPanel.isPreferredSizeSet(); + animator = new Animator(200, new Animator.TimingTarget() { + @Override + public void timingEvent(float fraction) { + float ratio = !showExpand ? fraction : 1f - fraction; + contentPanel.setPreferredSize(new Dimension(width, (int) (fullHeight * ratio))); + contentPanel.revalidate(); + contentPanel.repaint(); + } + + @Override + public void begin() { + contentPanel.setVisible(true); + if (showExpand) { + fullHeight = contentPanel.getPreferredSize().height; + } + } + + @Override + public void end() { + // 重置中心面板 + if (!sizeSet) { + contentPanel.setPreferredSize(null); + } + contentPanel.setVisible(!showExpand); + showExpand = !showExpand; + } + }); + } + protected void setcontentPanelontentPanelBorder() { } class PanelAction extends MouseAdapter { + @Override public void mouseClicked(MouseEvent e) { HeaderPane hp = (HeaderPane) e.getSource(); - if (contentPanel.isShowing()) { - contentPanel.setVisible(false); - hp.setShow(false); - } else { - contentPanel.setVisible(true); - hp.setShow(true); + if (!Animator.useAnimation()) { + contentPanel.setVisible(!showExpand); + } else if (!animator.isRunning()) { + animator.start(); } + hp.setShow(!showExpand); hp.setPressed(false); - hp.getParent().validate(); - hp.getParent().repaint(); } public void mousePressed(MouseEvent e) {