<commit_before>package de.tobject.findbugs.view;

import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.HashMap;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.ide.IDE;
import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import de.tobject.findbugs.reporter.MarkerUtil;
import edu.umd.cs.findbugs.BugAnnotation;
import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugPattern;
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.I18N;
import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.annotations.CheckForNull;

public class BugTreeView extends ViewPart{
	
	private TabFolder theFolder;
	
	public static BugTreeView bugTreeView;
	
	public HashMap<String, Tree> projectTrees; //maps project names to corresponding trees
	
	private HashMap<String, HashMap<String, TreeItem>> patternMap; //maps project names to HashMaps that map strings describing patterns to root TreeItems
	
	private HashMap<TreeItem, IMarker> instanceMap; //maps TreeItems to the markers they represent
	
	private IProject theProject; //used to communicate with run methods
	
	private IMarker theMarker; ///used to communicate with run methods
	
	private class BugTreeSelectionListener extends SelectionAdapter{
		private Tree theTree;
		public BugTreeSelectionListener(Tree theTree)
		{this.theTree = theTree;}
		
		public void widgetSelected(SelectionEvent e)
		{
			IMarker myMarker = instanceMap.get(theTree.getSelection()[0]);
			if(myMarker == null) return;
			DetailsView.showMarker(myMarker, false);
			UserAnnotationsView.showMarker(myMarker, false);
			try{IDE.openEditor(getSite().getPage(), myMarker, false);}
			catch(PartInitException ex){ex.printStackTrace();}
		}
		
		public void widgetDefaultSelected(SelectionEvent e)
		{
			TreeItem theItem = theTree.getSelection()[0];
			IMarker myMarker = instanceMap.get(theItem);
			if(myMarker == null)
				theItem.setExpanded(!theItem.getExpanded());
			else
			{
				DetailsView.showMarker(myMarker, false);
				UserAnnotationsView.showMarker(myMarker, false);
				try{IDE.openEditor(getSite().getPage(), myMarker, false);}
				catch(PartInitException ex){ex.printStackTrace();}
			}
		}
	}
	
	
	public static BugTreeView getBugTreeView()
	{
		return bugTreeView;
	}
	
	public static IMarker getMarkerForTreeItem(TreeItem theItem)
	{
		return bugTreeView.instanceMap.get(theItem);
	}
	
	public void setFocus() {}
	
	public void createPartControl(Composite parent)
	{
		theFolder = new TabFolder(parent, SWT.LEFT);
		projectTrees = new HashMap<String, Tree>();
		patternMap = new HashMap<String, HashMap<String, TreeItem>>();
		instanceMap = new HashMap<TreeItem, IMarker>();
		BugTreeView.bugTreeView = this;
		// initialize views with marker data
		IProject[] projectList = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for(IProject proj : projectList)
		{
			try{
				if(proj.isOpen())
					for(IMarker marker : proj.findMarkers(FindBugsMarker.NAME, true, IResource.DEPTH_INFINITE))
						addMarker(proj, marker);
			}
			catch(CoreException e)
			{
				FindbugsPlugin.getDefault().logException(e, "Core exception on tree initialization");
			}
		}				
	}
	
	public void clearTree(IProject currProject)
	{
		this.theProject = currProject;
		Display.getDefault().syncExec(new Runnable(){
			public void run(){
				Tree treeToRemove = projectTrees.get(theProject.getName());
				if(treeToRemove == null) return;
				for(TreeItem x : treeToRemove.getItems())
					instanceMap.remove(x);
				patternMap.get(theProject.getName()).clear();
				treeToRemove.removeAll();			
			}
		});
	}
	public void addMarker(IProject currProject, IMarker currMarker)
	{
		this.theProject = currProject;
		this.theMarker = currMarker;
		Display.getDefault().syncExec(new Runnable(){
			public void run(){<commit_msg>Fix sourceforge report [ 1674402 ] "Clear bug markers" is broken<commit_after>package de.tobject.findbugs.view;

import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.HashMap;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.ide.IDE;
import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import de.tobject.findbugs.reporter.MarkerUtil;
import edu.umd.cs.findbugs.BugAnnotation;
import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugPattern;
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.I18N;
import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.annotations.CheckForNull;

public class BugTreeView extends ViewPart{
	
	private TabFolder theFolder;
	
	public static BugTreeView bugTreeView;
	
	public HashMap<String, Tree> projectTrees; //maps project names to corresponding trees
	
	private HashMap<String, HashMap<String, TreeItem>> patternMap; //maps project names to HashMaps that map strings describing patterns to root TreeItems
	
	private HashMap<TreeItem, IMarker> instanceMap; //maps TreeItems to the markers they represent
	
	private IProject theProject; //used to communicate with run methods
	
	private IMarker theMarker; ///used to communicate with run methods
	
	private class BugTreeSelectionListener extends SelectionAdapter{
		private Tree theTree;
		public BugTreeSelectionListener(Tree theTree)
		{this.theTree = theTree;}
		
		public void widgetSelected(SelectionEvent e)
		{
			IMarker myMarker = instanceMap.get(theTree.getSelection()[0]);
			if(myMarker == null) return;
			DetailsView.showMarker(myMarker, false);
			UserAnnotationsView.showMarker(myMarker, false);
			try{IDE.openEditor(getSite().getPage(), myMarker, false);}
			catch(PartInitException ex){ex.printStackTrace();}
		}
		
		public void widgetDefaultSelected(SelectionEvent e)
		{
			TreeItem theItem = theTree.getSelection()[0];
			IMarker myMarker = instanceMap.get(theItem);
			if(myMarker == null)
				theItem.setExpanded(!theItem.getExpanded());
			else
			{
				DetailsView.showMarker(myMarker, false);
				UserAnnotationsView.showMarker(myMarker, false);
				try{IDE.openEditor(getSite().getPage(), myMarker, false);}
				catch(PartInitException ex){ex.printStackTrace();}
			}
		}
	}
	
	
	public static BugTreeView getBugTreeView()
	{
		return bugTreeView;
	}
	
	public static IMarker getMarkerForTreeItem(TreeItem theItem)
	{
		return bugTreeView.instanceMap.get(theItem);
	}
	
	public void setFocus() {}
	
	public void createPartControl(Composite parent)
	{
		theFolder = new TabFolder(parent, SWT.LEFT);
		projectTrees = new HashMap<String, Tree>();
		patternMap = new HashMap<String, HashMap<String, TreeItem>>();
		instanceMap = new HashMap<TreeItem, IMarker>();
		BugTreeView.bugTreeView = this;
		// initialize views with marker data
		IProject[] projectList = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for(IProject proj : projectList)
		{
			try{
				if(proj.isOpen())
					for(IMarker marker : proj.findMarkers(FindBugsMarker.NAME, true, IResource.DEPTH_INFINITE))
						addMarker(proj, marker);
			}
			catch(CoreException e)
			{
				FindbugsPlugin.getDefault().logException(e, "Core exception on tree initialization");
			}
		}				
	}
	
	public void clearTree(IProject currProject)
	{
		this.theProject = currProject;
		Display.getDefault().syncExec(new Runnable(){
			public void run(){
				Tree treeToRemove = projectTrees.get(theProject.getName());
				if(treeToRemove == null) return;
                if (treeToRemove.isDisposed()) return;
				for(TreeItem x : treeToRemove.getItems())
					instanceMap.remove(x);
				patternMap.get(theProject.getName()).clear();
				treeToRemove.removeAll();			
			}
		});
	}
	public void addMarker(IProject currProject, IMarker currMarker)
	{
		this.theProject = currProject;
		this.theMarker = currMarker;
		Display.getDefault().syncExec(new Runnable(){
			public void run(){
{'fraction_non_alphanumeric': 0.1097708082026538, 'fraction_numerical': 0.0012924349474409788, 'mean_word_length': 5.183803942461375, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '371831', 'n_tokens_mistral': 3996, 'n_tokens_neox': 3605, 'n_words': 708}