1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control.cell;
  27 
  28 import javafx.beans.value.ObservableValue;
  29 import javafx.scene.control.ProgressBar;
  30 import javafx.scene.control.TableCell;
  31 import javafx.scene.control.TableColumn;
  32 import javafx.util.Callback;
  33 
  34 /**
  35  * A class containing a {@link TableCell} implementation that draws a
  36  * {@link ProgressBar} node inside the cell.
  37  *
  38  * @param <S> The type of the elements contained within the TableView.
  39  * @since JavaFX 2.2
  40  */
  41 public class ProgressBarTableCell<S> extends TableCell<S, Double> {
  42 
  43     /***************************************************************************
  44      *                                                                         *
  45      * Static cell factories                                                   *
  46      *                                                                         *
  47      **************************************************************************/
  48 
  49     /**
  50      * Provides a {@link ProgressBar} that allows easy visualisation of a Number
  51      * value as it proceeds from 0.0 to 1.0. If the value is -1, the progress
  52      * bar will appear indeterminate.
  53      *
  54      * @return A {@link Callback} that can be inserted into the
  55      *      {@link TableColumn#cellFactoryProperty() cell factory property} of a
  56      *      TableColumn, that enables visualisation of a Number as it progresses
  57      *      from 0.0 to 1.0.
  58      */
  59     public static <S> Callback<TableColumn<S,Double>, TableCell<S,Double>> forTableColumn() {
  60         return param -> new ProgressBarTableCell<S>();
  61     }
  62 
  63 
  64 
  65     /***************************************************************************
  66      *                                                                         *
  67      * Fields                                                                  *
  68      *                                                                         *
  69      **************************************************************************/
  70 
  71     private final ProgressBar progressBar;
  72 
  73     private ObservableValue<Double> observable;
  74 
  75 
  76 
  77     /***************************************************************************
  78      *                                                                         *
  79      * Constructors                                                            *
  80      *                                                                         *
  81      **************************************************************************/
  82 
  83     /**
  84      * Creates a default {@link ProgressBarTableCell} instance
  85      */
  86     public ProgressBarTableCell() {
  87         this.getStyleClass().add("progress-bar-table-cell");
  88 
  89         this.progressBar = new ProgressBar();
  90         this.progressBar.setMaxWidth(Double.MAX_VALUE);
  91     }
  92 
  93 
  94 
  95     /***************************************************************************
  96      *                                                                         *
  97      * Public API                                                              *
  98      *                                                                         *
  99      **************************************************************************/
 100 
 101     /** {@inheritDoc} */
 102     @Override public void updateItem(Double item, boolean empty) {
 103         super.updateItem(item, empty);
 104 
 105         if (empty) {
 106             setGraphic(null);
 107         } else {
 108             progressBar.progressProperty().unbind();
 109 
 110             final TableColumn<S,Double> column = getTableColumn();
 111             observable = column == null ? null : column.getCellObservableValue(getIndex());
 112 
 113             if (observable != null) {
 114                 progressBar.progressProperty().bind(observable);
 115             } else if (item != null) {
 116                 progressBar.setProgress(item);
 117             }
 118 
 119             setGraphic(progressBar);
 120         }
 121     }
 122 }