mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-05 04:38:10 +08:00
PL-8363 Support IconProvider for LookupField
This commit is contained in:
parent
86a92bd90e
commit
9b1e13159e
@ -87,6 +87,9 @@ public class DesktopLookupField extends DesktopAbstractOptionsField<JComponent>
|
||||
|
||||
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
|
||||
|
||||
// just stub
|
||||
protected OptionIconProvider optionIconProvider;
|
||||
|
||||
public DesktopLookupField() {
|
||||
composition = new JPanel();
|
||||
composition.setLayout(new BorderLayout());
|
||||
@ -434,6 +437,19 @@ public class DesktopLookupField extends DesktopAbstractOptionsField<JComponent>
|
||||
return nullOptionVisible;
|
||||
}
|
||||
|
||||
public void setOptionIconProvider(OptionIconProvider<?> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setOptionIconProvider(Class<T> optionClass, OptionIconProvider<T> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
public OptionIconProvider<?> getOptionIconProvider() {
|
||||
return optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputPrompt() {
|
||||
return inputPrompt;
|
||||
|
@ -97,6 +97,9 @@ public class DesktopSearchField extends DesktopAbstractOptionsField<JComponent>
|
||||
};
|
||||
protected String inputPrompt;
|
||||
|
||||
// just stub
|
||||
protected OptionIconProvider optionIconProvider;
|
||||
|
||||
public DesktopSearchField() {
|
||||
composition = new JPanel();
|
||||
composition.setLayout(new BorderLayout());
|
||||
@ -462,6 +465,19 @@ public class DesktopSearchField extends DesktopAbstractOptionsField<JComponent>
|
||||
return nullOptionVisible;
|
||||
}
|
||||
|
||||
public void setOptionIconProvider(OptionIconProvider<?> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setOptionIconProvider(Class<T> optionClass, OptionIconProvider<T> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
public OptionIconProvider<?> getOptionIconProvider() {
|
||||
return optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputPrompt() {
|
||||
return inputPrompt;
|
||||
|
@ -93,6 +93,9 @@ public class DesktopSuggestionField extends DesktopAbstractOptionsField<JCompone
|
||||
protected boolean nullOptionVisible = true;
|
||||
private ArrowDownActionHandler arrowDownActionHandler;
|
||||
|
||||
// just stub
|
||||
protected OptionIconProvider optionIconProvider;
|
||||
|
||||
public DesktopSuggestionField() {
|
||||
composition = new JPanel();
|
||||
composition.setLayout(new BorderLayout());
|
||||
@ -543,6 +546,19 @@ public class DesktopSuggestionField extends DesktopAbstractOptionsField<JCompone
|
||||
return nullOptionVisible;
|
||||
}
|
||||
|
||||
public void setOptionIconProvider(OptionIconProvider<?> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setOptionIconProvider(Class<T> optionClass, OptionIconProvider<T> optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
}
|
||||
|
||||
public OptionIconProvider<?> getOptionIconProvider() {
|
||||
return optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputPrompt() {
|
||||
return inputPrompt;
|
||||
|
@ -79,6 +79,26 @@ public interface LookupField extends OptionsField, Component.HasInputPrompt {
|
||||
*/
|
||||
boolean isNullOptionVisible();
|
||||
|
||||
/**
|
||||
* Set the icon provider for the LookupField.
|
||||
*
|
||||
* @param optionIconProvider provider which provides icons for options
|
||||
*/
|
||||
void setOptionIconProvider(OptionIconProvider<?> optionIconProvider);
|
||||
|
||||
/**
|
||||
* Set the icon provider for LookupField.
|
||||
*
|
||||
* @param optionClass class of the option
|
||||
* @param optionIconProvider provider which provides icons for options
|
||||
*/
|
||||
<T> void setOptionIconProvider(Class<T> optionClass, OptionIconProvider<T> optionIconProvider);
|
||||
|
||||
/**
|
||||
* @return icon provider of the LookupField.
|
||||
*/
|
||||
OptionIconProvider<?> getOptionIconProvider();
|
||||
|
||||
enum FilterMode {
|
||||
NO,
|
||||
STARTS_WITH,
|
||||
@ -95,4 +115,18 @@ public interface LookupField extends OptionsField, Component.HasInputPrompt {
|
||||
*/
|
||||
void addNewOption(String caption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to set icons for particular elements in the options list.
|
||||
*/
|
||||
interface OptionIconProvider<T> {
|
||||
|
||||
/**
|
||||
* Called when component paints its content.
|
||||
*
|
||||
* @param item item from options list, options map or enum options
|
||||
* @return icon name or null to show no icon
|
||||
*/
|
||||
String getItemIcon(T item);
|
||||
}
|
||||
}
|
@ -41,6 +41,7 @@ import com.vaadin.event.ShortcutListener;
|
||||
import com.vaadin.server.ErrorMessage;
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
@ -64,6 +65,7 @@ public class WebLookupField extends WebAbstractOptionsField<CubaComboBox> implem
|
||||
protected Messages messages = AppBeans.get(Messages.NAME);
|
||||
|
||||
protected boolean nullOptionVisible = true;
|
||||
protected OptionIconProvider optionIconProvider;
|
||||
|
||||
public WebLookupField() {
|
||||
createComponent();
|
||||
@ -407,6 +409,42 @@ public class WebLookupField extends WebAbstractOptionsField<CubaComboBox> implem
|
||||
return nullOptionVisible;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setOptionIconProvider(OptionIconProvider<?> optionIconProvider) {
|
||||
setOptionIconProvider(Object.class, (OptionIconProvider) optionIconProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setOptionIconProvider(Class<T> optionClass, OptionIconProvider<T> optionIconProvider) {
|
||||
if (this.optionIconProvider != optionIconProvider) {
|
||||
// noinspection unchecked
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
|
||||
if (optionIconProvider == null) {
|
||||
component.setOptionIconProvider(null);
|
||||
} else {
|
||||
component.setOptionIconProvider(itemId -> {
|
||||
T typedItem = optionClass.cast(itemId);
|
||||
|
||||
String resourceId;
|
||||
try {
|
||||
// noinspection unchecked
|
||||
resourceId = optionIconProvider.getItemIcon(typedItem);
|
||||
} catch (Exception e) {
|
||||
LoggerFactory.getLogger(WebLookupField.class).warn("Error invoking OptionIconProvider getItemIcon method", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return WebComponentsHelper.getIcon(resourceId);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public OptionIconProvider<?> getOptionIconProvider() {
|
||||
return optionIconProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputPrompt() {
|
||||
return component.getInputPrompt();
|
||||
|
@ -34,6 +34,7 @@ public class CubaComboBox extends ComboBox implements Action.Container {
|
||||
* painting and handling as well.
|
||||
*/
|
||||
protected ActionManager shortcutsManager;
|
||||
protected OptionIconProvider optionIconProvider;
|
||||
|
||||
public CubaComboBox() {
|
||||
setValidationVisible(false);
|
||||
@ -122,6 +123,29 @@ public class CubaComboBox extends ComboBox implements Action.Container {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource getItemIcon(Object itemId) {
|
||||
if (optionIconProvider != null) {
|
||||
Resource itemIcon = optionIconProvider.getItemIcon(itemId);
|
||||
if (itemIcon != null) {
|
||||
return itemIcon;
|
||||
}
|
||||
}
|
||||
|
||||
return super.getItemIcon(itemId);
|
||||
}
|
||||
|
||||
public OptionIconProvider getOptionIconProvider() {
|
||||
return optionIconProvider;
|
||||
}
|
||||
|
||||
public void setOptionIconProvider(OptionIconProvider optionIconProvider) {
|
||||
if (this.optionIconProvider != optionIconProvider) {
|
||||
this.optionIconProvider = optionIconProvider;
|
||||
markAsDirty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ActionManager getActionManager() {
|
||||
if (shortcutsManager == null) {
|
||||
@ -159,4 +183,9 @@ public class CubaComboBox extends ComboBox implements Action.Container {
|
||||
public void removeActionHandler(Action.Handler actionHandler) {
|
||||
getActionManager().removeActionHandler(actionHandler);
|
||||
}
|
||||
|
||||
public interface OptionIconProvider {
|
||||
|
||||
Resource getItemIcon(Object item);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user