3

I am writing a java library ( jar ) that contains numerous API's that will be called by an external software . One of the API will be a blocking API and will be performing an operation that can be quite lengthy .

I want a way to be able to provide regular feedback ( % completion ) of the operation to the client .

NOTE Since its a library I cannot use the Java Swing mechanism of a progress bar to get and update a progress percent . Everything i search somehow leads to that .

Is there anyway to do this ?

Answer Update

Many thanks to all who posted answers . It helped me gain a grasp . I exposed the following interfaces on my jar . The client worker thread will register and listen for my progress updates . Then it will update the progress bar accordingly .

void LongAPI(){
while{
do work ; 
fireprogress();
     }
}
public void addProgressListener(Listener listener) {
    listeners.add(listener);

}

public void removeProgressListener(Listener listener) {
    listeners.add(listener);
}

private void fireProgress(double pourcent) {
    for (Listener listener : listeners) {
        listener.setProgress(pourcent);
    }
}

The Listener is an abstract interface that all clients much implement . It has the setProgress property using which the client can update its UI as it wishes .

rockstar
  • 3,512
  • 6
  • 40
  • 63
  • *"I want a way to be able to provide regular feedback ( % completion ) of the operation to the client"* Does the API actually provide any means to query the completion? If so, it should be easy to use that from within a `SwingWorker` to update a progress bar. If not, the best you can do is an indeterminate progress bar. Either way it leads back to `JProgressBar`. So why is it not suitable? – Andrew Thompson Oct 18 '13 at 04:09
  • @AndrewThompson: My guess -- it's not suitable in the observed class, which does make sense since the observer will likely be displaying the data. It smells like all he needs is a good observer design pattern. – Hovercraft Full Of Eels Oct 18 '13 at 04:12
  • @rockstar to hold EDT rulles, because `regular feedback ( % completion...` is only about your code – mKorbel Oct 18 '13 at 05:59

2 Answers2

4

I would give it a PropertyChangeSupport object as well as addPropertyChangeListener(...) and removePropertyChangeListener(...) methods. Internally, I'd create bound properties with setters that fire the support object. This way external libraries can be notified in a standard way that the property of interest has changed. If it is a Swing based library, consider using specifically a SwingPropertyChangeSupport object so that all PropertyChangeListeners that have been added to the support object are notified on the Swing event thread.

For example, please have a look at the answer to these questions:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

Because you want to give progress to a client you can use a specific model for that.

One approach could be to return a BoundedRangeModel to the client.

 public BoundedRangeModel getProgressModel();

A client can then get it and

  • connect it directly to a ui component, e.g. JProgressBar or JSlider or whatever.
  • or connect it to his own model objects and listen to the changes
  • or just retrieve the state when the client needs it.

I would prefer the BoundedRangeModel over a PropertyChangeListener, because a BoundedRangeModel provides a getValueAdjusting() method. The client can ask if the value is adjusting to prevent unnecessary repaints.

René Link
  • 48,224
  • 13
  • 108
  • 140