<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jme3.gde.angelfont;

import com.jme3.gde.core.assets.ProjectAssetManager;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;

public final class AngelFontWizardIterator implements WizardDescriptor.InstantiatingIterator {

    private int index;
    private WizardDescriptor wizard;
    private WizardDescriptor.Panel[] panels;

    /**
     * Initialize panels representing individual wizard's steps and sets
     * various properties for them influencing wizard appearance.
     */
    private WizardDescriptor.Panel[] getPanels() {
        if (panels == null) {
            panels = new WizardDescriptor.Panel[]{
                        new AngelFontWizardPanel1(),
                        new AngelFontWizardPanel2()
                    };
            String[] steps = createSteps();
            for (int i = 0; i < panels.length; i++) {
                Component c = panels[i].getComponent();
                if (steps[i] == null) {
                    // Default step name to component name of panel. Mainly
                    // useful for getting the name of the target chooser to
                    // appear in the list of steps.
                    steps[i] = c.getName();
                }
                if (c instanceof JComponent) { // assume Swing components
                    JComponent jc = (JComponent) c;
                    // Sets step number of a component
                    // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                    jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
                    // Sets steps names for a panel
                    jc.putClientProperty("WizardPanel_contentData", steps);
                    // Turn on subtitle creation on each step
                    jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
                    // Show steps on the left side with the image on the background
                    jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
                    // Turn on numbering of all steps
                    jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
                }
            }
        }
        return panels;
    }

    public Set<FileObject> instantiate() throws IOException {
        String name = (String) wizard.getProperty("font_name");
        int fontSize = (Integer) wizard.getProperty("font_size");
        int imageSize = (Integer) wizard.getProperty("image_size");
        int style = (Integer) wizard.getProperty("font_style");
        Project project = (Project) wizard.getProperty("project");
        ProjectAssetManager pm = project.getLookup().lookup(ProjectAssetManager.class);
        if (pm == null) {
            Logger.getLogger(AngelFontWizardIterator.class.getName()).log(Level.WARNING, "No ProjectAssetManager found!");
            return Collections.EMPTY_SET;
        }
        AngelFont font = FontCreator.buildFont(name, imageSize, fontSize, style);
        BufferedImage fontImage = font.getImage();
        ByteBuffer scratch = ByteBuffer.allocateDirect(4 * fontImage.getWidth() * fontImage.getHeight());
        byte[] data = (byte[]) fontImage.getRaster().getDataElements(0, 0,
                fontImage.getWidth(), fontImage.getHeight(), null);
        scratch.clear();
        scratch.put(data);
        scratch.rewind();
        name = name.replaceAll(" ", "");
        File outputFile;
        FileObject object;
        try {
            if (pm.getAssetFolder().getFileObject("Interface") == null) {
                pm.getAssetFolder().createFolder("Interface");
            }
            if (pm.getAssetFolder().getFileObject("Interface/Fonts") == null) {
                pm.getAssetFolder().getFileObject("Interface").createFolder("Fonts");
            }
            outputFile = FileUtil.toFile(pm.getAssetFolder().getFileObject("Interface/Fonts"));
            if (!outputFile.getName().endsWith(".png")) {
                outputFile = new File(outputFile.getAbsoluteFile() + File.separator + name + ".png");
            }
            // write png file
            ImageIO.write(fontImage, "PNG", outputFile);

            object = pm.getAssetFolder().getFileObject("Interface/Fonts/" + name, "fnt");
            if (object == null) {
                object = pm.getAssetFolder().getFileObject("Interface/Fonts").createData(name, "fnt");
            }
            OutputStreamWriter out = new OutputStreamWriter(object.getOutputStream());
            out.write(font.getDescription());
            out.close();
        } catch (Exception e) {
            Exceptions.printStackTrace(e);
            return Collections.EMPTY_SET;
        }
        Set<FileObject> set = new HashSet<FileObject>();
        set.add(FileUtil.toFileObject(outputFile));
        set.add(object);
        return set;
    }

    public void initialize(WizardDescriptor wizard) {
        this.wizard = wizard;
    }

    public void uninitialize(WizardDescriptor wizard) {
        panels = null;
    }

    public WizardDescriptor.Panel current() {
        return getPanels()[index];
    }

    public String name() {
        return index + 1 + ". from " + getPanels().length;
    }

    public boolean hasNext() {
        return index < getPanels().length - 1;
    }

    public boolean hasPrevious() {
        return index > 0;
    }
<commit_msg>- use only FileObject instead of File<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jme3.gde.angelfont;

import com.jme3.gde.core.assets.ProjectAssetManager;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;

public final class AngelFontWizardIterator implements WizardDescriptor.InstantiatingIterator {

    private int index;
    private WizardDescriptor wizard;
    private WizardDescriptor.Panel[] panels;

    /**
     * Initialize panels representing individual wizard's steps and sets
     * various properties for them influencing wizard appearance.
     */
    private WizardDescriptor.Panel[] getPanels() {
        if (panels == null) {
            panels = new WizardDescriptor.Panel[]{
                        new AngelFontWizardPanel1(),
                        new AngelFontWizardPanel2()
                    };
            String[] steps = createSteps();
            for (int i = 0; i < panels.length; i++) {
                Component c = panels[i].getComponent();
                if (steps[i] == null) {
                    // Default step name to component name of panel. Mainly
                    // useful for getting the name of the target chooser to
                    // appear in the list of steps.
                    steps[i] = c.getName();
                }
                if (c instanceof JComponent) { // assume Swing components
                    JComponent jc = (JComponent) c;
                    // Sets step number of a component
                    // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                    jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
                    // Sets steps names for a panel
                    jc.putClientProperty("WizardPanel_contentData", steps);
                    // Turn on subtitle creation on each step
                    jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
                    // Show steps on the left side with the image on the background
                    jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
                    // Turn on numbering of all steps
                    jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
                }
            }
        }
        return panels;
    }

    /**
     * Creates the files based on the info in the WizardDescriptor
     * @return
     * @throws IOException
     */
    public Set<FileObject> instantiate() throws IOException {
        String name = (String) wizard.getProperty("font_name");
        int fontSize = (Integer) wizard.getProperty("font_size");
        int imageSize = (Integer) wizard.getProperty("image_size");
        int style = (Integer) wizard.getProperty("font_style");
        Project project = (Project) wizard.getProperty("project");
        ProjectAssetManager pm = project.getLookup().lookup(ProjectAssetManager.class);
        if (pm == null) {
            Logger.getLogger(AngelFontWizardIterator.class.getName()).log(Level.WARNING, "No ProjectAssetManager found!");
            return Collections.EMPTY_SET;
        }
        AngelFont font = FontCreator.buildFont(name, imageSize, fontSize, style);
        BufferedImage fontImage = font.getImage();
        ByteBuffer scratch = ByteBuffer.allocateDirect(4 * fontImage.getWidth() * fontImage.getHeight());
        byte[] data = (byte[]) fontImage.getRaster().getDataElements(0, 0,
                fontImage.getWidth(), fontImage.getHeight(), null);
        scratch.clear();
        scratch.put(data);
        scratch.rewind();
        name = name.replaceAll(" ", "");
        FileObject imageFile;
        FileObject descriptionFile;
        try {
            //create fonts folder if it doesnt exist
            if (pm.getAssetFolder().getFileObject("Interface") == null) {
                pm.getAssetFolder().createFolder("Interface");
            }
            if (pm.getAssetFolder().getFileObject("Interface/Fonts") == null) {
                pm.getAssetFolder().getFileObject("Interface").createFolder("Fonts");
            }
            //create PNG file
            imageFile = pm.getAssetFolder().getFileObject("Interface/Fonts" + name, "png");
            if (imageFile == null) {
                imageFile = pm.getAssetFolder().getFileObject("Interface/Fonts").createData(name, "png");
            }
            OutputStream out = imageFile.getOutputStream();
            ImageIO.write(fontImage, "PNG", out);
            out.close();
            //create fnt file
            descriptionFile = pm.getAssetFolder().getFileObject("Interface/Fonts/" + name, "fnt");
            if (descriptionFile == null) {
                descriptionFile = pm.getAssetFolder().getFileObject("Interface/Fonts").createData(name, "fnt");
            }
            OutputStreamWriter out2 = new OutputStreamWriter(descriptionFile.getOutputStream());
            out2.write(font.getDescription());
            out2.close();
        } catch (Exception e) {
            Exceptions.printStackTrace(e);
            return Collections.EMPTY_SET;
        }
        Set<FileObject> set = new HashSet<FileObject>();
        set.add(imageFile);
        set.add(descriptionFile);
        return set;
    }

    public void initialize(WizardDescriptor wizard) {
        this.wizard = wizard;
    }

    public void uninitialize(WizardDescriptor wizard) {
        panels = null;
    }

    public WizardDescriptor.Panel current() {
        return getPanels()[index];
    }

    public String name() {
        return index + 1 + ". from " + getPanels().length;
    }

    public boolean hasNext() {
        return index < getPanels().length - 1;
    }

    public boolean hasPrevious() {
        return index > 0;
    }

{'fraction_non_alphanumeric': 0.10573777032958263, 'fraction_numerical': 0.0023142606336286013, 'mean_word_length': 2.2626920072897683, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '4941305', 'n_tokens_mistral': 3277, 'n_tokens_neox': 3144, 'n_words': 781}