|
| 1 | +#!/usr/bin/env jruby |
| 2 | +=begin |
| 3 | +Original Java source from: http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm |
| 4 | +/* |
| 5 | + * Copyright (c) 2011, 2012 Oracle and/or its affiliates. |
| 6 | + * All rights reserved. Use is subject to license terms. |
| 7 | + * |
| 8 | + * This file is available and licensed under the following license: |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions |
| 12 | + * are met: |
| 13 | + * |
| 14 | + * - Redistributions of source code must retain the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer. |
| 16 | + * - Redistributions in binary form must reproduce the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer in |
| 18 | + * the documentation and/or other materials provided with the distribution. |
| 19 | + * - Neither the name of Oracle nor the names of its |
| 20 | + * contributors may be used to endorse or promote products derived |
| 21 | + * from this software without specific prior written permission. |
| 22 | + * |
| 23 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 29 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 33 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + */ |
| 35 | +=end |
| 36 | + |
| 37 | +require 'jrubyfx' |
| 38 | + |
| 39 | +class MyTask < javafx.concurrent.Task |
| 40 | + def initialize(max_val=1000000) |
| 41 | + @max_val = max_val |
| 42 | + end |
| 43 | + |
| 44 | + def call |
| 45 | + puts "inside call" |
| 46 | + (1..@max_val).each do |i| |
| 47 | + if is_cancelled |
| 48 | + break |
| 49 | + end |
| 50 | + puts i |
| 51 | + updateProgress(i, @max_val) |
| 52 | + end |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +class MyService < javafx.concurrent.Service |
| 57 | + def initialize(bar) |
| 58 | + @bar = bar |
| 59 | + super() |
| 60 | + end |
| 61 | + |
| 62 | + def createTask |
| 63 | + MyTask.new.tap do |t| |
| 64 | + t.set_on_cancelled do |event| |
| 65 | + puts "task cancelled" |
| 66 | + end |
| 67 | + |
| 68 | + t.set_on_failed do |event| |
| 69 | + puts "task failed #{event.to_s}: #{@bar.progress}" |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +class ProgressBarWithServiceDemo < JRubyFX::Application |
| 76 | + def start(stage) |
| 77 | + with(stage, title: "Progress Bar Demo with binded Task", width: 200, |
| 78 | + height: 100) do |
| 79 | + layout_scene do |
| 80 | + vbox(15, alignment: Java::javafx.geometry.Pos::CENTER) do |
| 81 | + progress_bar(id: 'bar') |
| 82 | + button("restart") |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + stage['.button'].set_on_action { restart_task } |
| 88 | + bar = stage['#bar'] |
| 89 | + @my_service = MyService.new(bar) |
| 90 | + bar.progress_property.bind(@my_service.progress_property) |
| 91 | + |
| 92 | + @my_service.start |
| 93 | + stage.show |
| 94 | + end |
| 95 | + |
| 96 | + def restart_task |
| 97 | + @my_service.restart |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +ProgressBarDemo.launch |
0 commit comments