From bff6f60d6522a2f3b9730aec5d30c1f50c2e9fbb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Tue, 4 Mar 2014 15:48:32 +0200 Subject: [PATCH] Support font icons in ComboBox (#13376) Change-Id: Idb35b8116c28824c8ced9b8f230e06ba8e7ecaaf --- .../com/vaadin/client/ui/VFilterSelect.java | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/client/src/com/vaadin/client/ui/VFilterSelect.java b/client/src/com/vaadin/client/ui/VFilterSelect.java index b97eff0fbd..e0ced98394 100644 --- a/client/src/com/vaadin/client/ui/VFilterSelect.java +++ b/client/src/com/vaadin/client/ui/VFilterSelect.java @@ -54,11 +54,11 @@ import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HTML; -import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.PopupPanel.PositionCallback; import com.google.gwt.user.client.ui.SuggestOracle.Suggestion; import com.google.gwt.user.client.ui.TextBox; +import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.ComponentConnector; @@ -122,10 +122,9 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, @Override public String getDisplayString() { final StringBuffer sb = new StringBuffer(); - if (iconUri != null) { - sb.append("\"\""); + final Icon icon = client.getIcon(iconUri); + if (icon != null) { + sb.append(icon.getElement().getString()); } String content; if ("".equals(caption)) { @@ -990,7 +989,13 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, } }; - private final Image selectedItemIcon = new Image(); + private class IconWidget extends Widget { + IconWidget(Icon icon) { + setElement(icon.getElement()); + } + } + + private IconWidget selectedItemIcon; /** For internal use only. May be removed or replaced in the future. */ public ApplicationConnection client; @@ -1113,21 +1118,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, tb = createTextBox(); suggestionPopup = createSuggestionPopup(); - selectedItemIcon.setStyleName("v-icon"); - selectedItemIcon.addLoadHandler(new LoadHandler() { - - @Override - public void onLoad(LoadEvent event) { - if (BrowserInfo.get().isIE8()) { - // IE8 needs some help to discover it should reposition the - // text field - forceReflow(); - } - updateRootWidth(); - updateSelectedIconPosition(); - } - }); - popupOpener.sinkEvents(Event.ONMOUSEDOWN); Roles.getButtonRole() .setAriaHiddenState(popupOpener.getElement(), true); @@ -1413,20 +1403,36 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, * The URI of the icon */ public void setSelectedItemIcon(String iconUri) { + if (iconUri == null || iconUri.length() == 0) { - if (selectedItemIcon.isAttached()) { + if (selectedItemIcon != null) { panel.remove(selectedItemIcon); - if (BrowserInfo.get().isIE8()) { - // IE8 needs some help to discover it should reposition the - // text field - forceReflow(); - } - updateRootWidth(); + selectedItemIcon = null; + afterSelectedItemIconChange(); } } else { + if (selectedItemIcon != null) { + panel.remove(selectedItemIcon); + } + selectedItemIcon = new IconWidget(client.getIcon(iconUri)); + selectedItemIcon.addDomHandler(new LoadHandler() { + @Override + public void onLoad(LoadEvent event) { + afterSelectedItemIconChange(); + } + }, LoadEvent.getType()); panel.insert(selectedItemIcon, 0); - selectedItemIcon.setUrl(iconUri); - updateRootWidth(); + afterSelectedItemIconChange(); + } + } + + private void afterSelectedItemIconChange() { + if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isIE8()) { + // Some browsers need a nudge to reposition the text field + forceReflow(); + } + updateRootWidth(); + if (selectedItemIcon != null) { updateSelectedIconPosition(); } } @@ -1951,8 +1957,9 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, * locked */ if (!tb.getElement().getStyle().getWidth().endsWith("px")) { - tb.setWidth((tb.getOffsetWidth() - selectedItemIcon - .getOffsetWidth()) + "px"); + int iconWidth = selectedItemIcon == null ? 0 : selectedItemIcon + .getOffsetWidth(); + tb.setWidth((tb.getOffsetWidth() - iconWidth) + "px"); } } } -- 2.39.5