| 1 | package tmcsim.cadsimulator.viewer; |
|---|
| 2 | |
|---|
| 3 | import java.awt.*; |
|---|
| 4 | import java.awt.event.ActionEvent; |
|---|
| 5 | import java.awt.event.ActionListener; |
|---|
| 6 | import java.io.*; |
|---|
| 7 | import java.nio.file.FileSystems; |
|---|
| 8 | import java.nio.file.Path; |
|---|
| 9 | |
|---|
| 10 | import javax.swing.*; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * ConfigStatusPanel is a GUI object used for displaying the current configuration files. |
|---|
| 15 | * |
|---|
| 16 | * @author Drew Miller |
|---|
| 17 | * @version |
|---|
| 18 | */ |
|---|
| 19 | @SuppressWarnings("serial") |
|---|
| 20 | public class ConfigStatusPanel extends JPanel { |
|---|
| 21 | |
|---|
| 22 | private String[] configFileNames = { |
|---|
| 23 | "cad_simulator_config.properties", |
|---|
| 24 | "cad_simulator_media_config.properties", |
|---|
| 25 | "cad_simulator_paramics_config.properties", |
|---|
| 26 | "cad_simulator_smoketest_config.properties" |
|---|
| 27 | }; |
|---|
| 28 | private JTextArea view; |
|---|
| 29 | /** |
|---|
| 30 | * Constructor. Initialize data and GUI components. |
|---|
| 31 | */ |
|---|
| 32 | public ConfigStatusPanel() { |
|---|
| 33 | initComponents(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | private String getFileContent(Path pathToFile) throws IOException { |
|---|
| 37 | BufferedReader br = new BufferedReader(new FileReader(pathToFile.toString())); |
|---|
| 38 | String content = null; |
|---|
| 39 | String line = null; |
|---|
| 40 | while((line = br.readLine()) != null) { |
|---|
| 41 | content += line + "\n"; |
|---|
| 42 | } |
|---|
| 43 | return content; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | private Path getPathToFile(String fileName) { |
|---|
| 47 | Path p = FileSystems.getDefault().getPath("config", fileName); |
|---|
| 48 | return p; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | private void changeView(Path pathToFileName) { |
|---|
| 52 | try { |
|---|
| 53 | view.setText(getFileContent(pathToFileName)); |
|---|
| 54 | view.setCaretPosition(view.getDocument().getLength()); |
|---|
| 55 | } catch (IOException e) { |
|---|
| 56 | System.out.println("Error in setting text."); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | private void setupComboBox(JPanel container) { |
|---|
| 60 | JComboBox selectMenu = new JComboBox(configFileNames); |
|---|
| 61 | selectMenu.addActionListener(new ActionListener() { |
|---|
| 62 | public void actionPerformed(ActionEvent e) { |
|---|
| 63 | JComboBox b = (JComboBox)e.getSource(); |
|---|
| 64 | System.out.println(b.getSelectedItem()); |
|---|
| 65 | Path p = getPathToFile(b.getSelectedItem().toString()); |
|---|
| 66 | changeView(p); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | }); |
|---|
| 70 | container.add(selectMenu); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | private void setupTextView(JPanel container) { |
|---|
| 74 | view = new JTextArea(15, 30); |
|---|
| 75 | view.setEditable(false); |
|---|
| 76 | view.setLineWrap(true); |
|---|
| 77 | view.setWrapStyleWord(true); |
|---|
| 78 | Path p = FileSystems.getDefault().getPath("config", configFileNames[0]); |
|---|
| 79 | try { |
|---|
| 80 | view.setText(getFileContent(p)); |
|---|
| 81 | } catch (IOException e) { |
|---|
| 82 | System.out.println("Error in setting text."); |
|---|
| 83 | } |
|---|
| 84 | container.add(view); |
|---|
| 85 | } |
|---|
| 86 | /** |
|---|
| 87 | * Initialize GUI components. |
|---|
| 88 | */ |
|---|
| 89 | private void initComponents() { |
|---|
| 90 | configTab = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 91 | configTab.setPreferredSize(new Dimension(400, 400)); |
|---|
| 92 | setupComboBox(configTab); |
|---|
| 93 | setupTextView(configTab); |
|---|
| 94 | |
|---|
| 95 | add(configTab); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | private JPanel configTab; |
|---|
| 100 | |
|---|
| 101 | } |
|---|