Skip to content

Commit

Permalink
Merge pull request #48 from marcphilipp/source_folder_filter_fixes
Browse files Browse the repository at this point in the history
Source folder filter fixes
  • Loading branch information
NicoleRauch committed Feb 6, 2015
2 parents aa0864c + 155dec3 commit 879236f
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ public abstract class DependencyGraphView extends ViewPart implements IRestrictN
private final IPartListener2 selectionSynchronizationListener = new SelectionSynchronizationListener( this );
private RefactorActionGroup refactorAction;

public DependencyGraphView( DependencyGraphModel model ) {
super();
public DependencyGraphView( String viewId, DependencyGraphModel model ) {
this.model = model;
customFilterContext = new WorkbenchContext( getClass().getName() + ".context.customFilter" );
customFilterContext = new WorkbenchContext( viewId + ".context.customFilter" );
initUsusModelListener();
}

Expand Down Expand Up @@ -214,10 +213,6 @@ public synchronized void replaceCustomFilter( NodeAndEdgeFilter newCustomFilter
drawGraphUnconditionally();
}

protected String getCustomFilterContextId() {
return getClass().getName() + ".context.customFilter";
}

public synchronized void clearCustomFilter() {
if( customFilter != null ) {
graphViewer.removeFilter( customFilter );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class ShowInClassGraph extends AbstractHandler {

private static final String TARGET_VIEW_ID_PARAMETER = ShowInClassGraph.class.getName() + ".targetViewId";
private static final String TARGET_VIEW_ID_PARAMETER = "org.projectusus.ui.dependencygraph.handlers.ShowInClassGraph.targetViewId";

public Object execute( ExecutionEvent event ) throws ExecutionException {
PackagenameNodeFilter filter = createFilter( event );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.ISharedImages;
Expand All @@ -19,7 +19,7 @@
import com.google.common.base.Function;
import com.google.common.collect.Collections2;

public class ClassRepresenter implements GraphNode, IAdaptable {
public class ClassRepresenter extends PlatformObject implements GraphNode {

private final ClassDescriptor clazz;

Expand Down Expand Up @@ -175,10 +175,11 @@ public boolean represents( IJavaElement javaElement ) {
return javaElement.getResource().equals( getFile() );
}

@Override
public Object getAdapter( @SuppressWarnings( "rawtypes" ) Class adapter ) {
if( adapter.equals( IJavaElement.class ) ) {
return JavaCore.create( getFile() );
}
return null;
return super.getAdapter( adapter );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.jdt.ui.JavaElementLabels;
Expand All @@ -15,7 +15,7 @@
import com.google.common.base.Function;
import com.google.common.collect.Collections2;

public class PackageRepresenter implements GraphNode, IAdaptable {
public class PackageRepresenter extends PlatformObject implements GraphNode {

private final Packagename packagename;
private static PackageRelations relations;
Expand Down Expand Up @@ -120,11 +120,12 @@ public boolean represents( IJavaElement javaElement ) {
return pkg != null && getRelatedPackage().getJavaElement().getElementName().equals( pkg.getElementName() );
}

@Override
public Object getAdapter( @SuppressWarnings( "rawtypes" ) Class adapter ) {
if( adapter.equals( IJavaElement.class ) ) {
return getPackagename().getJavaElement();
}
return null;
return super.getAdapter( adapter );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ public List<IPath> getAllSourceFolders() {
return allSourceFolders;
}

public void updateSourceFolders( List<IPath> allSourceFolders ) {
public boolean updateSourceFolders( List<IPath> allSourceFolders ) {
if( this.allSourceFolders.equals( allSourceFolders ) ) {
return false;
}
this.visibleSourceFolders.retainAll( allSourceFolders );
this.visibleSourceFolders.addAll( addedSourceFolders( allSourceFolders ) );
this.allSourceFolders = new LinkedList<IPath>( allSourceFolders );
this.allSourceFolders = new ArrayList<IPath>( allSourceFolders );
return true;
}

private LinkedList<IPath> addedSourceFolders( List<IPath> newSourceFolders ) {
private List<IPath> addedSourceFolders( List<IPath> newSourceFolders ) {
List<IPath> oldSourceFolders = this.allSourceFolders;
LinkedList<IPath> result = new LinkedList<IPath>( newSourceFolders );
List<IPath> result = new LinkedList<IPath>( newSourceFolders );
result.removeAll( oldSourceFolders );
return result;
}
Expand All @@ -51,6 +55,10 @@ public String getDescription() {

@Override
protected boolean select( GraphNode node, Set<GraphNode> others ) {
return !isFiltering() || isContainedByVisibleSourceFolder( node );
}

private boolean isContainedByVisibleSourceFolder( GraphNode node ) {
IJavaElement javaElement = JavaCore.create( node.getFile() );
IJavaElement packageFragmentRoot = javaElement.getAncestor( IJavaElement.PACKAGE_FRAGMENT_ROOT );
IJavaProject javaProject = packageFragmentRoot.getJavaProject();
Expand All @@ -63,6 +71,6 @@ protected boolean select( GraphNode node, Set<GraphNode> others ) {
}

public boolean isFiltering() {
return getAllSourceFolders().size() > getVisibleSourceFolders().size();
return allSourceFolders.size() > visibleSourceFolders.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.projectusus.core.statistics.UsusModelProvider.ususModel;
import static org.projectusus.ui.dependencygraph.sourcefolder.SourceFolderChangeDetector.detectSourceFolderChange;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -77,9 +78,11 @@ private void recomputeSourceFolders() {
IJavaProject[] ususJavaProjects = convertToJavaProjects( ususProjects );
Set<IPath> allSourceFolders = scanForSourceFolders( ususJavaProjects );

filter.updateSourceFolders( new LinkedList<IPath>( allSourceFolders ) );
action.updateState();
refreshable.refresh();
boolean changed = filter.updateSourceFolders( new ArrayList<IPath>( allSourceFolders ) );
if( changed ) {
action.updateState();
refreshable.refresh();
}
}

private Set<IPath> scanForSourceFolders( IJavaProject[] ususJavaProjects ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private IPath makeRelativeToBasePath( IPath deepPath, IPath basePath ) {
return deepPath.makeRelativeTo( basePath );
}

public final static Comparator<IPath> PATH_COMPARATOR = new Comparator<IPath>() {
private final static Comparator<IPath> PATH_COMPARATOR = new Comparator<IPath>() {
public int compare( IPath path1, IPath path2 ) {
return path1.toPortableString().compareTo( path2.toPortableString() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public class ClassGraphView extends DependencyGraphView {

public static final String VIEW_ID = ClassGraphView.class.getName();
public static final String VIEW_ID = "org.projectusus.ui.dependencygraph.ClassGraphView";

private static final String ONLY_CROSS_PACKAGE = "Only cross-package relations";

Expand All @@ -39,7 +39,7 @@ protected Set<? extends GraphNode> getRefreshedNodes() {
private Composite additionalWidgetsComposite;

public ClassGraphView() {
super( classGraphModel );
super( VIEW_ID, classGraphModel );
sourceFolderFilterExtension = new SourceFolderFilterExtension( this );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class PackageGraphView extends DependencyGraphView {

public static final String VIEW_ID = PackageGraphView.class.getName();
public static final String VIEW_ID = "org.projectusus.ui.dependencygraph.PackageGraphView";

private static final String ONLY_IN_CYCLES = "Only cyclic dependencies";

Expand All @@ -22,7 +22,7 @@ protected Set<? extends GraphNode> getRefreshedNodes() {
};

public PackageGraphView() {
super( packageGraphModel );
super( VIEW_ID, packageGraphModel );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.projectusus.ui.dependencygraph.nodes.test;
package org.projectusus.ui.dependencygraph.nodes;

import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -10,6 +12,7 @@
import org.junit.Test;
import org.projectusus.core.filerelations.model.ClassDescriptor;
import org.projectusus.ui.dependencygraph.nodes.ClassRepresenter;
import org.projectusus.ui.dependencygraph.nodes.GraphNode;

public class ClassRepresenterTest {

Expand All @@ -31,4 +34,11 @@ public void childrenChangedInUnderlyingClassDescriptor() {
assertEquals( 1, clazz.getChildren().size() );
assertEquals( 1, representer.getChildren().size() );
}

@Test
public void isAdaptableToGraphNode() {
ClassRepresenter representer = new ClassRepresenter( mock( ClassDescriptor.class ) );
Object adapter = representer.getAdapter( GraphNode.class );
assertThat( adapter, notNullValue() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.projectusus.ui.dependencygraph.nodes;

import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

import org.eclipse.jdt.core.IPackageFragment;
import org.junit.Test;
import org.projectusus.core.filerelations.model.Packagename;

public class PackageRepresenterTest {

@Test
public void isAdaptableToGraphNode() {
PackageRepresenter representer = new PackageRepresenter( Packagename.of( "a", mock( IPackageFragment.class ) ) );
Object adapter = representer.getAdapter( GraphNode.class );
assertThat( adapter, notNullValue() );
}
}

0 comments on commit 879236f

Please sign in to comment.