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.Label;
031import javafx.scene.control.ListCell;
032import javafx.scene.control.ListView;
033import javafx.scene.control.TextField;
034import javafx.util.Callback;
035import javafx.util.StringConverter;
036import javafx.util.converter.DefaultStringConverter;
037
038/**
039 * A class containing a {@link ListCell} implementation that draws a 
040 * {@link TextField} node inside the cell.
041 * 
042 * <p>By default, the TextFieldListCell is rendered as a {@link Label} when not 
043 * being edited, and as a TextField when in editing mode. The TextField will, by 
044 * default, stretch to fill the entire list cell.
045 * 
046 * @param <T> The type of the elements contained within the ListView.
047 * @since 2.2
048 */
049public class TextFieldListCell<T> extends ListCell<T> {
050    
051    /***************************************************************************
052     *                                                                         *
053     * Static cell factories                                                   *
054     *                                                                         *
055     **************************************************************************/
056    
057    /**
058     * Provides a {@link TextField} that allows editing of the cell content when
059     * the cell is double-clicked, or when {@link ListView#edit(int)} is called. 
060     * This method will only work on {@link ListView} instances which are of 
061     * type String.
062     * 
063     * @return A {@link Callback} that can be inserted into the 
064     *      {@link ListView#cellFactoryProperty() cell factory property} of a 
065     *      ListView, that enables textual editing of the content.
066     */
067    public static Callback<ListView<String>, ListCell<String>> forListView() {
068        return forListView(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 {@link ListView#edit(int)} is called. 
074     * This method will work on any ListView instance, regardless of its generic 
075     * type. However, to enable this, a {@link StringConverter} must be provided 
076     * that will convert the given String (from what the user typed in) into an 
077     * instance of type T. This item will then be passed along to the 
078     * {@link ListView#onEditCommitProperty()} callback.
079     * 
080     * @param converter A {@link StringConverter} that can convert the given String 
081     *      (from what the user typed in) into an instance of type T.
082     * @return A {@link Callback} that can be inserted into the 
083     *      {@link ListView#cellFactoryProperty() cell factory property} of a 
084     *      ListView, that enables textual editing of the content.
085     */
086    public static <T> Callback<ListView<T>, ListCell<T>> forListView(final StringConverter<T> converter) {
087        return new Callback<ListView<T>, ListCell<T>>() {
088            @Override public ListCell<T> call(ListView<T> list) {
089                return new TextFieldListCell<T>(converter);
090            }
091        };
092    }
093    
094    
095    
096    /***************************************************************************
097     *                                                                         *
098     * Fields                                                                  *
099     *                                                                         *
100     **************************************************************************/    
101    private TextField textField;
102    
103    
104    
105    /***************************************************************************
106     *                                                                         *
107     * Constructors                                                            *
108     *                                                                         *
109     **************************************************************************/    
110    
111    /**
112     * Creates a default TextFieldListCell 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 TextFieldListCell() { 
119        this(null);
120    }
121    
122    /**
123     * Creates a TextFieldListCell 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 ListView 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 ListView#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 TextFieldListCell(StringConverter<T> converter) {
136        this.getStyleClass().add("text-field-list-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     * Public API                                                              *
177     *                                                                         *
178     **************************************************************************/
179    
180    /** {@inheritDoc} */
181    @Override public void startEdit() {
182        if (! isEditable() || ! getListView().isEditable()) {
183            return;
184        }
185        super.startEdit();
186        
187        if (textField == null) {
188            textField = CellUtils.createTextField(this, getConverter());
189        }
190        
191        CellUtils.startEdit(this, getConverter(), null, null, textField);
192    }
193
194    /** {@inheritDoc} */
195    @Override public void cancelEdit() {
196        super.cancelEdit();
197        CellUtils.cancelEdit(this, getConverter(), null);
198    }
199    
200    /** {@inheritDoc} */
201    @Override public void updateItem(T item, boolean empty) {
202        super.updateItem(item, empty);
203        CellUtils.updateItem(this, getConverter(), null, null, textField);
204    }
205}