插件开发工具库,推荐依赖该工具库。
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.
 
 

122 lines
3.5 KiB

package com.fanruan.api.design.work.component;
import com.fanruan.api.design.ui.component.UIButton;
import com.fanruan.api.design.ui.component.UIComboBox;
import com.fanruan.api.util.IOKit;
import com.fanruan.api.util.StringKit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author richie
* @version 10.0
* Created by richie on 2021/3/18
*/
public abstract class ItemEditableComboBoxPanel extends JPanel {
private static final long serialVersionUID = 1L;
protected static final Object EMPTY = new Object() {
public String toString() {
return StringKit.EMPTY;
}
};
protected UIComboBox<?> itemComboBox;
protected UIButton editButton;
protected UIButton refreshButton;
public ItemEditableComboBoxPanel() {
super();
initComponents();
}
protected void initComponents() {
this.setLayout(new BorderLayout(4, 4));
Dimension buttonSize = new Dimension(26, 20);
itemComboBox = new UIComboBox<>();
itemComboBox.setEnabled(true);
this.add(itemComboBox, BorderLayout.CENTER);
refreshButton = new UIButton(IOKit.readIcon("/com/fr/design/images/control/refresh.png"));
JPanel jPanel = new JPanel(new GridLayout(0, 2, 4, 4));
editButton = initEditButton(editButton, buttonSize);
jPanel.add(editButton);
jPanel.add(refreshButton);
this.add(jPanel, BorderLayout.EAST);
refreshButton.setPreferredSize(buttonSize);
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
refreshItems();
}
});
}
protected UIButton initEditButton(UIButton editButton, Dimension buttonSize) {
editButton = new UIButton(IOKit.readIcon("/com/fr/design/images/control/control-center2.png"));
editButton.setPreferredSize(buttonSize);
editButton.addActionListener(evt -> editItems());
return editButton;
}
/**
* 给itemComboBox添加ActionListener
*/
public void addComboBoxActionListener(ActionListener l) {
itemComboBox.addActionListener(l);
}
protected void refreshItems() {
// 记录原来选中的Item,重新加载后需要再次选中
Object lastSelectedItem = itemComboBox.getSelectedItem();
DefaultComboBoxModel<Object> model = ((DefaultComboBoxModel<Object>) itemComboBox.getModel());
model.removeAllElements();
// 先加EMPTY,再加items
model.addElement(EMPTY);
java.util.Iterator<String> itemIt = items();
while (itemIt.hasNext()) {
model.addElement(itemIt.next());
}
// 再次选中之前选中的Item
int idx = model.getIndexOf(lastSelectedItem);
if (idx < 0) {
idx = 0;
}
itemComboBox.setSelectedIndex(idx);
}
/*
* 得到其中的itemComboBox所选中的Item
*/
public String getSelectedItem() {
Object selected = itemComboBox.getSelectedItem();
return selected instanceof String ? (String) selected : null;
}
/*
* 选中name项
*/
public void setSelectedItem(String name) {
DefaultComboBoxModel model = ((DefaultComboBoxModel) itemComboBox.getModel());
model.setSelectedItem(name);
}
/*
* 刷新ComboBox.items
*/
protected abstract java.util.Iterator<String> items();
/*
* 弹出对话框编辑Items
*/
protected abstract void editItems();
}