Git Product home page Git Product logo

swing-tasks's Introduction

swing-tasks

This is a small library that aims to help with executing tasks from swing applications. It consist of a set of classes that build around the SwingWorker class.

Dependencies

The only dependency is to the slf4j logging api.

Compile

Use maven to compile the sources:

mvn install

Usage

The class TaskManager is the main entry point. Usually, you would use this as an application wide singleton. The TaskManager is used to submit tasks that implement either Task, Callable or Runnable. It can query currently active tasks and can be used to register listeners that receive events from running tasks.

TaskManager taskManager = new TaskManagerImpl();

For swing related tasks, it is recommended to implement the Task interface, as it mimics the SwingWorker interface and thus provides some features regarding swing. The TaskManager creates a TaskControl object for each Task that provides the usual control methods and additionally defines a method to get the TaskContext of the current task. The TaskContext can be used to get some information about the task and to add listeners that receive events for this task only.

If a Task is submitted a TaskControl object is received in response. But task has not been started yet and is still in state PENDING. In order to start the task, call execute() on the TaskControl object. The idea is to be able to register listeners for exactly this task prior to starting it. Example:

Task<Long, Long> task = new LongTask();
TaskControl<Long> control = taskManager.create(task);
control.getContext().addListener(new TaskListener() {
  @Override
  public void stateChanged(ChangeEvent<State> event) {
    log.info(">>> State: " + event.getOldValue() + " => " + event.getNewValue());
    log.info("Started: " + event.getSource().getStartedTimestamp());
  }

  @Override
  public void progressChanged(ChangeEvent<Integer> event) {
    log.info(">>> Progress: " + event.getOldValue() + " => " + event.getNewValue());
  }

  @Override
  public void phaseChanged(ChangeEvent<String> event) {
    log.info(">>> Phase: " + event.getOldValue() + " => " + event.getNewValue());
  }
});
Long value = control.waitFor();
log.info("Waited for task: " + value);

The package org.eknet.swing.task.ui provides some simple swing ui classes for displaying running task and a default glass pane. You can use the glass pane with a JFrame. It will popup if any task of mode BLOCKING is executed and shows a list of tasks currently running -- as well as a button to cancel them.

JFrame frame = new JFrame("my app");
frame.setGlassPane(new TaskGlassPane(taskManager));

swing-tasks's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.