Browse Source

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

fbp/feature
Levy.Xie-解安森 4 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; package com.fr.design.foldablepane;
import com.formdev.flatlaf.util.Animator;
import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.fr.design.border.FineBorderFactory; import com.fr.design.border.FineBorderFactory;
import javax.swing.JPanel; import javax.swing.JPanel;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Dimension;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; 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.column;
import static com.fine.swing.ui.layout.Layouts.fix; 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 { public class UIExpandablePane extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private HeaderPane headerPanel; private boolean showExpand = true;
private JPanel contentPanel; private Animator animator;
private Color color = Color.black; private int fullHeight;
private String title; private final JPanel contentPanel;
private int headWidth; private final String title;
private int headHeight;
public JPanel getContentPanel() { public JPanel getContentPanel() {
return contentPanel; 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(); super();
this.title = title; this.title = title;
this.headWidth = headWidth;
this.headHeight = headHeight;
this.contentPanel = contentPanel; this.contentPanel = contentPanel;
initComponents(false); initComponents(false);
} }
@ -48,48 +48,80 @@ public class UIExpandablePane extends JPanel {
public UIExpandablePane(String title, JPanel contentPanel, boolean withUnderline) { public UIExpandablePane(String title, JPanel contentPanel, boolean withUnderline) {
super(); super();
this.title = title; this.title = title;
this.headHeight = headHeight;
this.contentPanel = contentPanel; this.contentPanel = contentPanel;
initComponents(withUnderline); initComponents(withUnderline);
} }
private void initComponents(boolean withUnderline) { private void initComponents(boolean withUnderline) {
this.setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
HeaderPane headerPanel = new HeaderPane(title);
headerPanel = new HeaderPane(title);
headerPanel.addMouseListener(new PanelAction()); headerPanel.addMouseListener(new PanelAction());
setcontentPanelontentPanelBorder(); setcontentPanelontentPanelBorder();
if (withUnderline) { if (withUnderline) {
this.add(column( 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()))) fix(1).with(it -> it.setBorder(FineBorderFactory.createDefaultUnderlineBorder())))
.getComponent()); .getComponent());
} else { } else {
this.add(headerPanel, BorderLayout.NORTH); this.add(headerPanel, BorderLayout.NORTH);
this.add(contentPanel, BorderLayout.CENTER); this.add(contentPanel, BorderLayout.CENTER);
} }
initAnimation();
setOpaque(false); 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() { protected void setcontentPanelontentPanelBorder() {
} }
class PanelAction extends MouseAdapter { class PanelAction extends MouseAdapter {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
HeaderPane hp = (HeaderPane) e.getSource(); HeaderPane hp = (HeaderPane) e.getSource();
if (contentPanel.isShowing()) { if (!Animator.useAnimation()) {
contentPanel.setVisible(false); contentPanel.setVisible(!showExpand);
hp.setShow(false); } else if (!animator.isRunning()) {
} else { animator.start();
contentPanel.setVisible(true);
hp.setShow(true);
} }
hp.setShow(!showExpand);
hp.setPressed(false); hp.setPressed(false);
hp.getParent().validate();
hp.getParent().repaint();
} }
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {

Loading…
Cancel
Save