refactor & add Icon Mac Windows
This commit is contained in:
parent
a683062122
commit
1edeae2c66
15
pom.xml
15
pom.xml
|
@ -32,6 +32,8 @@
|
||||||
<fastjson.version>2.0.32</fastjson.version>
|
<fastjson.version>2.0.32</fastjson.version>
|
||||||
<oshi-core.version>6.4.4</oshi-core.version>
|
<oshi-core.version>6.4.4</oshi-core.version>
|
||||||
<jfreechart.version>1.5.4</jfreechart.version>
|
<jfreechart.version>1.5.4</jfreechart.version>
|
||||||
|
<imaging.version>1.0-alpha3</imaging.version>
|
||||||
|
<xmlwise.version>1.2.11</xmlwise.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -131,18 +133,13 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-imaging</artifactId>
|
<artifactId>commons-imaging</artifactId>
|
||||||
<version>1.0-alpha3</version>
|
<version>${imaging.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.twelvemonkeys.imageio</groupId>
|
<groupId>xmlwise</groupId>
|
||||||
<artifactId>imageio-icns</artifactId>
|
<artifactId>xmlwise</artifactId>
|
||||||
<version>3.10.1</version>
|
<version>${xmlwise.version}</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>ru.oleg-cherednik.icoman</groupId>
|
|
||||||
<artifactId>icon-manager</artifactId>
|
|
||||||
<version>1.0</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -8,14 +8,17 @@ import com.intellij.uiDesigner.core.Spacer;
|
||||||
import com.luoboduner.moo.info.App;
|
import com.luoboduner.moo.info.App;
|
||||||
import com.luoboduner.moo.info.ui.UiConsts;
|
import com.luoboduner.moo.info.ui.UiConsts;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.apache.commons.imaging.ImageReadException;
|
|
||||||
import org.apache.commons.imaging.formats.icns.IcnsImageParser;
|
import org.apache.commons.imaging.formats.icns.IcnsImageParser;
|
||||||
import org.apache.commons.lang3.SystemUtils;
|
import org.apache.commons.lang3.SystemUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import oshi.PlatformEnum;
|
import oshi.PlatformEnum;
|
||||||
import oshi.SystemInfo;
|
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 xmlwise.Plist;
|
||||||
|
|
||||||
|
import javax.swing.Timer;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.filechooser.FileSystemView;
|
import javax.swing.filechooser.FileSystemView;
|
||||||
import javax.swing.table.*;
|
import javax.swing.table.*;
|
||||||
|
@ -23,10 +26,10 @@ import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.net.URI;
|
||||||
import java.util.HashMap;
|
import java.net.URISyntaxException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ProcessesForm
|
* ProcessesForm
|
||||||
|
@ -40,9 +43,9 @@ public class ProcessesForm {
|
||||||
"Process Name"};
|
"Process Name"};
|
||||||
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 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 final static Map<Integer, OSProcess> priorSnapshotMap = new HashMap<>();
|
||||||
|
|
||||||
private static HashMap<String, Icon> iconCacheMap = new HashMap<>();
|
private final static HashMap<String, Icon> iconCacheMap = new HashMap<>();
|
||||||
|
|
||||||
private static final Log logger = LogFactory.get();
|
private static final Log logger = LogFactory.get();
|
||||||
|
|
||||||
|
@ -193,7 +196,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); // getProcessIcon(p.getCommandLine());
|
procArr[i][0] = getProcessIcon(p.getPath());
|
||||||
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();
|
||||||
|
@ -219,56 +222,78 @@ public class ProcessesForm {
|
||||||
return procArr;
|
return procArr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
private static Icon getProcessIcon(OSProcess p) {
|
* Retrieves the executable's associated icon if available, or returns null.
|
||||||
String fullProcessPathName = p.getPath();
|
*
|
||||||
|
* @param fullProcessPathName The full path of the executable process.
|
||||||
|
* @return The associated icon, or null if not found.
|
||||||
|
*/
|
||||||
|
private static Icon getProcessIcon(String fullProcessPathName) {
|
||||||
if (iconCacheMap.containsKey(fullProcessPathName)) {
|
if (iconCacheMap.containsKey(fullProcessPathName)) {
|
||||||
return iconCacheMap.get(fullProcessPathName);
|
return iconCacheMap.get(fullProcessPathName);
|
||||||
}
|
}
|
||||||
|
|
||||||
File file = new File(fullProcessPathName);
|
File file = new File(fullProcessPathName);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
//or add default no icon
|
// Alternatively, provide a default icon or return nul
|
||||||
return null;
|
return UIManager.getIcon("FileView.fileIcon");
|
||||||
}
|
}
|
||||||
if (SystemUtils.IS_OS_WINDOWS) {
|
|
||||||
try {
|
try {
|
||||||
|
if (SystemUtils.IS_OS_WINDOWS) {
|
||||||
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
|
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
|
||||||
iconCacheMap.put(fullProcessPathName, icon);
|
iconCacheMap.put(fullProcessPathName, icon);
|
||||||
return 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";
|
|
||||||
//Imaging.getAllBufferedImages(iconFilePath);
|
|
||||||
try {
|
|
||||||
if (file.exists()) {
|
|
||||||
List<BufferedImage> is = new IcnsImageParser().getAllBufferedImages(new File(iconFilePath));
|
|
||||||
BufferedImage bufferedImage = is.get(0);
|
|
||||||
|
|
||||||
BufferedImage bufferedImageR = resizeImage(bufferedImage, 28, 28);
|
// macOS-specific icon retrieval
|
||||||
// Icon icon = new ImageIcon(bufferedImageR);
|
// maybe find a simple solution
|
||||||
// return icon;
|
// Todo : fix various icons like VLC ...
|
||||||
|
if (SystemUtils.IS_OS_MAC && fullProcessPathName.contains("MacOS")) {
|
||||||
|
String iconPathSplit = fullProcessPathName.split("MacOS")[0];
|
||||||
|
String plistFilePath = iconPathSplit + "Info.plist";
|
||||||
|
|
||||||
|
File pListFile = getFileWithSpaceUri(plistFilePath);
|
||||||
|
if (pListFile.exists()) {
|
||||||
|
Map<String, Object> properties = Plist.load(plistFilePath);
|
||||||
|
String cFBundleIconFile = (String) properties.get("CFBundleIconFile");
|
||||||
|
String iconFilePath = iconPathSplit + "Resources/" + cFBundleIconFile;
|
||||||
|
|
||||||
|
// Ensure file path ends with ".icns"
|
||||||
|
if (!iconFilePath.contains(".icns")) {
|
||||||
|
iconFilePath += ".icns";
|
||||||
}
|
}
|
||||||
} catch (ImageReadException | IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
File iconFile = getFileWithSpaceUri(iconFilePath);
|
||||||
|
if (iconFile.canRead()) {
|
||||||
|
List<BufferedImage> iconImages = new IcnsImageParser().getAllBufferedImages(iconFile);
|
||||||
|
Optional<BufferedImage> resultImage = iconImages
|
||||||
|
.stream().parallel()
|
||||||
|
.filter(num -> num.getRaster().getHeight() == 32).findAny();
|
||||||
|
|
||||||
|
if (resultImage.isPresent()) {
|
||||||
|
BufferedImage bufferedImageR = resizeImage(resultImage.get(), 26, 26);
|
||||||
|
return new ImageIcon(bufferedImageR);
|
||||||
}
|
}
|
||||||
//return new ImageIcon(Toolkit.getDefaultToolkit().getImage(iconFilePath));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
return UIManager.getIcon("FileView.fileIcon");
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
/**
|
||||||
|
* Workaround to open folders files with spaces
|
||||||
|
* @param filePath
|
||||||
|
* @return file
|
||||||
|
*/
|
||||||
|
private static File getFileWithSpaceUri(String filePath) throws URISyntaxException {
|
||||||
|
URI outputURI = new URI(("file:///" + filePath.replaceAll(" ", "%20")));
|
||||||
|
File outputFile = new File(outputURI);
|
||||||
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue