source: tmcsimulator/trunk/src/tmcsim/cadsimulator/viewer/ConfigStatusTab.java @ 230

Revision 230, 4.3 KB checked in by jdalbey, 8 years ago (diff)

ConfigStatusTab?.java Improved display and fixed defect so it now uses the correct config directory.

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