package jasext.flatfileserver;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import jas2.util.*;
import jas2.jds.module.*;
import hepjas.analysis.*;
import tablelayout.*;
public class LocalFlatFileDIM implements LocalDIM
{
public JASWizardPage getSetupPage()
{
m_wizardPage = new FlatFileWizardPage();
return m_wizardPage;
}
public void setLocalJobBuilder(LocalJobBuilder ljb)
{
m_ljb = ljb;
}
public String toString()
{
return "Flat file";
}
public EventSource openDataSet(String fileName)
{
this.fileName = fileName;
final FlatFileEventSource ffes = new FlatFileEventSource();
ffes.setFileName(fileName);
ffes.setDelim(m_delim);
ffes.setQuoteChar(m_stringDelimChar);
ffes.setHasHeaders(m_hasHeaders);
ffes.setFirstLineHasTitle(m_firstLineHasTitle);
ffes.setLocalJobBuilder(m_ljb);
ffes.setGZIP(m_gzip);
ffes.setDateTimeFormat(m_dateTimeFormat);
ffes.setTimeFormat(m_timeFormat);
ffes.setDateFormat(m_dateFormat);
// Since this is likely to be a time consuming operation, we are allowed
// to return null now, and continue to open the dataSet from another thread
// and then call m_ljb.setEventSource from that thread once the file is
// ready.
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
ffes.init();
if (m_wizardPage != null && m_wizardPage.isVisible())
m_wizardPage.dispose();
m_ljb.setEventSource(ffes);
}
catch (ModuleException x)
{
m_wizardPage.handleError("Error opening file",x);
}
}
});
t.start();
return null;
}
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
m_prop = UserProperties.getUserProperties();
}
private int m_delim; // delimiting character
private int m_stringDelimChar;
private boolean m_firstLineHasTitle, m_hasHeaders;
private boolean m_gzip;
private String m_dateTimeFormat;
private String m_dateFormat;
private String m_timeFormat;
transient private LocalJobBuilder m_ljb;
transient private jas2.util.UserProperties m_prop = UserProperties.getUserProperties();
transient private FlatFileWizardPage m_wizardPage;
private class FlatFileWizardPage extends JASWizardPage implements Finishable, ChangeListener,
HasHelpPage
{
FlatFileWizardPage()
{
super(new BorderLayout());
// delim: panel for selecting delimiting character
JPanel delim = new JPanel();
m_delimiterOption = new JComboBox(); // combo box contains standard choices
for (int i = 0; i < delimiterStrings.length; i++)
m_delimiterOption.addItem(delimiterStrings[i]);
m_delimiterOption.setSelectedIndex(m_prop.getInteger(m_lastDelim_Key, 4));
m_delimiterOption.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
enableCustomDelimiter();
doEnable();
}
});
int lastDelimChar = m_prop.getInteger(m_lastCustDelimChar_Key, 0);
if (lastDelimChar > 0)
m_custDelimiter = new JASTextField(String.valueOf((char) lastDelimChar), 3);
else
m_custDelimiter = new JASTextField(3);
m_custDelimiter.addKeyListener(this);
delim.add(m_delimiterOption);
delim.add(m_customDelimiterLabel);
delim.add(m_custDelimiter);
delim.setBorder(BorderFactory.createTitledBorder("Set column delimiter"));
enableCustomDelimiter();
// stringDelimiterPanel: panel for setting string delimiter
JPanel stringDelimiterPanel = new JPanel();
stringDelimiterPanel.setBorder(BorderFactory.createTitledBorder("Select string delimiter:"));
m_stringDelim = new JComboBox();
for (int i = 0; i < stringDelimiterStrings.length; i++)
m_stringDelim.addItem(stringDelimiterStrings[i]);
m_stringDelim.setSelectedIndex(m_prop.getInteger(m_lastStringDelim_Key, 2));
stringDelimiterPanel.add(m_stringDelim);
// m_filePanel: panel for selecting the file itself
m_filePanel = new OpenLocalFilePanel(true, true, "FlatFile");
m_filePanel.addChangeListener(this);
// headerLinesPanel: panel for setting properties of first line
JPanel headerLinesPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JCheckBox title = new JCheckBox("Title in first line",
m_firstLineHasTitle = m_prop.getBoolean(m_firstLineHasTitle_Key, false));
title.setMnemonic('T');
final JCheckBox headers = new JCheckBox(headerCheckBoxString()+" ",
m_hasHeaders = m_prop.getBoolean(m_hasHeaders_Key, true));
// The extra spaces are there to ensure the label has enough space
// in case the length increases.
headers.setMnemonic('h');
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JCheckBox cb = (JCheckBox) e.getSource();
if (cb == headers)
m_prop.setBoolean(m_hasHeaders_Key, m_hasHeaders = cb.isSelected());
else
{
m_prop.setBoolean(m_firstLineHasTitle_Key,
m_firstLineHasTitle = cb.isSelected());
headers.setText(headerCheckBoxString());
}
}
};
title.addActionListener(al);
headers.addActionListener(al);
headerLinesPanel.add(title);
headerLinesPanel.add(headers);
m_dateTimeFormat = m_prop.getString(m_dateTimeFormat_Key, "dd-MMM-yyyy HH:mm:ss");
m_timeFormat = m_prop.getString(m_timeFormat_Key, "HH:mm:ss");
m_dateFormat = m_prop.getString(m_dateFormat_Key, "dd-MMM-yyyy");
JButton format = new JButton("Date/Time Formats...");
format.setMnemonic('D');
ActionListener al2 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Component f = FlatFileWizardPage.this;
while (!(f instanceof Frame)) f = f.getParent();
JASDialog dlg = new DateTimeFormatDialog((Frame) f);
dlg.doModal();
}
};
format.addActionListener(al2);
headerLinesPanel.add(format);
// building the page
add(BorderLayout.SOUTH, headerLinesPanel);
add(BorderLayout.CENTER, delim);
add(BorderLayout.EAST, stringDelimiterPanel);
add(BorderLayout.NORTH, m_filePanel);
}
public String getHelpTopic()
{
return "builtInModules.flatFile";
}
public void stateChanged(ChangeEvent e)
{
doEnable();
}
public void doEnable()
{
super.doEnable();
}
protected boolean getFinishEnabled()
{
return m_filePanel.getText().length() > 0 &&
(delimiterChars[m_delimiterOption.getSelectedIndex()] != CUSTOM_DELIM
|| m_custDelimiter.getText().length() > 0);
}
private void enableCustomDelimiter()
{
boolean customSelected =
(delimiterChars[m_delimiterOption.getSelectedIndex()] == CUSTOM_DELIM);
m_customDelimiterLabel.setEnabled(customSelected);
m_customDelimiterLabel.repaint();
m_custDelimiter.setEnabled(customSelected);
m_custDelimiter.repaint();
}
private String headerCheckBoxString()
{
return (m_firstLineHasTitle ? "Second" : "First")
+" line contains column headers";
}
public void onFinish()
{
int stringDelimiterIndex = m_stringDelim.getSelectedIndex();
m_prop.setInteger(m_lastStringDelim_Key, stringDelimiterIndex);
m_stringDelimChar = stringDelimiterChars[stringDelimiterIndex];
int delimiterOptionIndex = m_delimiterOption.getSelectedIndex();
m_prop.setInteger(m_lastDelim_Key, delimiterOptionIndex);
m_delim = delimiterChars[delimiterOptionIndex];
if (m_delim == CUSTOM_DELIM)
{
String s = m_custDelimiter.getText();
if (s.length() == 0)
{
JOptionPane.showMessageDialog(this, "You must enter a custom delimiter "+
"character.", "Error", JOptionPane.ERROR_MESSAGE);
return; // without calling dispose(), so Wizard stays open
}
else if (s.length() == 1)
{
m_delim = s.charAt(0);
m_prop.setInteger(m_lastCustDelimChar_Key, m_delim);
}
else
{
JOptionPane.showMessageDialog(this, "Cannot enter more than one delimiting character",
"Error", JOptionPane.ERROR_MESSAGE);
return; // without calling dispose(), so Wizard stays open
}
}
String fileName = m_filePanel.getText();
m_gzip = m_filePanel.getGZIPed();
m_ljb.createJob(fileName);
m_filePanel.saveState();
}
protected void dispose()
{
super.dispose();
}
protected void handleError(String message, Throwable t)
{
super.handleError(message,t);
}
private JComboBox m_delimiterOption;
private JASTextField m_custDelimiter;
private final JLabel m_customDelimiterLabel = new JLabel("Enter custom delimiter: ");
private JComboBox m_stringDelim;
private jas2.util.OpenLocalFilePanel m_filePanel;
// below: codes for string delimiters
private final String[] stringDelimiterStrings = {"Double Quotation (\")", "Single Quotation (\')", "None" };
private final int[] stringDelimiterChars = { '\"' , '\'' , FlatFileEventSource.NO_STRING_DELIMITER};
// below: delimiting characters and corresponding Strings
private final String[] delimiterStrings = {"Tab ('\\t')", "Space (' ')", "Comma (',')", "Semi-colon (';')", "Whitespace" , "Custom"};
private final int[] delimiterChars = { '\t' , ' ' , ',' , ';' , FlatFileEventSource.WHITESPACE_DELIMITED, CUSTOM_DELIM};
// below: keys for properties hashtable
final private String m_hasHeaders_Key = "HasHeaders";
final private String m_firstLineHasTitle_Key = "FirstLineHasTitle";
final private String m_lastDelim_Key = "LastDelim";
final private String m_lastCustDelimChar_Key = "LastCustDelimChar";
final private String m_lastStringDelim_Key = "LastStringDelim";
final private String m_dateTimeFormat_Key = "DateTimeFormat";
final private String m_timeFormat_Key = "TimeFormat";
final private String m_dateFormat_Key = "DateFormat";
private class DateTimeFormatDialog extends JASDialog
{
DateTimeFormatDialog(Frame f)
{
super(f,"Date/Time Format");
Container root = getContentPane();
root.setLayout(new TableLayout());
root.add("1 1",new JLabel("Date-Time Format"));
root.add("2 1",m_dateTimeField);
root.add("1 2",new JLabel("Time Format"));
root.add("2 2",m_timeField);
root.add("1 3",new JLabel("Date Format"));
root.add("2 3",m_dateField);
setHelpTopic("builtInModules.flatFile.dateTimeFormat");
pack();
}
public void onOK()
{
super.onOK();
m_dateTimeFormat = m_dateTimeField.getText();
m_timeFormat = m_timeField.getText();
m_dateFormat = m_dateField.getText();
m_prop.setString(m_dateTimeFormat_Key, m_dateTimeFormat);
m_prop.setString(m_timeFormat_Key, m_timeFormat);
m_prop.setString(m_dateFormat_Key, m_dateFormat);
}
private JASTextField m_dateTimeField = new JASTextField(m_dateTimeFormat);
private JASTextField m_timeField = new JASTextField(m_timeFormat);
private JASTextField m_dateField = new JASTextField(m_dateFormat);
}
}
final int CUSTOM_DELIM = -1;
public String getFileName()
{
return fileName;
}
private String fileName;
}
Ads help maintain this website.