<commit_before>/**
 * Copyright (c) 2009 - 2010 AppWork UG(haftungsbeschränkt) <<EMAIL>>
 * 
 * This file is part of org.appwork.utils.swing
 * 
 * This software is licensed under the Artistic License 2.0,
 * see the LICENSE file or http://www.opensource.org/licenses/artistic-license-2.0.php
 * for details
 */
package org.appwork.utils.swing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;

import javax.swing.JTree;
import javax.swing.tree.TreePath;

import org.appwork.utils.logging.Log;

/**
 * This class is used to save the selection states and expanded states of each
 * tree node before rebuilding the tree. After rebuilding, the model is able to
 * restore these states.
 * 
 * @author $Author: unknown$
 * 
 */
public class TreeModelStateSaver {

    protected JTree                        tree;
    /**
     * Stores for each node the expanded state
     */
    private final HashMap<Object, Boolean> expandCache;

    /**
     * treePath for internal use
     */
    private TreePath                       treePath;

    /**
     * Stores all selected Pathes
     */
    protected TreePath[]                   selectedPathes;

    /**
     * @param tree
     */
    public TreeModelStateSaver(final JTree tree) {
        this.tree = tree;
        this.expandCache = new HashMap<Object, Boolean>();
    }

    /**
     * @return the expandCache
     */
    public HashMap<Object, Boolean> getExpandCache() {
        return this.expandCache;
    }

    /**
     * @return the {@link TreeModelStateSaver#selectedPathes}
     * @see TreeModelStateSaver#selectedPathes
     */
    public TreePath[] getSelectedPathes() {
        return this.selectedPathes;
    }

    /**
     * Restore the saved tree state
     */
    public void restore() {
        new EDTHelper<Object>() {

            @Override
            public Object edtRun() {
                try {
                    if (TreeModelStateSaver.this.tree.getModel() != null) {
                        TreeModelStateSaver.this.restoreState(TreeModelStateSaver.this.tree.getModel().getRoot(), new ArrayList<Object>());
                    }
                    final TreePath[] selectedPathes = TreeModelStateSaver.this.getSelectedPathes();
                    if (selectedPathes != null && selectedPathes.length > 0) {
                        TreeModelStateSaver.this.tree.getSelectionModel().clearSelection();
                        TreeModelStateSaver.this.tree.getSelectionModel().setSelectionPaths(selectedPathes);
                    }
                } catch (Throwable e) {
                    Log.exception(Level.WARNING, e);
                }
                return null;
            }

        }.start();
    }

    protected void restoreState(final Object node, final ArrayList<Object> path) {
        new EDTHelper<Object>() {

            @Override
            public Object edtRun() {
                if (node == null) { return null; }
                path.add(node);
                TreeModelStateSaver.this.treePath = new TreePath(path.toArray(new Object[] {}));
                final Boolean bo = TreeModelStateSaver.this.expandCache.get(node);
                if (bo != null && bo.booleanValue()) {
                    TreeModelStateSaver.this.tree.expandPath(TreeModelStateSaver.this.treePath);
                }

                for (int i = 0; i < TreeModelStateSaver.this.tree.getModel().getChildCount(node); i++) {
                    TreeModelStateSaver.this.restoreState(TreeModelStateSaver.this.tree.getModel().getChild(node, i), new ArrayList<Object>(path));
                }
                return null;
            }

        }.start();

    }

    /**
     * Save the current state of the tree
     */
    public void save() {
        if (this.tree.getModel() != null) {
            this.saveState(this.tree.getModel().getRoot(), new ArrayList<Object>());
        }
        this.selectedPathes = this.tree.getSelectionPaths();
    }

    /**
     * Saves the expaned states of each node to cacheMap runs rekursive
     * 
     * @param root
     */
    private void saveState(final Object node, final ArrayList<Object> path) {
        path.add(node);
        try {<commit_msg>TreeModelStateSaver: catch exceptions while doing async tree changes<commit_after>/**
 * Copyright (c) 2009 - 2010 AppWork UG(haftungsbeschränkt) <<EMAIL>>
 * 
 * This file is part of org.appwork.utils.swing
 * 
 * This software is licensed under the Artistic License 2.0,
 * see the LICENSE file or http://www.opensource.org/licenses/artistic-license-2.0.php
 * for details
 */
package org.appwork.utils.swing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;

import javax.swing.JTree;
import javax.swing.tree.TreePath;

import org.appwork.utils.logging.Log;

/**
 * This class is used to save the selection states and expanded states of each
 * tree node before rebuilding the tree. After rebuilding, the model is able to
 * restore these states.
 * 
 * @author $Author: unknown$
 * 
 */
public class TreeModelStateSaver {

    protected JTree                        tree;
    /**
     * Stores for each node the expanded state
     */
    private final HashMap<Object, Boolean> expandCache;

    /**
     * treePath for internal use
     */
    private TreePath                       treePath;

    /**
     * Stores all selected Pathes
     */
    protected TreePath[]                   selectedPathes;

    /**
     * @param tree
     */
    public TreeModelStateSaver(final JTree tree) {
        this.tree = tree;
        this.expandCache = new HashMap<Object, Boolean>();
    }

    /**
     * @return the expandCache
     */
    public HashMap<Object, Boolean> getExpandCache() {
        return this.expandCache;
    }

    /**
     * @return the {@link TreeModelStateSaver#selectedPathes}
     * @see TreeModelStateSaver#selectedPathes
     */
    public TreePath[] getSelectedPathes() {
        return this.selectedPathes;
    }

    /**
     * Restore the saved tree state
     */
    public void restore() {
        new EDTHelper<Object>() {

            @Override
            public Object edtRun() {
                try {
                    if (TreeModelStateSaver.this.tree.getModel() != null) {
                        TreeModelStateSaver.this.restoreState(TreeModelStateSaver.this.tree.getModel().getRoot(), new ArrayList<Object>());
                    }
                    final TreePath[] selectedPathes = TreeModelStateSaver.this.getSelectedPathes();
                    if (selectedPathes != null && selectedPathes.length > 0) {
                        TreeModelStateSaver.this.tree.getSelectionModel().clearSelection();
                        TreeModelStateSaver.this.tree.getSelectionModel().setSelectionPaths(selectedPathes);
                    }
                } catch (Throwable e) {
                    Log.exception(Level.WARNING, e);
                }
                return null;
            }

        }.start();
    }

    protected void restoreState(final Object node, final ArrayList<Object> path) {
        new EDTHelper<Object>() {

            @Override
            public Object edtRun() {
                if (node == null) { return null; }
                path.add(node);
                TreeModelStateSaver.this.treePath = new TreePath(path.toArray(new Object[] {}));
                final Boolean bo = TreeModelStateSaver.this.expandCache.get(node);
                try {
                    if (bo != null && bo.booleanValue()) {
                        TreeModelStateSaver.this.tree.expandPath(TreeModelStateSaver.this.treePath);
                    }
                } catch (final Throwable e) {
                    Log.exception(Level.WARNING, e);
                }

                for (int i = 0; i < TreeModelStateSaver.this.tree.getModel().getChildCount(node); i++) {
                    try {
                        TreeModelStateSaver.this.restoreState(TreeModelStateSaver.this.tree.getModel().getChild(node, i), new ArrayList<Object>(path));
                    } catch (final Throwable e) {
                        Log.exception(Level.WARNING, e);
                    }
                }
                return null;
            }

        }.start();

    }

    /**
     * Save the current state of the tree
     */
    public void save() {
        if (this.tree.getModel() != null) {
            this.saveState(this.tree.getModel().getRoot(), new ArrayList<Object>());
        }
        this.selectedPathes = this.tree.getSelectionPaths();
    }

    /**
     * Saves the expaned states of each node to cacheMap runs rekursive
     * 
     * @param root
     */
    private void saveState(final Object node, final ArrayList<Object> path) {
        path.add(node);
        try {
{'fraction_non_alphanumeric': 0.1047424087342204, 'fraction_numerical': 0.003184351188445354, 'mean_word_length': 1.8738562091503268, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '821997', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2304, 'n_words': 561}