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.value.ObservableValue;
029import javafx.scene.control.ProgressBar;
030import javafx.scene.control.TableCell;
031import javafx.scene.control.TableColumn;
032import javafx.util.Callback;
033
034/**
035 * A class containing a {@link TableCell} implementation that draws a 
036 * {@link ProgressBar} node inside the cell.
037 * 
038 * @param <S> The type of the elements contained within the TableView.
039 * @since 2.2
040 */
041public class ProgressBarTableCell<S> extends TableCell<S, Double> {
042    
043    /***************************************************************************
044     *                                                                         *
045     * Static cell factories                                                   *
046     *                                                                         *
047     **************************************************************************/
048    
049    /**
050     * Provides a {@link ProgressBar} that allows easy visualisation of a Number 
051     * value as it proceeds from 0.0 to 1.0. If the value is -1, the progress 
052     * bar will appear indeterminate.
053     * 
054     * @return A {@link Callback} that can be inserted into the 
055     *      {@link TableColumn#cellFactoryProperty() cell factory property} of a
056     *      TableColumn, that enables visualisation of a Number as it progresses 
057     *      from 0.0 to 1.0.
058     */
059    public static <S> Callback<TableColumn<S,Double>, TableCell<S,Double>> forTableColumn() {
060        return new Callback<TableColumn<S, Double>, TableCell<S, Double>>() {
061            @Override public TableCell<S, Double> call(TableColumn<S, Double> param) {
062                return new ProgressBarTableCell<S>();
063            }
064        };
065    }
066    
067    
068    
069    /***************************************************************************
070     *                                                                         *
071     * Fields                                                                  *
072     *                                                                         *
073     **************************************************************************/
074    
075    private final ProgressBar progressBar;
076    
077    private ObservableValue observable;
078    
079    
080    
081    /***************************************************************************
082     *                                                                         *
083     * Constructors                                                            *
084     *                                                                         *
085     **************************************************************************/    
086    
087    /**
088     * Creates a default {@link ProgressBarTableCell} instance
089     */
090    public ProgressBarTableCell() {
091        this.getStyleClass().add("progress-bar-table-cell");
092        
093        this.progressBar = new ProgressBar();
094        setGraphic(progressBar);
095    }
096    
097    
098    
099    /***************************************************************************
100     *                                                                         *
101     * Public API                                                              *
102     *                                                                         *
103     **************************************************************************/    
104    
105    /** {@inheritDoc} */
106    @Override public void updateItem(Double item, boolean empty) {
107        super.updateItem(item, empty);
108        
109        if (empty) {
110            setGraphic(null);
111        } else {
112            progressBar.progressProperty().unbind();
113            
114            observable = getTableColumn().getCellObservableValue(getIndex());
115            if (observable != null) {
116                progressBar.progressProperty().bind(observable);
117            } else {
118                progressBar.setProgress(item);
119            }
120            
121            setGraphic(progressBar);
122        }
123    }
124}