import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.concurrent.ExecutionException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;


/**
 * @author xye002
 *
 */
public class SWWorker extends JFrame {
	private JPanel contentPane;
	private JButton computeButton, counterButton;
	private JTextArea computeText, counterText;
	private JScrollPane computeScrollPane, counterScrollPane;
    private int counter=0;
    private int computeCount = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SWWorker frame = new SWWorker();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SWWorker() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		counterButton = new JButton("counter");
        counterText = new JTextArea();
        counterScrollPane = new JScrollPane(counterText);
        computeButton = new JButton("compute");
        computeText = new JTextArea();
        computeScrollPane = new JScrollPane(computeText);

        JPanel left = new JPanel();
        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
        left.add(counterButton);
        left.add(counterScrollPane);
        JPanel right = new JPanel();
        right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
        right.add(computeButton);
        right.add(computeScrollPane);
        
        counterButton.setText("counter");
        counterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	counter++;
                counterText.setText(counter + "");
            }
        });

        computeButton.setText("compute");
        computeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	// create a worker object
            	MySwingWorker msw = new MySwingWorker();
            	
            	// start the worker object
                msw.execute(); 
            }
        });
        
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
		contentPane.add(left);
		contentPane.add(Box.createRigidArea(new Dimension(10, 0)));
		contentPane.add(right);
		setContentPane(contentPane);
	}
	
	/*
	 * The class carries out the task when the button is clicked.
	 */
    class MySwingWorker extends SwingWorker<Double, Double> {

    	// This method carries out the task in background.
        @Override
        public Double doInBackground() {
            double x = 1.9999999999999;
            double y = 9.9999999999999999;
            double z = 0;

            long i = 9;
            while (i > 0) {
                z = x * y + z;
                i--;
                publish(new Double(z)); // output intermediate value
                try {
                    Thread.sleep(5000); // use sleep to simulate a long computation.
                } catch (InterruptedException ex) {
                	System.out.println(ex.toString());
                }
            }
            return new Double(z);
        }

        // This method displays the intermediate results.
        @Override
        protected void process(java.util.List<Double> numbers) {
            Double num = numbers.get(numbers.size() - 1);
            computeText.append(num.toString()+"\n");
        }

        // This method is called when the task of the SwingWorker object is completed.
        @Override
        protected void done() {
            try {
                Double value = super.get();
                computeText.append(value.toString()+"\n");
                computeText.append("done "+computeCount+"\n");
                computeCount++;
            } catch (InterruptedException ex) {
            	System.out.println(ex.toString());
            } catch (ExecutionException ex) {
            	System.out.println(ex.toString());
            }

        }
    }                                  

}
