Browse Source

下拉框控件

pull/11/head
richie 5 years ago
parent
commit
e1f8e2d811
  1. 72
      src/main/java/com/fanruan/api/design/ui/component/UIComboBox.java
  2. 191
      src/main/java/com/fanruan/api/design/ui/component/UILazyComboBox.java

72
src/main/java/com/fanruan/api/design/ui/component/UIComboBox.java

@ -1,6 +1,15 @@
package com.fanruan.api.design.ui.component;
import com.fanruan.api.util.AssistKit;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.stable.Constants;
import com.fr.stable.StringUtils;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import java.awt.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;
@ -30,6 +39,7 @@ public class UIComboBox<T> extends com.fr.design.gui.icombobox.UIComboBox {
/**
* 重新设置下拉选项
*
* @param list 下拉选项的集合
*/
public void refreshSelectableItems(List<T> list) {
@ -40,4 +50,66 @@ public class UIComboBox<T> extends com.fr.design.gui.icombobox.UIComboBox {
}
getModel().setSelectedItem(el);
}
public static class UIComboBoxEditor extends BasicComboBoxEditor {
protected UITextField textField;
private Object oldValue;
public UIComboBoxEditor() {
textField = new UITextField();
textField.setRectDirection(Constants.RIGHT);
}
@Override
public Component getEditorComponent() {
return textField;
}
@Override
public void setItem(Object anObject) {
if (anObject != null) {
textField.setText(anObject.toString());
oldValue = anObject;
} else {
textField.setText(StringUtils.EMPTY);
}
}
@Override
public Object getItem() {
Object newValue = textField.getText();
if (oldValue != null && !(oldValue instanceof String)) {
if (AssistKit.equals(newValue, oldValue.toString())) {
return oldValue;
} else {
Class cls = oldValue.getClass();
try {
Method method = cls.getMethod("valueOf", new Class[]{String.class});
newValue = method.invoke(oldValue, new Object[]{textField.getText()});
} catch (Exception ignore) {
}
}
}
return newValue;
}
@Override
public void selectAll() {
textField.selectAll();
textField.requestFocus();
}
@Override
public void addActionListener(ActionListener l) {
textField.addActionListener(l);
}
@Override
public void removeActionListener(ActionListener l) {
textField.removeActionListener(l);
}
}
}

191
src/main/java/com/fanruan/api/design/ui/component/UILazyComboBox.java

@ -0,0 +1,191 @@
package com.fanruan.api.design.ui.component;
import com.fanruan.api.design.DesignKit;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.util.concurrent.ExecutionException;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-28
* 懒加载下拉框点击下拉之后才开始加载数据
*/
public abstract class UILazyComboBox<T> extends UIComboBox<T> implements PopupMenuListener {
private static final int NUM = 80;
private static final String[] PENDING_CONTENT = new String[]{StringKit.EMPTY, DesignKit.i18nText("Fine-Design_Basic_Loading") + "..."};
/**
* 是否加载完成
*/
protected boolean loaded = false;
/**
* 初始化选项
*/
private Object initialSelected = null;
protected UILazyComboBox() {
super();
this.setEditor(new UILazyComboBox.FilterComboBoxEditor());
addPopupMenuListener(this);
}
public void setLoaded(boolean loaded) {
this.loaded = loaded;
}
/**
* 加载下拉框中的选项
*
* @return 下拉框中的选项
*/
public abstract Object[] load();
@Override
public void setSelectedItem(Object anObject) {
initialSelected = anObject;
if (loaded) {
super.setSelectedItem(anObject);
} else {
this.setModel(new DefaultComboBoxModel<>(new Object[]{anObject}));
super.setSelectedItem(anObject);
}
}
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
if (loaded) {
return;
}
DefaultComboBoxModel<String> loadingModel = new DefaultComboBoxModel<>(PENDING_CONTENT);
this.setModel(loadingModel);
new SwingWorker<Object[], Void>() {
@Override
protected Object[] doInBackground() {
return load();
}
@Override
public void done() {
try {
UILazyComboBox.this.loadList(get());
} catch (InterruptedException | ExecutionException exception) {
LogKit.debug(exception.getMessage());
}
UILazyComboBox.this.showPopup();
}
}.execute();
}
/**
* 加载下拉列表
*/
public void loadList() {
DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<>(load());
model.setSelectedItem(initialSelected);
this.setModel(model);
this.selectedItemReminder = initialSelected;
loaded = true;
}
/**
* 加载下拉列表
*
* @param contents 下拉列表内容
*/
private void loadList(Object[] contents) {
DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<>(contents);
model.setSelectedItem(initialSelected);
this.setModel(model);
this.selectedItemReminder = initialSelected;
loaded = true;
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
@Override
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
dim.width = NUM;
return dim;
}
class FilterComboBoxEditor extends UIComboBoxEditor implements DocumentListener {
private Object item;
private volatile boolean filtering = false;
private volatile boolean setting = false;
public FilterComboBoxEditor() {
super();
textField.getDocument().addDocumentListener(this);
}
@Override
public void setItem(Object item) {
if (filtering) {
return;
}
this.item = item;
this.setting = true;
textField.setSetting(true);
String newText = (item == null) ? "" : item.toString();
textField.setText(newText);
textField.setSetting(false);
this.setting = false;
}
@Override
public Object getItem() {
return this.item;
}
@Override
public void insertUpdate(DocumentEvent e) {
handleChange();
}
@Override
public void removeUpdate(DocumentEvent e) {
handleChange();
}
@Override
public void changedUpdate(DocumentEvent e) {
handleChange();
}
void handleChange() {
if (setting) {
return;
}
filtering = true;
String xx = textField.getText();
UILazyComboBox.this.setSelectedItem(xx);
this.item = textField.getText();
setPopupVisible(true);
filtering = false;
}
}
}
Loading…
Cancel
Save