ProjectView: sort by type doesn't work when 'folders always on top' is disabled

This commit is contained in:
Anton Makeev
2014-10-20 12:39:05 +02:00
parent 331dea7479
commit f7d60da2a3
13 changed files with 186 additions and 31 deletions
@@ -0,0 +1,133 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.projectView;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.ide.projectView.impl.AbstractProjectViewPSIPane;
import com.intellij.ide.projectView.impl.ProjectViewImpl;
import com.intellij.openapi.ui.Queryable;
import com.intellij.openapi.util.Disposer;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.ui.tree.TreeUtil;
import javax.swing.tree.DefaultMutableTreeNode;
public class ProjectTreeSortingTest extends BaseProjectViewTestCase {
private ProjectView myProjectView;
private AbstractProjectViewPSIPane myPane;
private boolean myOriginalSortByType;
private boolean myOriginalFoldersAlwaysOnTop;
@Override
public void setUp() throws Exception {
super.setUp();
myPane = new TestProjectViewPSIPane(myProject, myStructure, 9);
myPane.createComponent();
Disposer.register(myStructure, myPane);
myProjectView = ProjectView.getInstance(myProject);
myProjectView.addProjectPane(myPane);
myOriginalSortByType = myProjectView.isSortByType(myPane.getId());
myOriginalFoldersAlwaysOnTop = ((ProjectViewImpl)myProjectView).isFoldersAlwaysOnTop();
TreeUtil.expandAll(myPane.getTree());
}
@Override
public void tearDown() throws Exception {
myProjectView.setSortByType(myPane.getId(), myOriginalSortByType);
((ProjectViewImpl)myProjectView).setFoldersAlwaysOnTop(myOriginalFoldersAlwaysOnTop);
myProjectView.removeProjectPane(myPane);
super.tearDown();
}
public void testSortByType() throws Exception {
myProjectView.setSortByType(myPane.getId(), false);
assertTree("-sortByType\n" +
" a.java\n" +
" a.txt\n" +
" b.java\n" +
" b.txt\n");
myProjectView.setSortByType(myPane.getId(), true);
assertTree("-sortByType\n" +
" a.java\n" +
" b.java\n" +
" a.txt\n" +
" b.txt\n");
}
public void testFoldersOnTop() throws Exception {
// first, check with 'sort by type' disabled
myProjectView.setSortByType(myPane.getId(), false);
((ProjectViewImpl)myProjectView).setFoldersAlwaysOnTop(true);
assertTree("-foldersOnTop\n" +
" -b.java\n" +
" dummy.txt\n" +
" -b.txt\n" +
" dummy.txt\n" +
" a.java\n" +
" a.txt\n" +
" c.java\n" +
" c.txt\n");
((ProjectViewImpl)myProjectView).setFoldersAlwaysOnTop(false);
assertTree("-foldersOnTop\n" +
" a.java\n" +
" a.txt\n" +
" -b.java\n" +
" dummy.txt\n" +
" -b.txt\n" +
" dummy.txt\n" +
" c.java\n" +
" c.txt\n");
// now let's check the behavior, when sortByType is enabled
myProjectView.setSortByType(myPane.getId(), true);
((ProjectViewImpl)myProjectView).setFoldersAlwaysOnTop(true);
assertTree("-foldersOnTop\n" +
" -b.java\n" +
" dummy.txt\n" +
" -b.txt\n" +
" dummy.txt\n" +
" a.java\n" +
" c.java\n" +
" a.txt\n" +
" c.txt\n");
((ProjectViewImpl)myProjectView).setFoldersAlwaysOnTop(false);
// extension of the folder is not taken into account, fallback to name sorting here
assertTree("-foldersOnTop\n" +
" a.java\n" +
" -b.java\n" +
" dummy.txt\n" +
" -b.txt\n" +
" dummy.txt\n" +
" c.java\n" +
" a.txt\n" +
" c.txt\n");
}
private void assertTree(String expected) {
DefaultMutableTreeNode element = myPane.getTreeBuilder().getNodeForElement(getContentDirectory());
assertNotNull("Element for " + getContentDirectory() + " not found", element);
assertEquals(expected, PlatformTestUtil.print(myPane.getTree(), element, new Queryable.PrintInfo(), false));
}
}
@@ -63,9 +63,12 @@ public class GroupByTypeComparator implements Comparator<NodeDescriptor> {
if (descriptor1 instanceof ProjectViewNode && descriptor2 instanceof ProjectViewNode) {
final Project project = descriptor1.getProject();
final ProjectView projectView = ProjectView.getInstance(project);
if (!(projectView instanceof ProjectViewImpl && !((ProjectViewImpl)projectView).isFoldersAlwaysOnTop())) {
ProjectViewNode node1 = (ProjectViewNode)descriptor1;
ProjectViewNode node2 = (ProjectViewNode)descriptor2;
ProjectViewNode node1 = (ProjectViewNode)descriptor1;
ProjectViewNode node2 = (ProjectViewNode)descriptor2;
boolean isFoldersOnTop = !(projectView instanceof ProjectViewImpl && !((ProjectViewImpl)projectView).isFoldersAlwaysOnTop());
if (isFoldersOnTop) {
int typeWeight1 = node1.getTypeSortWeight(isSortByType());
int typeWeight2 = node2.getTypeSortWeight(isSortByType());
if (typeWeight1 != 0 && typeWeight2 == 0) {
@@ -77,30 +80,32 @@ public class GroupByTypeComparator implements Comparator<NodeDescriptor> {
if (typeWeight1 != 0 && typeWeight2 != typeWeight1) {
return typeWeight1 - typeWeight2;
}
if (isSortByType()) {
final Comparable typeSortKey1 = node1.getTypeSortKey();
final Comparable typeSortKey2 = node2.getTypeSortKey();
if (typeSortKey1 != null && typeSortKey2 != null) {
final int result = typeSortKey1.compareTo(typeSortKey2);
if (result != 0) return result;
}
}
if (isSortByType()) {
final Comparable typeSortKey1 = node1.getTypeSortKey();
final Comparable typeSortKey2 = node2.getTypeSortKey();
if (typeSortKey1 != null && typeSortKey2 != null) {
//noinspection unchecked
final int result = typeSortKey1.compareTo(typeSortKey2);
if (result != 0) return result;
}
else {
final Comparable typeSortKey1 = node1.getSortKey();
final Comparable typeSortKey2 = node2.getSortKey();
if (typeSortKey1 != null && typeSortKey2 != null) {
final int result = typeSortKey1.compareTo(typeSortKey2);
if (result != 0) return result;
}
}
else {
final Comparable typeSortKey1 = node1.getSortKey();
final Comparable typeSortKey2 = node2.getSortKey();
if (typeSortKey1 != null && typeSortKey2 != null) {
//noinspection unchecked
final int result = typeSortKey1.compareTo(typeSortKey2);
if (result != 0) return result;
}
}
if (isAbbreviateQualifiedNames()) {
String key1 = node1.getQualifiedNameSortKey();
String key2 = node2.getQualifiedNameSortKey();
if (key1 != null && key2 != null) {
return key1.compareToIgnoreCase(key2);
}
if (isAbbreviateQualifiedNames()) {
String key1 = node1.getQualifiedNameSortKey();
String key2 = node2.getQualifiedNameSortKey();
if (key1 != null && key2 != null) {
return key1.compareToIgnoreCase(key2);
}
}
}
@@ -119,5 +124,4 @@ public class GroupByTypeComparator implements Comparator<NodeDescriptor> {
private boolean isAbbreviateQualifiedNames() {
return myProjectView != null && myProjectView.isAbbreviatePackageNames(myPaneId);
}
}
@@ -121,12 +121,23 @@ public class PlatformTestUtil {
}
public static String print(JTree tree, boolean withSelection) {
return print(tree, withSelection, null);
return print(tree, tree.getModel().getRoot(), withSelection, null, null);
}
public static String print(JTree tree, Object root, @Nullable Queryable.PrintInfo printInfo, boolean withSelection) {
return print(tree, root, withSelection, printInfo, null);
}
public static String print(JTree tree, boolean withSelection, @Nullable Condition<String> nodePrintCondition) {
return print(tree, tree.getModel().getRoot(), withSelection, null, nodePrintCondition);
}
public static String print(JTree tree, Object root,
boolean withSelection,
@Nullable Queryable.PrintInfo printInfo,
@Nullable Condition<String> nodePrintCondition) {
StringBuilder buffer = new StringBuilder();
final Collection<String> strings = printAsList(tree, withSelection, nodePrintCondition);
final Collection<String> strings = printAsList(tree, root, withSelection, printInfo, nodePrintCondition);
for (String string : strings) {
buffer.append(string).append("\n");
}
@@ -134,9 +145,15 @@ public class PlatformTestUtil {
}
public static Collection<String> printAsList(JTree tree, boolean withSelection, @Nullable Condition<String> nodePrintCondition) {
return printAsList(tree, tree.getModel().getRoot(), withSelection, null, nodePrintCondition);
}
private static Collection<String> printAsList(JTree tree, Object root,
boolean withSelection,
@Nullable Queryable.PrintInfo printInfo,
Condition<String> nodePrintCondition) {
Collection<String> strings = new ArrayList<String>();
Object root = tree.getModel().getRoot();
printImpl(tree, root, strings, 0, withSelection, nodePrintCondition);
printImpl(tree, root, strings, 0, withSelection, printInfo, nodePrintCondition);
return strings;
}
@@ -145,13 +162,14 @@ public class PlatformTestUtil {
Collection<String> strings,
int level,
boolean withSelection,
@Nullable Queryable.PrintInfo printInfo,
@Nullable Condition<String> nodePrintCondition) {
DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode)root;
final Object userObject = defaultMutableTreeNode.getUserObject();
String nodeText;
if (userObject != null) {
nodeText = toString(userObject, null);
nodeText = toString(userObject, printInfo);
}
else {
nodeText = "null";
@@ -183,7 +201,7 @@ public class PlatformTestUtil {
int childCount = tree.getModel().getChildCount(root);
if (expanded) {
for (int i = 0; i < childCount; i++) {
printImpl(tree, tree.getModel().getChild(root, i), strings, level + 1, withSelection, nodePrintCondition);
printImpl(tree, tree.getModel().getChild(root, i), strings, level + 1, withSelection, printInfo, nodePrintCondition);
}
}
}