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 * @param <S> The type of the TableView generic type 55 * @return A {@link Callback} that can be inserted into the 56 * {@link TableColumn#cellFactoryProperty() cell factory property} of a 57 * TableColumn, that enables visualisation of a Number as it progresses 58 * from 0.0 to 1.0. 59 */ 60 public static <S> Callback<TableColumn<S,Double>, TableCell<S,Double>> forTableColumn() { 61 return param -> new ProgressBarTableCell<S>(); 62 } 63 64 65 66 /*************************************************************************** 67 * * 68 * Fields * 69 * * 70 **************************************************************************/ 71 72 private final ProgressBar progressBar; 73 74 private ObservableValue<Double> observable; 75 76 77 78 /*************************************************************************** 79 * * 80 * Constructors * 81 * * 82 **************************************************************************/ 83 84 /** 85 * Creates a default {@link ProgressBarTableCell} instance 86 */ 87 public ProgressBarTableCell() { 88 this.getStyleClass().add("progress-bar-table-cell"); 89 90 this.progressBar = new ProgressBar(); 91 this.progressBar.setMaxWidth(Double.MAX_VALUE); 92 } 93 94 95 96 /*************************************************************************** 97 * * 98 * Public API * 99 * * 100 **************************************************************************/ 101 102 /** {@inheritDoc} */ 103 @Override public void updateItem(Double item, boolean empty) { 104 super.updateItem(item, empty); 105 106 if (empty) { 107 setGraphic(null); 108 } else { 109 progressBar.progressProperty().unbind(); 110 111 final TableColumn<S,Double> column = getTableColumn(); 112 observable = column == null ? null : column.getCellObservableValue(getIndex()); 113 114 if (observable != null) { 115 progressBar.progressProperty().bind(observable); 116 } else if (item != null) { 117 progressBar.setProgress(item); 118 } 119 120 setGraphic(progressBar); 121 } 122 } 123 }