forked from fanruan/design
pengda
3 years ago
11 changed files with 232 additions and 31 deletions
@ -0,0 +1,162 @@
|
||||
package com.fr.design.gui.ilist; |
||||
|
||||
import com.fr.design.event.StateChangeListener; |
||||
import com.fr.design.gui.icheckbox.UICheckBox; |
||||
import com.fr.design.gui.itree.checkboxtree.TristateCheckBox; |
||||
import com.fr.design.i18n.Toolkit; |
||||
import com.fr.design.layout.FRGUIPaneFactory; |
||||
|
||||
import javax.swing.AbstractListModel; |
||||
import javax.swing.DefaultListCellRenderer; |
||||
import javax.swing.JList; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.ListCellRenderer; |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Component; |
||||
import java.awt.event.MouseAdapter; |
||||
import java.awt.event.MouseEvent; |
||||
import java.awt.event.MouseListener; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 支持全选、全不选、半选的CheckBoxList面板 |
||||
* @author Yvan |
||||
*/ |
||||
public class CheckBoxListWithPartialSelect extends JPanel { |
||||
|
||||
private UICheckBox[] dataCheckBoxes; |
||||
|
||||
private TristateCheckBox chooseAllCheckBox; |
||||
|
||||
private UIList dataList; |
||||
|
||||
public CheckBoxListWithPartialSelect (Object[] data) { |
||||
this.setLayout(FRGUIPaneFactory.createBorderLayout()); |
||||
init(data); |
||||
this.add(chooseAllCheckBox, BorderLayout.NORTH); |
||||
this.add(dataList, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void init(Object[] data) { |
||||
// 复选框组
|
||||
dataCheckBoxes = new UICheckBox[data.length]; |
||||
for (int i = 0; i < dataCheckBoxes.length; i++) { |
||||
dataCheckBoxes[i] = new UICheckBox(transferDataValue2Show(data[i])); |
||||
dataCheckBoxes[i].setSelected(true); |
||||
} |
||||
|
||||
// UIList
|
||||
dataList = new UIList(dataCheckBoxes); |
||||
dataList.setModel(getListModel()); |
||||
dataList.setCellRenderer(getListCellRenderer()); |
||||
|
||||
// 全选框
|
||||
chooseAllCheckBox = new TristateCheckBox(Toolkit.i18nText("Fine-Design_Basic_Remove_All_Selected")); |
||||
chooseAllCheckBox.setState(TristateCheckBox.SELECTED); |
||||
chooseAllCheckBox.setFocusable(false); |
||||
chooseAllCheckBox.addStateChangeListener(getChooseAllCheckBoxStateChangeListener()); |
||||
dataList.addMouseListener(getDataListMouseListener()); |
||||
} |
||||
|
||||
public List<Object> getSelectedObjects() { |
||||
return dataList.getSelectedValuesList(); |
||||
} |
||||
|
||||
protected MouseListener getDataListMouseListener() { |
||||
return new MouseAdapter() { |
||||
@Override |
||||
public void mousePressed(MouseEvent e) { |
||||
super.mousePressed(e); |
||||
int index = dataList.getSelectedIndex(); |
||||
if (index < 0) { |
||||
return; |
||||
} |
||||
UICheckBox checkBox = (UICheckBox) dataList.getModel().getElementAt(index); |
||||
checkBox.setSelected(!checkBox.isSelected()); |
||||
//根据CheckBoxes中的选择情况来更新全选框的状态
|
||||
int selectedCount = calculateSelectedNum(); |
||||
if (selectedCount == 0) { |
||||
chooseAllCheckBox.setState(TristateCheckBox.NOT_SELECTED); |
||||
} else if (selectedCount == dataCheckBoxes.length) { |
||||
chooseAllCheckBox.setState(TristateCheckBox.SELECTED); |
||||
} else { |
||||
chooseAllCheckBox.setState(TristateCheckBox.DO_NOT_CARE); |
||||
} |
||||
dataList.repaint(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* 获取全选框状态改变监听 |
||||
* @return |
||||
*/ |
||||
protected StateChangeListener getChooseAllCheckBoxStateChangeListener() { |
||||
return () -> { |
||||
if (chooseAllCheckBox.getState() == TristateCheckBox.DO_NOT_CARE) { |
||||
return; |
||||
} |
||||
boolean isSelected = chooseAllCheckBox.isSelected(); |
||||
for (int i = 0; i < dataList.getModel().getSize(); i++) { |
||||
UICheckBox checkBox = (UICheckBox) dataList.getModel().getElementAt(i); |
||||
checkBox.setSelected(isSelected); |
||||
} |
||||
dataList.repaint(); |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* 计算CheckBox的选中情况,用来更新全选框的状态 |
||||
* @return |
||||
*/ |
||||
protected int calculateSelectedNum() { |
||||
int count = 0; |
||||
for (UICheckBox dataCheckBox : dataCheckBoxes) { |
||||
if (dataCheckBox.isSelected()) { |
||||
count++; |
||||
} |
||||
} |
||||
return count; |
||||
} |
||||
|
||||
/** |
||||
* 将传入的Object转化为字符串展示,默认调用toString方法 |
||||
* @param object |
||||
* @return |
||||
*/ |
||||
protected String transferDataValue2Show(Object object) { |
||||
return object.toString(); |
||||
} |
||||
|
||||
protected AbstractListModel getListModel() { |
||||
return new SelectedListDataModel(); |
||||
} |
||||
|
||||
protected ListCellRenderer getListCellRenderer() { |
||||
return new SelectedListCellRender(); |
||||
} |
||||
|
||||
private class SelectedListCellRender extends DefaultListCellRenderer { |
||||
|
||||
@Override |
||||
public Component getListCellRendererComponent(JList list, Object value, int index, final boolean isSelected, boolean cellHasFocus) { |
||||
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
||||
dataCheckBoxes[index] = (UICheckBox) value; |
||||
dataCheckBoxes[index].setBackground(list.getBackground()); |
||||
return dataCheckBoxes[index]; |
||||
} |
||||
|
||||
} |
||||
|
||||
private class SelectedListDataModel extends AbstractListModel { |
||||
@Override |
||||
public int getSize() { |
||||
return dataCheckBoxes.length; |
||||
} |
||||
|
||||
@Override |
||||
public Object getElementAt(int index) { |
||||
return (index > getSize() - 1 || index < 0) ? null : dataCheckBoxes[index]; |
||||
} |
||||
} |
||||
} |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.0 KiB |
Loading…
Reference in new issue