View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.demo;
6   
7   import static javax.swing.JFrame.EXIT_ON_CLOSE;
8   
9   import java.awt.BorderLayout;
10  import java.awt.Container;
11  
12  import javax.swing.JButton;
13  import javax.swing.JFrame;
14  import javax.swing.JMenuBar;
15  import javax.swing.SwingUtilities;
16  import javax.swing.WindowConstants;
17  
18  import oshi.SystemInfo;
19  import oshi.demo.gui.Config;
20  import oshi.demo.gui.FileStorePanel;
21  import oshi.demo.gui.InterfacePanel;
22  import oshi.demo.gui.MemoryPanel;
23  import oshi.demo.gui.OsHwTextPanel;
24  import oshi.demo.gui.OshiJPanel;
25  import oshi.demo.gui.ProcessPanel;
26  import oshi.demo.gui.ProcessorPanel;
27  import oshi.demo.gui.UsbPanel;
28  
29  /**
30   * Basic Swing class to demonstrate potential uses for OSHI in a monitoring GUI. Not ready for production use and
31   * intended as inspiration/examples.
32   */
33  public class OshiGui {
34  
35      private JFrame mainFrame;
36      private JButton jMenu;
37  
38      private SystemInfo si = new SystemInfo();
39  
40      public static void main(String[] args) {
41          OshiGui gui = new OshiGui();
42          gui.init();
43          SwingUtilities.invokeLater(gui::setVisible);
44      }
45  
46      private void setVisible() {
47          mainFrame.setVisible(true);
48          mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
49          jMenu.doClick();
50      }
51  
52      private void init() {
53          // Create the external frame
54          mainFrame = new JFrame(Config.GUI_TITLE);
55          mainFrame.setSize(Config.GUI_WIDTH, Config.GUI_HEIGHT);
56          mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
57          mainFrame.setResizable(true);
58          mainFrame.setLocationByPlatform(true);
59          mainFrame.setLayout(new BorderLayout());
60          // Add a menu bar
61          JMenuBar menuBar = new JMenuBar();
62          mainFrame.setJMenuBar(menuBar);
63          // Assign the first menu option to be clicked on visibility
64          jMenu = getJMenu("OS & HW Info", 'O', "Hardware & OS Summary", new OsHwTextPanel(si));
65          menuBar.add(jMenu);
66          // Add later menu items
67          menuBar.add(getJMenu("Memory", 'M', "Memory Summary", new MemoryPanel(si)));
68          menuBar.add(getJMenu("CPU", 'C', "CPU Usage", new ProcessorPanel(si)));
69          menuBar.add(getJMenu("FileStores", 'F', "FileStore Usage", new FileStorePanel(si)));
70          menuBar.add(getJMenu("Processes", 'P', "Processes", new ProcessPanel(si)));
71          menuBar.add(getJMenu("USB Devices", 'U', "USB Device list", new UsbPanel(si)));
72          menuBar.add(getJMenu("Network", 'N', "Network Params and Interfaces", new InterfacePanel(si)));
73      }
74  
75      private JButton getJMenu(String title, char mnemonic, String toolTip, OshiJPanel panel) {
76          JButton button = new JButton(title);
77          button.setMnemonic(mnemonic);
78          button.setToolTipText(toolTip);
79          button.addActionListener(e -> {
80              Container contentPane = this.mainFrame.getContentPane();
81              if (contentPane.getComponents().length <= 0 || contentPane.getComponent(0) != panel) {
82                  resetMainGui();
83                  this.mainFrame.getContentPane().add(panel);
84                  refreshMainGui();
85              }
86          });
87  
88          return button;
89      }
90  
91      private void resetMainGui() {
92          this.mainFrame.getContentPane().removeAll();
93      }
94  
95      private void refreshMainGui() {
96          this.mainFrame.revalidate();
97          this.mainFrame.repaint();
98      }
99  }