Exemplo n.º 1
0
    public void setTask(Task<?> task) {
      if (_task != null) {
        _task.cancel();
        _animation.stop();
        visibleProperty().unbind();
        log.info("Task Canceled");
      }

      _task = task;
      if (_task != null) {
        visibleProperty().bind(task.runningProperty());
        _task
            .runningProperty()
            .addListener(
                o -> {
                  SimpleBooleanProperty running = (SimpleBooleanProperty) o;
                  if (running.get()) {
                    _animation.play();
                  } else {
                    _animation.stop();
                  }
                });

        task.setOnFailed(
            e -> {
              log.error(_task.getMessage());
              setTask(null);
            });
      }
    }
Exemplo n.º 2
0
  /** Función encargada de descargar la actualización */
  private void downloadupdate() {
    // El usuario aceptó la actualización entonces se le pide seleccione un directirio para guardar
    final File file = new DirectoryChooser().showDialog(splashStage);

    // Creamos el archivo para escribir.
    final File filetowrite = new File(file.getAbsolutePath() + "/" + FILE_NAME);

    // Eliminamos los botones de control.
    splashLayout.getChildren().remove(updateLayout);

    // Animamos la barra de progreso.
    progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);

    // Tarea encargada de realizar la descarga.
    Task download =
        new Task() {
          @Override
          protected Object call() throws Exception {
            updateMessage(". . . D O W N L O A D I N G . . ");

            // Creamos el objeto que contendrá el enlace.
            URL source = new URL(checkupdates.get());
            if (source != null) {
              // Copiamos el arhcivo del enlace.
              Files.copy(
                  source.openStream(), filetowrite.toPath(), StandardCopyOption.REPLACE_EXISTING);
              updateMessage(" READY ");
              Thread.sleep(SHORT_WAIT_MILLIS);
            } else {
              cancel();
            }
            return null;
          }
        };

    // Permitimos que la tarea maneje el texto del splash
    status.textProperty().bind(download.messageProperty());

    // Si se ha terminado la descarga.
    download.setOnSucceeded(
        event -> {

          // Tarea encargada de mostrar que se a terminado la descarga.
          Task timer =
              new Task() {
                @Override
                protected Object call() throws Exception {
                  updateMessage("PLEASE START THE NEW VERSION");
                  Thread.sleep(SHORT_WAIT_MILLIS);
                  return null;
                }
              };

          // Permitimos que la tarea maneje el texto del splash
          status.textProperty().bind(timer.messageProperty());

          // cuando termine la tarea cerramos la aplicación.
          timer.setOnSucceeded(event1 -> System.exit(0));

          // Mostramos animación de la barra de progreso.
          showFinishProgress(timer);
        });

    // Si ha fallado la descarga.
    download.setOnFailed(
        event -> {

          // Tarea encargada de mostrar que ha fallado la descarga.
          Task timer =
              new Task() {
                @Override
                protected Object call() throws Exception {
                  updateMessage("Descarga Fallida");
                  Thread.sleep(SHORT_WAIT_MILLIS);
                  return null;
                }
              };

          // Permitimos que la tarea maneje el texto del splash
          status.textProperty().bind(timer.messageProperty());

          timer.setOnSucceeded(
              event1 -> {
                splashStage.close();
                isready.set(true);
              });
          // Mostramos animación de la barra de progreso.
          showFinishProgress(timer);
        });

    // Creamos el hilo y lo iniciamos.
    new Thread(download).start();
  }
Exemplo n.º 3
0
  /** Función encargada de revisar si existen actualizaciones. */
  private void checkforupdates() {
    /**
     * Esta Función descarga un archivo txt que contiene la información y enlace de descarga de las
     * distintas versiones de la aplicación, compara la versión del programa y si encuentra una
     * versión mas reciente en el archivo manda llamar a la función de descarga.
     */

    // Tarea encargada de revisar si hay actualización.
    checkupdates =
        new Task<String>() {
          @Override
          protected String call() throws Exception {

            // Avisamos al usuario que se van a revisar las actualziaciones.
            updateMessage("CHECKING FOR UPDATES");
            Thread.sleep(500);

            // Creamos una dirección para el archivo de texto.
            URL updatefile = new URL(UPDATE_TXT);

            // Si existe el archivo de texto.
            if (updatefile != null) {
              // Creamos un lector.
              BufferedReader in =
                  new BufferedReader(new InputStreamReader(updatefile.openStream()));

              // Buffer.
              String str;
              // Para cada linea.
              while ((str = in.readLine()) != null) {
                // Partimos cada linea "repositorio; version; URL" notese que el separador es "; "
                String[] buf = str.split("; ");

                // Si la versión es mayor y el repositorio corresponde entonces preguntamos si se
                // desea descargar.
                if ((Double.parseDouble(buf[1]) > VERSION)
                    && (buf[0].equalsIgnoreCase(REPOSITORY))) {
                  updateMessage(
                      " A NEW VERSION OF TRUDISP IS AVAILABLE\n V_: " + buf[1] + "\n DOWNLOAD?");
                  isupdating = true;
                  return buf[2];
                } else {
                  isupdating = false;
                }
              }
            } else // No se ha podido establecer conexión con el archivo de texto.
            {
              updateMessage("CONNECTION ERROR");
              Thread.sleep(SHORT_WAIT_MILLIS);
              isupdating = false;
              cancel();
            }
            return null;
          }
        };

    // Permitimos que la tarea maneje los mensajes de la barra de estado.
    status.textProperty().bind(checkupdates.messageProperty());

    // Si se ha terminado la tarea
    checkupdates.setOnSucceeded(
        event -> {
          if (isupdating) {
            // Agregamos los botones de control.
            splashLayout.getChildren().add(updateLayout);
            splashStage.sizeToScene();
            progressBar.setProgress(0);
          } else {
            // Cerramos el splash y avisamos que se ha terminado.
            splashStage.close();
            isready.set(true);
          }
        });

    // Si ha fallado latarea.
    checkupdates.setOnFailed(
        event -> {
          splashStage.close();
          isready.set(true);
        });

    // Creamos e iniciamos el hilo.
    new Thread(checkupdates).start();
  }