richie
5 years ago
2 changed files with 263 additions and 0 deletions
@ -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…
Reference in new issue