|
|
@ -2,19 +2,20 @@ package com.fr.design.gui.icombocheckbox; |
|
|
|
|
|
|
|
|
|
|
|
import com.fine.theme.icon.LazyIcon; |
|
|
|
import com.fine.theme.icon.LazyIcon; |
|
|
|
import com.fine.theme.light.ui.FineRoundBorder; |
|
|
|
import com.fine.theme.light.ui.FineRoundBorder; |
|
|
|
import com.fine.theme.utils.FineUIScale; |
|
|
|
import com.fine.theme.utils.FineUIUtils; |
|
|
|
|
|
|
|
import com.formdev.flatlaf.util.ScaledEmptyBorder; |
|
|
|
import com.fr.design.event.GlobalNameListener; |
|
|
|
import com.fr.design.event.GlobalNameListener; |
|
|
|
import com.fr.design.event.GlobalNameObserver; |
|
|
|
import com.fr.design.event.GlobalNameObserver; |
|
|
|
import com.fr.design.event.HoverAware; |
|
|
|
import com.fr.design.event.HoverAware; |
|
|
|
import com.fr.design.event.UIObserver; |
|
|
|
import com.fr.design.event.UIObserver; |
|
|
|
import com.fr.design.event.UIObserverListener; |
|
|
|
import com.fr.design.event.UIObserverListener; |
|
|
|
import com.fr.design.gui.ibutton.UIButton; |
|
|
|
import com.fr.design.gui.ibutton.UIButton; |
|
|
|
import com.fr.design.gui.ilable.UILabel; |
|
|
|
|
|
|
|
import com.fr.design.gui.itextfield.UITextField; |
|
|
|
import com.fr.design.gui.itextfield.UITextField; |
|
|
|
import com.fr.design.layout.FRGUIPaneFactory; |
|
|
|
import com.fr.design.layout.FRGUIPaneFactory; |
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.Icon; |
|
|
|
import javax.swing.Icon; |
|
|
|
|
|
|
|
import javax.swing.JComboBox; |
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JFrame; |
|
|
|
import javax.swing.JFrame; |
|
|
|
import javax.swing.JPanel; |
|
|
|
import javax.swing.JPanel; |
|
|
@ -36,6 +37,11 @@ import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static com.fine.theme.utils.FineUIStyle.OFFET_LEFT_TEXT_FIELD; |
|
|
|
|
|
|
|
import static com.fine.theme.utils.FineUIStyle.PLAIN_ARROW_BUTTON; |
|
|
|
|
|
|
|
import static com.fine.theme.utils.FineUIStyle.TRANSPARENT_TEXT_FIELD; |
|
|
|
|
|
|
|
import static com.fine.theme.utils.FineUIStyle.setStyle; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 设计器下拉复选框组件 |
|
|
|
* 设计器下拉复选框组件 |
|
|
|
* 支持全选、半选 |
|
|
|
* 支持全选、半选 |
|
|
@ -43,10 +49,10 @@ import java.util.Map; |
|
|
|
* 可以省略显示 |
|
|
|
* 可以省略显示 |
|
|
|
* |
|
|
|
* |
|
|
|
* @author |
|
|
|
* @author |
|
|
|
* @since |
|
|
|
* @since Created on |
|
|
|
* Created on |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNameObserver, HoverAware { |
|
|
|
public class UIComboCheckBox extends JComboBox implements UIObserver, GlobalNameObserver, HoverAware { |
|
|
|
|
|
|
|
private static final String UI_CLASS_ID = "ComboCheckBoxUI"; |
|
|
|
//下拉框的值
|
|
|
|
//下拉框的值
|
|
|
|
private Object[] values; |
|
|
|
private Object[] values; |
|
|
|
//已经选中的值
|
|
|
|
//已经选中的值
|
|
|
@ -55,8 +61,8 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
private List<ActionListener> listeners = new ArrayList<ActionListener>(); |
|
|
|
private List<ActionListener> listeners = new ArrayList<ActionListener>(); |
|
|
|
private UICheckListPopup popup; |
|
|
|
private UICheckListPopup popup; |
|
|
|
private UITextField editor; |
|
|
|
private UITextField editor; |
|
|
|
|
|
|
|
private final JPanel clickPane = new JPanel(new BorderLayout()); |
|
|
|
private UIButton arrowButton; |
|
|
|
private UIButton arrowButton; |
|
|
|
private UILabel label; |
|
|
|
|
|
|
|
//选中的值之间显示的分隔符
|
|
|
|
//选中的值之间显示的分隔符
|
|
|
|
private String valueSperator; |
|
|
|
private String valueSperator; |
|
|
|
private static final String DEFAULT_VALUE_SPERATOR = ","; |
|
|
|
private static final String DEFAULT_VALUE_SPERATOR = ","; |
|
|
@ -71,6 +77,7 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
private String placeHolder = StringUtils.EMPTY; |
|
|
|
private String placeHolder = StringUtils.EMPTY; |
|
|
|
|
|
|
|
|
|
|
|
private boolean rollOver; |
|
|
|
private boolean rollOver; |
|
|
|
|
|
|
|
private boolean popupVisible = false; |
|
|
|
|
|
|
|
|
|
|
|
public UIComboCheckBox(Object[] value) { |
|
|
|
public UIComboCheckBox(Object[] value) { |
|
|
|
this(value, DEFAULT_VALUE_SPERATOR, true); |
|
|
|
this(value, DEFAULT_VALUE_SPERATOR, true); |
|
|
@ -123,19 +130,22 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initComponent() { |
|
|
|
private void initComponent() { |
|
|
|
this.popup = new UICheckListPopup(values, supportSelectAll); |
|
|
|
this.popup = new UICheckListPopup(values, supportSelectAll) { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Dimension getPreferredSize() { |
|
|
|
|
|
|
|
Dimension size = super.getPreferredSize(); |
|
|
|
|
|
|
|
size.width = UIComboCheckBox.this.getWidth(); |
|
|
|
|
|
|
|
return size; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
this.popup.setBorder(new FineRoundBorder()); |
|
|
|
this.popup.addActionListener(new PopupAction()); |
|
|
|
this.popup.addActionListener(new PopupAction()); |
|
|
|
this.editor = createEditor(); |
|
|
|
initClickPane(); |
|
|
|
this.arrowButton = createArrowButton(); |
|
|
|
|
|
|
|
this.label = new UILabel(getIcon()); |
|
|
|
|
|
|
|
label.setPreferredSize(FineUIScale.scale(new Dimension(24, 24))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setLayoutAndAddComponents(); |
|
|
|
setLayoutAndAddComponents(); |
|
|
|
setText(); |
|
|
|
setText(); |
|
|
|
|
|
|
|
|
|
|
|
addPopupListener(this); |
|
|
|
addPopupListener(this); |
|
|
|
setBackground(Color.WHITE); |
|
|
|
|
|
|
|
setOpaque(true); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -152,8 +162,8 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
return popup; |
|
|
|
return popup; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public UITextField getEditor() { |
|
|
|
public JPanel getClickPane() { |
|
|
|
return editor; |
|
|
|
return clickPane; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getPlaceHolder() { |
|
|
|
public String getPlaceHolder() { |
|
|
@ -164,31 +174,37 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
this.placeHolder = placeHolder; |
|
|
|
this.placeHolder = placeHolder; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public UIButton getArrowButton() { |
|
|
|
private void initClickPane() { |
|
|
|
return arrowButton; |
|
|
|
editor = new TextField(); |
|
|
|
} |
|
|
|
editor.setEditable(false); |
|
|
|
|
|
|
|
setStyle(editor, OFFET_LEFT_TEXT_FIELD); |
|
|
|
|
|
|
|
|
|
|
|
public UILabel getLabel() { |
|
|
|
arrowButton = new UIButton(getIcon()); |
|
|
|
return label; |
|
|
|
setStyle(arrowButton, PLAIN_ARROW_BUTTON); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private UIButton createArrowButton() { |
|
|
|
clickPane.add(editor, BorderLayout.CENTER); |
|
|
|
final UIButton arrowBtn = new UIButton(); |
|
|
|
clickPane.add(arrowButton, BorderLayout.EAST); |
|
|
|
arrowBtn.setNormalPainted(false); |
|
|
|
|
|
|
|
arrowBtn.setPreferredSize(FineUIScale.scale(new Dimension(20, 5))); |
|
|
|
|
|
|
|
arrowBtn.setIcon(getIcon()); |
|
|
|
|
|
|
|
arrowBtn.setExtraPainted(false); |
|
|
|
|
|
|
|
addPopupListener(arrowBtn); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return arrowBtn; |
|
|
|
addPopupListener(editor); |
|
|
|
|
|
|
|
addPopupListener(arrowButton); |
|
|
|
|
|
|
|
addHoverStatusListener(editor); |
|
|
|
|
|
|
|
addHoverStatusListener(arrowButton); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private UITextField createEditor() { |
|
|
|
protected void addHoverStatusListener(JComponent component) { |
|
|
|
UITextField editor = new TextField(); |
|
|
|
component.addMouseListener(new MouseAdapter() { |
|
|
|
editor.setEditable(false); |
|
|
|
@Override |
|
|
|
addPopupListener(editor); |
|
|
|
public void mouseEntered(MouseEvent e) { |
|
|
|
|
|
|
|
rollOver = true; |
|
|
|
|
|
|
|
repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return editor; |
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseExited(MouseEvent e) { |
|
|
|
|
|
|
|
rollOver = popup.isVisible(); |
|
|
|
|
|
|
|
repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -202,18 +218,6 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
public void mouseClicked(MouseEvent e) { |
|
|
|
public void mouseClicked(MouseEvent e) { |
|
|
|
togglePopup(); |
|
|
|
togglePopup(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseEntered(MouseEvent e) { |
|
|
|
|
|
|
|
rollOver = true; |
|
|
|
|
|
|
|
repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseExited(MouseEvent e) { |
|
|
|
|
|
|
|
rollOver = false; |
|
|
|
|
|
|
|
repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -240,7 +244,7 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean isHovered() { |
|
|
|
public boolean isHovered() { |
|
|
|
return rollOver || this.popup.isShowing(); |
|
|
|
return rollOver; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class PopupAction implements ActionListener { |
|
|
|
private class PopupAction implements ActionListener { |
|
|
@ -258,12 +262,34 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
|
|
|
|
|
|
|
|
private void togglePopup() { |
|
|
|
private void togglePopup() { |
|
|
|
if (this.arrowButton.isEnabled()) { |
|
|
|
if (this.arrowButton.isEnabled()) { |
|
|
|
popup.setBorder(new FineRoundBorder()); |
|
|
|
if (popupVisible) { |
|
|
|
popup.setPreferredSize(new Dimension(getWidth(), this.popup.getPreferredSize().height)); |
|
|
|
hidePopup(); |
|
|
|
popup.show(this, 0, getHeight()); |
|
|
|
} else { |
|
|
|
|
|
|
|
showPopup(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 展示 popup |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void showPopup() { |
|
|
|
|
|
|
|
this.popup.show(this, 0, getHeight()); |
|
|
|
|
|
|
|
arrowButton.setIcon(new LazyIcon("up_arrow")); |
|
|
|
|
|
|
|
arrowButton.repaint(); |
|
|
|
|
|
|
|
popupVisible = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 隐藏 popup |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void hidePopup() { |
|
|
|
|
|
|
|
popup.setVisible(false); |
|
|
|
|
|
|
|
arrowButton.setIcon(new LazyIcon("down_arrow")); |
|
|
|
|
|
|
|
arrowButton.repaint(); |
|
|
|
|
|
|
|
popupVisible = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 清除文本框 |
|
|
|
* 清除文本框 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -301,6 +327,7 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为为添加placeholder |
|
|
|
* 为为添加placeholder |
|
|
|
|
|
|
|
* |
|
|
|
* @param editor |
|
|
|
* @param editor |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void setEditorPlaceHolder(UITextField editor) { |
|
|
|
protected void setEditorPlaceHolder(UITextField editor) { |
|
|
@ -335,7 +362,7 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= chars.length; i++) { |
|
|
|
for (int i = 1; i <= chars.length; i++) { |
|
|
|
//如果原文本+省略号长度超过文本框
|
|
|
|
//如果原文本+省略号长度超过文本框
|
|
|
|
if (fontMetrics.charsWidth(chars, 0, i) + omitLength > textEditor.getPreferredSize().getWidth()) { |
|
|
|
if (fontMetrics.charsWidth(chars, 0, i) + omitLength > textEditor.getWidth()) { |
|
|
|
//从第i-1的位置截断再拼上省略号
|
|
|
|
//从第i-1的位置截断再拼上省略号
|
|
|
|
omitText = text.substring(0, i - 2) + OMIT_TEXT; |
|
|
|
omitText = text.substring(0, i - 2) + OMIT_TEXT; |
|
|
|
break; |
|
|
|
break; |
|
|
@ -466,8 +493,14 @@ public class UIComboCheckBox extends JComponent implements UIObserver, GlobalNam |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String getUIClassID() { |
|
|
|
|
|
|
|
return UI_CLASS_ID; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 简单的测试demo |
|
|
|
* 简单的测试demo |
|
|
|
|
|
|
|
* |
|
|
|
* @param args |
|
|
|
* @param args |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void main(String args[]) { |
|
|
|
public static void main(String args[]) { |
|
|
|