<commit_before>/*
 * Created on Oct 29, 2006
 * @author Fabio
 */
package org.python.pydev.navigator.ui;

import java.util.HashSet;
import java.util.LinkedList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.navigator.ContributorTrackingSet;
import org.eclipse.ui.internal.navigator.NavigatorContentService;
import org.eclipse.ui.navigator.CommonNavigator;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.navigator.INavigatorPipelineService;
import org.eclipse.ui.navigator.PipelinedShapeModification;
import org.eclipse.ui.part.IShowInTarget;
import org.eclipse.ui.part.ShowInContext;
import org.python.pydev.navigator.IWrappedResource;
import org.python.pydev.navigator.PythonFile;

/**
 * This class is the package explorer for pydev. It uses the CNF (Common Navigator Framework) to show
 * the resources as python elements.
 */
public class PydevPackageExplorer extends CommonNavigator implements IShowInTarget {

    /**
     * This viewer is the one used instead of the common viewer -- should only be used to fix failures in the base class.
     */
	public static class PydevCommonViewer extends CommonViewer {
		public PydevCommonViewer(String id, Composite parent, int style) {
			super(id, parent, style);
		}
        /**
         * Returns the tree path for the given item.
         * 
         * It's overriden because when using mylar, the paths may be expanded but not shown, so segment is null
         * -- that's why we return null if a given segment is null (instead of the assert that it contains in the superclass) 
         * @since 3.2
         */
        @Override
        protected TreePath getTreePathFromItem(Item item) {
            LinkedList<Object> segments = new LinkedList<Object>();
            while(item!=null) {
                Object segment = item.getData();
                if(segment == null){
                    return null;
                }
                segments.addFirst(segment);
                item = getParentItem(item);
            }
            return new TreePath(segments.toArray());
        }
	}

    /**
     * This is the memento to be used.
     */
	private IMemento memento;

    /**
     * Overridden to keep the memento to be used later (it's private in the superclass).
     */
	public void init(IViewSite aSite, IMemento aMemento) throws PartInitException {
		super.init(aSite, aMemento);
		memento = aMemento;
	}

    /**
     * Overridden to create our viewer and not the superclass CommonViewer.
     * 
     * (Unfortunately, the superclass does a little more than creating it, so, we have to do those operations here 
     * too -- that's why we have to keep the memento object in the init method).
     */
	@Override
	protected CommonViewer createCommonViewer(Composite aParent) {
        //super.createCommonViewer(aParent); -- don't even call the super class
		CommonViewer aViewer = new PydevCommonViewer(getViewSite().getId(), aParent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		initListeners(aViewer);
		//we do that only after the part is completely created (because otherwise the state is reverted later)
		//aViewer.getNavigatorContentService().restoreState(memento);
		return aViewer;
	}

	/**
	 * Overridden because if the state is not restored as the last thing, it is reverted back to the previous state.
	 */
	@Override
	public void createPartControl(Composite aParent) {
	    super.createPartControl(aParent);
	    getCommonViewer().getNavigatorContentService().restoreState(memento);
	}
	
	/**
	 * Returns the element contained in the EditorInput
	 */
	Object getElementOfInput(IEditorInput input) {
		if (input instanceof IFileEditorInput) {
			return ((IFileEditorInput) input).getFile();
		}
		return null;
	}

	/**
	 * Implements the 'show in...' action
	 */
	public boolean show(ShowInContext context) {
		Object elementOfInput = null;
		ISelection selection = context.getSelection();
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
			if (structuredSelection.size() == 1) {<commit_msg>*** empty log message ***<commit_after>/*
 * Created on Oct 29, 2006
 * @author Fabio
 */
package org.python.pydev.navigator.ui;

import java.util.HashSet;
import java.util.LinkedList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.navigator.ContributorTrackingSet;
import org.eclipse.ui.internal.navigator.NavigatorContentService;
import org.eclipse.ui.navigator.CommonNavigator;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.navigator.INavigatorPipelineService;
import org.eclipse.ui.navigator.PipelinedShapeModification;
import org.eclipse.ui.part.IShowInTarget;
import org.eclipse.ui.part.ShowInContext;
import org.python.pydev.navigator.IWrappedResource;
import org.python.pydev.navigator.PythonFile;
import org.python.pydev.plugin.PydevPlugin;

/**
 * This class is the package explorer for pydev. It uses the CNF (Common Navigator Framework) to show
 * the resources as python elements.
 */
public class PydevPackageExplorer extends CommonNavigator implements IShowInTarget {

    /**
     * This viewer is the one used instead of the common viewer -- should only be used to fix failures in the base class.
     */
	public static class PydevCommonViewer extends CommonViewer {
		public PydevCommonViewer(String id, Composite parent, int style) {
			super(id, parent, style);
		}
        /**
         * Returns the tree path for the given item.
         * 
         * It's overriden because when using mylar, the paths may be expanded but not shown, so segment is null
         * -- that's why we return null if a given segment is null (instead of the assert that it contains in the superclass) 
         * @since 3.2
         */
        @Override
        protected TreePath getTreePathFromItem(Item item) {
            LinkedList<Object> segments = new LinkedList<Object>();
            while(item!=null) {
                Object segment = item.getData();
                if(segment == null){
                    return null;
                }
                segments.addFirst(segment);
                item = getParentItem(item);
            }
            return new TreePath(segments.toArray());
        }
	}

    /**
     * This is the memento to be used.
     */
	private IMemento memento;

    /**
     * Overridden to keep the memento to be used later (it's private in the superclass).
     */
	public void init(IViewSite aSite, IMemento aMemento) throws PartInitException {
		super.init(aSite, aMemento);
		memento = aMemento;
	}

    /**
     * Overridden to create our viewer and not the superclass CommonViewer.
     * 
     * (Unfortunately, the superclass does a little more than creating it, so, we have to do those operations here 
     * too -- that's why we have to keep the memento object in the init method).
     */
	@Override
	protected CommonViewer createCommonViewer(Composite aParent) {
        //super.createCommonViewer(aParent); -- don't even call the super class
		CommonViewer aViewer = new PydevCommonViewer(getViewSite().getId(), aParent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		initListeners(aViewer);
		
		//commented: we do that only after the part is completely created (because otherwise the state is reverted later)
		//aViewer.getNavigatorContentService().restoreState(memento);
		
		try {
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IProject[] projects = root.getProjects();
			if(projects != null && projects.length > 0){
				//just to initialize it...
				//see bug https://sourceforge.net/tracker/index.php?func=detail&aid=1747611&group_id=85796&atid=577329 
				//for details (it's trying to load the extensions while trying to access them, so, this is to initialize
				//them early).
				//this can probably be removed once that's fixed.
				//TODO: Check once eclipse 3.3.1 is released.
				((NavigatorContentService)aViewer.getNavigatorContentService()).findContentExtensionsByTriggerPoint(projects[0]);
			}
		} catch (RuntimeException e) {
			PydevPlugin.log(e);
		}
		return aViewer;
	}

	/**
	 * Overridden because if the state is not restored as the last thing, it is reverted back to the previous state.
	 */
	@Override
	public void createPartControl(Composite aParent) {
	    super.createPartControl(aParent);
	    getCommonViewer().getNavigatorContentService().restoreState(memento);
	}
	
	/**
	 * Returns the element contained in the EditorInput
	 */
	Object getElementOfInput(IEditorInput input) {
		if (input instanceof IFileEditorInput) {
			return ((IFileEditorInput) input).getFile();
		}
		return null;
	}

	/**
	 * Implements the 'show in...' action
	 */
	public boolean show(ShowInContext context) {
		Object elementOfInput = null;
		ISelection selection = context.getSelection();
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
			if (structuredSelection.size() == 1) {
{'fraction_non_alphanumeric': 0.0940046565774156, 'fraction_numerical': 0.003977493209157935, 'mean_word_length': 3.4958569559529002, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '2774082', 'n_tokens_mistral': 3170, 'n_tokens_neox': 2962, 'n_words': 859}