com.google.gwt.user.client.ui.SuggestOracle.Suggestion Java Examples

The following examples show how to use com.google.gwt.user.client.ui.SuggestOracle.Suggestion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 6 votes vote down vote up
/**
 * @param itemValues the itemsSelected to set
 * @param fireEvents will fire value change event if true
 * @see #setValue(Object)
 */
public void setItemValues(List<String> itemValues, boolean fireEvents) {
    if (itemValues == null) {
        clear();
        return;
    }
    List<Suggestion> list = new ArrayList<>(itemValues.size());
    for (String value : itemValues) {
        Suggestion suggestion = new gwt.material.design.client.base.Suggestion(value, value);
        list.add(suggestion);
    }
    setValue(list, fireEvents);
    if (itemValues.size() > 0) {
        label.addStyleName(CssName.ACTIVE);
    }
}
 
Example #2
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 6 votes vote down vote up
@Override
public MaterialChip getChip(Suggestion suggestion) {
    final MaterialChip chip = new MaterialChip();

    String imageChip = suggestion.getDisplayString();
    String textChip = imageChip;

    String s = "<img src=\"";
    if (imageChip.contains(s)) {
        int ix = imageChip.indexOf(s) + s.length();
        imageChip = imageChip.substring(ix, imageChip.indexOf("\"", ix + 1));
        chip.setUrl(imageChip);
        textChip = textChip.replaceAll("[<](/)?img[^>]*[>]", "");
    }
    chip.setText(textChip);
    chip.setIconType(IconType.CLOSE);

    return chip;
}
 
Example #3
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
@Override
public HandlerRegistration addSelectionHandler(final SelectionHandler<Suggestion> handler) {
    return addHandler(new SelectionHandler<Suggestion>() {
        @Override
        public void onSelection(SelectionEvent<Suggestion> event) {
            if (isEnabled()) {
                handler.onSelection(event);
            }
        }
    }, SelectionEvent.getType());
}
 
Example #4
Source File: SectioningStatusPage.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void onSuccess(List<String[]> result) {
	ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
	for (String[] suggestion: result) {
		suggestions.add(new SimpleSuggestion(suggestion[1], suggestion[0]));
	}
	iCallback.onSuggestionsReady(iRequest, new Response(suggestions));
}
 
Example #5
Source File: CourseSelectionSuggestBox.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void onSuccess(Collection<ClassAssignmentInterface.CourseAssignment> result) {
	ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
	iValidCourseNames.clear();
	for (ClassAssignmentInterface.CourseAssignment course: result) {
		SimpleSuggestion suggestion = new SimpleSuggestion(course, iShowCourses);
		suggestions.add(suggestion);
		iValidCourseNames.put(suggestion.getReplacementString().toLowerCase(), suggestion.getRequestedCourse());
	}
	iCallback.onSuggestionsReady(iRequest, new Response(suggestions));
}
 
Example #6
Source File: CourseSelectionSuggestBox.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void onFailure(final Throwable caught) {
	iValidCourseNames.clear();
	final ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
	if (iFreeTimeParser != null) {
		iFreeTimeParser.getData(iRequest.getQuery(), new AsyncCallback<List<FreeTime>>() {
			@Override
			public void onFailure(Throwable e) {
				if (iRequest.getQuery().toLowerCase().startsWith(CONSTANTS.freePrefix().toLowerCase())) {
					suggestions.add(new SimpleSuggestion(e));
					// setStatus(e.getMessage());
				} else {
					suggestions.add(new SimpleSuggestion(caught));
					// setStatus(caught.getMessage());
				}
			}

			@Override
			public void onSuccess(List<FreeTime> freeTimes) {
				suggestions.add(new SimpleSuggestion(freeTimes));
			}
		});
	} else {
		suggestions.add(new SimpleSuggestion(caught));
		// setStatus(caught.getMessage());
	}
	iCallback.onSuggestionsReady(iRequest, new Response(suggestions));
}
 
Example #7
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 5 votes vote down vote up
private SuggestionMenuItem(final Suggestion suggestion) {
	super(suggestion.getDisplayString(), iOracle.isDisplayStringHTML(), new ScheduledCommand() {
		@Override
		public void execute() {
			iSuggestionCallback.onSuggestionSelected(suggestion);
		}
	});
	setStyleName("item");
	getElement().setAttribute("whiteSpace", "nowrap");
	iSuggestion = suggestion;
}
 
Example #8
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
@Override
public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<List<? extends Suggestion>> handler) {
    return addHandler(new ValueChangeHandler<List<? extends Suggestion>>() {
        @Override
        public void onValueChange(ValueChangeEvent<List<? extends Suggestion>> event) {
            if (isEnabled()) {
                handler.onValueChange(event);
            }
        }
    }, ValueChangeEvent.getType());
}
 
Example #9
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
/**
 * Generate and build the List Items to be set on Auto Complete box.
 */
protected void setup(SuggestOracle suggestions) {

    if (itemBoxKeyDownHandler != null) {
        itemBoxKeyDownHandler.removeHandler();
    }

    list.setStyleName(AddinsCssName.MULTIVALUESUGGESTBOX_LIST);
    this.suggestions = suggestions;
    final ListItem item = new ListItem();

    item.setStyleName(AddinsCssName.MULTIVALUESUGGESTBOX_INPUT_TOKEN);

    suggestBox = new SuggestBox(suggestions, itemBox);
    suggestBox.addSelectionHandler(selectionEvent -> {
        Suggestion selectedItem = selectionEvent.getSelectedItem();
        itemBox.setValue("");
        if (addItem(selectedItem)) {
            ValueChangeEvent.fire(MaterialAutoComplete.this, getValue());
        }
        itemBox.setFocus(true);
    });

    loadHandlers();

    setLimit(this.limit);
    String autocompleteId = DOM.createUniqueId();
    itemBox.getElement().setId(autocompleteId);

    item.add(suggestBox);
    item.add(label);
    list.add(item);

    panel.add(list);
    panel.getElement().setAttribute("onclick",
            "document.getElementById('" + autocompleteId + "').focus()");
    panel.add(errorLabel);
    suggestBox.setFocus(true);
}
 
Example #10
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(List<? extends Suggestion> value, boolean fireEvents) {
    clear();
    if (value != null) {
        label.addStyleName(CssName.ACTIVE);
        for (Suggestion suggestion : value) {
            addItem(suggestion);
        }
    }
    super.setValue(value, fireEvents);
}
 
Example #11
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
protected boolean tryRemoveSuggestion(Widget widget) {
    Set<Entry<Suggestion, Widget>> entrySet = suggestionMap.entrySet();
    for (Entry<Suggestion, Widget> entry : entrySet) {
        if (widget.equals(entry.getValue())) {
            if (chipProvider.isChipRemovable(entry.getKey())) {
                suggestionMap.remove(entry.getKey());
                return true;
            }
            return false;
        }
    }
    return false;
}
 
Example #12
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
/**
 * @return the item values on autocomplete
 * @see #getValue()
 */
public List<String> getItemValues() {
    Set<Suggestion> keySet = suggestionMap.keySet();
    List<String> values = new ArrayList<>(keySet.size());
    for (Suggestion suggestion : keySet) {
        values.add(suggestion.getReplacementString());
    }
    return values;
}
 
Example #13
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isChipRemovable(Suggestion suggestion) {
    return true;
}
 
Example #14
Source File: SectioningStatusPage.java    From unitime with Apache License 2.0 4 votes vote down vote up
public void onFailure(Throwable caught) {
	ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
	// suggestions.add(new SimpleSuggestion("<font color='red'>"+caught.getMessage()+"</font>", ""));
	iCallback.onSuggestionsReady(iRequest, new Response(suggestions));
	ToolBox.checkAccess(caught);
}
 
Example #15
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 4 votes vote down vote up
/**
 * Adding the item value using Material Chips added on auto complete box
 */
protected boolean addItem(final Suggestion suggestion) {
    SelectionEvent.fire(MaterialAutoComplete.this, suggestion);
    if (getLimit() > 0) {
        if (suggestionMap.size() >= getLimit()) {
            return false;
        }
    }

    if (suggestionMap.containsKey(suggestion)) {
        return false;
    }

    final ListItem displayItem = new ListItem();
    displayItem.setStyleName(AddinsCssName.MULTIVALUESUGGESTBOX_TOKEN);

    if (getType() == AutocompleteType.TEXT) {
        suggestionMap.clear();
        itemBox.setText(suggestion.getReplacementString());
    } else {
        final MaterialChip chip = chipProvider.getChip(suggestion);
        if (chip == null) {
            return false;
        }

        registerHandler(chip.addClickHandler(event -> {
            if (chipProvider.isChipSelectable(suggestion)) {
                if (itemsHighlighted.contains(displayItem)) {
                    chip.removeStyleName(selectedChipStyle);
                    itemsHighlighted.remove(displayItem);
                } else {
                    chip.addStyleName(selectedChipStyle);
                    itemsHighlighted.add(displayItem);
                }
            }
        }));

        if (chip.getIcon() != null) {
            registerHandler(chip.getIcon().addClickHandler(event -> {
                if (chipProvider.isChipRemovable(suggestion)) {
                    suggestionMap.remove(suggestion);
                    list.remove(displayItem);
                    itemsHighlighted.remove(displayItem);
                    ValueChangeEvent.fire(MaterialAutoComplete.this, getValue());
                    suggestBox.showSuggestionList();
                }
            }));
        }

        suggestionMap.put(suggestion, chip);
        displayItem.add(chip);
        list.insert(displayItem, list.getWidgetCount() - 1);
    }
    return true;
}
 
Example #16
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
public AriaSuggestBox(AriaTextBox box, SuggestOracle oracle) {
	iOracle = oracle;
	iText = box;
	iText.setStyleName("gwt-SuggestBox");
	initWidget(iText);
	
	addEventsToTextBox();
	
	iSuggestionMenu = new SuggestionMenu();
	
	iPopupScroll = new ScrollPanel(iSuggestionMenu);
	iPopupScroll.addStyleName("scroll");
	
	iSuggestionPopup = new PopupPanel(true, false);
	iSuggestionPopup.setPreviewingAllNativeEvents(true);
	iSuggestionPopup.setStyleName("unitime-SuggestBoxPopup");
	iSuggestionPopup.setWidget(iPopupScroll);
	iSuggestionPopup.addAutoHidePartner(getElement());
	
	iSuggestionCallback = new SuggestionCallback() {
		@Override
		public void onSuggestionSelected(Suggestion suggestion) {
			if (!suggestion.getReplacementString().isEmpty()) {
				setStatus(ARIA.suggestionSelected(status(suggestion)));
			}
			iCurrentText = suggestion.getReplacementString();
			setText(suggestion.getReplacementString());
			hideSuggestionList();
			fireSuggestionEvent(suggestion);
		}
	};
	
	iOracleCallback = new SuggestOracle.Callback() {
		@Override
		public void onSuggestionsReady(Request request, Response response) {
			if (response.getSuggestions() == null || response.getSuggestions().isEmpty()) {
				if (iSuggestionPopup.isShowing()) iSuggestionPopup.hide();
			} else {
				iSuggestionMenu.clearItems();
				SuggestOracle.Suggestion first = null;
				for (SuggestOracle.Suggestion suggestion: response.getSuggestions()) {
					iSuggestionMenu.addItem(new SuggestionMenuItem(suggestion));
					if (first == null) first = suggestion;
				}
				iSuggestionMenu.selectItem(0);
				ToolBox.setMinWidth(iSuggestionMenu.getElement().getStyle(), (iText.getElement().getClientWidth() - 4) + "px");
				iSuggestionPopup.showRelativeTo(iText);
				iSuggestionMenu.scrollToView();
				if (response.getSuggestions().size() == 1) {
					if (first.getReplacementString().isEmpty())
						setStatus(status(first));
					else
						setStatus(ARIA.showingOneSuggestion(status(first)));
				} else {
					setStatus(ARIA.showingMultipleSuggestions(response.getSuggestions().size(), request.getQuery(), status(first)));
				}
			}
		}
	};
	
	Roles.getTextboxRole().setAriaAutocompleteProperty(iText.getElement(), AutocompleteValue.NONE);
	
	iSuggestionPopup.getElement().setAttribute("id", DOM.createUniqueId());
	Roles.getTextboxRole().setAriaOwnsProperty(iText.getElement(), Id.of(iSuggestionPopup.getElement()));
}
 
Example #17
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
private String status(Suggestion suggestion) {
	return suggestion instanceof HasStatus ? ((HasStatus)suggestion).getStatusString() : suggestion.getDisplayString();
}
 
Example #18
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
public Suggestion getSelectedSuggestion() {
	MenuItem selectedItem = getSelectedItem();
	return selectedItem == null ? null : ((SuggestionMenuItem)selectedItem).getSuggestion();
}
 
Example #19
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isChipSelectable(Suggestion suggestion) {
    return true;
}
 
Example #20
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
public Suggestion getSuggestion() {
	return iSuggestion;
}
 
Example #21
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
@Override
public HandlerRegistration addSelectionHandler(SelectionHandler<Suggestion> handler) {
	return addHandler(handler, SelectionEvent.getType());
}
 
Example #22
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 4 votes vote down vote up
private void fireSuggestionEvent(Suggestion selectedSuggestion) {
	SelectionEvent.fire(this, selectedSuggestion);
}
 
Example #23
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 2 votes vote down vote up
/**
 * Creates and returns a {@link MaterialChip} based on the selected
 * {@link Suggestion}.
 *
 * @param suggestion the selected {@link Suggestion}
 * @return the created MaterialChip, or <code>null</code> if the
 * suggestion should be ignored.
 */
MaterialChip getChip(Suggestion suggestion);
 
Example #24
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether the chip defined by the suggestion should be selected when the user clicks on it.
 * <p>
 * <p>
 * Selecion of chips is used to batch remove suggestions, for example.
 * </p>
 *
 * @param suggestion the selected {@link Suggestion}
 * @see MaterialAutoComplete#setSelectedChipStyle(String)
 */
boolean isChipSelectable(Suggestion suggestion);
 
Example #25
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether the chip defined by the suggestion should be removed from the autocomplete when clicked on its icon.
 * <p>
 * <p>
 * Override this method returning <code>false</code> to implement your own logic when the user clicks on the chip icon.
 * </p>
 *
 * @param suggestion the selected {@link Suggestion}
 */
boolean isChipRemovable(Suggestion suggestion);
 
Example #26
Source File: MaterialAutoComplete.java    From gwt-material-addins with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the selected {@link Suggestion}s. Modifications to the list are
 * not propagated to the component.
 *
 * @return the list of selected {@link Suggestion}s, or empty if none was
 * selected (never <code>null</code>).
 */
@Override
public List<? extends Suggestion> getValue() {
    return new ArrayList<>(suggestionMap.keySet());
}
 
Example #27
Source File: AriaSuggestBox.java    From unitime with Apache License 2.0 votes vote down vote up
void onSuggestionSelected(Suggestion suggestion);