<commit_before>/**
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */
package com.pump.icon;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;

import com.pump.util.JVM;

/** This offers a static method to best retrieve a File's icon.
 */
public class FileIcon {
	
	/** This captures a File's icon and saves it as a PNG.
	 * This is tested on Mac; I'm not sure how it will perform
	 * on Windows. You may need to modify the icon size. As of this
	 * writing on Mac: you can pass most any Dimension object and
	 * the icon will scale upwards safely.
	 */
	public static void main(String[] args) throws IOException {
		FileDialog fd = new FileDialog(new Frame());
		fd.pack();
		fd.setVisible(true);
		File f = new File(fd.getDirectory()+fd.getFile());
		Icon icon = getIcon(f);
		icon = new ScaledIcon(icon, 24, 24);
		BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = bi.createGraphics();
		icon.paintIcon(null, g, 0, 0);
		g.dispose();
		File f2 = new File(f.getParentFile(), f.getName()+".png");
		if(f2.exists())
			System.exit(1);
		ImageIO.write(bi, "png", f2);
		System.exit(0);;
	}
	
	/** Return the icon of a File.
	 * <p>Unfortunately Windows and Mac require different
	 * approaches.
	 * 
	 * @param file the file to get the icon of.
	 * @return an icon for this file.
	 */
	public static Icon getIcon(File file) {
		if(file==null) throw new NullPointerException();
		
		if(JVM.isWindows) {
			//on Macs this appears to only return the vanilla folder/file icons:
			FileSystemView fsv = FileSystemView.getFileSystemView();
			Icon icon = fsv.getSystemIcon(file);
			return icon;
		} else {
			//but this returns different icons for different folders/icons:
			FileView fileView = getFileView();
			return fileView.getIcon(file);
		}
	}

	private static FileView sharedFileView;
	protected static FileView getFileView() {
		if(sharedFileView==null) {
			JFileChooser chooser = new JFileChooser();
			sharedFileView = chooser.getUI().getFileView(chooser);
		}
		return sharedFileView;
	}
}<commit_msg>Updating how we retrieve file icons on Mac<commit_after>/**
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */
package com.pump.icon;

import java.io.File;
import java.lang.reflect.Constructor;

import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;

import com.pump.util.JVM;

/**
 * This offers a static method to best retrieve a File's icon.
 */
public class FileIcon {

	/**
	 * Return the icon of a File.
	 * <p>
	 * Unfortunately Windows and Mac require different approaches.
	 * 
	 * @param file
	 *            the file to get the icon of.
	 * @return an icon for this file.
	 */
	public static Icon getIcon(File file) {
		if (file == null)
			throw new NullPointerException();

		if (JVM.isWindows) {
			// on Macs this appears to only return the vanilla folder/file
			// icons:
			FileSystemView fsv = FileSystemView.getFileSystemView();
			Icon icon = fsv.getSystemIcon(file);
			if (icon != null)
				return icon;
		}
		if (JVM.isMac) {
			try {
				Class<?> z = Class.forName("com.apple.laf.AquaIcon$FileIcon");
				Constructor<?> constructor = z
						.getConstructor(new Class[] { File.class });
				constructor.setAccessible(true);
				Icon icon = (Icon) constructor.newInstance(file);
				if (icon != null)
					return icon;
			} catch (Throwable t) {
			}
		}

		// but this returns different icons for different folders/icons:
		FileView fileView = getFileView();
		return fileView.getIcon(file);
	}

	private static FileView sharedFileView;

	protected static FileView getFileView() {
		if (sharedFileView == null) {
			JFileChooser chooser = new JFileChooser();
			sharedFileView = chooser.getUI().getFileView(chooser);
		}
		return sharedFileView;
	}
}
{'fraction_non_alphanumeric': 0.10915934755332497, 'fraction_numerical': 0.0027185278126306985, 'mean_word_length': 3.7782217782217784, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6954502', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1480, 'n_words': 460}