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

javafx.scene.control.cell.CellUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package javafx.scene.control.cell;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Cell;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.util.StringConverter;

// Package protected - not intended for external use
class CellUtils {
    
    static int TREE_VIEW_HBOX_GRAPHIC_PADDING = 3; 
    
    /***************************************************************************
     *                                                                         *
     * Private fields                                                          *
     *                                                                         *
     **************************************************************************/    
    
    private final static StringConverter defaultStringConverter = new StringConverter() {
        @Override public String toString(Object t) {
            return t == null ? null : t.toString();
        }

        @Override public Object fromString(String string) {
            return (Object) string;
        }
    };
    
    private final static StringConverter defaultTreeItemStringConverter =
        new StringConverter() {
            @Override public String toString(TreeItem treeItem) {
                return (treeItem == null || treeItem.getValue() == null) ? 
                        "" : treeItem.getValue().toString();
            }

            @Override public TreeItem fromString(String string) {
                return new TreeItem(string);
            }
        };
    
    /***************************************************************************
     *                                                                         *
     * General convenience                                                     *
     *                                                                         *
     **************************************************************************/    
    
    /*
     * Simple method to provide a StringConverter implementation in various cell
     * implementations.
     */
    static  StringConverter defaultStringConverter() {
        return (StringConverter) defaultStringConverter;
    }
    
    /*
     * Simple method to provide a TreeItem-specific StringConverter 
     * implementation in various cell implementations.
     */
    static  StringConverter> defaultTreeItemStringConverter() {
        return (StringConverter>) defaultTreeItemStringConverter;
    }
    
    private static  String getItemText(Cell cell, StringConverter converter) {
        return converter == null ?
            cell.getItem() == null ? "" : cell.getItem().toString() :
            converter.toString(cell.getItem());
    }
    
    
    static Node getGraphic(TreeItem treeItem) {
        return treeItem == null ? null : treeItem.getGraphic();
    }
    

    
    /***************************************************************************
     *                                                                         *
     * ChoiceBox convenience                                                   *
     *                                                                         *
     **************************************************************************/   
    
    static  void updateItem(final Cell cell, 
                               final StringConverter converter,
                               final ChoiceBox choiceBox) {
        updateItem(cell, converter, null, null, choiceBox);
    }
    
    static  void updateItem(final Cell cell,
                               final StringConverter converter,
                               final HBox hbox,
                               final Node graphic,
                               final ChoiceBox choiceBox) {
        if (cell.isEmpty()) {
            cell.setText(null);
            cell.setGraphic(null);
        } else {
            if (cell.isEditing()) {
                if (choiceBox != null) {
                    choiceBox.getSelectionModel().select(cell.getItem());
                }
                cell.setText(null);
                
                if (graphic != null) {
                    hbox.getChildren().setAll(graphic, choiceBox);
                    cell.setGraphic(hbox);
                } else {
                    cell.setGraphic(choiceBox);
                }
            } else {
                cell.setText(getItemText(cell, converter));
                cell.setGraphic(graphic);
            }
        }
    };
    
    static  ChoiceBox createChoiceBox(
            final Cell cell, 
            final ObservableList items) {
        ChoiceBox choiceBox = new ChoiceBox(items);
        choiceBox.setMaxWidth(Double.MAX_VALUE);
        choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, T oldValue, T newValue) {
                if (cell.isEditing()) {
                    cell.commitEdit(newValue);
                }
            }
        });
        return choiceBox;
    }
    
    
    
    /***************************************************************************
     *                                                                         *
     * TextField convenience                                                   *
     *                                                                         *
     **************************************************************************/  
    
    static  void updateItem(final Cell cell, 
                               final StringConverter converter,
                               final TextField textField) {
        updateItem(cell, converter, null, null, textField);
    }
    
    static  void updateItem(final Cell cell, 
                               final StringConverter converter,
                               final HBox hbox,
                               final Node graphic,
                               final TextField textField) {
        if (cell.isEmpty()) {
            cell.setText(null);
            cell.setGraphic(null);
        } else {
            if (cell.isEditing()) {
                if (textField != null) {
                    textField.setText(getItemText(cell, converter));
                }
                cell.setText(null);
                
                if (graphic != null) {
                    hbox.getChildren().setAll(graphic, textField);
                    cell.setGraphic(hbox);
                } else {
                    cell.setGraphic(textField);
                }
            } else {
                cell.setText(getItemText(cell, converter));
                cell.setGraphic(graphic);
            }
        }
    }
    
    static  void startEdit(final Cell cell, 
                              final StringConverter converter,
                              final HBox hbox,
                              final Node graphic,
                              final TextField textField) {
        if (textField != null) {
            textField.setText(getItemText(cell, converter));
        }
        cell.setText(null);
        
        if (graphic != null) {
            hbox.getChildren().setAll(graphic, textField);
            cell.setGraphic(hbox);
        } else {
            cell.setGraphic(textField);
        }
        
        textField.selectAll();
    }
    
    static  void cancelEdit(Cell cell, final StringConverter converter, Node graphic) {
        cell.setText(getItemText(cell, converter));
        cell.setGraphic(graphic);
    }
    
    static  TextField createTextField(final Cell cell, final StringConverter converter) {
        final TextField textField = new TextField(getItemText(cell, converter));
        textField.setOnKeyReleased(new EventHandler() {
            @Override public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ENTER) {
                    if (converter == null) {
                        throw new IllegalStateException(
                            "Attempting to convert text input into Object, but provided "
                                + "StringConverter is null. Be sure to set a StringConverter "
                                + "in your cell factory.");
                    }
                    cell.commitEdit(converter.fromString(textField.getText()));
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    cell.cancelEdit();
                }
            }
        });
        return textField;
    }
    
    
    
    /***************************************************************************
     *                                                                         *
     * ComboBox convenience                                                   *
     *                                                                         *
     **************************************************************************/ 
    
    static  void updateItem(Cell cell, StringConverter converter, ComboBox comboBox) {
        updateItem(cell, converter, null, null, comboBox);
    }
    
    static  void updateItem(final Cell cell, 
                               final StringConverter converter,
                               final HBox hbox,
                               final Node graphic,
                               final ComboBox comboBox) {
        if (cell.isEmpty()) {
            cell.setText(null);
            cell.setGraphic(null);
        } else {
            if (cell.isEditing()) {
                if (comboBox != null) {
                    comboBox.getSelectionModel().select(cell.getItem());
                }
                cell.setText(null);
                
                if (graphic != null) {
                    hbox.getChildren().setAll(graphic, comboBox);
                    cell.setGraphic(hbox);
                } else {
                    cell.setGraphic(comboBox);
                }
            } else {
                cell.setText(getItemText(cell, converter));
                cell.setGraphic(graphic);
            }
        }
    };
    
    static  ComboBox createComboBox(final Cell cell, final ObservableList items, final StringConverter converter) {
        ComboBox comboBox = new ComboBox(items);
        comboBox.setConverter(converter);
        comboBox.setMaxWidth(Double.MAX_VALUE);
        comboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override public void changed(ObservableValue ov, T oldValue, T newValue) {
                if (cell.isEditing()) {
                    cell.commitEdit(newValue);
                }
            }
        });
        return comboBox;
    }
}