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

org.datafx.control.cell.LabelCellFactory Maven / Gradle / Ivy

There is a newer version: 8.0b1
Show newest version
/**
 * Copyright (c) 2011, 2013, Jonathan Giles, Johan Vos, Hendrik Ebbers
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *     * Neither the name of DataFX, the website javafxdata.org, nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL  BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.datafx.control.cell;

import java.text.Format;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeView;
import javafx.scene.text.TextAlignment;
import javafx.util.Callback;

/**
 * A class containing cell factories that make it easy to manipulate the String representation of
 * items contained within ListView, TreeView and TableView controls.
 *
 * @author Jonathan Giles
 */
public class LabelCellFactory {
    private LabelCellFactory() {
    }

    /**
     * Returns a cell factory that creates a default {@link LabelListCell}.
     */
    public static  Callback, ListCell> forListView() {
        return forListView(TextAlignment.LEFT);
    }

    public static  Callback, ListCell> forListView(TextAlignment align) {
        return forListView(align, null);
    }

    public static  Callback, ListCell> forListView(Format format) {
        return forListView(TextAlignment.LEFT, format);
    }

    public static  Callback, ListCell> forListView(final TextAlignment align, final Format format) {
        return new Callback, ListCell>() {
            @Override public ListCell call(ListView list) {
                return new LabelListCell(align, format);
            }
        };
    }

    /**
     * Returns a cell factory that creates a {@link LabelListCell} that uses the provided {@link Callback}
     * to convert from the generic T type to a String, such that it is rendered appropriately.
     *
     * @param toString A {@link Callback} that converts an instance of type T to a String, for rendering
     * in the {@link LabelCellFactory}.
     */
    public static  Callback, ListCell> forListView(final Callback toString) {
        return new Callback, ListCell>() {
            @Override public ListCell call(ListView list) {
                return new LabelListCell() {
                    @Override public String toString(T item) {
                        return toString == null ? super.toString(item) : toString.call(item);
                    }
                ;
            }
        ;
    }

    };
    }
    
    /**
     * Returns a cell factory that creates a default {@link LabelTreeCell}.
     */
    public static  Callback, TreeCell> forTreeView() {
        return forTreeView(TextAlignment.LEFT);
    }

    public static  Callback, TreeCell> forTreeView(TextAlignment align) {
        return forTreeView(align, null);
    }

    public static  Callback, TreeCell> forTreeView(Format format) {
        return forTreeView(TextAlignment.LEFT, format);
    }

    public static  Callback, TreeCell> forTreeView(final TextAlignment align, final Format format) {
        return new Callback, TreeCell>() {
            @Override public TreeCell call(TreeView list) {
                return new LabelTreeCell(align, format);
            }
        };
    }

    /**
     * Returns a cell factory that creates a {@link LabelTableCell} that uses the provided {@link Callback}
     * to convert from the generic T type to a String, such that it is rendered appropriately.
     *
     * @param toString A {@link Callback} that converts an instance of type T to a String, for rendering
     * in the {@link TableCell}.
     */
    public static  Callback, TreeCell> forTreeView(final Callback toString) {
        return new Callback, TreeCell>() {
            @Override public TreeCell call(TreeView list) {
                return new LabelTreeCell() {
                    @Override public String toString(T item) {
                        return toString == null ? super.toString(item) : toString.call(item);
                    }
                ;
            }
        ;
    }

    };
    }
    
    /**
     * Returns a cell factory that creates a default {@link LabelTableCell}.
     */
    public static  Callback, TableCell> forTableColumn() {
        return forTableColumn(TextAlignment.LEFT);
    }

    public static  Callback, TableCell> forTableColumn(TextAlignment align) {
        return forTableColumn(align, null);
    }

    public static  Callback, TableCell> forTableColumn(Format format) {
        return forTableColumn(TextAlignment.LEFT, format);
    }

    public static  Callback, TableCell> forTableColumn(final TextAlignment align, final Format format) {
        return new Callback, TableCell>() {
            @Override public TableCell call(TableColumn list) {
                return new LabelTableCell(align, format);
            }
        };
    }

    /**
     * Returns a cell factory that creates a {@link LabelTreeCell} that uses the provided {@link Callback}
     * to convert from the generic T type to a String, such that it is rendered appropriately.
     *
     * @param toString A {@link Callback} that converts an instance of type T to a String, for rendering
     * in the {@link TreeCell}.
     */
    public static  Callback, TableCell> forTableColumn(final Callback toString) {
        return new Callback, TableCell>() {
            @Override public TableCell call(TableColumn list) {
                return new LabelTableCell() {
                    @Override public String toString(T item) {
                        return toString == null ? super.toString(item) : toString.call(item);
                    }
                ;
            }
        ;
    }
};
}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy