All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jfoenix.controls.JFXAutoCompletePopup Maven / Gradle / Ivy

There is a newer version: 9.0.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.jfoenix.controls;

import com.jfoenix.controls.events.JFXAutoCompleteEvent;
import com.jfoenix.skins.JFXAutoCompletePopupSkin;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.PopupControl;
import javafx.scene.control.Skin;
import javafx.scene.layout.Region;
import javafx.stage.Window;
import javafx.util.Callback;

import java.util.function.Predicate;

/**
 * JFXAutoCompletePopup is an animated popup list view that allow filtering
 * suggestions according to some predicate.
 *
 * @author Shadi Shaheen
 * @version 1.0.0
 * @since 2018-02-01
 */
public class JFXAutoCompletePopup extends PopupControl {

    private final ObservableList suggestions = FXCollections.observableArrayList();
    private final ObjectProperty>> selectionHandler = new SimpleObjectProperty<>();
    private final FilteredList filteredData = new FilteredList(suggestions, s -> true);
    private final ObjectProperty, ListCell>> suggestionsCellFactory = new SimpleObjectProperty, ListCell>>();

    public JFXAutoCompletePopup() {
        this.setAutoFix(true);
        this.setAutoHide(true);
        this.setHideOnEscape(true);
        this.getStyleClass().add("autocomplete-popup");
    }

    @Override
    protected Skin createDefaultSkin() {
        return new JFXAutoCompletePopupSkin(this);
    }

    public void show(Node node){
        if(!isShowing()){
            if(node.getScene() == null || node.getScene().getWindow() == null)
                throw new IllegalStateException("Can not show popup. The node must be attached to a scene/window.");
            Window parent = node.getScene().getWindow();
            this.show(parent, parent.getX() + node.localToScene(0, 0).getX() +
                              node.getScene().getX(),
                parent.getY() + node.localToScene(0, 0).getY() +
                node.getScene().getY() + ((Region)node).getHeight());
            ((JFXAutoCompletePopupSkin)getSkin()).animate();
        }
    }

    public ObservableList getSuggestions() {
        return suggestions;
    }

    public void filter(Predicate predicate){
        filteredData.setPredicate(predicate);
    }

    public ObservableList getFilteredSuggestions() {
        return filteredData;
    }

    public EventHandler> getSelectionHandler() {
        return selectionHandler.get();
    }

    public void setSelectionHandler(EventHandler> selectionHandler){
        this.selectionHandler.set(selectionHandler);
    }

    public final ObjectProperty, ListCell>> suggestionsCellFactoryProperty() {
        return this.suggestionsCellFactory;
    }


    public final Callback, ListCell> getSuggestionsCellFactory() {
        return this.suggestionsCellFactoryProperty().get();
    }


    public final void setSuggestionsCellFactory(
        final javafx.util.Callback, ListCell> suggestionsCellFactory) {
        this.suggestionsCellFactoryProperty().set(suggestionsCellFactory);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy