<commit_before>package org.jboss.reddeer.eclipse.jdt.ui;

import java.util.ArrayList;
import java.util.List;

import org.jboss.reddeer.eclipse.core.resources.AbstractProject;
import org.jboss.reddeer.eclipse.core.resources.ExplorerItem;
import org.jboss.reddeer.eclipse.core.resources.Project;
import org.jboss.reddeer.eclipse.exception.EclipseLayerException;
import org.jboss.reddeer.eclipse.utils.DeleteUtils;
import org.jboss.reddeer.jface.viewer.handler.TreeViewerHandler;
import org.jboss.reddeer.swt.api.Shell;
import org.jboss.reddeer.swt.api.Tree;
import org.jboss.reddeer.swt.api.TreeItem;
import org.jboss.reddeer.core.condition.JobIsRunning;
import org.jboss.reddeer.swt.impl.button.CheckBox;
import org.jboss.reddeer.swt.impl.button.PushButton;
import org.jboss.reddeer.swt.impl.menu.ContextMenu;
import org.jboss.reddeer.swt.impl.shell.DefaultShell;
import org.jboss.reddeer.swt.impl.tree.DefaultTree;
import org.jboss.reddeer.common.wait.TimePeriod;
import org.jboss.reddeer.common.wait.WaitWhile;
import org.jboss.reddeer.workbench.impl.view.WorkbenchView;

/**
 * Common ancestor for Package and Project Explorer and Resource Navigator and any similar ones.
 * Contains common operations for those explorers.
 * 
 * @author <NAME>
 * @author <EMAIL>
 *
 */
public class AbstractExplorer extends WorkbenchView {

	public AbstractExplorer(String viewTitle) {
		super(viewTitle);
	}

	/**
	 * Select given project names
	 * @param projectName
	 */
	public void selectProjects(String... projectName){
		ArrayList<TreeItem> selectTreeItems = new ArrayList<TreeItem>();
		for(String pname: projectName){
			selectTreeItems.add(getProject(pname).getTreeItem()); //check if project exists
		}
		if (selectTreeItems.size() > 0){
			getTree().selectItems(selectTreeItems.toArray(new TreeItem[]{}));
		}
	}
	
	/**
	 * Select all projects
	 */
	public void selectAllProjects(){
		Tree projectsTree = getTree();
		List<TreeItem> projects = projectsTree.getItems();
		if(projects.size() > 0){
			projectsTree.selectItems(projects.toArray(new TreeItem[projects.size()]));
		}
	}
	
	/**
	 * Check if project exists in explorer
	 * @param projectName given project name to be checked
	 * @return true if project exists and false if not
	 */
	public boolean containsProject (String projectName){
	  boolean result = false;
	  try{
		getProject(projectName);
	    result = true;
	  } catch (EclipseLayerException ele){
	    result = false;
	  }
	  return result;
	}
	
	/**
	 * Provides list of projects in explorer
	 * @return list of projects
	 */
	public List<Project> getProjects(){
		List<Project> projects = new ArrayList<Project>();
		TreeViewerHandler treeViewerHandler = TreeViewerHandler.getInstance();
		
		for (TreeItem item : getTree().getItems()){			
			if (org.jboss.reddeer.direct.project.Project
					.isProject(treeViewerHandler.getNonStyledText(item))) {
				projects.add(new Project(item));
			}
		}
		return projects;
	}
	
	/**
	 * Provides list of all items in explorer
	 * @return list of explorer items
	 */
	public List<ExplorerItem> getExplorerItems() {
		List<ExplorerItem> items = new ArrayList<ExplorerItem>();
		
		for (TreeItem item : getTree().getItems()) {
			items.add(new ExplorerItem(item));
		}		
		return items;
	}
	
	/**
	 * Removes all projects from file system.
	 */
	public void deleteAllProjects(){
		deleteAllProjects(true);
	}
	
	/**
	 * Removes all projects.
	 * @param deleteFromFileSystem true if project should be deleted from file system, false otherwise
	 */
	public void deleteAllProjects(boolean deleteFromFileSystem){
		deleteAllProjects(deleteFromFileSystem, TimePeriod.VERY_LONG);
	}
	
	/**
	 * Removes all projects with custom timeout
	 * @param deleteFromFileSystem true if project should be deleted from file system, false otherwise
	 * @param timeout for waits
	 */
	public void deleteAllProjects(boolean deleteFromFileSystem, TimePeriod timeout){
		activate();
		if(getProjects().size() > 0){
			selectAllProjects();
			new ContextMenu("Refresh").select();
			new WaitWhile(new JobIsRunning(), timeout);
			new ContextMenu("Delete").select();
			Shell s = new DefaultShell("Delete Resources");
			new CheckBox().toggle(deleteFromFileSystem);
			new PushButton("OK").click();
			DeleteUtils.handleDeletion(s, timeout);
		}
	}

	private DefaultTree getTree(){
		activate();
		return new DefaultTree();
	}
		
	/**
	 * Get project form explorer 
	 * @param projectName given project name
	 * @return project instance
	 */
	public Project getProject(String projectName){
		activate();
		for (Project project : getProjects()){
			if (project.getName().equals(projectName)){
				return project;
			}
		}
		throw new EclipseLayerException("There is no project with name " + projectName);
	}	
	
	/**
	 * Gets project with specific project type defined by subclass of Abstract Project.
	 * 
	 * @param projectName name of project to get
	 * @param projectType type of project to get
	 * @return project of specific type with defined name
	 */
	public <T extends AbstractProject> T getProject(final String projectName, Class<T> projectType) {		<commit_msg>Fixed *projects methods in AbstractExplorer to work only with projects (fixes #1064)<commit_after>package org.jboss.reddeer.eclipse.jdt.ui;

import java.util.ArrayList;
import java.util.List;

import org.jboss.reddeer.eclipse.core.resources.AbstractProject;
import org.jboss.reddeer.eclipse.core.resources.ExplorerItem;
import org.jboss.reddeer.eclipse.core.resources.Project;
import org.jboss.reddeer.eclipse.exception.EclipseLayerException;
import org.jboss.reddeer.eclipse.utils.DeleteUtils;
import org.jboss.reddeer.jface.viewer.handler.TreeViewerHandler;
import org.jboss.reddeer.swt.api.Shell;
import org.jboss.reddeer.swt.api.TreeItem;
import org.jboss.reddeer.core.condition.JobIsRunning;
import org.jboss.reddeer.swt.impl.button.CheckBox;
import org.jboss.reddeer.swt.impl.button.PushButton;
import org.jboss.reddeer.swt.impl.menu.ContextMenu;
import org.jboss.reddeer.swt.impl.shell.DefaultShell;
import org.jboss.reddeer.swt.impl.tree.DefaultTree;
import org.jboss.reddeer.common.wait.TimePeriod;
import org.jboss.reddeer.common.wait.WaitWhile;
import org.jboss.reddeer.workbench.impl.view.WorkbenchView;

/**
 * Common ancestor for Package and Project Explorer and Resource Navigator and any similar ones.
 * Contains common operations for those explorers.
 * 
 * @author <NAME>
 * @author <EMAIL>
 *
 */
public class AbstractExplorer extends WorkbenchView {

	public AbstractExplorer(String viewTitle) {
		super(viewTitle);
	}

	/**
	 * Selects projects with specified names.
	 * 
	 * @param projectName names of projects
	 */
	public void selectProjects(String... projectName){
		ArrayList<TreeItem> selectTreeItems = new ArrayList<TreeItem>();
		for(String pname: projectName){
			selectTreeItems.add(getProject(pname).getTreeItem()); //check if project exists
		}
		if (selectTreeItems.size() > 0){
			getTree().selectItems(selectTreeItems.toArray(new TreeItem[]{}));
		}
	}
	
	/**
	 * Selects all projects. If there are not projects do nothing.
	 */
	public void selectAllProjects(){
		List<Project> projects = getProjects();
		List<TreeItem> projectsItems = new ArrayList<TreeItem>();
		if (projects.size() > 0) {
			for (Project project: projects) {
				projectsItems.add(project.getTreeItem());
			}
		}
		getTree().selectItems(projectsItems.toArray(new TreeItem[projectsItems.size()]));
	}
	
	/**
	 * Finds out whether a project with specified name exists in explorer or not.
	 * 
	 * @param projectName name of a project
	 * @return true if project exists, false otherwise
	 */
	public boolean containsProject(String projectName) {
		boolean result = false;
		try{
			getProject(projectName);
			result = true;
			} catch (EclipseLayerException ele){
				result = false;
			}
		return result;
	}
	
	/**
	 * Gets all projects located in explorer.
	 * 
	 * @return list of projects in explorer
	 */
	public List<Project> getProjects(){
		List<Project> projects = new ArrayList<Project>();
		TreeViewerHandler treeViewerHandler = TreeViewerHandler.getInstance();
		
		for (TreeItem item : getTree().getItems()){			
			if (org.jboss.reddeer.direct.project.Project
					.isProject(treeViewerHandler.getNonStyledText(item))) {
				projects.add(new Project(item));
			}
		}
		return projects;
	}
	
	/**
	 * Provides list of all items in explorer.
	 * @return list of explorer items
	 */
	public List<ExplorerItem> getExplorerItems() {
		List<ExplorerItem> items = new ArrayList<ExplorerItem>();
		
		for (TreeItem item : getTree().getItems()) {
			items.add(new ExplorerItem(item));
		}		
		return items;
	}
	
	/**
	 * Removes all projects from file system.
	 */
	public void deleteAllProjects(){
		deleteAllProjects(true);
	}
	
	/**
	 * Removes all projects.
	 * 
	 * @param deleteFromFileSystem true if project should be deleted from file system, false otherwise
	 */
	public void deleteAllProjects(boolean deleteFromFileSystem){
		deleteAllProjects(deleteFromFileSystem, TimePeriod.VERY_LONG);
	}
	
	/**
	 * Removes all projects. Wait for a specified time period while refreshing
	 * a project and while handling its deletion.
	 * 
	 * @param deleteFromFileSystem true if project should be deleted from file system, false otherwise
	 * @param timeout time to wait for refresh of a project and its deletion
	 */
	public void deleteAllProjects(boolean deleteFromFileSystem, TimePeriod timeout){
		activate();
		if(getProjects().size() > 0){
			selectAllProjects();
			new ContextMenu("Refresh").select();
			new WaitWhile(new JobIsRunning(), timeout);
			new ContextMenu("Delete").select();
			Shell s = new DefaultShell("Delete Resources");
			new CheckBox().toggle(deleteFromFileSystem);
			new PushButton("OK").click();
			DeleteUtils.handleDeletion(s, timeout);
		}
	}

	private DefaultTree getTree(){
		activate();
		return new DefaultTree();
	}
		
	/**
	 * Gets project with specified project name located in explorer.
	 *  
	 * @param projectName name of a project
	 * @return project with specified name
	 */
	public Project getProject(String projectName){
		activate();
		for (Project project : getProjects()){
			if (project.getName().equals(projectName)){
				return project;
			}
		}
		throw new EclipseLayerException("There is no project with name " + projectName);
	}	
	
	/**
	 * Gets project with specific project type defined by subclass of Abstract Project.
	 * 
	 * @param projectName name of project to get
	 * @param projectType type of project to get
	 * @return project of specific type with defined name
	 */
	public <T extends AbstractProject> T getProject(final String projectName, Class<T> projectType) {		
{'fraction_non_alphanumeric': 0.1107981220657277, 'fraction_numerical': 0.0009389671361502347, 'mean_word_length': 4.301642608262817, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, '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': '6350876', 'n_tokens_mistral': 3546, 'n_tokens_neox': 3133, 'n_words': 834}