package ru.jnano.swing.bsaf;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.validation.ValidationResultModel;
import javax.swing.Action;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskEvent;
import org.jdesktop.application.TaskListener;
import ru.jnano.swing.ErrorDialog;
public class ControlTaskJPanel extends JPanel {
private final String PROP_ERROR_VALIDATE="controltaskjpanel.errorvalidate";
private final String PROP_INFORM="controltaskjpanel.inform";
private final String PROP_RUN_TASK="controltaskjpanel.runtask";
private final String PROP_CANCELL_TASK="controltaskjpanel.cancelltask";
private final String PROP_RUNNED_TASK="controltaskjpanel.runnedtask";
private final String PROP_ERROR_TASK="controltaskjpanel.errortask";
private final String PROP_INTERRUPT_TASK="controltaskjpanel.interrupttask";
private JProgressBar progressBar;
private JButton btnCancell;
private JButton btnRun;
private ValidationResultModel validationResultModel;
private ResourceMap res;
public ControlTaskJPanel(Action runAction, Action cancellAction,
final ValidationResultModel validationResultModel, Application application) {
this.validationResultModel=validationResultModel;
this.res = application.getContext().getResourceMap();
setLayout(new FormLayout(new ColumnSpec[] {
FormFactory.RELATED_GAP_COLSPEC,
ColumnSpec.decode("default:grow"),
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,},
new RowSpec[] {
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,}));
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
add(progressBar, "2, 2");
btnCancell = new JButton(cancellAction);
btnCancell.setEnabled(false);
add(btnCancell, "4, 2");
btnRun = new JButton(runAction);
add(btnRun, "6, 2");
if (validationResultModel!=null){
validationResultModel.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("result")){
updateValidateStatus();
}
}
});
updateValidateStatus();
}
}
private void updateValidateStatus(){
if (validationResultModel.hasErrors()){
progressBar.setString(res.getString(PROP_ERROR_VALIDATE));
btnRun.setEnabled(false);
}else{
progressBar.setString(res.getString(PROP_INFORM));
btnRun.setEnabled(true);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void controlTask(Task task){
task.addTaskListener(new LocalTaskListener());
}
private class LocalTaskListener implements TaskListener<Object, Object>{
@Override
public void doInBackground(TaskEvent<Void> arg0) {
progressBar.setString(res.getString(PROP_RUN_TASK));
progressBar.setIndeterminate(true);
btnCancell.setEnabled(true);
}
@Override
public void cancelled(TaskEvent<Void> arg0) {
progressBar.setString(res.getString(PROP_CANCELL_TASK));
}
@Override
public void failed(TaskEvent<Throwable> event) {
progressBar.setString(res.getString(PROP_ERROR_TASK));
ErrorDialog dialog = new ErrorDialog(event.getValue());
dialog.setVisible(true);
}
@Override
public void succeeded(TaskEvent<Object> arg0) {
progressBar.setString(res.getString(PROP_RUNNED_TASK));
}
@Override
public void finished(TaskEvent<Void> arg0) {
progressBar.setIndeterminate(false);
btnCancell.setEnabled(false);
}
@Override
public void interrupted(TaskEvent<InterruptedException> arg0) {
progressBar.setString(res.getString(PROP_INTERRUPT_TASK));
}
@Override
public void process(TaskEvent<List<Object>> arg0) {
// TODO Auto-generated method stub
}
}
}