| 1 | |
|---|
| 2 | package tmcsim.cadsimulator.viewer; |
|---|
| 3 | |
|---|
| 4 | import java.awt.GraphicsEnvironment; |
|---|
| 5 | import java.awt.event.ActionEvent; |
|---|
| 6 | import java.awt.event.ActionListener; |
|---|
| 7 | import java.io.FileInputStream; |
|---|
| 8 | import java.io.FileNotFoundException; |
|---|
| 9 | import java.nio.file.FileSystems; |
|---|
| 10 | import java.nio.file.Path; |
|---|
| 11 | import java.util.Scanner; |
|---|
| 12 | import javax.swing.JComboBox; |
|---|
| 13 | import javax.swing.JFrame; |
|---|
| 14 | import javax.swing.JPanel; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * ConfigStatusPanel displays the content of configuration files. |
|---|
| 18 | * |
|---|
| 19 | * @author Drew Miller |
|---|
| 20 | * @author jdalbey |
|---|
| 21 | */ |
|---|
| 22 | public class ConfigStatusTab extends javax.swing.JPanel |
|---|
| 23 | { |
|---|
| 24 | /** Root config directory name */ |
|---|
| 25 | private String configDir; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * Creates new form |
|---|
| 29 | */ |
|---|
| 30 | public ConfigStatusTab() |
|---|
| 31 | { |
|---|
| 32 | initComponents(); |
|---|
| 33 | if (System.getProperty("CONFIG_DIR") == null) |
|---|
| 34 | { |
|---|
| 35 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 36 | } |
|---|
| 37 | configDir = System.getProperty("CONFIG_DIR"); |
|---|
| 38 | setupComboBox(); |
|---|
| 39 | setupTextView(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | private String getFileContent(Path pathToFile) |
|---|
| 43 | { |
|---|
| 44 | FileInputStream fis = null; |
|---|
| 45 | try |
|---|
| 46 | { |
|---|
| 47 | //Read a file into a single string. |
|---|
| 48 | fis = new FileInputStream(pathToFile.toString()); |
|---|
| 49 | Scanner s = new Scanner(fis).useDelimiter("\\A"); |
|---|
| 50 | return s.next(); |
|---|
| 51 | } |
|---|
| 52 | catch (FileNotFoundException ex) |
|---|
| 53 | { |
|---|
| 54 | return "Error: config file not found."; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | private Path getPathToFile(String fileName) |
|---|
| 60 | { |
|---|
| 61 | Path p = FileSystems.getDefault().getPath(configDir, fileName); |
|---|
| 62 | return p; |
|---|
| 63 | } |
|---|
| 64 | /** Refresh the display using the given config file */ |
|---|
| 65 | private void updateDisplay(Path pathToFileName) |
|---|
| 66 | { |
|---|
| 67 | view.setText(getFileContent(pathToFileName)); |
|---|
| 68 | view.setCaretPosition(view.getDocument().getLength()); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** Combo Box with names of config files for user to select */ |
|---|
| 72 | private void setupComboBox() |
|---|
| 73 | { |
|---|
| 74 | selectMenu.addActionListener(new ActionListener() |
|---|
| 75 | { |
|---|
| 76 | public void actionPerformed(ActionEvent e) |
|---|
| 77 | { |
|---|
| 78 | JComboBox src = (JComboBox) e.getSource(); |
|---|
| 79 | Path p = getPathToFile(src.getSelectedItem().toString()); |
|---|
| 80 | updateDisplay(p); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | }); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private void setupTextView() |
|---|
| 87 | { |
|---|
| 88 | view.setEditable(false); |
|---|
| 89 | view.setLineWrap(true); |
|---|
| 90 | view.setWrapStyleWord(true); |
|---|
| 91 | // Initialize with first config file in list |
|---|
| 92 | Path path = getPathToFile(selectMenu.getItemAt(0)); |
|---|
| 93 | view.setText(getFileContent(path)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Local main for unit testing. |
|---|
| 98 | * |
|---|
| 99 | * @param args not used |
|---|
| 100 | */ |
|---|
| 101 | public static void main(String[] args) |
|---|
| 102 | { |
|---|
| 103 | JFrame frame = new JFrame("Config Status Panel Test"); |
|---|
| 104 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 105 | frame.setSize(1050, 450); |
|---|
| 106 | ConfigStatusTab pnl = new ConfigStatusTab(); |
|---|
| 107 | frame.add(pnl); |
|---|
| 108 | frame.setVisible(true); |
|---|
| 109 | } |
|---|
| 110 | /** |
|---|
| 111 | * This method is called from within the constructor to initialize the form. |
|---|
| 112 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 113 | * regenerated by the Form Editor. |
|---|
| 114 | */ |
|---|
| 115 | @SuppressWarnings("unchecked") |
|---|
| 116 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 117 | private void initComponents() |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | selectMenu = new javax.swing.JComboBox<>(); |
|---|
| 121 | jScrollPane1 = new javax.swing.JScrollPane(); |
|---|
| 122 | view = new javax.swing.JTextArea(); |
|---|
| 123 | |
|---|
| 124 | setLayout(new java.awt.BorderLayout()); |
|---|
| 125 | |
|---|
| 126 | selectMenu.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "cad_simulator_config.properties", "cad_simulator_media_config.properties", "cad_simulator_paramics_config.properties", "cad_simulator_smoketest_config.properties", "traffic_model_config.properties" })); |
|---|
| 127 | add(selectMenu, java.awt.BorderLayout.PAGE_START); |
|---|
| 128 | |
|---|
| 129 | view.setColumns(20); |
|---|
| 130 | view.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N |
|---|
| 131 | view.setRows(5); |
|---|
| 132 | jScrollPane1.setViewportView(view); |
|---|
| 133 | |
|---|
| 134 | add(jScrollPane1, java.awt.BorderLayout.CENTER); |
|---|
| 135 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 139 | private javax.swing.JScrollPane jScrollPane1; |
|---|
| 140 | private javax.swing.JComboBox<String> selectMenu; |
|---|
| 141 | private javax.swing.JTextArea view; |
|---|
| 142 | // End of variables declaration//GEN-END:variables |
|---|
| 143 | } |
|---|