* fixed bug: disabled progress indicator although there is still some formatting going on in the background

This commit is contained in:
Reinhard Pointner 2009-07-19 23:38:20 +00:00
parent 78b77034b1
commit 472ed8aac0
1 changed files with 20 additions and 7 deletions

View File

@ -21,8 +21,9 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
@ -81,6 +82,8 @@ public class EpisodeFormatDialog extends JDialog {
private ExecutorService previewExecutor = createPreviewExecutor(); private ExecutorService previewExecutor = createPreviewExecutor();
private RunnableFuture<String> currentPreviewFuture = null;
private ProgressIndicator progressIndicator = new ProgressIndicator(); private ProgressIndicator progressIndicator = new ProgressIndicator();
private JTextField editor = new JTextField(); private JTextField editor = new JTextField();
@ -102,6 +105,7 @@ public class EpisodeFormatDialog extends JDialog {
super(owner, "Episode Format", ModalityType.DOCUMENT_MODAL); super(owner, "Episode Format", ModalityType.DOCUMENT_MODAL);
editor.setFont(new Font(MONOSPACED, PLAIN, 14)); editor.setFont(new Font(MONOSPACED, PLAIN, 14));
progressIndicator.setVisible(false);
// bold title label in header // bold title label in header
JLabel title = new JLabel(this.getTitle()); JLabel title = new JLabel(this.getTitle());
@ -350,7 +354,7 @@ public class EpisodeFormatDialog extends JDialog {
private ExecutorService createPreviewExecutor() { private ExecutorService createPreviewExecutor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), new DefaultThreadFactory("PreviewFormatter")) { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1), new DefaultThreadFactory("PreviewFormatter")) {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
@ -368,7 +372,7 @@ public class EpisodeFormatDialog extends JDialog {
// log access of potentially unsafe method // log access of potentially unsafe method
Logger.getLogger(getClass().getName()).warning("Thread was forcibly terminated"); Logger.getLogger(getClass().getName()).warning("Thread was forcibly terminated");
} }
} catch (InterruptedException e) { } catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Thread was not terminated", e); Logger.getLogger(getClass().getName()).log(Level.WARNING, "Thread was not terminated", e);
} }
@ -388,7 +392,7 @@ public class EpisodeFormatDialog extends JDialog {
// check syntax in foreground // check syntax in foreground
final ExpressionFormat format = new ExpressionFormat(editor.getText().trim()); final ExpressionFormat format = new ExpressionFormat(editor.getText().trim());
// format in background // activate delayed to avoid flickering when formatting takes only a couple of milliseconds
final Timer progressIndicatorTimer = TunedUtilities.invokeLater(400, new Runnable() { final Timer progressIndicatorTimer = TunedUtilities.invokeLater(400, new Runnable() {
@Override @Override
@ -397,7 +401,8 @@ public class EpisodeFormatDialog extends JDialog {
} }
}); });
previewExecutor.execute(new SwingWorker<String, Void>() { // create new worker
currentPreviewFuture = new SwingWorker<String, Void>() {
@Override @Override
protected String doInBackground() throws Exception { protected String doInBackground() throws Exception {
@ -430,11 +435,19 @@ public class EpisodeFormatDialog extends JDialog {
preview.setVisible(preview.getText().trim().length() > 0); preview.setVisible(preview.getText().trim().length() > 0);
editor.setForeground(defaultColor); editor.setForeground(defaultColor);
// stop progress indicator from becoming visible, if we have been fast enough
progressIndicatorTimer.stop(); progressIndicatorTimer.stop();
// hide progress indicator, if this still is the current worker
if (this == currentPreviewFuture) {
progressIndicator.setVisible(false); progressIndicator.setVisible(false);
} }
} }
}); }
};
// submit new worker
previewExecutor.execute(currentPreviewFuture);
} catch (ScriptException e) { } catch (ScriptException e) {
// incorrect syntax // incorrect syntax
status.setText(ExceptionUtilities.getRootCauseMessage(e)); status.setText(ExceptionUtilities.getRootCauseMessage(e));