// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.*;

public class MainPanel extends JPanel {
  protected final JTextArea area = new JTextArea();
  protected final JButton runButton = new JButton("run");
  // protected transient SwingWorker<String, String> worker;
  protected final transient ProgressMonitor monitor; // = new ProgressMonitor(p, "message", "note", 0, 100);

  public MainPanel() {
    super(new BorderLayout(5, 5));
    monitor = new ProgressMonitor(this, "message", "note", 0, 100);
    area.setEditable(false);

    runButton.addActionListener(e -> {
      // System.out.println("actionPerformed() is EDT?: " + EventQueue.isDispatchThread());
      runButton.setEnabled(false);
      monitor.setProgress(0);
      SwingWorker<String, String> worker = new BackgroundTask() {
        @Override protected void process(List<String> chunks) {
          // System.out.println("process() is EDT?: " + EventQueue.isDispatchThread());
          if (isCancelled()) {
            return;
          }
          if (!isDisplayable()) {
            cancel(true);
            return;
          }
          for (String message: chunks) {
            monitor.setNote(message);
          }
        }

        @Override public void done() {
          // System.out.println("done() is EDT?: " + EventQueue.isDispatchThread());
          runButton.setEnabled(true);
          monitor.close();
          try {
            if (isCancelled()) {
              area.append("Cancelled\n");
            } else {
              area.append(get() + "\n");
            }
          } catch (InterruptedException | ExecutionException ex) {
            area.append("Interrupted\n");
          }
          area.setCaretPosition(area.getDocument().getLength());
        }
      };
      worker.addPropertyChangeListener(new ProgressListener(monitor));
      worker.execute();
    });

    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());
    box.add(runButton);
    add(new JScrollPane(area));
    add(box, BorderLayout.NORTH);
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    // frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class BackgroundTask extends SwingWorker<String, String> {
  @Override public String doInBackground() {
    // System.out.println("doInBackground() is EDT?: " + EventQueue.isDispatchThread());
    int current = 0;
    int lengthOfTask = 120; // list.size();
    while (current < lengthOfTask && !isCancelled()) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException ex) {
        return "Interrupted";
      }
      current++;
      setProgress(100 * current / lengthOfTask);
      publish(current + "/" + lengthOfTask);
    }
    return "Done";
  }
}

class ProgressListener implements PropertyChangeListener {
  private final ProgressMonitor monitor;

  protected ProgressListener(ProgressMonitor monitor) {
    this.monitor = monitor;
    this.monitor.setProgress(0);
  }

  @Override public void propertyChange(PropertyChangeEvent e) {
    String strPropertyName = e.getPropertyName();
    if ("progress".equals(strPropertyName)) {
      monitor.setProgress((Integer) e.getNewValue());
      Object o = e.getSource();
      if (o instanceof SwingWorker) {
        SwingWorker<?, ?> task = (SwingWorker<?, ?>) o;
        if (task.isDone() || monitor.isCanceled()) {
          task.cancel(true);
        }
      }
    }
  }
}

@@ -295,16 +295,22 @@
 %0Apublic 
+final 
 class Ma
@@ -342,202 +342,66 @@
   pr
-otected final JTextArea area = new JTextArea();%0A  protected final JButton runButton = new JButton(%22run%22);%0A  // protected transient SwingWorker%3CString, String%3E worker;%0A  protected final transient
+ivate MainPanel() %7B%0A    super(new BorderLayout(5, 5));%0A   
  Pro
@@ -420,20 +420,16 @@
  monitor
-; //
  = new P
@@ -443,17 +443,20 @@
 Monitor(
-p
+this
 , %22messa
@@ -484,161 +484,117 @@
 %0A%0A  
-public MainPanel() %7B%0A    super(new BorderLayout(5, 5));%0A    monitor = new ProgressMonitor(this, %22message%22, %22note%22, 0, 100);%0A    area.setEditable(false);%0A
+  JTextArea area = new JTextArea();%0A    area.setEditable(false);%0A%0A    JButton runButton = new JButton(%22run%22);
 %0A   
@@ -1005,32 +1005,35 @@
 ad());%0A         
+ //
  if (isCancelled
@@ -1039,32 +1039,35 @@
 d()) %7B%0A         
+ //
    return;%0A     
@@ -1062,32 +1062,35 @@
 eturn;%0A         
+ //
  %7D%0A          if 
@@ -3078,16 +3078,44 @@
 ground()
+ throws InterruptedException
  %7B%0A    /
@@ -3318,30 +3318,16 @@
 ed()) %7B%0A
-      try %7B%0A  
       Th
@@ -3346,88 +3346,8 @@
 0);%0A
-      %7D catch (InterruptedException ex) %7B%0A        return %22Interrupted%22;%0A      %7D%0A
     

convert field to local variable in constructor