package tmcsim.cadsimulator.viewer;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Properties;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* ConfigStatusPanel displays the content of configuration files.
* A ComboBox allows the user to select from a list of current properties files.
*
* @author Drew Miller
* @author jdalbey
*/
public class ConfigStatusTab extends javax.swing.JPanel
{
/**
* Properties file for the CAD Server.
*/
private Properties cadServerProperties;
/**
* Creates new form
*/
public ConfigStatusTab(String propertiesFile)
{
initComponents();
createMenu(propertiesFile);
addMenuListener();
setupTextView();
}
/** Add the properties filenames to the combobox menu */
private void createMenu(String propertiesFile)
{
selectMenu.addItem(propertiesFile);
try {
cadServerProperties = new Properties();
cadServerProperties.load(new FileInputStream(propertiesFile));
// Add each item that's a properties file to the menu
for (String propname: cadServerProperties.stringPropertyNames())
{
String currVal = (String) cadServerProperties.get(propname);
if (currVal.endsWith(".properties"))
{
selectMenu.addItem(currVal);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ConfigStatusTab.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ConfigStatusTab.class.getName()).log(Level.SEVERE, null, ex);
}
}
/** Determine complete path to file, given the filename */
private Path getPathToFile(String fileName)
{
return FileSystems.getDefault().getPath(fileName);
}
/** Return the text from the given file */
private String getFileContent(Path pathToFile)
{
FileInputStream fis = null;
try
{
//Read a file into a single string.
fis = new FileInputStream(pathToFile.toString());
Scanner s = new Scanner(fis).useDelimiter("\\A");
return s.next();
}
catch (FileNotFoundException ex)
{
return "Error: config file not found.";
}
}
/** Refresh the display using the given properties file path */
private void updateDisplay(Path pathToFileName)
{
view.setText(getFileContent(pathToFileName));
view.setCaretPosition(view.getDocument().getLength());
}
/** Add menu listener to the Combo Box */
private void addMenuListener()
{
selectMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Find which menu item was clicked
JComboBox src = (JComboBox) e.getSource();
// Get the menu item string
String menuItem = src.getSelectedItem().toString();
// Get the path to that file
Path p = getPathToFile(menuItem);
// Display the contents of that file
updateDisplay(p);
}
});
}
private void setupTextView()
{
view.setEditable(false);
view.setLineWrap(true);
view.setWrapStyleWord(true);
// Initially display the first config file in list
Path path = getPathToFile(selectMenu.getItemAt(0));
view.setText(getFileContent(path));
}
/**
* Local main for unit testing.
*
* @param args not used
*/
public static void main(String[] args)
{
JFrame frame = new JFrame("Config Status Panel Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1050, 450);
ConfigStatusTab pnl = new ConfigStatusTab("config/cad_server.properties");
frame.add(pnl);
frame.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
selectMenu = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
view = new javax.swing.JTextArea();
setLayout(new java.awt.BorderLayout());
add(selectMenu, java.awt.BorderLayout.PAGE_START);
view.setColumns(20);
view.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
view.setRows(5);
jScrollPane1.setViewportView(view);
add(jScrollPane1, java.awt.BorderLayout.CENTER);
}// //GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JComboBox selectMenu;
private javax.swing.JTextArea view;
// End of variables declaration//GEN-END:variables
}