package dnb.ui;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Logger;
import javax.swing.JTextField;
import dnb.analyze.MatchResult;
import dnb.analyze.nfo.NfoField;
import dnb.analyze.nfo.NfoLyzeResult;
import dnb.analyze.nfo.NfoLyzer;
import dnb.data.filetree.NfoFile;
public class DataValidationController extends WindowAdapter implements ActionListener, FocusListener {
private final DataValidationDialog dialog;
private final DataFieldLegendController fieldLegendController;
private Object signal = new Object();
private static final Logger LOGGER = Logger.getLogger(NfoLyzer.class.getName());
private boolean updateResult = false;
private NfoLyzeResult result;
private NfoFile file;
DataValidationController(DataValidationDialog dialog) {
super();
this.dialog = dialog;
// add action listeners
dialog.getBtnDiscard().addActionListener(this);
dialog.getBtnSave().addActionListener(this);
dialog.getJxhlFilename().addActionListener(this);
dialog.addWindowListener(this);
// legend controller for building the legend
dialog.getPnlData().setLayout(new GridBagLayout()); // force layout for data panel
fieldLegendController = new DataFieldLegendController(dialog.getPnlData(), this);
}
private void clearIdTagData() {
dialog.getLblArtist().setText("");
dialog.getLblAlbum().setText("");
dialog.getLblYear().setText("");
dialog.getLblGenre().setText("");
dialog.getLblCode().setText("");
dialog.getLblNumber().setText("");
dialog.getLblSuffix().setText("");
}
private void doUpdate(final NfoFile file,
final NfoLyzeResult result) {
// init nfo type & file name
dialog.getLblNfoType().setText(result.getNfoType().toString());
dialog.getJxhlFilename().setText(file.getName());
// path & nfo fields
dialog.getTxtContents().init(result, MatchResult.Source.NFO);
dialog.getTxtPath().init(result, MatchResult.Source.PATH);
clearIdTagData(); // id tag data
// fields
fieldLegendController.loadResults(result.getMatches());
dialog.getBtnSave().setEnabled(true);
dialog.getBtnSave().requestFocusInWindow();
dialog.getBtnDiscard().setEnabled(true);
dialog.pack();
}
public boolean update(final NfoFile file, final NfoLyzeResult result) {
this.result = result;
this.file = file;
this.updateResult = false; // just 2 b sure
if (EventQueue.isDispatchThread()) {
doUpdate(file, result);
return updateResult;
} else {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override public void run() {
doUpdate(file, result);
}
});} catch (Exception e) {
return false;
}
// we 're here => controller initialized => wait for signal
synchronized(signal) {
try {
signal.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return updateResult;
}
}
@Override public void windowClosing(WindowEvent e) {
updateResult = false;
disableButtonsAndSignal();
}
@Override public void actionPerformed(ActionEvent e) {
if (e.getSource() == dialog.getBtnDiscard()) {
updateResult = false;
disableButtonsAndSignal();
} else if (e.getSource() == dialog.getBtnSave()) {
fieldLegendController.storeResults(result.getMatches());
updateResult = true;
disableButtonsAndSignal();
} else if (e.getSource() == dialog.getJxhlFilename()) {
try {
// XXX platform dependent: read from program config
Runtime.getRuntime().exec(
"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe \"" + file.pathString() + "\"");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
LOGGER.severe("Unknown event source:" + e.getSource());
}
}
private void disableButtonsAndSignal() {
dialog.getBtnSave().setEnabled(false);
dialog.getBtnDiscard().setEnabled(false);
synchronized(signal) {
signal.notifyAll();
}
}
@Override public void focusGained(FocusEvent e) {
NfoField field = (NfoField) fieldLegendController.lookup((JTextField) e.getSource());
dialog.getTxtContents().highlight(field);
dialog.getTxtPath().highlight(field);
}
@Override public void focusLost(FocusEvent e) {}
}