You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
4.3 KiB
151 lines
4.3 KiB
package com.fr.design.components.page; |
|
|
|
import com.fr.base.svg.IconUtils; |
|
import com.fr.design.gui.ibutton.UIButton; |
|
import com.fr.design.gui.ilable.UILabel; |
|
|
|
import javax.swing.BorderFactory; |
|
import javax.swing.JPanel; |
|
import java.awt.BorderLayout; |
|
import java.awt.Dimension; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.util.function.Function; |
|
|
|
/** |
|
* 翻页组件 |
|
* 相关使用方式见 <a href="https://kms.fineres.com/pages/viewpage.action?pageId=415212013">翻页组件</a> |
|
* |
|
* created by Harrison on 2022/05/26 |
|
**/ |
|
public class PageControlPanel extends JPanel { |
|
|
|
private static final long serialVersionUID = 8140501834691131305L; |
|
|
|
private static final Dimension PAGE_CONTROL_BUTTON_DIMENSION = new Dimension(20, 20); |
|
|
|
private UIButton previous; |
|
private Runnable previousAction; |
|
|
|
private UILabel content; |
|
private PageControlModel model; |
|
|
|
private UIButton next; |
|
private Runnable nextAction; |
|
|
|
public PageControlPanel(PageControlModel model) { |
|
|
|
this.model = model; |
|
setBorder(BorderFactory.createEmptyBorder()); |
|
setLayout(new BorderLayout(6, 0)); |
|
|
|
this.previous = new UIButton(); |
|
previous.setPreferredSize(PAGE_CONTROL_BUTTON_DIMENSION); |
|
previous.setIcon(IconUtils.readIcon("/com/fr/design/standard/arrow/arrow_enable_left.svg")); |
|
previous.setDisabledIcon(IconUtils.readIcon("/com/fr/design/standard/arrow/arrow_disable_left.svg")); |
|
previous.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
performAction(previousAction); |
|
} |
|
}); |
|
|
|
this.add(previous, BorderLayout.WEST); |
|
|
|
this.content = new UILabel(model.toContent()); |
|
this.add(content, BorderLayout.CENTER); |
|
|
|
next = new UIButton(); |
|
next.setPreferredSize(PAGE_CONTROL_BUTTON_DIMENSION); |
|
next.setIcon(IconUtils.readIcon("/com/fr/design/standard/arrow/arrow_enable_right.svg")); |
|
next.setDisabledIcon(IconUtils.readIcon("/com/fr/design/standard/arrow/arrow_disable_right.svg")); |
|
next.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
performAction(nextAction); |
|
} |
|
}); |
|
this.add(next, BorderLayout.EAST); |
|
|
|
refresh(); |
|
} |
|
|
|
public PageControlModel performPrevious() { |
|
|
|
update(PageControlModel::previous); |
|
return this.model; |
|
} |
|
|
|
public PageControlModel performNext() { |
|
|
|
update(PageControlModel::next); |
|
return this.model; |
|
} |
|
|
|
public PageControlModel getModel() { |
|
|
|
return this.model; |
|
} |
|
|
|
public void update(PageControlModel model) { |
|
|
|
this.model.setIndex(model.getIndex()); |
|
this.model.setSummary(model.getSummary()); |
|
refresh(); |
|
} |
|
|
|
public void update(Function<PageControlModel, PageControlModel> updateAction) { |
|
|
|
PageControlModel newModel = updateAction.apply(this.model); |
|
update(newModel); |
|
refresh(); |
|
} |
|
|
|
public void refresh() { |
|
|
|
this.content.setText(this.model.toContent()); |
|
this.content.repaint(); |
|
|
|
this.previous.setEnabled(true); |
|
this.next.setEnabled(true); |
|
if (model.getNumber() == 1) { |
|
// 禁用上一个 |
|
disableButton(this.previous); |
|
// 禁用next |
|
if (model.getNumber() == model.getSummary()) { |
|
disableButton(this.next); |
|
} |
|
return; |
|
} |
|
|
|
// 禁用next |
|
if (model.getNumber() == model.getSummary()) { |
|
disableButton(this.next); |
|
} |
|
} |
|
|
|
private void enable(UIButton button) { |
|
|
|
button.setEnabled(true); |
|
} |
|
|
|
private void disableButton(UIButton button) { |
|
|
|
button.setEnabled(false); |
|
} |
|
|
|
private void performAction(Runnable action) { |
|
|
|
if (action != null) { |
|
action.run(); |
|
refresh(); |
|
} |
|
} |
|
|
|
public void actions(Runnable previousAction, Runnable nextAction) { |
|
|
|
this.previousAction = previousAction; |
|
this.nextAction = nextAction; |
|
} |
|
|
|
}
|
|
|