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