Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control.cell;
027
028import javafx.beans.property.ObjectProperty;
029import javafx.beans.property.SimpleObjectProperty;
030import javafx.scene.control.TextField;
031import javafx.scene.control.TreeTableCell;
032import javafx.scene.control.TreeTableColumn;
033import javafx.util.Callback;
034import javafx.util.StringConverter;
035import javafx.util.converter.DefaultStringConverter;
036
037/**
038 * A class containing a {@link TableCell} implementation that draws a 
039 * {@link TextField} node inside the cell.
040 * 
041 * <p>By default, the TextFieldTableCell is rendered as a {@link Label} when not
042 * being edited, and as a TextField when in editing mode. The TextField will, by 
043 * default, stretch to fill the entire table cell.
044 * 
045 * @param <T> The type of the elements contained within the TreeTableColumn.
046 * @since 8.0
047 */
048public class TextFieldTreeTableCell<S,T> extends TreeTableCell<S,T> {
049    
050    /***************************************************************************
051     *                                                                         *
052     * Static cell factories                                                   *
053     *                                                                         *
054     **************************************************************************/
055    
056    /**
057     * Provides a {@link TextField} that allows editing of the cell content when
058     * the cell is double-clicked, or when 
059     * {@link TableView#edit(int, javafx.scene.control.TreeTableColumn)} is called. 
060     * This method will only  work on {@link TreeTableColumn} instances which are of
061     * type String.
062     * 
063     * @return A {@link Callback} that can be inserted into the 
064     *      {@link TreeTableColumn#cellFactoryProperty() cell factory property} of a 
065     *      TreeTableColumn, that enables textual editing of the content.
066     */
067    public static <S> Callback<TreeTableColumn<S,String>, TreeTableCell<S,String>> forTreeTableColumn() {
068        return forTreeTableColumn(new DefaultStringConverter());
069    }
070    
071    /**
072     * Provides a {@link TextField} that allows editing of the cell content when
073     * the cell is double-clicked, or when 
074     * {@link TableView#edit(int, javafx.scene.control.TreeTableColumn) } is called. 
075     * This method will work  on any {@link TreeTableColumn} instance, regardless of 
076     * its generic type. However, to enable this, a {@link StringConverter} must 
077     * be provided that will convert the given String (from what the user typed 
078     * in) into an instance of type T. This item will then be passed along to the 
079     * {@link TreeTableColumn#onEditCommitProperty()} callback.
080     * 
081     * @param converter A {@link StringConverter} that can convert the given String 
082     *      (from what the user typed in) into an instance of type T.
083     * @return A {@link Callback} that can be inserted into the 
084     *      {@link TreeTableColumn#cellFactoryProperty() cell factory property} of a 
085     *      TreeTableColumn, that enables textual editing of the content.
086     */
087    public static <S,T> Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> forTreeTableColumn(
088            final StringConverter<T> converter) {
089        return new Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>>() {
090            @Override public TreeTableCell<S,T> call(TreeTableColumn<S,T> list) {
091                return new TextFieldTreeTableCell<S,T>(converter);
092            }
093        };
094    }
095    
096    
097    /***************************************************************************
098     *                                                                         *
099     * Fields                                                                  *
100     *                                                                         *
101     **************************************************************************/    
102    
103    private TextField textField;
104    
105    
106    
107    /***************************************************************************
108     *                                                                         *
109     * Constructors                                                            *
110     *                                                                         *
111     **************************************************************************/
112
113    /**
114     * Creates a default TextFieldTreeTableCell with a null converter. Without a 
115     * {@link StringConverter} specified, this cell will not be able to accept
116     * input from the TextField (as it will not know how to convert this back
117     * to the domain object). It is therefore strongly encouraged to not use
118     * this constructor unless you intend to set the converter separately.
119     */
120    public TextFieldTreeTableCell() { 
121        this(null);
122    } 
123    
124    /**
125     * Creates a TextFieldTreeTableCell that provides a {@link TextField} when put 
126     * into editing mode that allows editing of the cell content. This method 
127     * will work on any TreeTableColumn instance, regardless of its generic type. 
128     * However, to enable this, a {@link StringConverter} must be provided that 
129     * will convert the given String (from what the user typed in) into an 
130     * instance of type T. This item will then be passed along to the 
131     * {@link TreeTableColumn#onEditCommitProperty()} callback.
132     * 
133     * @param converter A {@link StringConverter converter} that can convert 
134     *      the given String (from what the user typed in) into an instance of 
135     *      type T.
136     */
137    public TextFieldTreeTableCell(StringConverter<T> converter) {
138        this.getStyleClass().add("text-field-tree-table-cell");
139        setConverter(converter);
140    }
141        
142    
143    
144    /***************************************************************************
145     *                                                                         *
146     * Properties                                                              *
147     *                                                                         *
148     **************************************************************************/
149    
150    // --- converter
151    private ObjectProperty<StringConverter<T>> converter = 
152            new SimpleObjectProperty<StringConverter<T>>(this, "converter");
153
154    /**
155     * The {@link StringConverter} property.
156     */
157    public final ObjectProperty<StringConverter<T>> converterProperty() { 
158        return converter; 
159    }
160    
161    /** 
162     * Sets the {@link StringConverter} to be used in this cell.
163     */
164    public final void setConverter(StringConverter<T> value) { 
165        converterProperty().set(value); 
166    }
167    
168    /**
169     * Returns the {@link StringConverter} used in this cell.
170     */
171    public final StringConverter<T> getConverter() { 
172        return converterProperty().get(); 
173    }  
174    
175    
176    
177    /***************************************************************************
178     *                                                                         *
179     * Public API                                                              *
180     *                                                                         *
181     **************************************************************************/
182    
183    /** {@inheritDoc} */
184    @Override public void startEdit() {
185        if (! isEditable() 
186                || ! getTreeTableView().isEditable() 
187                || ! getTableColumn().isEditable()) {
188            return;
189        }
190        super.startEdit();
191        
192        if (textField == null) {
193            textField = CellUtils.createTextField(this, getConverter());
194        }
195        
196        CellUtils.startEdit(this, getConverter(), null, null, textField);
197    }
198
199    /** {@inheritDoc} */
200    @Override public void cancelEdit() {
201        super.cancelEdit();
202        CellUtils.cancelEdit(this, getConverter(), null);
203    }
204    
205    /** {@inheritDoc} */
206    @Override public void updateItem(T item, boolean empty) {
207        super.updateItem(item, empty);
208        CellUtils.updateItem(this, getConverter(), null, null, textField);
209    }
210}