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

Revision 664, 5.5 KB checked in by jdalbey, 4 years ago (diff)

Multifile commit - revise source to match revisions to config filenames. Fix broken system tests. Fix defect #160.

Line 
1
2package tmcsim.cadsimulator.viewer;
3
4import java.awt.GraphicsEnvironment;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.IOException;
10import java.nio.file.FileSystems;
11import java.nio.file.Path;
12import java.util.Properties;
13import java.util.Scanner;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import javax.swing.JComboBox;
17import javax.swing.JFrame;
18import javax.swing.JPanel;
19
20/**
21 * ConfigStatusPanel displays the content of configuration files.
22 * A ComboBox allows the user to select from a list of current properties files.
23 *
24 * @author Drew Miller
25 * @author jdalbey
26 */
27public class ConfigStatusTab extends javax.swing.JPanel
28{
29    /**
30     * Properties file for the CAD Server.
31     */
32    private Properties cadServerProperties;
33
34    /**
35     * Creates new form
36     */
37    public ConfigStatusTab(String propertiesFile)
38    {
39        initComponents();
40        createMenu(propertiesFile);
41        addMenuListener();
42        setupTextView();
43    }
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    }
66
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 */
73    private String getFileContent(Path pathToFile) 
74    {
75        FileInputStream fis = null;
76        try
77        {
78            //Read a file into a single string.
79            fis = new FileInputStream(pathToFile.toString());
80            Scanner s = new Scanner(fis).useDelimiter("\\A");
81            return s.next();
82        }
83        catch (FileNotFoundException ex)
84        {
85            return "Error: config file not found.";
86        }
87    }
88    /** Refresh the display using the given properties file path */
89    private void updateDisplay(Path pathToFileName)
90    {
91        view.setText(getFileContent(pathToFileName));
92        view.setCaretPosition(view.getDocument().getLength());
93    }
94
95    /** Add menu listener to the Combo Box */
96    private void addMenuListener()
97    {
98        selectMenu.addActionListener(new ActionListener()
99        {
100            public void actionPerformed(ActionEvent e)
101            {
102                // Find which menu item was clicked
103                JComboBox src = (JComboBox) e.getSource();
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
109                updateDisplay(p);
110            }
111        });
112    }
113
114    private void setupTextView()
115    {
116        view.setEditable(false);
117        view.setLineWrap(true);
118        view.setWrapStyleWord(true);
119        // Initially display the first config file in list
120        Path path = getPathToFile(selectMenu.getItemAt(0));
121        view.setText(getFileContent(path));
122    }
123
124     /**
125     * Local main for unit testing.
126     *
127     * @param args not used
128     */
129    public static void main(String[] args)
130    {
131        JFrame frame = new JFrame("Config Status Panel Test");
132        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
133        frame.setSize(1050, 450);
134        ConfigStatusTab pnl = new ConfigStatusTab("config/cad_server.properties");
135        frame.add(pnl);
136        frame.setVisible(true);
137    }
138    /**
139     * This method is called from within the constructor to initialize the form.
140     * WARNING: Do NOT modify this code. The content of this method is always
141     * regenerated by the Form Editor.
142     */
143    @SuppressWarnings("unchecked")
144    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
145    private void initComponents() {
146
147        selectMenu = new javax.swing.JComboBox<>();
148        jScrollPane1 = new javax.swing.JScrollPane();
149        view = new javax.swing.JTextArea();
150
151        setLayout(new java.awt.BorderLayout());
152        add(selectMenu, java.awt.BorderLayout.PAGE_START);
153
154        view.setColumns(20);
155        view.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
156        view.setRows(5);
157        jScrollPane1.setViewportView(view);
158
159        add(jScrollPane1, java.awt.BorderLayout.CENTER);
160    }// </editor-fold>//GEN-END:initComponents
161
162
163    // Variables declaration - do not modify//GEN-BEGIN:variables
164    private javax.swing.JScrollPane jScrollPane1;
165    private javax.swing.JComboBox<String> selectMenu;
166    private javax.swing.JTextArea view;
167    // End of variables declaration//GEN-END:variables
168}
Note: See TracBrowser for help on using the repository browser.