<commit_before>package org.curriki.gwt.client.widgets.browseasset;

import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Tree;
import org.curriki.gwt.client.CurrikiService;
import org.curriki.gwt.client.CurrikiAsyncCallback;
import org.curriki.gwt.client.Main;
import org.curriki.gwt.client.SelectedAssetChangeEvent;
import org.curriki.gwt.client.SelectedAssetChangeListener;
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 * @author jeremi
 */

public class BrowseAsset  extends Tree implements SelectedAssetChangeListener {
    protected AssetTreeItem rootItem = null;
    protected WindowResizeListener resizeListener = null;
    protected AssetTreeItem currentItem = null;

    public BrowseAsset(){
        super();
        addStyleName("browse-asset-tree");
    }

    public BrowseAsset(String rootAsset){
        super();
        initTree(rootAsset, null);
        addStyleName("browse-asset-tree");
    }

    public String getRootItemName(){
        return rootItem.getPageName();
    }

    public BrowseAsset(String rootAsset, WindowResizeListener resizeListener){
        super();
        this.resizeListener = resizeListener;
        initTree(rootAsset, null);
    }

    public boolean selectItem(String assetPageName){
        return selectItem(assetPageName, null, false);
    }

    public boolean selectCurrentItem(String assetPageName){
        return selectItem(assetPageName, null, true);
    }

    private boolean selectItem(String assetPageName, AssetTreeItem treeItem, boolean isCurrentItem){
        if (assetPageName == null)
            return false;

        if (treeItem == null){
            if (rootItem == null) {
                return false;
            }
            treeItem = (AssetTreeItem) getItem(0);
            if (treeItem == null) {
                return false;
            }
        }
        if (treeItem.getPageName().equals(assetPageName)) {
            if (isCurrentItem)
                setCurrentItem(treeItem);
            else {
                // For selection state
                treeItem.setSelected(true);
                // Mark it selected in the tree
                setSelectedItem(treeItem, false);

            }
            // make sure it is visible
            ensureSelectedItemVisible();
            return true;
        }

        for (int i = 0; i < treeItem.getChildCount(); i++){
            if (!(treeItem.getChild(i) instanceof AssetTreeItem))
                continue;
            AssetTreeItem child = (AssetTreeItem) treeItem.getChild(i);
            if(selectItem(assetPageName, child, isCurrentItem)) {
                return true;
            }
        }
        return false;
    }

    public void initTree(String rootAsset, CurrikiAsyncCallback callback){
        loadChildren(rootAsset, callback);
    }


    private void setCurrentItem(AssetTreeItem item){
        if (currentItem != null) {
            currentItem.setCurrentItem(false);
        }
        currentItem = item;
        currentItem.setCurrentItem(true);
    }

    /**
     * Force a reload of the tree
     */
    public void reload(CurrikiAsyncCallback callback){
        if (rootItem != null)
            loadChildren(rootItem.getPageName(), callback);
    }


    private void loadChildren(String rootAsset, final CurrikiAsyncCallback callback){
        CurrikiService.App.getInstance().getFullTreeItem(rootAsset, new CurrikiAsyncCallback(){

            public void onFailure(Throwable caught) {
                super.onFailure(caught);
                if (callback!=null)
                 callback.onFailure(caught);
            }

            public void onSuccess(Object result) {
                super.onSuccess(result);
                AssetItem items = (AssetItem) result;
                rootItem = new AssetTreeItem(items);
                clear();
                addItem(rootItem);
                selectCurrentItem(Main.getSingleton().getEditor().getCurrentAsset().getFullName());
                rootItem.setState(true, false);
                if (resizeListener != null){
                    resizeListener.onWindowResized(getOffsetHeight(), getOffsetWidth());
                }
                if (callback!=null)
                 callback.onSuccess(result);
            }
        });
    }


    public void setResizeListener(WindowResizeListener resizeListener) {
        this.resizeListener = resizeListener;
    }

    public void onSelectedAssetChange(SelectedAssetChangeEvent evt) {
        selectItem(evt.getFullName());
    }

    public String findParent(String currentAssetPageName) {
        if (rootItem==null)
         return "";
        else
         return findParent(currentAssetPageName, rootItem);
    }

    private String findParent(String assetPageName, AssetTreeItem treeItem){
        if (assetPageName == null)
            return "";

        if (treeItem == null){
            treeItem = (AssetTreeItem) getItem(0);
            if (treeItem == null) {
                return "";
            }
        }
        if (treeItem.getPageName().equals(assetPageName)) {
            if (treeItem.getParentItem()!=null) {
              String pageName = ((AssetTreeItem)treeItem.getParentItem()).getPageName();
              if ((pageName!=null)&&(!pageName.equals("")))
                return pageName;
              else
                return "";
            }
        }

        for (int i = 0; i < treeItem.getChildCount(); i++){
            if (!(treeItem.getChild(i) instanceof AssetTreeItem))
                continue;
            AssetTreeItem child = (AssetTreeItem) treeItem.getChild(i);
            String parent = findParent(assetPageName, child);
            if ((!parent.equals("")) && (parent!=null))<commit_msg>CURRIKI-3122 Edit in CB not working with an ACE Template<commit_after>package org.curriki.gwt.client.widgets.browseasset;

import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Tree;
import org.curriki.gwt.client.CurrikiService;
import org.curriki.gwt.client.CurrikiAsyncCallback;
import org.curriki.gwt.client.Main;
import org.curriki.gwt.client.SelectedAssetChangeEvent;
import org.curriki.gwt.client.SelectedAssetChangeListener;
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 * @author jeremi
 */

public class BrowseAsset  extends Tree implements SelectedAssetChangeListener {
    protected AssetTreeItem rootItem = null;
    protected WindowResizeListener resizeListener = null;
    protected AssetTreeItem currentItem = null;

    public BrowseAsset(){
        super();
        addStyleName("browse-asset-tree");
    }

    public BrowseAsset(String rootAsset){
        super();
        initTree(rootAsset, null);
        addStyleName("browse-asset-tree");
    }

    public String getRootItemName(){
        return rootItem.getPageName();
    }

    public BrowseAsset(String rootAsset, WindowResizeListener resizeListener){
        super();
        this.resizeListener = resizeListener;
        initTree(rootAsset, null);
    }

    public boolean selectItem(String assetPageName){
        return selectItem(assetPageName, null, false);
    }

    public boolean selectCurrentItem(String assetPageName){
        return selectItem(assetPageName, null, true);
    }

    private boolean selectItem(String assetPageName, AssetTreeItem treeItem, boolean isCurrentItem){
        if (assetPageName == null)
            return false;

        if (treeItem == null){
            if (rootItem == null) {
                return false;
            }
            treeItem = (AssetTreeItem) getItem(0);
            if (treeItem == null) {
                return false;
            }
        }
        if (treeItem.getPageName().equals(assetPageName)) {
            if (isCurrentItem)
                setCurrentItem(treeItem);
            else {
                // For selection state
                treeItem.setSelected(true);
                // Mark it selected in the tree
                setSelectedItem(treeItem, false);

            }
            // make sure it is visible
            ensureSelectedItemVisible();
            return true;
        }

        for (int i = 0; i < treeItem.getChildCount(); i++){
            if (!(treeItem.getChild(i) instanceof AssetTreeItem))
                continue;
            AssetTreeItem child = (AssetTreeItem) treeItem.getChild(i);
            if(selectItem(assetPageName, child, isCurrentItem)) {
                return true;
            }
        }
        return false;
    }

    public void initTree(String rootAsset, CurrikiAsyncCallback callback){
        loadChildren(rootAsset, callback);
    }


    private void setCurrentItem(AssetTreeItem item){
        if (currentItem != null) {
            currentItem.setCurrentItem(false);
        }
        currentItem = item;
        currentItem.setCurrentItem(true);
    }

    /**
     * Force a reload of the tree
     */
    public void reload(CurrikiAsyncCallback callback){
        if (rootItem != null)
            loadChildren(rootItem.getPageName(), callback);
    }


    private void loadChildren(String rootAsset, final CurrikiAsyncCallback callback){
        CurrikiService.App.getInstance().getFullTreeItem(rootAsset, new CurrikiAsyncCallback(){

            public void onFailure(Throwable caught) {
                super.onFailure(caught);
                if (callback!=null)
                 callback.onFailure(caught);
            }

            public void onSuccess(Object result) {
                super.onSuccess(result);
                AssetItem items = (AssetItem) result;
                rootItem = new AssetTreeItem(items);
                clear();
                addItem(rootItem);
                selectCurrentItem(Main.getSingleton().getEditor().getCurrentAsset().getFullName());
                rootItem.setState(true, false);
                if (resizeListener != null){
                    resizeListener.onWindowResized(getOffsetHeight(), getOffsetWidth());
                }
                if (callback!=null)
                 callback.onSuccess(result);
            }
        });
    }


    public void setResizeListener(WindowResizeListener resizeListener) {
        this.resizeListener = resizeListener;
    }

    public void onSelectedAssetChange(SelectedAssetChangeEvent evt) {
        selectItem(evt.getFullName());
    }

    public String findParent(String currentAssetPageName) {
        if (rootItem==null)
         return "";
        else
         return findParent(currentAssetPageName, rootItem);
    }

    private String findParent(String assetPageName, AssetTreeItem treeItem){
        if (assetPageName == null)
            return "";

        if (treeItem == null){
            treeItem = (AssetTreeItem) getItem(0);
            if (treeItem == null) {
                return "";
            }
        }
        if (treeItem.getPageName().equals(assetPageName)) {
            if (treeItem.getParentItem()!=null) {
              String pageName = ((AssetTreeItem)treeItem.getParentItem()).getPageName();
              if ((pageName!=null)&&(!pageName.equals("")))
                return pageName;
            }
            return "";
        }

        for (int i = 0; i < treeItem.getChildCount(); i++){
            if (!(treeItem.getChild(i) instanceof AssetTreeItem))
                continue;
            AssetTreeItem child = (AssetTreeItem) treeItem.getChild(i);
            String parent = findParent(assetPageName, child);
            if ((!parent.equals("")) && (parent!=null))
{'fraction_non_alphanumeric': 0.09153439153439154, 'fraction_numerical': 0.002872260015117158, 'mean_word_length': 2.2588669950738915, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2772432', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3329, 'n_words': 901}