|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.jfoenix.controls; |
| 21 | + |
| 22 | +import com.jfoenix.skins.JFXToggleButtonSkin; |
| 23 | +import javafx.css.*; |
| 24 | +import javafx.css.converter.BooleanConverter; |
| 25 | +import javafx.css.converter.PaintConverter; |
| 26 | +import javafx.scene.control.Skin; |
| 27 | +import javafx.scene.control.ToggleButton; |
| 28 | +import javafx.scene.paint.Color; |
| 29 | +import javafx.scene.paint.Paint; |
| 30 | +import org.jackhuang.hmcl.ui.animation.AnimationUtils; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +/** |
| 37 | + * JFXToggleButton is the material design implementation of a toggle button. |
| 38 | + * important CSS Selectors: |
| 39 | + * <p> |
| 40 | + * .jfx-toggle-button{ |
| 41 | + * -fx-toggle-color: color-value; |
| 42 | + * -fx-untoggle-color: color-value; |
| 43 | + * -fx-toggle-line-color: color-value; |
| 44 | + * -fx-untoggle-line-color: color-value; |
| 45 | + * } |
| 46 | + * <p> |
| 47 | + * To change the rippler color when toggled: |
| 48 | + * <p> |
| 49 | + * .jfx-toggle-button .jfx-rippler{ |
| 50 | + * -fx-rippler-fill: color-value; |
| 51 | + * } |
| 52 | + * <p> |
| 53 | + * .jfx-toggle-button:selected .jfx-rippler{ |
| 54 | + * -fx-rippler-fill: color-value; |
| 55 | + * } |
| 56 | + * |
| 57 | + * @author Shadi Shaheen |
| 58 | + * @version 1.0 |
| 59 | + * @since 2016-03-09 |
| 60 | + */ |
| 61 | +public class JFXToggleButton extends ToggleButton { |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritDoc} |
| 65 | + */ |
| 66 | + public JFXToggleButton() { |
| 67 | + initialize(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritDoc} |
| 72 | + */ |
| 73 | + @Override |
| 74 | + protected Skin<?> createDefaultSkin() { |
| 75 | + return new JFXToggleButtonSkin(this); |
| 76 | + } |
| 77 | + |
| 78 | + private void initialize() { |
| 79 | + this.getStyleClass().add(DEFAULT_STYLE_CLASS); |
| 80 | + // it's up for the user to add this behavior |
| 81 | +// toggleColor.addListener((o, oldVal, newVal) -> { |
| 82 | +// // update line color in case not set by the user |
| 83 | +// if(newVal instanceof Color) |
| 84 | +// toggleLineColor.set(((Color)newVal).desaturate().desaturate().brighter()); |
| 85 | +// }); |
| 86 | + } |
| 87 | + |
| 88 | + /*************************************************************************** |
| 89 | + * * |
| 90 | + * styleable Properties * |
| 91 | + * * |
| 92 | + **************************************************************************/ |
| 93 | + |
| 94 | + /** |
| 95 | + * Initialize the style class to 'jfx-toggle-button'. |
| 96 | + * <p> |
| 97 | + * This is the selector class from which CSS can be used to style |
| 98 | + * this control. |
| 99 | + */ |
| 100 | + private static final String DEFAULT_STYLE_CLASS = "jfx-toggle-button"; |
| 101 | + |
| 102 | + /** |
| 103 | + * default color used when the button is toggled |
| 104 | + */ |
| 105 | + private final StyleableObjectProperty<Paint> toggleColor = new SimpleStyleableObjectProperty<>(StyleableProperties.TOGGLE_COLOR, |
| 106 | + JFXToggleButton.this, |
| 107 | + "toggleColor", |
| 108 | + Color.valueOf( |
| 109 | + "#009688")); |
| 110 | + |
| 111 | + public Paint getToggleColor() { |
| 112 | + return toggleColor == null ? Color.valueOf("#009688") : toggleColor.get(); |
| 113 | + } |
| 114 | + |
| 115 | + public StyleableObjectProperty<Paint> toggleColorProperty() { |
| 116 | + return this.toggleColor; |
| 117 | + } |
| 118 | + |
| 119 | + public void setToggleColor(Paint color) { |
| 120 | + this.toggleColor.set(color); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * default color used when the button is not toggled |
| 125 | + */ |
| 126 | + private StyleableObjectProperty<Paint> untoggleColor = new SimpleStyleableObjectProperty<>(StyleableProperties.UNTOGGLE_COLOR, |
| 127 | + JFXToggleButton.this, |
| 128 | + "unToggleColor", |
| 129 | + Color.valueOf( |
| 130 | + "#FAFAFA")); |
| 131 | + |
| 132 | + public Paint getUnToggleColor() { |
| 133 | + return untoggleColor == null ? Color.valueOf("#FAFAFA") : untoggleColor.get(); |
| 134 | + } |
| 135 | + |
| 136 | + public StyleableObjectProperty<Paint> unToggleColorProperty() { |
| 137 | + return this.untoggleColor; |
| 138 | + } |
| 139 | + |
| 140 | + public void setUnToggleColor(Paint color) { |
| 141 | + this.untoggleColor.set(color); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * default line color used when the button is toggled |
| 146 | + */ |
| 147 | + private final StyleableObjectProperty<Paint> toggleLineColor = new SimpleStyleableObjectProperty<>( |
| 148 | + StyleableProperties.TOGGLE_LINE_COLOR, |
| 149 | + JFXToggleButton.this, |
| 150 | + "toggleLineColor", |
| 151 | + Color.valueOf("#77C2BB")); |
| 152 | + |
| 153 | + public Paint getToggleLineColor() { |
| 154 | + return toggleLineColor == null ? Color.valueOf("#77C2BB") : toggleLineColor.get(); |
| 155 | + } |
| 156 | + |
| 157 | + public StyleableObjectProperty<Paint> toggleLineColorProperty() { |
| 158 | + return this.toggleLineColor; |
| 159 | + } |
| 160 | + |
| 161 | + public void setToggleLineColor(Paint color) { |
| 162 | + this.toggleLineColor.set(color); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * default line color used when the button is not toggled |
| 167 | + */ |
| 168 | + private final StyleableObjectProperty<Paint> untoggleLineColor = new SimpleStyleableObjectProperty<>( |
| 169 | + StyleableProperties.UNTOGGLE_LINE_COLOR, |
| 170 | + JFXToggleButton.this, |
| 171 | + "unToggleLineColor", |
| 172 | + Color.valueOf("#999999")); |
| 173 | + |
| 174 | + public Paint getUnToggleLineColor() { |
| 175 | + return untoggleLineColor == null ? Color.valueOf("#999999") : untoggleLineColor.get(); |
| 176 | + } |
| 177 | + |
| 178 | + public StyleableObjectProperty<Paint> unToggleLineColorProperty() { |
| 179 | + return this.untoggleLineColor; |
| 180 | + } |
| 181 | + |
| 182 | + public void setUnToggleLineColor(Paint color) { |
| 183 | + this.untoggleLineColor.set(color); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Default size of the toggle button. |
| 188 | + */ |
| 189 | + private final StyleableDoubleProperty size = new SimpleStyleableDoubleProperty( |
| 190 | + StyleableProperties.SIZE, |
| 191 | + JFXToggleButton.this, |
| 192 | + "size", |
| 193 | + 10.0); |
| 194 | + |
| 195 | + public double getSize() { |
| 196 | + return size.get(); |
| 197 | + } |
| 198 | + |
| 199 | + public StyleableDoubleProperty sizeProperty() { |
| 200 | + return this.size; |
| 201 | + } |
| 202 | + |
| 203 | + public void setSize(double size) { |
| 204 | + this.size.set(size); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Disable the visual indicator for focus |
| 209 | + */ |
| 210 | + private final StyleableBooleanProperty disableVisualFocus = new SimpleStyleableBooleanProperty(StyleableProperties.DISABLE_VISUAL_FOCUS, |
| 211 | + JFXToggleButton.this, |
| 212 | + "disableVisualFocus", |
| 213 | + false); |
| 214 | + |
| 215 | + public final StyleableBooleanProperty disableVisualFocusProperty() { |
| 216 | + return this.disableVisualFocus; |
| 217 | + } |
| 218 | + |
| 219 | + public final Boolean isDisableVisualFocus() { |
| 220 | + return disableVisualFocus != null && this.disableVisualFocusProperty().get(); |
| 221 | + } |
| 222 | + |
| 223 | + public final void setDisableVisualFocus(final Boolean disabled) { |
| 224 | + this.disableVisualFocusProperty().set(disabled); |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + /** |
| 229 | + * disable animation on button action |
| 230 | + */ |
| 231 | + private final StyleableBooleanProperty disableAnimation = new SimpleStyleableBooleanProperty(StyleableProperties.DISABLE_ANIMATION, |
| 232 | + JFXToggleButton.this, |
| 233 | + "disableAnimation", |
| 234 | + !AnimationUtils.isAnimationEnabled()); |
| 235 | + |
| 236 | + public final StyleableBooleanProperty disableAnimationProperty() { |
| 237 | + return this.disableAnimation; |
| 238 | + } |
| 239 | + |
| 240 | + public final Boolean isDisableAnimation() { |
| 241 | + return disableAnimation != null && this.disableAnimationProperty().get(); |
| 242 | + } |
| 243 | + |
| 244 | + public final void setDisableAnimation(final Boolean disabled) { |
| 245 | + this.disableAnimationProperty().set(disabled); |
| 246 | + } |
| 247 | + |
| 248 | + private static final class StyleableProperties { |
| 249 | + private static final CssMetaData<JFXToggleButton, Paint> TOGGLE_COLOR = |
| 250 | + new CssMetaData<>("-jfx-toggle-color", |
| 251 | + PaintConverter.getInstance(), Color.valueOf("#009688")) { |
| 252 | + @Override |
| 253 | + public boolean isSettable(JFXToggleButton control) { |
| 254 | + return control.toggleColor == null || !control.toggleColor.isBound(); |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public StyleableProperty<Paint> getStyleableProperty(JFXToggleButton control) { |
| 259 | + return control.toggleColorProperty(); |
| 260 | + } |
| 261 | + }; |
| 262 | + |
| 263 | + private static final CssMetaData<JFXToggleButton, Paint> UNTOGGLE_COLOR = |
| 264 | + new CssMetaData<>("-jfx-untoggle-color", |
| 265 | + PaintConverter.getInstance(), Color.valueOf("#FAFAFA")) { |
| 266 | + @Override |
| 267 | + public boolean isSettable(JFXToggleButton control) { |
| 268 | + return control.untoggleColor == null || !control.untoggleColor.isBound(); |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + public StyleableProperty<Paint> getStyleableProperty(JFXToggleButton control) { |
| 273 | + return control.unToggleColorProperty(); |
| 274 | + } |
| 275 | + }; |
| 276 | + |
| 277 | + private static final CssMetaData<JFXToggleButton, Paint> TOGGLE_LINE_COLOR = |
| 278 | + new CssMetaData<>("-jfx-toggle-line-color", |
| 279 | + PaintConverter.getInstance(), Color.valueOf("#77C2BB")) { |
| 280 | + @Override |
| 281 | + public boolean isSettable(JFXToggleButton control) { |
| 282 | + return control.toggleLineColor == null || !control.toggleLineColor.isBound(); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public StyleableProperty<Paint> getStyleableProperty(JFXToggleButton control) { |
| 287 | + return control.toggleLineColorProperty(); |
| 288 | + } |
| 289 | + }; |
| 290 | + |
| 291 | + private static final CssMetaData<JFXToggleButton, Paint> UNTOGGLE_LINE_COLOR = |
| 292 | + new CssMetaData<>("-jfx-untoggle-line-color", |
| 293 | + PaintConverter.getInstance(), Color.valueOf("#999999")) { |
| 294 | + @Override |
| 295 | + public boolean isSettable(JFXToggleButton control) { |
| 296 | + return control.untoggleLineColor == null || !control.untoggleLineColor.isBound(); |
| 297 | + } |
| 298 | + |
| 299 | + @Override |
| 300 | + public StyleableProperty<Paint> getStyleableProperty(JFXToggleButton control) { |
| 301 | + return control.unToggleLineColorProperty(); |
| 302 | + } |
| 303 | + }; |
| 304 | + |
| 305 | + private static final CssMetaData<JFXToggleButton, Number> SIZE = |
| 306 | + new CssMetaData<>("-jfx-size", |
| 307 | + StyleConverter.getSizeConverter(), 10.0) { |
| 308 | + @Override |
| 309 | + public boolean isSettable(JFXToggleButton control) { |
| 310 | + return !control.size.isBound(); |
| 311 | + } |
| 312 | + |
| 313 | + @Override |
| 314 | + public StyleableProperty<Number> getStyleableProperty(JFXToggleButton control) { |
| 315 | + return control.sizeProperty(); |
| 316 | + } |
| 317 | + }; |
| 318 | + private static final CssMetaData<JFXToggleButton, Boolean> DISABLE_VISUAL_FOCUS = |
| 319 | + new CssMetaData<>("-jfx-disable-visual-focus", |
| 320 | + BooleanConverter.getInstance(), false) { |
| 321 | + @Override |
| 322 | + public boolean isSettable(JFXToggleButton control) { |
| 323 | + return control.disableVisualFocus == null || !control.disableVisualFocus.isBound(); |
| 324 | + } |
| 325 | + |
| 326 | + @Override |
| 327 | + public StyleableBooleanProperty getStyleableProperty(JFXToggleButton control) { |
| 328 | + return control.disableVisualFocusProperty(); |
| 329 | + } |
| 330 | + }; |
| 331 | + |
| 332 | + private static final CssMetaData<JFXToggleButton, Boolean> DISABLE_ANIMATION = |
| 333 | + new CssMetaData<>("-jfx-disable-animation", |
| 334 | + BooleanConverter.getInstance(), false) { |
| 335 | + @Override |
| 336 | + public boolean isSettable(JFXToggleButton control) { |
| 337 | + return control.disableAnimation == null || !control.disableAnimation.isBound(); |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public StyleableBooleanProperty getStyleableProperty(JFXToggleButton control) { |
| 342 | + return control.disableAnimationProperty(); |
| 343 | + } |
| 344 | + }; |
| 345 | + |
| 346 | + private static final List<CssMetaData<? extends Styleable, ?>> CHILD_STYLEABLES; |
| 347 | + |
| 348 | + static { |
| 349 | + final List<CssMetaData<? extends Styleable, ?>> styleables = |
| 350 | + new ArrayList<>(ToggleButton.getClassCssMetaData()); |
| 351 | + Collections.addAll(styleables, |
| 352 | + SIZE, |
| 353 | + TOGGLE_COLOR, |
| 354 | + UNTOGGLE_COLOR, |
| 355 | + TOGGLE_LINE_COLOR, |
| 356 | + UNTOGGLE_LINE_COLOR, |
| 357 | + DISABLE_VISUAL_FOCUS, |
| 358 | + DISABLE_ANIMATION |
| 359 | + ); |
| 360 | + CHILD_STYLEABLES = Collections.unmodifiableList(styleables); |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + @Override |
| 365 | + public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() { |
| 366 | + return getClassCssMetaData(); |
| 367 | + } |
| 368 | + |
| 369 | + public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() { |
| 370 | + return StyleableProperties.CHILD_STYLEABLES; |
| 371 | + } |
| 372 | + |
| 373 | +} |
0 commit comments