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

com.dlsc.showcase.impl.SamplePageTableHelper Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
/*
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * 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 Oracle Corporation 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 THE COPYRIGHT
 * OWNER OR CONTRIBUTORS 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 com.dlsc.showcase.impl;

import javafx.beans.binding.StringBinding;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * Helper class for creating table views for testing
 */
public class SamplePageTableHelper {

    public static class Person {

        private BooleanProperty invited;
        private StringProperty firstName;
        private StringProperty lastName;
        private StringProperty name;
        private StringProperty email;

        private final String country = "New Zealand";

        public Person(String fName, String lName) {
            this(fName, lName, null);
        }

        public Person(String fName, String lName, String email) {
            this(fName, lName, email, false);
        }

        public Person(String fName, String lName, String email, boolean invited) {
            firstName = new SimpleStringProperty(fName);
            lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
            this.invited = new SimpleBooleanProperty(invited);
            name = new SimpleStringProperty();
            name.bind(new StringBinding() {
                {
                    bind(firstName, lastName);
                }

                @Override
                protected String computeValue() {
                    return firstName.get() + " " + lastName.get();
                }
            });
            this.invited.addListener((ov, t, t1) -> System.out.println(getFirstName() + " invited: " + t1));
        }

        public Boolean isInvited() {
            return invited.get();
        }

        public BooleanProperty invitedProperty() {
            return invited;
        }

        public String getName() {
            return name.get();
        }

        public StringProperty nameProperty() {
            return name;
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String firstName) {
            this.firstName.set(firstName);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String lastName) {
            this.lastName.set(lastName);
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String email) {
            this.email.set(email);
        }

        public StringProperty emailProperty() {
            return email;
        }

        public String getCountry() {
            return country;
        }

        public String toString() {
            return "Person [ " + getFirstName() + " " + getLastName()/* + ", " + getEmail()*/ + " ]";
        }
    }

    private static ObservableList data = FXCollections.observableArrayList();

    static {
        // Data
        data.addAll(
                new Person("Jacob", "Smith\nSmith\nSmith", "jacob.smithexample.com", true),
                new Person("Isabella", "Johnson", "isabella.johnsonexample.com"),
                new Person("Ethan", "Williams", "ethan.williamsexample.com", true),
                new Person("Emma", "Jones", "emma.jonesexample.com"),
                new Person("Michael", "Brown", "michael.brownexample.com", true),
                new Person("Olivia", "Davis", "olivia.davisexample.com"),
                new Person("Alexander", "Miller", "alexander.millerexample.com", true),
                new Person("Sophia", "Wilson", "sophia.wilsonexample.com"),
                new Person("William", "Moore", "william.mooreexample.com", true),
                new Person("Ava", "Taylor", "ava.taylorexample.com"),
                new Person("Joshua", "Anderson", "joshua.andersonexample.com"),
                new Person("Emily", "Thomas", "emily.thomasexample.com"),
                new Person("Daniel", "Jackson", "daniel.jacksonexample.com"),
                new Person("Madison", "White", "madison.whiteexample.com"),
                new Person("Jayden", "Harris", "jayden.harrisexample.com"),
                new Person("Abigail", "Martin", "abigail.martinexample.com"),
                new Person("Noah", "Thompson", "noah.thompsonexample.com"),
                new Person("Chloe", "Garcia", "chloe.garciaexample.com"),
                new Person("Anthony", "Martinez", "anthony.martinezexample.com"),
                new Person("Mia", "Robinson", "mia.robinsonexample.com"),
                new Person("Jacob", "Smith"),
                new Person("Isabella", "Johnson"),
                new Person("Ethan", "Williams"),
                new Person("Emma", "Jones"),
                new Person("Michael", "Brown"),
                new Person("Olivia", "Davis"),
                new Person("Alexander", "Miller"),
                new Person("Sophia", "Wilson"),
                new Person("William", "Moore"),
                new Person("Ava", "Taylor"),
                new Person("Joshua", "Anderson"),
                new Person("Emily", "Thomas"),
                new Person("Daniel", "Jackson"),
                new Person("Madison", "White"),
                new Person("Jayden", "Harris"),
                new Person("Abigail", "Martin"),
                new Person("Noah", "Thompson"),
                new Person("Chloe", "Garcia"),
                new Person("Anthony", "Martinez"),
                new Person("Mia", "Robinson")
        );

    }

    static TableView createTableViewSimple(int width, boolean rowSelection, boolean constrainedResize) {
        TableColumn nameCol, emailCol, countryCol;
        // Columns
        nameCol = new TableColumn();
        nameCol.setText("Name");
        nameCol.setCellValueFactory(new PropertyValueFactory("name"));
        emailCol = new TableColumn();
        emailCol.setText("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(p -> p.getValue().emailProperty());
        countryCol = new TableColumn();
        countryCol.setText("Country");
        countryCol.setCellValueFactory(p -> new ReadOnlyObjectWrapper("New Zealand"));
        // Create TableView
        TableView tableView = new TableView();
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        tableView.getSelectionModel().setCellSelectionEnabled(!rowSelection);
        tableView.setTableMenuButtonVisible(false);
        if (constrainedResize) tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        tableView.setItems(data);
        tableView.getColumns().addAll(nameCol, emailCol, countryCol);
        tableView.setPrefSize(width, 300);
        if (rowSelection) {
            tableView.getSelectionModel().selectRange(2, 5);
        } else {
            tableView.getSelectionModel().select(2, emailCol);
            tableView.getSelectionModel().select(3, nameCol);
            tableView.getSelectionModel().select(3, countryCol);
            tableView.getSelectionModel().select(5, nameCol);
        }
        tableView.getSortOrder().addAll(nameCol, emailCol, countryCol);
        return tableView;
    }

    static TableView createTableView(int width, boolean rowSelection) {
        TableColumn firstNameCol, lastNameCol, nameCol, emailCol, countryCol;
        TableColumn invitedCol;
        // Columns
        firstNameCol = new TableColumn();
        firstNameCol.setText("First");
//        Rectangle sortNode = new Rectangle(10, 10, Color.RED);
//        sortNode.fillProperty().bind(new ObjectBinding() {
//            { bind(firstNameCol.sortTypeProperty()); }
//            @Override protected Paint computeValue() {
//                switch (firstNameCol.getSortType()) {
//                    case ASCENDING: return Color.GREEN;
//                    case DESCENDING: return Color.RED;
//                    default: return Color.BLACK;
//                }
//            }
//        });
//        firstNameCol.setSortNode(sortNode);
        firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
        firstNameCol.setOnEditCommit(t -> System.out.println("Edit commit event: " + t.getNewValue()));
        lastNameCol = new TableColumn();
        lastNameCol.setText("Last");
        lastNameCol.setSortType(TableColumn.SortType.DESCENDING);
        lastNameCol.setCellValueFactory(p -> p.getValue().lastNameProperty());
        nameCol = new TableColumn();
        nameCol.setText("Name");
        nameCol.getColumns().addAll(firstNameCol, lastNameCol);
        emailCol = new TableColumn();
        emailCol.setText("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(p -> p.getValue().emailProperty());
        countryCol = new TableColumn();
        countryCol.setText("Country");
        countryCol.setCellValueFactory(p -> new ReadOnlyObjectWrapper("New Zealand"));
        // Test case for RT-28410 MODENA: can't make tree/table cell factories change color based
        // on background when setGraphic(...) is used
        countryCol.setCellFactory(param -> {
            Label label = new Label();
            label.setStyle(
                    "-fx-font-family: 'Times New Roman';" +
                            "-fx-font-size: 0.8em;" +
                            "-fx-text-fill: ladder(-fx-background, yellow 49%, red 50%);");
            TableCell cell = new TableCell() {
                @Override
                protected void updateItem(Object item, boolean empty) {
                    label.setText(empty ? null : item.toString());
                }
            };
            cell.setGraphic(label);
            return cell;
        });

        invitedCol = new TableColumn();
        invitedCol.setText("Invited");
        invitedCol.setPrefWidth(55);
        invitedCol.setMaxWidth(55);
        invitedCol.setCellValueFactory(new PropertyValueFactory("invited"));
        invitedCol.setCellFactory(p -> new CheckBoxTableCell());


        TableView tableView = new TableView();
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        tableView.getSelectionModel().setCellSelectionEnabled(!rowSelection);
        tableView.setTableMenuButtonVisible(true);
        tableView.setItems(data);
        tableView.getColumns().addAll(invitedCol, nameCol, emailCol, countryCol);
        tableView.setPrefSize(width, 300);
        if (rowSelection) {
            tableView.getSelectionModel().selectRange(2, 5);
        } else {
            tableView.getSelectionModel().select(2, emailCol);
            tableView.getSelectionModel().select(3, firstNameCol);
            tableView.getSelectionModel().select(3, countryCol);
            tableView.getSelectionModel().select(4, lastNameCol);
        }
        tableView.getSortOrder().addAll(firstNameCol, lastNameCol, emailCol, countryCol);
        return tableView;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy