Browse Source

REPORT-145112 feat:设计器交互动画-折叠面板

fbp/feature
Levy.Xie-解安森 2 months ago
parent
commit
4bba3f18ac
  1. 84
      designer-base/src/main/java/com/fr/design/foldablepane/UIExpandablePane.java

84
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) {

Loading…
Cancel
Save