Threaded View

  1. #1
    Sencha Premium Member
    Join Date
    Nov 2013
    Location
    Boston, MA
    Posts
    22
    Vote Rating
    0
    sbusch is on a distinguished road

      0  

    Default Answered: CheckBoxCell tooltip

    Answered: CheckBoxCell tooltip


    How do I generate a tooltip for a checkbox cell? Below does not work.

    Thanks
    Code:
    final CheckBoxCell checkboxCell = new CheckBoxCell() {
        @Override
        public boolean isDisabled() {
            return true;
        }
                
        @Override
        public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
            super.render(context, value, sb);
            sb.appendHtmlConstant("<span style='display:block' qwidth='400' qtip='"
                    + getFormattedToolTipValue(context.getIndex()) + "'></span>");
        }
    };

  2. We're using version 3.0.6 too.
    I'm very sorry.
    I copied some of our code without realizing that TooltipCell and TooltipValueProvider are classes of our own.

    Here's the code:
    TooltipCell.java
    Code:
    import <your path>.PlainStringTooltipValueProvider;
    import <your path>.TooltipValueProvider;
    import com.google.gwt.cell.client.AbstractCell;
    import com.google.gwt.cell.client.Cell;
    import com.google.gwt.cell.client.ValueUpdater;
    import com.google.gwt.dom.client.Element;
    import com.google.gwt.dom.client.NativeEvent;
    import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
    import com.google.gwt.safehtml.shared.SafeHtmlUtils;
    
    import java.util.Set;
    
    /**
     * This class wraps a cell into a cell that adds a QTip
     *
     * @param <S>
     */
    public class TooltipCell<S> implements Cell<S> {
    
        private final Cell<S> realCell;
        private final TooltipValueProvider<S> tooltipValueProvider;
    
        /**
         * Wraps an existing cell into a QuickTip rendering cell.
         *
         * @param realCell if null, a fallback is used, simple doing a toString() on the data.
         */
        public TooltipCell(Cell<S> realCell) {
            this(realCell, new PlainStringTooltipValueProvider<S>());
        }
    
        public TooltipCell(Cell<S> realCell, TooltipValueProvider<S> tooltipValueProvider) {
            if (realCell == null) {
                this.realCell = new NullCell<S>();
            } else {
                this.realCell = realCell;
            }
            this.tooltipValueProvider = tooltipValueProvider;
        }
    
        @Override
        public boolean dependsOnSelection() {
            return realCell.dependsOnSelection();
        }
    
        @Override
        public Set<String> getConsumedEvents() {
            return realCell.getConsumedEvents();
        }
    
        @Override
        public boolean handlesSelection() {
            return realCell.handlesSelection();
        }
    
        @Override
        public boolean isEditing(Context context, Element element, S s) {
            return realCell.isEditing(context, element, s);
        }
    
        @Override
        public void onBrowserEvent(Context context, Element element, S s, NativeEvent nativeEvent, ValueUpdater<S> sValueUpdater) {
            realCell.onBrowserEvent(context, element, s, nativeEvent, sValueUpdater);
        }
    
        @Override
        public void render(Context context, S s, SafeHtmlBuilder safeHtmlBuilder) {
            if (s != null) {
                String tooltip = SafeHtmlUtils.htmlEscape(tooltipValueProvider.get(context, s, realCell));
                safeHtmlBuilder.appendHtmlConstant("<span qtip='" + tooltip + "'>");
            }
            realCell.render(context, s, safeHtmlBuilder);
            if (s != null) {
                safeHtmlBuilder.appendHtmlConstant("</span>");
            }
        }
    
        @Override
        public boolean resetFocus(Context context, Element element, S s) {
            return realCell.resetFocus(context, element, s);
        }
    
        @Override
        public void setValue(Context context, Element element, S s) {
            realCell.setValue(context, element, s);
        }
    
        private class NullCell<S> extends AbstractCell<S> {
    
            @Override
            public void render(Context context, S s, SafeHtmlBuilder safeHtmlBuilder) {
                if (s != null) {
                    safeHtmlBuilder.appendEscaped("" + s);
                } else {
                    safeHtmlBuilder.appendEscaped("");
                }
            }
    
        }
    
    }
    TooltipValueProvider is just an interface:
    Code:
    import com.google.gwt.cell.client.Cell;
    
    /**
     * Renders the value S into a String by using the Cell
     */
    public interface TooltipValueProvider<S> {
    
        String get(Cell.Context context, S s, Cell<S> cell);
    }
    PlainStringTooltipValueProvider.java:

    Code:
    import com.google.gwt.cell.client.Cell;
    import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
    
    /**
     * Makes a plain string out of an Object S by striping all HTML tags once the Cell has rendered the Object S
     */
    
    public class PlainStringTooltipValueProvider<S> implements TooltipValueProvider<S> {
    
        @Override
        public String get(Cell.Context context, S s, Cell<S> cell) {
            SafeHtmlBuilder builder = new SafeHtmlBuilder();
            cell.render(context, s, builder);
            return builder.toSafeHtml().asString().replaceAll("\\<.*?>", "");
        }
    }
    Given this classes and interface, you can write the code I posted earlier.
    I hope this makes more sense and helps you.

Thread Participants: 1