<commit_before>/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.terminal.gwt.client.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
import com.itmill.toolkit.terminal.gwt.client.Focusable;
import com.itmill.toolkit.terminal.gwt.client.Paintable;
import com.itmill.toolkit.terminal.gwt.client.UIDL;
import com.itmill.toolkit.terminal.gwt.client.Util;

/**
 * 
 * TODO needs major refactoring (to be extensible etc)
 */
public class IFilterSelect extends Composite implements Paintable, Field,
        KeyboardListener, ClickListener, FocusListener, Focusable {

    public class FilterSelectSuggestion implements Suggestion, Command {

        private final String key;
        private final String caption;
        private String iconUri;

        public FilterSelectSuggestion(UIDL uidl) {
            key = uidl.getStringAttribute("key");
            caption = uidl.getStringAttribute("caption");
            if (uidl.hasAttribute("icon")) {
                iconUri = client.translateToolkitUri(uidl
                        .getStringAttribute("icon"));
            }
        }

        public String getDisplayString() {
            final StringBuffer sb = new StringBuffer();
            if (iconUri != null) {
                sb.append("<img src=\"");
                sb.append(iconUri);
                sb.append("\" alt=\"icon\" class=\"i-icon\" />");
            }
            sb.append(Util.escapeHTML(caption));
            return sb.toString();
        }

        public String getReplacementString() {
            return caption;
        }

        public int getOptionKey() {
            return Integer.parseInt(key);
        }

        public String getIconUri() {
            return iconUri;
        }

        public void execute() {
            onSuggestionSelected(this);
        }
    }

    /**
     * @author mattitahvonen
     * 
     */
    public class SuggestionPopup extends ToolkitOverlay implements
            PositionCallback, PopupListener {
        private static final int EXTRASPACE = 8;

        private static final String Z_INDEX = "30000";

        private final SuggestionMenu menu;

        private final Element up = DOM.createDiv();
        private final Element down = DOM.createDiv();
        private final Element status = DOM.createDiv();

        private boolean isPagingEnabled = true;

        private long lastAutoClosed;

        SuggestionPopup() {
            super(true);
            menu = new SuggestionMenu();
            setWidget(menu);
            setStyleName(CLASSNAME + "-suggestpopup");
            DOM.setStyleAttribute(getElement(), "zIndex", Z_INDEX);

            final Element root = getContainerElement();

            DOM.setInnerHTML(up, "<span>Prev</span>");
            DOM.sinkEvents(up, Event.ONCLICK);
            DOM.setInnerHTML(down, "<span>Next</span>");
            DOM.sinkEvents(down, Event.ONCLICK);
            DOM.insertChild(root, up, 0);
            DOM.appendChild(root, down);
            DOM.appendChild(root, status);
            DOM.setElementProperty(status, "className", CLASSNAME + "-status");

            addPopupListener(this);
        }

        public void showSuggestions(Collection currentSuggestions,
                int currentPage, int totalSuggestions) {

            if (ApplicationConnection.isTestingMode()) {
                // Add TT anchor point
                DOM.setElementProperty(getElement(), "id", paintableId
                        + "_OPTIONLIST");
            }

            menu.setSuggestions(currentSuggestions);
            final int x = IFilterSelect.this.getAbsoluteLeft();
            int y = tb.getAbsoluteTop();
            y += tb.getOffsetHeight();
            setPopupPosition(x, y);
            final int first = currentPage * PAGELENTH
                    + (nullSelectionAllowed && currentPage > 0 ? 0 : 1);
            final int last = first + currentSuggestions.size() - 1;
            final int matches = totalSuggestions
                    - (nullSelectionAllowed ? 1 : 0);
            if (last > 0) {
                // nullsel not counted, as requested by user
                DOM.setInnerText(status, (totalSuggestions == 0 ? 0 : first)
                        + "-"
                        + ("".equals(lastFilter) && nullSelectionAllowed
                                && currentPage == 0 ? last - 1 : last) + "/"
                        + matches);
            } else {
                DOM.setInnerText(status, "");
            }<commit_msg>FilterSelect status no longer says 1-0/0. Fixes #1706<commit_after>/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.terminal.gwt.client.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
import com.itmill.toolkit.terminal.gwt.client.Focusable;
import com.itmill.toolkit.terminal.gwt.client.Paintable;
import com.itmill.toolkit.terminal.gwt.client.UIDL;
import com.itmill.toolkit.terminal.gwt.client.Util;

/**
 * 
 * TODO needs major refactoring (to be extensible etc)
 */
public class IFilterSelect extends Composite implements Paintable, Field,
        KeyboardListener, ClickListener, FocusListener, Focusable {

    public class FilterSelectSuggestion implements Suggestion, Command {

        private final String key;
        private final String caption;
        private String iconUri;

        public FilterSelectSuggestion(UIDL uidl) {
            key = uidl.getStringAttribute("key");
            caption = uidl.getStringAttribute("caption");
            if (uidl.hasAttribute("icon")) {
                iconUri = client.translateToolkitUri(uidl
                        .getStringAttribute("icon"));
            }
        }

        public String getDisplayString() {
            final StringBuffer sb = new StringBuffer();
            if (iconUri != null) {
                sb.append("<img src=\"");
                sb.append(iconUri);
                sb.append("\" alt=\"icon\" class=\"i-icon\" />");
            }
            sb.append(Util.escapeHTML(caption));
            return sb.toString();
        }

        public String getReplacementString() {
            return caption;
        }

        public int getOptionKey() {
            return Integer.parseInt(key);
        }

        public String getIconUri() {
            return iconUri;
        }

        public void execute() {
            onSuggestionSelected(this);
        }
    }

    /**
     * @author mattitahvonen
     * 
     */
    public class SuggestionPopup extends ToolkitOverlay implements
            PositionCallback, PopupListener {
        private static final int EXTRASPACE = 8;

        private static final String Z_INDEX = "30000";

        private final SuggestionMenu menu;

        private final Element up = DOM.createDiv();
        private final Element down = DOM.createDiv();
        private final Element status = DOM.createDiv();

        private boolean isPagingEnabled = true;

        private long lastAutoClosed;

        SuggestionPopup() {
            super(true);
            menu = new SuggestionMenu();
            setWidget(menu);
            setStyleName(CLASSNAME + "-suggestpopup");
            DOM.setStyleAttribute(getElement(), "zIndex", Z_INDEX);

            final Element root = getContainerElement();

            DOM.setInnerHTML(up, "<span>Prev</span>");
            DOM.sinkEvents(up, Event.ONCLICK);
            DOM.setInnerHTML(down, "<span>Next</span>");
            DOM.sinkEvents(down, Event.ONCLICK);
            DOM.insertChild(root, up, 0);
            DOM.appendChild(root, down);
            DOM.appendChild(root, status);
            DOM.setElementProperty(status, "className", CLASSNAME + "-status");

            addPopupListener(this);
        }

        public void showSuggestions(Collection currentSuggestions,
                int currentPage, int totalSuggestions) {

            if (ApplicationConnection.isTestingMode()) {
                // Add TT anchor point
                DOM.setElementProperty(getElement(), "id", paintableId
                        + "_OPTIONLIST");
            }

            menu.setSuggestions(currentSuggestions);
            final int x = IFilterSelect.this.getAbsoluteLeft();
            int y = tb.getAbsoluteTop();
            y += tb.getOffsetHeight();
            setPopupPosition(x, y);
            final int first = currentPage * PAGELENTH
                    + (nullSelectionAllowed && currentPage > 0 ? 0 : 1);
            final int last = first + currentSuggestions.size() - 1;
            final int matches = totalSuggestions
                    - (nullSelectionAllowed ? 1 : 0);
            if (last > 0) {
                // nullsel not counted, as requested by user
                DOM.setInnerText(status, (matches == 0 ? 0 : first)
                        + "-"
                        + ("".equals(lastFilter) && nullSelectionAllowed
                                && currentPage == 0 ? last - 1 : last) + "/"
                        + matches);
            } else {
                DOM.setInnerText(status, "");
            }
{'fraction_non_alphanumeric': 0.10821064041188691, 'fraction_numerical': 0.004064673471231145, 'mean_word_length': 2.162524992859183, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115217', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2971, 'n_words': 621}