Skip to content

Commit 5194048

Browse files
BananeweizenRoiSoleil
authored andcommitted
Improve missing tests per project view
* sort projects in combo box alphabetically, case insensitive * add packages to the tree view * sort classes in tree view
1 parent ccdbfeb commit 5194048

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

org.moreunit.plugin/src/org/moreunit/elements/MissingClassTreeContentProvider.java

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.moreunit.elements;
22

3-
import java.util.ArrayList;
3+
import java.util.Comparator;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import org.eclipse.jdt.core.ICompilationUnit;
79
import org.eclipse.jdt.core.IJavaElement;
@@ -17,33 +19,54 @@
1719
public class MissingClassTreeContentProvider implements ITreeContentProvider
1820
{
1921

20-
public MissingClassTreeContentProvider()
21-
{
22-
23-
}
24-
25-
public Object[] getChildren(Object arg0)
22+
public Object[] getChildren(Object parent)
2623
{
24+
if(parent instanceof IPackageFragment packageFragment)
25+
{
26+
try
27+
{
28+
Set<ICompilationUnit> compilationUnits = new HashSet<>();
29+
for (ICompilationUnit compilationUnit : packageFragment.getCompilationUnits())
30+
{
31+
if(compilationUnit.findPrimaryType() != null)
32+
{
33+
ClassTypeFacade classTypeFacade = new ClassTypeFacade(compilationUnit);
34+
if(! TypeFacade.isTestCase(compilationUnit) && ! classTypeFacade.hasTestCase())
35+
{
36+
compilationUnits.add(compilationUnit);
37+
}
38+
}
39+
}
40+
return compilationUnits.stream().sorted(Comparator.comparing(Object::toString, String.CASE_INSENSITIVE_ORDER)).toArray(ICompilationUnit[]::new);
41+
}
42+
catch (JavaModelException e)
43+
{
44+
e.printStackTrace();
45+
}
46+
}
2747
return null;
2848
}
2949

30-
public Object getParent(Object arg0)
50+
public Object getParent(Object child)
3151
{
52+
if(child instanceof ICompilationUnit unit)
53+
{
54+
return unit.getParent();
55+
}
3256
return null;
3357
}
3458

35-
public boolean hasChildren(Object arg0)
59+
public boolean hasChildren(Object parent)
3660
{
37-
return false;
61+
return ! (parent instanceof ICompilationUnit);
3862
}
3963

4064
public Object[] getElements(Object inputElement)
4165
{
42-
List<Object> elements = new ArrayList<Object>();
43-
44-
if(inputElement instanceof MissingTestsViewPart)
66+
Set<IPackageFragment> packages = new HashSet<>();
67+
if(inputElement instanceof MissingTestsViewPart missingTestsViewPart)
4568
{
46-
IJavaProject javaProject = ((MissingTestsViewPart) inputElement).getSelectedJavaProject();
69+
IJavaProject javaProject = missingTestsViewPart.getSelectedJavaProject();
4770
if(javaProject != null)
4871
{
4972
List<IPackageFragmentRoot> allSourceFolderFromProject = PluginTools.getAllSourceFolderFromProject(javaProject);
@@ -62,7 +85,8 @@ public Object[] getElements(Object inputElement)
6285
ClassTypeFacade classTypeFacade = new ClassTypeFacade(compilationUnit);
6386
if(! TypeFacade.isTestCase(compilationUnit) && ! classTypeFacade.hasTestCase())
6487
{
65-
elements.add(compilationUnit);
88+
packages.add((IPackageFragment) javaPackage);
89+
break;
6690
}
6791
}
6892
}
@@ -75,8 +99,7 @@ public Object[] getElements(Object inputElement)
7599
}
76100
}
77101
}
78-
79-
return elements.toArray();
102+
return packages.stream().sorted(Comparator.comparing(Object::toString, String.CASE_INSENSITIVE_ORDER)).toArray(IJavaElement[]::new);
80103
}
81104

82105
public void inputChanged(Viewer arg0, Object arg1, Object arg2)

org.moreunit.plugin/src/org/moreunit/ui/MissingTestsViewPart.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.eclipse.jdt.core.ICompilationUnit;
1414
import org.eclipse.jdt.core.IJavaProject;
1515
import org.eclipse.jdt.core.JavaCore;
16+
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
1617
import org.eclipse.jdt.ui.JavaElementLabelProvider;
1718
import org.eclipse.jface.viewers.DoubleClickEvent;
1819
import org.eclipse.jface.viewers.IDoubleClickListener;
@@ -61,7 +62,7 @@ public void createPartControl(Composite parent)
6162

6263
private String[] getNamesOfJavaProjects()
6364
{
64-
return PluginTools.getJavaProjectsFromWorkspace().stream().map(IJavaProject::getElementName).toArray(String[]::new);
65+
return PluginTools.getJavaProjectsFromWorkspace().stream().map(IJavaProject::getElementName).sorted(String.CASE_INSENSITIVE_ORDER).toArray(String[]::new);
6566
}
6667

6768
@Override
@@ -79,6 +80,7 @@ public void widgetSelected(SelectionEvent e)
7980
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
8081
selectedJavaProject = JavaCore.create(project);
8182
treeViewer.refresh();
83+
treeViewer.expandAll();
8284
}
8385

8486
public IJavaProject getSelectedJavaProject()
@@ -89,8 +91,17 @@ public IJavaProject getSelectedJavaProject()
8991
public void doubleClick(DoubleClickEvent event)
9092
{
9193
ITreeSelection selection = (ITreeSelection) this.treeViewer.getSelection();
92-
ICompilationUnit compilationUnit = (ICompilationUnit) selection.getFirstElement();
93-
new EditorUI().open(compilationUnit);
94+
Object firstElement = selection.getFirstElement();
95+
if(firstElement instanceof ICompilationUnit compilationUnit)
96+
{
97+
new EditorUI().open(compilationUnit);
98+
}
99+
else
100+
{
101+
PackageExplorerPart part = PackageExplorerPart.getFromActivePerspective();
102+
part.selectAndReveal(firstElement);
103+
part.setFocus();
104+
}
94105
}
95106

96107
@Override
@@ -108,7 +119,7 @@ public void resourceChanged(IResourceChangeEvent event)
108119
return;
109120
}
110121

111-
if (event.getType() != IResourceChangeEvent.POST_CHANGE)
122+
if(event.getType() != IResourceChangeEvent.POST_CHANGE)
112123
return;
113124

114125
if(selectedJavaProject == null)
@@ -147,7 +158,7 @@ public boolean visit(IResourceDelta delta) throws CoreException
147158
e.printStackTrace();
148159
}
149160

150-
if(!addedOrRemovedResource.isEmpty())
161+
if(! addedOrRemovedResource.isEmpty())
151162
{
152163
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
153164
{
@@ -187,7 +198,7 @@ public boolean visit(IResourceDelta delta) throws CoreException
187198
e.printStackTrace();
188199
}
189200

190-
if(!addedProjects.isEmpty())
201+
if(! addedProjects.isEmpty())
191202
{
192203
updateProjectsInComboBox();
193204
}

0 commit comments

Comments
 (0)