package net.sf.jpluck.ui;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.ConversionAdapter;
import net.sf.jpluck.swing.GUIUtil;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class ConversionFrame extends JFrame {
private Conversion conversion;
private CancelAction cancelAction=new CancelAction();
public ConversionFrame(Conversion conversion, String title) {
super();
this.conversion = conversion;
setTitle(title);
initComponents();
}
private void initComponents() {
try {
setIconImage(ImageIO.read(this.getClass().getClassLoader().getResource("net/sf/jpluck/res/jpluck16.gif")));
} catch (IOException e) {
}
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setResizable(false);
ConversionPane conversionPane = new ConversionPane();
conversion.addConversionListener(conversionPane);
conversion.addSpiderListener(conversionPane);
conversion.addRecordListener(conversionPane);
conversionPane.setConversion(conversion);
ButtonBarBuilder bbb=new ButtonBarBuilder();
bbb.addGlue();
bbb.addGridded(new JButton(cancelAction));
bbb.addGlue();
GUIUtil.configureEscapeKey(getRootPane(), cancelAction);
FormLayout formLayout = new FormLayout("fill:pref:grow", "fill:pref:grow, 7dlu, pref");
PanelBuilder panelBuilder = new PanelBuilder(formLayout);
panelBuilder.setDefaultDialogBorder();
panelBuilder.add(conversionPane, "1,1");
panelBuilder.add(bbb.getPanel(), "1,3");
setContentPane(panelBuilder.getPanel());
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
cancelAction.actionPerformed(null);
}
});
conversion.addConversionListener(new ConversionAdapter() {
public void conversionFinished() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
cancelAction.putValue(Action.NAME, "Close");
}
});
}
});
}
private class CancelAction extends AbstractAction implements Runnable {
public CancelAction() {
putValue(Action.NAME, "Cancel");
}
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
});
new Thread(this).start();
}
public void run() {
conversion.cancel();
setCursor(Cursor.getDefaultCursor());
hide();
}
}
public void cancel() {
cancelAction.actionPerformed(null);
}
}