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.*;
031import javafx.util.Callback;
032import javafx.util.StringConverter;
033import javafx.util.converter.DefaultStringConverter;
034
035/**
036 * A class containing a {@link TableCell} implementation that draws a 
037 * {@link TextField} node inside the cell.
038 * 
039 * <p>By default, the TextFieldTableCell is rendered as a {@link Label} when not
040 * being edited, and as a TextField when in editing mode. The TextField will, by 
041 * default, stretch to fill the entire table cell.
042 * 
043 * @param <T> The type of the elements contained within the TableColumn.
044 * @since 2.2
045 */
046public class TextFieldTableCell<S,T> extends TableCell<S,T> {
047    
048    /***************************************************************************
049     *                                                                         *
050     * Static cell factories                                                   *
051     *                                                                         *
052     **************************************************************************/
053    
054    /**
055     * Provides a {@link TextField} that allows editing of the cell content when
056     * the cell is double-clicked, or when 
057     * {@link TableView#edit(int, javafx.scene.control.TableColumn)} is called. 
058     * This method will only  work on {@link TableColumn} instances which are of
059     * type String.
060     * 
061     * @return A {@link Callback} that can be inserted into the 
062     *      {@link TableColumn#cellFactoryProperty() cell factory property} of a 
063     *      TableColumn, that enables textual editing of the content.
064     */
065    public static <S> Callback<TableColumn<S,String>, TableCell<S,String>> forTableColumn() {
066        return forTableColumn(new DefaultStringConverter());
067    }
068    
069    /**
070     * Provides a {@link TextField} that allows editing of the cell content when
071     * the cell is double-clicked, or when 
072     * {@link TableView#edit(int, javafx.scene.control.TableColumn) } is called. 
073     * This method will work  on any {@link TableColumn} instance, regardless of 
074     * its generic type. However, to enable this, a {@link StringConverter} must 
075     * be provided that will convert the given String (from what the user typed 
076     * in) into an instance of type T. This item will then be passed along to the 
077     * {@link TableColumn#onEditCommitProperty()} callback.
078     * 
079     * @param converter A {@link StringConverter} that can convert the given String 
080     *      (from what the user typed in) into an instance of type T.
081     * @return A {@link Callback} that can be inserted into the 
082     *      {@link TableColumn#cellFactoryProperty() cell factory property} of a 
083     *      TableColumn, that enables textual editing of the content.
084     */
085    public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn(
086            final StringConverter<T> converter) {
087        return new Callback<TableColumn<S,T>, TableCell<S,T>>() {
088            @Override public TableCell<S,T> call(TableColumn<S,T> list) {
089                return new TextFieldTableCell<S,T>(converter);
090            }
091        };
092    }
093    
094    
095    /***************************************************************************
096     *                                                                         *
097     * Fields                                                                  *
098     *                                                                         *
099     **************************************************************************/    
100    
101    private TextField textField;
102    
103    
104    
105    /***************************************************************************
106     *                                                                         *
107     * Constructors                                                            *
108     *                                                                         *
109     **************************************************************************/
110
111    /**
112     * Creates a default TextFieldTableCell with a null converter. Without a 
113     * {@link StringConverter} specified, this cell will not be able to accept
114     * input from the TextField (as it will not know how to convert this back
115     * to the domain object). It is therefore strongly encouraged to not use
116     * this constructor unless you intend to set the converter separately.
117     */
118    public TextFieldTableCell() { 
119        this(null);
120    } 
121    
122    /**
123     * Creates a TextFieldTableCell that provides a {@link TextField} when put 
124     * into editing mode that allows editing of the cell content. This method 
125     * will work on any TableColumn instance, regardless of its generic type. 
126     * However, to enable this, a {@link StringConverter} must be provided that 
127     * will convert the given String (from what the user typed in) into an 
128     * instance of type T. This item will then be passed along to the 
129     * {@link TableColumn#onEditCommitProperty()} callback.
130     * 
131     * @param converter A {@link StringConverter converter} that can convert 
132     *      the given String (from what the user typed in) into an instance of 
133     *      type T.
134     */
135    public TextFieldTableCell(StringConverter<T> converter) {
136        this.getStyleClass().add("text-field-table-cell");
137        setConverter(converter);
138    }
139        
140    
141    
142    /***************************************************************************
143     *                                                                         *
144     * Properties                                                              *
145     *                                                                         *
146     **************************************************************************/
147    
148    // --- converter
149    private ObjectProperty<StringConverter<T>> converter = 
150            new SimpleObjectProperty<StringConverter<T>>(this, "converter");
151
152    /**
153     * The {@link StringConverter} property.
154     */
155    public final ObjectProperty<StringConverter<T>> converterProperty() { 
156        return converter; 
157    }
158    
159    /** 
160     * Sets the {@link StringConverter} to be used in this cell.
161     */
162    public final void setConverter(StringConverter<T> value) { 
163        converterProperty().set(value); 
164    }
165    
166    /**
167     * Returns the {@link StringConverter} used in this cell.
168     */
169    public final StringConverter<T> getConverter() { 
170        return converterProperty().get(); 
171    }  
172    
173    
174    
175    /***************************************************************************
176     *                                                                         *
177     * Public API                                                              *
178     *                                                                         *
179     **************************************************************************/
180    
181    /** {@inheritDoc} */
182    @Override public void startEdit() {
183        if (! isEditable() 
184                || ! getTableView().isEditable() 
185                || ! getTableColumn().isEditable()) {
186            return;
187        }
188        super.startEdit();
189        
190        if (textField == null) {
191            textField = CellUtils.createTextField(this, getConverter());
192        }
193        
194        CellUtils.startEdit(this, getConverter(), null, null, textField);
195    }
196
197    /** {@inheritDoc} */
198    @Override public void cancelEdit() {
199        super.cancelEdit();
200        CellUtils.cancelEdit(this, getConverter(), null);
201    }
202    
203    /** {@inheritDoc} */
204    @Override public void updateItem(T item, boolean empty) {
205        super.updateItem(item, empty);
206        CellUtils.updateItem(this, getConverter(), null, null, textField);
207    }
208}