From e1f8e2d81156ba80fb183fcf4f7828524ea5a5f1 Mon Sep 17 00:00:00 2001 From: richie Date: Wed, 28 Aug 2019 21:42:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E6=8B=89=E6=A1=86=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/design/ui/component/UIComboBox.java | 72 +++++++ .../design/ui/component/UILazyComboBox.java | 191 ++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100644 src/main/java/com/fanruan/api/design/ui/component/UILazyComboBox.java diff --git a/src/main/java/com/fanruan/api/design/ui/component/UIComboBox.java b/src/main/java/com/fanruan/api/design/ui/component/UIComboBox.java index 1a53d68..0210e71 100644 --- a/src/main/java/com/fanruan/api/design/ui/component/UIComboBox.java +++ b/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 extends com.fr.design.gui.icombobox.UIComboBox { /** * 重新设置下拉选项 + * * @param list 下拉选项的集合 */ public void refreshSelectableItems(List list) { @@ -40,4 +50,66 @@ public class UIComboBox 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); + } + } } diff --git a/src/main/java/com/fanruan/api/design/ui/component/UILazyComboBox.java b/src/main/java/com/fanruan/api/design/ui/component/UILazyComboBox.java new file mode 100644 index 0000000..4e5e7d0 --- /dev/null +++ b/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 extends UIComboBox 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 loadingModel = new DefaultComboBoxModel<>(PENDING_CONTENT); + this.setModel(loadingModel); + new SwingWorker() { + + @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 model = new DefaultComboBoxModel<>(load()); + model.setSelectedItem(initialSelected); + this.setModel(model); + this.selectedItemReminder = initialSelected; + loaded = true; + } + + /** + * 加载下拉列表 + * + * @param contents 下拉列表内容 + */ + private void loadList(Object[] contents) { + DefaultComboBoxModel 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; + } + } +}