View Javadoc
1   /*
2    * Copyright 2020-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.demo.gui;
6   
7   import java.awt.BorderLayout;
8   import java.awt.GridBagConstraints;
9   import java.awt.GridBagLayout;
10  import java.awt.Insets;
11  import java.time.Instant;
12  import java.util.List;
13  import java.util.Locale;
14  
15  import javax.swing.JLabel;
16  import javax.swing.JPanel;
17  import javax.swing.JTextArea;
18  import javax.swing.Timer;
19  
20  import com.fasterxml.jackson.core.JsonProcessingException;
21  import com.fasterxml.jackson.databind.ObjectMapper;
22  
23  import oshi.SystemInfo;
24  import oshi.hardware.CentralProcessor;
25  import oshi.hardware.ComputerSystem;
26  import oshi.hardware.Display;
27  import oshi.software.os.OperatingSystem;
28  import oshi.util.EdidUtil;
29  import oshi.util.FormatUtil;
30  
31  /**
32   * Displays text in panes covering mostly-static information. Uptime is refreshed every second.
33   */
34  public class OsHwTextPanel extends OshiJPanel { // NOSONAR squid:S110
35  
36      private static final long serialVersionUID = 1L;
37  
38      private static final String OPERATING_SYSTEM = "Operating System";
39      private static final String HARDWARE_INFORMATION = "Hardware Information";
40      private static final String PROCESSOR = "Processor";
41      private static final String DISPLAYS = "Displays";
42      private String osPrefix;
43  
44      public OsHwTextPanel(SystemInfo si) {
45          super();
46          init(si);
47      }
48  
49      private void init(SystemInfo si) {
50          osPrefix = getOsPrefix(si);
51  
52          GridBagConstraints osLabel = new GridBagConstraints();
53          GridBagConstraints osConstraints = new GridBagConstraints();
54          osConstraints.gridy = 1;
55          osConstraints.fill = GridBagConstraints.BOTH;
56          osConstraints.insets = new Insets(0, 0, 15, 15); // T,L,B,R
57  
58          GridBagConstraints procLabel = (GridBagConstraints) osLabel.clone();
59          procLabel.gridy = 2;
60          GridBagConstraints procConstraints = (GridBagConstraints) osConstraints.clone();
61          procConstraints.gridy = 3;
62  
63          GridBagConstraints displayLabel = (GridBagConstraints) procLabel.clone();
64          displayLabel.gridy = 4;
65          GridBagConstraints displayConstraints = (GridBagConstraints) osConstraints.clone();
66          displayConstraints.gridy = 5;
67          displayConstraints.insets = new Insets(0, 0, 0, 15); // T,L,B,R
68  
69          GridBagConstraints csLabel = (GridBagConstraints) osLabel.clone();
70          csLabel.gridx = 1;
71          GridBagConstraints csConstraints = new GridBagConstraints();
72          csConstraints.gridx = 1;
73          csConstraints.gridheight = 6;
74          csConstraints.fill = GridBagConstraints.BOTH;
75  
76          JPanel oshwPanel = new JPanel();
77          oshwPanel.setLayout(new GridBagLayout());
78  
79          JTextArea osArea = new JTextArea(0, 0);
80          osArea.setText(updateOsData(si));
81          oshwPanel.add(new JLabel(OPERATING_SYSTEM), osLabel);
82          oshwPanel.add(osArea, osConstraints);
83  
84          JTextArea procArea = new JTextArea(0, 0);
85          procArea.setText(getProc(si));
86          oshwPanel.add(new JLabel(PROCESSOR), procLabel);
87          oshwPanel.add(procArea, procConstraints);
88  
89          JTextArea displayArea = new JTextArea(0, 0);
90          displayArea.setText(getDisplay(si));
91          oshwPanel.add(new JLabel(DISPLAYS), displayLabel);
92          oshwPanel.add(displayArea, displayConstraints);
93  
94          JTextArea csArea = new JTextArea(0, 0);
95          csArea.setText(getHw(si));
96          oshwPanel.add(new JLabel(HARDWARE_INFORMATION), csLabel);
97          oshwPanel.add(csArea, csConstraints);
98  
99          add(oshwPanel, BorderLayout.CENTER);
100 
101         // Update up time every second
102         Timer timer = new Timer(Config.REFRESH_FAST, e -> osArea.setText(updateOsData(si)));
103         timer.start();
104     }
105 
106     private static String getOsPrefix(SystemInfo si) {
107         StringBuilder sb = new StringBuilder(OPERATING_SYSTEM);
108 
109         OperatingSystem os = si.getOperatingSystem();
110         sb.append(String.valueOf(os));
111         sb.append("\n\n").append("Booted: ").append(Instant.ofEpochSecond(os.getSystemBootTime())).append('\n')
112                 .append("Uptime: ");
113         return sb.toString();
114     }
115 
116     private static String getHw(SystemInfo si) {
117         StringBuilder sb = new StringBuilder();
118         ObjectMapper mapper = new ObjectMapper();
119         ComputerSystem computerSystem = si.getHardware().getComputerSystem();
120         try {
121             sb.append(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(computerSystem));
122         } catch (JsonProcessingException e) {
123             sb.append(e.getMessage());
124         }
125         return sb.toString();
126     }
127 
128     private static String getProc(SystemInfo si) {
129         StringBuilder sb = new StringBuilder();
130         CentralProcessor proc = si.getHardware().getProcessor();
131         sb.append(proc.toString());
132 
133         return sb.toString();
134     }
135 
136     private static String getDisplay(SystemInfo si) {
137         StringBuilder sb = new StringBuilder();
138         List<Display> displays = si.getHardware().getDisplays();
139         if (displays.isEmpty()) {
140             sb.append("None detected.");
141         } else {
142             int i = 0;
143             for (Display display : displays) {
144                 byte[] edid = display.getEdid();
145                 byte[][] desc = EdidUtil.getDescriptors(edid);
146                 String name = "Display " + i;
147                 for (byte[] b : desc) {
148                     if (EdidUtil.getDescriptorType(b) == 0xfc) {
149                         name = EdidUtil.getDescriptorText(b);
150                     }
151                 }
152                 if (i++ > 0) {
153                     sb.append('\n');
154                 }
155                 sb.append(name).append(": ");
156                 int hSize = EdidUtil.getHcm(edid);
157                 int vSize = EdidUtil.getVcm(edid);
158                 sb.append(String.format(Locale.ROOT, "%d x %d cm (%.1f x %.1f in)", hSize, vSize, hSize / 2.54,
159                         vSize / 2.54));
160             }
161         }
162         return sb.toString();
163     }
164 
165     private String updateOsData(SystemInfo si) {
166         return osPrefix + FormatUtil.formatElapsedSecs(si.getOperatingSystem().getSystemUptime());
167     }
168 }