it.tidalwave.netbeans.swing.OpenIDEWorker Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://openbluesky.java.net
* SCM: https://bitbucket.org/tidalwave/openbluesky-src
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.swing;
import java.util.logging.Logger;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.ErrorManager;
/*******************************************************************************
*
* This class is an extension to SwingWorker which provides the following
* additional features:
*
*
* - notifies exceptions to the OpenIDE runtime;
* - supports progress management with values in a custom range which
* can be different from 0..100;
* - can automatically manage a ProgressHandle;
*
* @author Fabrizio Giudici
* @version $Id$
*
******************************************************************************/
public abstract class OpenIDEWorker extends SwingWorker
{
private final static String CLASS = OpenIDEWorker.class.getName();
private final static Logger logger = Logger.getLogger(CLASS);
private final String name;
private ProgressHandle progressHandle;
private int workUnits;
private int progress2;
private boolean switchedToDeterminate;
/***************************************************************************
*
* Convenience method to start an OpenIDEWorker
.
*
**************************************************************************/
public static void post (final OpenIDEWorker worker)
{
logger.info("post(" + worker + ")");
worker.startProgressHandle();
worker.execute();
}
/***************************************************************************
*
* Creates a new instance with no progress handle.
*
**************************************************************************/
public OpenIDEWorker()
{
this(null);
}
/***************************************************************************
*
* Creates a new instance with a progress handle of the given name.
* The progress handle will start in indeterminate mode, switching to
* determinate whenever setProgress()
or
* setProgress2()
are called.
*
* @param name the name of the progress handle
*
**************************************************************************/
public OpenIDEWorker (final String name)
{
this.name = name;
addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange (final PropertyChangeEvent event)
{
logger.finer("propertyChange(" + event + ") - progressHandle: " + progressHandle);
if ((progressHandle != null) && ("progress".equals(event.getPropertyName())))
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// In the meantime it could have been set to null.
final ProgressHandle progressHandle2 = progressHandle;
if (progressHandle2 != null)
{
final int progress = (Integer)event.getNewValue();
if (!switchedToDeterminate)
{
logger.finer(">>>> switchToDeterminate(" + progress+ ")");
progressHandle2.switchToDeterminate(progress);
switchedToDeterminate = true;
}
else
{
logger.finer(">>>> progress(" + progress+ ")");
progressHandle2.progress(progress);
}
}
}
});
}
}
});
}
/***************************************************************************
*
* This method should be overridden in place of done()
. It
* provides automatically exception management and tidy cleanup of the
* progress handle.
*
**************************************************************************/
@Override
public final void done()
{
try
{
done2();
}
catch (Exception e)
{
ErrorManager.getDefault().notify(e);
}
if (progressHandle != null)
{
progressHandle.finish();
progressHandle = null;
}
}
/***************************************************************************
*
*
**************************************************************************/
public abstract void done2()
throws Exception;
/***************************************************************************
*
*
**************************************************************************/
public final void setWorkUnits (final int workUnits)
{
if (workUnits <= 0)
{
throw new IllegalArgumentException("workUnits must be > 0");
}
this.workUnits = workUnits;
}
/***************************************************************************
*
* Similar to setProgress()
, accepts a parameter in a custom
* range rather than 0..100. The upper limit must be set with a call to
* setWorkUnits()
.
*
* @param progress2 the progress
*
**************************************************************************/
public final void setProgress2 (int progress2)
{
this.progress2 = progress2;
setProgress(progress2 / workUnits);
}
/***************************************************************************
*
* Increments the current progress.
*
**************************************************************************/
public final void incrementProgress2()
{
setProgress2(progress2 + 1);
}
/***************************************************************************
*
*
**************************************************************************/
private void startProgressHandle()
{
if (name != null)
{
progressHandle = ProgressHandleFactory.createHandle(name);
progressHandle.start();
}
}
}