Ignore:
Timestamp:
07/18/2019 08:20:12 AM (7 years ago)
Author:
jdalbey
Message:

ConfigStatusTab? updated to fix @169.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/viewer/ConfigStatusTab.java

    r231 r455  
    77import java.io.FileInputStream; 
    88import java.io.FileNotFoundException; 
     9import java.io.IOException; 
    910import java.nio.file.FileSystems; 
    1011import java.nio.file.Path; 
     12import java.util.Properties; 
    1113import java.util.Scanner; 
     14import java.util.logging.Level; 
     15import java.util.logging.Logger; 
    1216import javax.swing.JComboBox; 
    1317import javax.swing.JFrame; 
     
    1620/** 
    1721 * ConfigStatusPanel displays the content of configuration files. 
     22 * A ComboBox allows the user to select from a list of current properties files. 
    1823 * 
    1924 * @author Drew Miller 
     
    2227public class ConfigStatusTab extends javax.swing.JPanel 
    2328{ 
    24     /** Root config directory name */ 
    25     private String configDir; 
    26      
     29    /** 
     30     * Properties file for the CAD Server. 
     31     */ 
     32    private Properties cadServerProperties; 
     33 
    2734    /** 
    2835     * Creates new form  
    2936     */ 
    30     public ConfigStatusTab() 
     37    public ConfigStatusTab(String propertiesFile) 
    3138    { 
    3239        initComponents(); 
    33         if (System.getProperty("CONFIG_DIR") == null) 
    34         { 
    35             System.setProperty("CONFIG_DIR", "config"); 
    36         } 
    37         configDir = System.getProperty("CONFIG_DIR"); 
    38         setupComboBox(); 
     40        createMenu(propertiesFile); 
     41        addMenuListener(); 
    3942        setupTextView(); 
    4043    } 
     44    /** Add the properties filenames to the combobox menu */ 
     45    private void createMenu(String propertiesFile) 
     46    { 
     47        selectMenu.addItem(propertiesFile); 
     48        try { 
     49            cadServerProperties = new Properties(); 
     50            cadServerProperties.load(new FileInputStream(propertiesFile)); 
     51            // Add each item that's a properties file to the menu 
     52            for (String propname: cadServerProperties.stringPropertyNames()) 
     53            { 
     54                String currVal = (String) cadServerProperties.get(propname); 
     55                if (currVal.endsWith(".properties")) 
     56                { 
     57                    selectMenu.addItem(currVal); 
     58                } 
     59            } 
     60        } catch (FileNotFoundException ex) { 
     61            Logger.getLogger(ConfigStatusTab.class.getName()).log(Level.SEVERE, null, ex); 
     62        } catch (IOException ex) { 
     63            Logger.getLogger(ConfigStatusTab.class.getName()).log(Level.SEVERE, null, ex); 
     64        }         
     65    } 
    4166 
     67    /** Determine complete path to file, given the filename */ 
     68    private Path getPathToFile(String fileName) 
     69    { 
     70        return FileSystems.getDefault().getPath(fileName); 
     71    } 
     72    /** Return the text from the given file */ 
    4273    private String getFileContent(Path pathToFile)  
    4374    { 
     
    5485            return "Error: config file not found."; 
    5586        } 
    56          
    5787    } 
    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 */ 
     88    /** Refresh the display using the given properties file path */ 
    6589    private void updateDisplay(Path pathToFileName) 
    6690    { 
     
    6993    } 
    7094 
    71     /** Combo Box with names of config files for user to select */ 
    72     private void setupComboBox() 
     95    /** Add menu listener to the Combo Box */ 
     96    private void addMenuListener() 
    7397    { 
    7498        selectMenu.addActionListener(new ActionListener() 
     
    76100            public void actionPerformed(ActionEvent e) 
    77101            { 
     102                // Find which menu item was clicked 
    78103                JComboBox src = (JComboBox) e.getSource(); 
    79                 Path p = getPathToFile(src.getSelectedItem().toString()); 
     104                // Get the menu item string  
     105                String menuItem = src.getSelectedItem().toString(); 
     106                // Get the path to that file 
     107                Path p = getPathToFile(menuItem); 
     108                // Display the contents of that file 
    80109                updateDisplay(p); 
    81110            } 
    82  
    83111        }); 
    84112    } 
     
    89117        view.setLineWrap(true); 
    90118        view.setWrapStyleWord(true); 
    91         // Initialize with first config file in list 
     119        // Initially display the first config file in list 
    92120        Path path = getPathToFile(selectMenu.getItemAt(0)); 
    93121        view.setText(getFileContent(path)); 
     
    104132        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    105133        frame.setSize(1050, 450); 
    106         ConfigStatusTab pnl = new ConfigStatusTab(); 
     134        ConfigStatusTab pnl = new ConfigStatusTab("config/cad_simulator_config.properties"); 
    107135        frame.add(pnl); 
    108136        frame.setVisible(true); 
     
    115143    @SuppressWarnings("unchecked") 
    116144    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
    117     private void initComponents() 
    118     { 
     145    private void initComponents() { 
    119146 
    120147        selectMenu = new javax.swing.JComboBox<>(); 
     
    123150 
    124151        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" })); 
    127152        add(selectMenu, java.awt.BorderLayout.PAGE_START); 
    128153 
Note: See TracChangeset for help on using the changeset viewer.