public Component getComponent(SimulationGui s, final Properties p) {
final Logger l = Logger.getLogger(p.getProperty("logger.name"));
final JTextArea jta = new JTextArea(25, 80);
jta.setEditable(false);
final TextAreaHandler h = new TextAreaHandler(jta, p);
h.setLevel(Level.parse(p.getProperty("handler.level", "OFF")));
String formatterClassName =
p.getProperty("handler.formatter.class",
"org.mitre.sim.util.BasicFormatter");
String formatterFormat = p.getProperty("handler.formatter.format");
Formatter formatter = null;
try {
formatter = (Formatter)Class.forName(formatterClassName).newInstance();
}
catch (Throwable t) {
formatter = new BasicFormatter();
}
if ((formatter instanceof BasicFormatter) && (formatterFormat != null)) {
((BasicFormatter)formatter).applyFormat(formatterFormat);
}
h.setFormatter(formatter);
l.addHandler(h);
JPanel handlerLevelPanel = new JPanel();
handlerLevelPanel.setLayout(new BoxLayout(handlerLevelPanel,
BoxLayout.X_AXIS));
handlerLevelPanel.add(new JLabel("Log Level"));
JComboBox jcb = new JComboBox(new Object[]{
"ALL",
"FINEST",
"FINER",
"FINE",
"CONFIG",
"INFO",
"WARNING",
"SEVERE",
"OFF"});
jcb.setMaximumRowCount(9);
jcb.setEditable(false);
jcb.setSelectedItem(h.getLevel().toString());
jcb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
h.setLevel(Level.parse((String)e.getItem()));
if (logFileHandler != null)
logFileHandler.setLevel(h.getLevel());
}
}
});
handlerLevelPanel.add(Box.createHorizontalStrut(10));
handlerLevelPanel.add(jcb);
JCheckBox logToFile = new JCheckBox("Log to file");
logToFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
@SuppressWarnings("hiding")
final JCheckBox jcb = (JCheckBox)ae.getSource();
// Start a log. Create a new event to be run synchronously
// to obtain the log file name
if (jcb.isSelected()) {
SwingUtilities.invokeLater(new Runnable() {
private void showErrorMessage(File f, @SuppressWarnings("hiding")
String s) {
JOptionPane.showMessageDialog(jta,
"Unable to "+s+"write "+f+".",
"File not writeable",
JOptionPane.ERROR_MESSAGE);
}
public void run() {
File selectedFile = null;
while (true) {
JFileChooser jfc = new JFileChooser(lastDirectory);
jfc.setApproveButtonText("Create/Open");
jfc.setDialogTitle("Select file to log to");
int returnValue = jfc.showDialog(jta, null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile = jfc.getSelectedFile();
lastDirectory = selectedFile.getParentFile();
if (selectedFile.exists()) {
if (selectedFile.canWrite()) {
int confirm =
JOptionPane.showConfirmDialog(
jta,
"File "+selectedFile+" exists.\nOverwrite?",
"File Exists",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
break;
}
}
else {
showErrorMessage(selectedFile, "over");
}
}
// Try and create the file
else {
try {
if (!selectedFile.createNewFile())
showErrorMessage(selectedFile, "");
else break;
}
catch (IOException ioe) {
showErrorMessage(selectedFile, "");
}
}
}
else if (returnValue == JFileChooser.CANCEL_OPTION) {
jcb.setSelected(false);
return;
}
}
if (selectedFile != null) {
try {
FileOutputStream fos =
new FileOutputStream(selectedFile);
logFileHandler =
new StreamHandler(fos, h.getFormatter());
logFileHandler.setLevel(h.getLevel());
l.addHandler(logFileHandler);
jcb.setText("Logging to file: "+selectedFile);
}
catch (Throwable t) {
t.printStackTrace();