add Icon support for Windows

Tested on Win11 Parallels19
This commit is contained in:
Hansjörg Hofer 2024-01-14 09:02:09 +01:00
parent 42a8c75cdf
commit 34466f488c
1 changed files with 37 additions and 24 deletions

View File

@ -16,20 +16,12 @@ import oshi.SystemInfo;
import oshi.software.os.OSProcess; import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem; import oshi.software.os.OperatingSystem;
import oshi.util.FormatUtil; import oshi.util.FormatUtil;
import ru.olegcherednik.icoman.IconFile;
import ru.olegcherednik.icoman.IconManager;
import ru.olegcherednik.icoman.exceptions.IconManagerException;
import ru.olegcherednik.icoman.exceptions.IconNotFoundException;
import sun.awt.shell.ShellFolder;
import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileSystemView; import javax.swing.filechooser.FileSystemView;
import javax.swing.table.*; import javax.swing.table.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -44,12 +36,14 @@ import java.util.Map;
*/ */
@Getter @Getter
public class ProcessesForm { public class ProcessesForm {
private static final String[] COLUMNS = {"ICON", "PID", "PPID", "Threads", "% CPU", "Cumulative", "VSZ", "RSS", "% Memory", private static final String[] COLUMNS = {"Icon", "PID", "PPID", "Threads", "% CPU", "Cumulative", "VSZ", "RSS", "% Memory",
"Process Name"}; "Process Name"};
private static final double[] COLUMN_WIDTH_PERCENT = {0.001, 0.07, 0.07, 0.07, 0.07, 0.09, 0.1, 0.1, 0.08, 0.35}; private static final double[] COLUMN_WIDTH_PERCENT = {0.01, 0.07, 0.07, 0.07, 0.07, 0.09, 0.1, 0.1, 0.08, 0.35};
private transient static Map<Integer, OSProcess> priorSnapshotMap = new HashMap<>(); private transient static Map<Integer, OSProcess> priorSnapshotMap = new HashMap<>();
private static HashMap<String, Icon> iconCacheMap = new HashMap<>();
private static final Log logger = LogFactory.get(); private static final Log logger = LogFactory.get();
private static ProcessesForm processesForm; private static ProcessesForm processesForm;
@ -118,13 +112,18 @@ public class ProcessesForm {
TableModel model = new DefaultTableModel(parseProcesses(os.getProcesses(null, null, 0), App.si), COLUMNS) { TableModel model = new DefaultTableModel(parseProcesses(os.getProcesses(null, null, 0), App.si), COLUMNS) {
@Override @Override
public Class<?> getColumnClass(int column) { public Class<?> getColumnClass(int column) {
if (column == 0) return ImageIcon.class; if (column == 0){
return ImageIcon.class;
}
return Object.class; return Object.class;
} }
}; };
JTable procTable = getInstance().getProcessTable(); JTable procTable = getInstance().getProcessTable();
procTable.setModel(model); procTable.setModel(model);
resizeColumns(procTable.getColumnModel()); resizeColumns(procTable.getColumnModel());
procTable.getColumnModel().getColumn(0).setMaxWidth(32);
procTable.getColumnModel().getColumn(0).setMinWidth(32);
procTable.setShowGrid(true); procTable.setShowGrid(true);
DefaultTableCellRenderer hr = (DefaultTableCellRenderer) procTable.getTableHeader() DefaultTableCellRenderer hr = (DefaultTableCellRenderer) procTable.getTableHeader()
@ -194,7 +193,7 @@ public class ProcessesForm {
// Matches order of COLUMNS field // Matches order of COLUMNS field
i--; i--;
int pid = p.getProcessID(); int pid = p.getProcessID();
procArr[i][0] = getProcessIcon(p.getCommandLine()); procArr[i][0] = getProcessIcon(p); // getProcessIcon(p.getCommandLine());
procArr[i][1] = pid; procArr[i][1] = pid;
procArr[i][2] = p.getParentProcessID(); procArr[i][2] = p.getParentProcessID();
procArr[i][3] = p.getThreadCount(); procArr[i][3] = p.getThreadCount();
@ -220,24 +219,38 @@ public class ProcessesForm {
return procArr; return procArr;
} }
private static Icon getProcessIcon(String fullProcessPathName) {
if (SystemUtils.IS_OS_WINDOWS) { private static Icon getProcessIcon(OSProcess p) {
String fullProcessPathName = p.getPath();
if (iconCacheMap.containsKey(fullProcessPathName)){
return iconCacheMap.get(fullProcessPathName);
}
File file = new File(fullProcessPathName); File file = new File(fullProcessPathName);
// Get metadata and create an icon if (!file.exists()){
Icon icon = FileSystemView.getFileSystemView() //or add default no icon
.getSystemIcon(file); return null;
// ShellFolder sf = }
// ShellFolder.getShellFolder(file); if (SystemUtils.IS_OS_WINDOWS) {
// Icon icon = new ImageIcon(sf.getIcon(true)); try {
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
iconCacheMap.put(fullProcessPathName,icon);
return icon; return icon;
}catch (Exception e){
// e.printStackTrace();
}
} }
if (SystemUtils.IS_OS_MAC) { if (SystemUtils.IS_OS_MAC) {
final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
Icon icon = fc.getUI().getFileView(fc).getIcon(new File(fullProcessPathName));
/*
final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
Icon icon = fc.getUI().getFileView(fc).getIcon(file);
* */
if (fullProcessPathName.contains("MacOS")) { if (fullProcessPathName.contains("MacOS")) {
String iconPath[] = fullProcessPathName.split("MacOS"); String iconPath[] = fullProcessPathName.split("MacOS");
if (iconPath.length > 0) { if (iconPath.length > 0) {
//"AppIcon.icns" //"AppIcon.icns"
String iconFilePath = iconPath[0] + "Resources/AppIcon.icns"; String iconFilePath = iconPath[0] + "Resources/AppIcon.icns";
File file = new File(iconFilePath);
//Imaging.getAllBufferedImages(iconFilePath); //Imaging.getAllBufferedImages(iconFilePath);
try { try {
if (file.exists()) { if (file.exists()) {
@ -245,8 +258,8 @@ public class ProcessesForm {
BufferedImage bufferedImage = is.get(0); BufferedImage bufferedImage = is.get(0);
BufferedImage bufferedImageR = resizeImage(bufferedImage, 28, 28); BufferedImage bufferedImageR = resizeImage(bufferedImage, 28, 28);
Icon icon = new ImageIcon(bufferedImageR); // Icon icon = new ImageIcon(bufferedImageR);
return icon; // return icon;
} }
} catch (ImageReadException | IOException e) { } catch (ImageReadException | IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);