package net.sf.jpluck.ui;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.ConversionListener;
import net.sf.jpluck.palm.RecordEvent;
import net.sf.jpluck.palm.RecordListener;
import net.sf.jpluck.spider.SpiderListener;
import net.sf.jpluck.util.JEditTextAreaHandler;
import net.sf.jpluck.util.LogFormatter;
import net.sf.jpluck.util.LogUtil;
import org.syntax.jedit.JEditTextArea;
import org.syntax.jedit.TextAreaDefaults;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ConversionPane extends JTabbedPane implements SpiderListener, ConversionListener, RecordListener {
private JEditTextArea textArea;
private JEditTextAreaHandler logHandler;
private JLabel parsingLabel = new JLabel();
private JLabel retrievalLabel = new JLabel();
private JProgressBar parsingProgress = new JProgressBar();
private JProgressBar retrievalProgress = new JProgressBar();
private JTabbedPane tabbedPane = new JTabbedPane();
private LogUtil logUtil;
private StatusList statusList = new StatusList();
private volatile boolean running;
private int currentRecord;
private int idx;
public ConversionPane() {
initComponents();
}
public void setConversion(Conversion conversion) {
textArea.setText("");
net.sf.jpluck.jxl.Document[] documents = conversion.getDocuments();
String[] labels = new String[documents.length];
for (int i = 0; i < documents.length; i++) {
labels[i] = documents[i].getName();
}
statusList.setItems(labels);
for (int i = 0; i < labels.length; i++) {
statusList.updateState(i, StatusList.PENDING);
}
idx = 0;
}
public void conversionCompleted(net.sf.jpluck.jxl.Document jxlDocument, final int current, final int total,
final boolean success) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (!success) {
statusList.updateState(idx, StatusList.FAILED);
}
idx++;
if (current == total) {
retrievalLabel.setText("Completed");
//parsingLabel.setText("Completed");
textArea.setEditable(true);
}
}
});
}
public void conversionFinished() {
logUtil.removeHandler(logHandler);
}
public void conversionStarted(net.sf.jpluck.jxl.Document jxlDocument, int current, int total) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
retrievalProgress.setValue(0);
retrievalProgress.setMaximum(0);
retrievalProgress.setString(" ");
parsingProgress.setValue(0);
parsingProgress.setMaximum(0);
parsingProgress.setString(" ");
statusList.updateState(idx, StatusList.CONVERTING);
textArea.setEditable(false);
}
});
}
public void generationCompleted(net.sf.jpluck.jxl.Document jxlDocument) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingLabel.setText("Document written");
parsingProgress.setString("Done!");
statusList.updateState(idx, StatusList.COMPLETED);
}
});
}
public void generationStarted(final net.sf.jpluck.jxl.Document jxlDocument) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
retrievalLabel.setText("Completed");
retrievalProgress.setString(" ");
parsingLabel.setText("Writing " + jxlDocument.getName());
parsingProgress.setString(" ");
currentRecord = 0;
parsingProgress.setValue(0);
}
});
}
public void parsingCompleted(String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingProgress.setValue(parsingProgress.getValue() + 1);
parsingProgress.setString(parsingProgress.getValue() + " of " + parsingProgress.getMaximum());
}
});
}
public void parsingScheduled(String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingProgress.setMaximum(parsingProgress.getMaximum() + 1);
if (parsingProgress.getValue() > 0) {
parsingProgress.setString(parsingProgress.getValue() + " of " +
parsingProgress.getMaximum());
}
}
});
}
public void parsingStarted(final String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingLabel.setText("Parsing " + uri);
}
});
}
public void recordCompleted(RecordEvent ev) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingProgress.setValue(currentRecord);
}
});
}
public void recordStarted(final RecordEvent ev) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
currentRecord++;
parsingProgress.setMaximum(ev.getRecordCount());
parsingProgress.setString(currentRecord + " of " + ev.getRecordCount());
}
});
}
public void retrievalCompleted(String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
retrievalProgress.setValue(retrievalProgress.getValue() + 1);
retrievalProgress.setString(retrievalProgress.getValue() + " of " +
retrievalProgress.getMaximum());
}
});
}
public void retrievalScheduled(String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
retrievalProgress.setMaximum(retrievalProgress.getMaximum() + 1);
retrievalProgress.setString(retrievalProgress.getValue() + 1 + " of " +
retrievalProgress.getMaximum());
parsingLabel.setText("Waiting...");
}
});
}
public void retrievalStarted(final String uri) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
retrievalLabel.setText("Retrieving " + uri);
}
});
}
public void statusMessage(final String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parsingLabel.setText(message);
}
});
}
private void initComponents() {
retrievalLabel.setText("Retrieving...");
retrievalProgress.setStringPainted(true);
parsingLabel.setText(String.valueOf((char) 160));
parsingProgress.setStringPainted(true);
statusList.setVisibleRowCount(14);
TextAreaDefaults defaults = TextAreaDefaults.getDefaults();
defaults.font = statusList.getFont();
defaults.eolMarkers = false;
defaults.lineHighlight = false;
defaults.caretVisible = false;
defaults.cols = 40;
defaults.rows = 16;
textArea = new JEditTextArea(defaults);
textArea.setEditable(false);
textArea.setBorder(UIManager.getBorder("TextField.border"));
logUtil = new LogUtil(LogUtil.DEFAULT_LOGGER_NAMES);
logHandler = new JEditTextAreaHandler(textArea, new LogFormatter());
logUtil.addHandler(logHandler);
FormLayout formLayout = new FormLayout("fill:250dlu:grow",
"pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, " +
"7dlu, fill:pref:grow");
PanelBuilder panelBuilder = new PanelBuilder(formLayout);
panelBuilder.setDefaultDialogBorder();
panelBuilder.add(retrievalLabel, "1,1");
panelBuilder.add(retrievalProgress, "1,3");
panelBuilder.add(parsingLabel, "1,5");
panelBuilder.add(parsingProgress, "1,7");
panelBuilder.add(new JScrollPane(statusList), "1,9");
addTab("Progress", panelBuilder.getPanel());
setMnemonicAt(0, 'P');
panelBuilder = new PanelBuilder(new FormLayout("fill:pref:grow", "fill:pref:grow"));
panelBuilder.setDefaultDialogBorder();
panelBuilder.add(textArea);
addTab("Log", panelBuilder.getPanel());
setMnemonicAt(1, 'L');
setSelectedIndex(0);
}
}