package application;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;

public class TransformAction implements ActionListener {
	private JComponent transformButton = null;
	private JLabel statuslabel;
	
	public TransformAction(JLabel statuslabel) {
		this.statuslabel = statuslabel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		transformButton = (JComponent)e.getSource();
		transformButton.setEnabled(false);

		@SuppressWarnings("rawtypes")
		List<Class> tf_classes = new ArrayList<Class>();
		for(UITask tf: UI.ui_tf) {
			if(tf.check.isSelected()) {
				tf_classes.add(tf.t);
			}			
		}

		@SuppressWarnings("rawtypes")
		List<Class> tm_classes = new ArrayList<Class>();
		for(UITask tm: UI.ui_tm) {
			if(tm.check.isSelected()) {
				tm_classes.add(tm.t);
			}			
		}

		TD.setTasks(tf_classes, tm_classes);
		try {
			Thread t = new Thread(new Runnable() {
				@Override
				public void run() {
					TD.doTransform(TransformAction.this);
					done();
				}} );
			t.start();
		} finally {
//			if(b != null)
//				b.setEnabled(true);
		}
	}

	void progress(final String msg) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if(statuslabel != null)
					statuslabel.setText(msg);
			}
		});

	}

	void done() {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if(transformButton != null)
					transformButton.setEnabled(true);
			}
		});

	}
}
