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.OperatingSystem;
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.filechooser.FileSystemView;
import javax.swing.table.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@ -44,12 +36,14 @@ import java.util.Map;
*/
@Getter
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"};
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 static HashMap<String, Icon> iconCacheMap = new HashMap<>();
private static final Log logger = LogFactory.get();
private static ProcessesForm processesForm;
@ -118,13 +112,18 @@ public class ProcessesForm {
TableModel model = new DefaultTableModel(parseProcesses(os.getProcesses(null, null, 0), App.si), COLUMNS) {
@Override
public Class<?> getColumnClass(int column) {
if (column == 0) return ImageIcon.class;
if (column == 0){
return ImageIcon.class;
}
return Object.class;
}
};
JTable procTable = getInstance().getProcessTable();
procTable.setModel(model);
resizeColumns(procTable.getColumnModel());
procTable.getColumnModel().getColumn(0).setMaxWidth(32);
procTable.getColumnModel().getColumn(0).setMinWidth(32);
procTable.setShowGrid(true);
DefaultTableCellRenderer hr = (DefaultTableCellRenderer) procTable.getTableHeader()
@ -194,7 +193,7 @@ public class ProcessesForm {
// Matches order of COLUMNS field
i--;
int pid = p.getProcessID();
procArr[i][0] = getProcessIcon(p.getCommandLine());
procArr[i][0] = getProcessIcon(p); // getProcessIcon(p.getCommandLine());
procArr[i][1] = pid;
procArr[i][2] = p.getParentProcessID();
procArr[i][3] = p.getThreadCount();
@ -220,24 +219,38 @@ public class ProcessesForm {
return procArr;
}
private static Icon getProcessIcon(String fullProcessPathName) {
private static Icon getProcessIcon(OSProcess p) {
String fullProcessPathName = p.getPath();
if (iconCacheMap.containsKey(fullProcessPathName)){
return iconCacheMap.get(fullProcessPathName);
}
File file = new File(fullProcessPathName);
if (!file.exists()){
//or add default no icon
return null;
}
if (SystemUtils.IS_OS_WINDOWS) {
File file = new File(fullProcessPathName);
// Get metadata and create an icon
Icon icon = FileSystemView.getFileSystemView()
.getSystemIcon(file);
// ShellFolder sf =
// ShellFolder.getShellFolder(file);
// Icon icon = new ImageIcon(sf.getIcon(true));
return icon;
try {
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
iconCacheMap.put(fullProcessPathName,icon);
return icon;
}catch (Exception e){
// e.printStackTrace();
}
}
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")) {
String iconPath[] = fullProcessPathName.split("MacOS");
if (iconPath.length > 0) {
//"AppIcon.icns"
String iconFilePath = iconPath[0] + "Resources/AppIcon.icns";
File file = new File(iconFilePath);
//Imaging.getAllBufferedImages(iconFilePath);
try {
if (file.exists()) {
@ -245,8 +258,8 @@ public class ProcessesForm {
BufferedImage bufferedImage = is.get(0);
BufferedImage bufferedImageR = resizeImage(bufferedImage, 28, 28);
Icon icon = new ImageIcon(bufferedImageR);
return icon;
// Icon icon = new ImageIcon(bufferedImageR);
// return icon;
}
} catch (ImageReadException | IOException e) {
throw new RuntimeException(e);