1 /*
   2  * Copyright (c) 2010, 2016, 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;
  27 
  28 import javafx.scene.control.skin.ProgressBarSkin;
  29 
  30 import javafx.beans.value.WritableValue;
  31 import javafx.css.StyleableProperty;
  32 import javafx.scene.AccessibleAttribute;
  33 import javafx.geometry.Orientation;
  34 
  35 /**
  36  * A specialization of the ProgressIndicator which is represented as a
  37  * horizontal bar.
  38  * <p>
  39  * ProgressBar sets focusTraversable to false.
  40  * </p>
  41  *
  42  * <p>
  43  * This first example creates a ProgressBar with an indeterminate value :
  44  * <pre><code>
  45  * import javafx.scene.control.ProgressBar;
  46  *
  47  * ProgressBar p1 = new ProgressBar();
  48  * </code></pre>
  49  * <p>
  50  * This next example creates a ProgressBar which is 25% complete :
  51  * <pre><code>
  52  * import javafx.scene.control.ProgressBar;
  53  * ProgressBar p2 = new ProgressBar();
  54  * p2.setProgress(0.25F);
  55  * </code></pre>
  56  *
  57  * Implementation of ProgressBar According to JavaFX UI Control API Specification
  58  * @since JavaFX 2.0
  59  */
  60 public class ProgressBar extends ProgressIndicator {
  61 
  62 
  63     /***************************************************************************
  64      *                                                                         *
  65      * Constructors                                                            *
  66      *                                                                         *
  67      **************************************************************************/
  68 
  69     /**
  70      * Creates a new indeterminate ProgressBar.
  71      */
  72     public ProgressBar() {
  73         this(INDETERMINATE_PROGRESS);
  74     }
  75 
  76     /**
  77      * Creates a new ProgressBar with the given progress value.
  78      */
  79     public ProgressBar(double progress) {
  80         // focusTraversable is styleable through css. Calling setFocusTraversable
  81         // makes it look to css like the user set the value and css will not
  82         // override. Initializing focusTraversable by calling set on the
  83         // CssMetaData ensures that css will be able to override the value.
  84         ((StyleableProperty<Boolean>)(WritableValue<Boolean>)focusTraversableProperty()).applyStyle(null, Boolean.FALSE);
  85         setProgress(progress);
  86         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
  87     }
  88 
  89     /***************************************************************************
  90      *                                                                         *
  91      * Methods                                                                 *
  92      *                                                                         *
  93      **************************************************************************/
  94 
  95     /** {@inheritDoc} */
  96     @Override protected Skin<?> createDefaultSkin() {
  97         return new ProgressBarSkin(this);
  98     }
  99 
 100     /***************************************************************************
 101      *                                                                         *
 102      * Stylesheet Handling                                                     *
 103      *                                                                         *
 104      **************************************************************************/
 105 
 106     /**
 107      * Initialize the style class to 'progress-bar'.
 108      *
 109      * This is the selector class from which CSS can be used to style
 110      * this control.
 111      */
 112     private static final String DEFAULT_STYLE_CLASS = "progress-bar";
 113 
 114     /**
 115      * Returns the initial focus traversable state of this control, for use
 116      * by the JavaFX CSS engine to correctly set its initial value. This method
 117      * is overridden as by default UI controls have focus traversable set to true,
 118      * but that is not appropriate for this control.
 119      *
 120      * @since 9
 121      */
 122     @Override protected Boolean getInitialFocusTraversable() {
 123         return Boolean.FALSE;
 124     }
 125 
 126 
 127     /***************************************************************************
 128      *                                                                         *
 129      * Accessibility handling                                                  *
 130      *                                                                         *
 131      **************************************************************************/
 132 
 133     @Override
 134     public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) {
 135         switch (attribute) {
 136             case ORIENTATION: return Orientation.HORIZONTAL;
 137             default: return super.queryAccessibleAttribute(attribute, parameters);
 138         }
 139     }
 140 }