Skip to content

Commit 2e5a8a1

Browse files
committed
Sort method list alphabetically
1 parent 1c9013b commit 2e5a8a1

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/filter/CategoryActionGroup.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import com.intellij.openapi.actionSystem.AnAction;
1212
import com.intellij.openapi.actionSystem.AnActionEvent;
1313
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
14+
import de.fraunhofer.iem.swan.assist.util.Constants;
1415
import de.fraunhofer.iem.swan.assist.util.Formatter;
1516
import de.fraunhofer.iem.swan.data.Category;
16-
import de.fraunhofer.iem.swan.assist.util.Constants;
1717
import javafx.util.Pair;
1818
import org.jetbrains.annotations.NotNull;
1919
import org.jetbrains.annotations.Nullable;
@@ -54,15 +54,12 @@ public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
5454
ArrayList<FilterAction> cweFilter = new ArrayList<FilterAction>();
5555
ArrayList<FilterAction> typeFilter = new ArrayList<FilterAction>();
5656

57-
if (JSONFileLoader.isFileSelected()) {
58-
59-
for (Category category : JSONFileLoader.getCategories()) {
57+
for (Category category : JSONFileLoader.getCategories()) {
6058

61-
if (category.isCwe())
62-
cweFilter.add(new FilterAction(new Pair<>(Constants.FILTER_CWE, Formatter.toTitleCase(category.toString()))));
63-
else
64-
typeFilter.add(new FilterAction(new Pair<>(Constants.FILTER_TYPE, Formatter.toTitleCase(category.toString()))));
65-
}
59+
if (category.isCwe())
60+
cweFilter.add(new FilterAction(new Pair<>(Constants.FILTER_CWE, Formatter.toTitleCase(category.toString()))));
61+
else
62+
typeFilter.add(new FilterAction(new Pair<>(Constants.FILTER_TYPE, Formatter.toTitleCase(category.toString()))));
6663
}
6764

6865
if (this.toString().contains(Constants.FILTER_CWE))

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/data/MethodWrapper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
/**
1919
* Wrapper class to format data in method object to display in plugin.
2020
*/
21-
public class MethodWrapper {
21+
public class MethodWrapper implements Comparable<MethodWrapper> {
22+
2223

2324
public enum MethodStatus {
2425
NONE,
@@ -70,6 +71,11 @@ public MethodWrapper(String methodName, List<String> parameters, String returnTy
7071

7172
}
7273

74+
@Override
75+
public int compareTo(@NotNull MethodWrapper o) {
76+
return this.getMethodName(false).compareTo(o.getMethodName(false));
77+
}
78+
7379
/**
7480
* Returns Method object
7581
* @return Method object

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/ui/MethodListTree.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,36 @@ public void mouseClicked(MouseEvent e) {
127127
if (node != null) {
128128
Object object = node.getUserObject();
129129

130-
if (object instanceof MethodWrapper) {
130+
if (object instanceof Pair) {
131+
132+
Pair classPair = (Pair) object;
133+
String classname = classPair.getKey().toString();
134+
String simpleClassname = classname.substring(classname.lastIndexOf(".") + 1);
135+
136+
PsiFile[] files = FilenameIndex.getFilesByName(project, simpleClassname + ".java", GlobalSearchScope.allScope(project));
137+
138+
boolean methodFound = false;
139+
140+
for (PsiFile file : files) {
141+
142+
PsiJavaFile psiJavaFile = (PsiJavaFile) file;
143+
144+
if ((psiJavaFile.getPackageName() + "." + simpleClassname).contentEquals(classname)) {
145+
146+
methodFound = true;
147+
148+
OpenFileDescriptor fileDescriptor = new OpenFileDescriptor(project, psiJavaFile.getVirtualFile());
149+
fileDescriptor.navigateInEditor(project, true);
150+
}
151+
}
152+
153+
if (!methodFound) {
154+
JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(resource.getString("Messages.Error.ClassNotFound"), MessageType.ERROR, null)
155+
.createBalloon()
156+
.show(JBPopupFactory.getInstance().guessBestPopupLocation((JComponent) e.getComponent()), Balloon.Position.below);
157+
}
158+
159+
} else if (object instanceof MethodWrapper) {
131160

132161
MethodWrapper method = (MethodWrapper) object;
133162

@@ -504,7 +533,7 @@ private void addNode(MethodWrapper method) {
504533
*/
505534
private void loadMethods() {
506535

507-
HashMap<String, ArrayList<MethodWrapper>> methods = JSONFileLoader.getMethodsForTree(TREE_FILTERS, currentFile, project);
536+
TreeMap<String, ArrayList<MethodWrapper>> methods = JSONFileLoader.getMethodsForTree(TREE_FILTERS, currentFile, project);
508537

509538
if (methods.size() > 0) {
510539

@@ -517,7 +546,11 @@ private void loadMethods() {
517546
methodCount = methods.get(classname).size();
518547
totalMethods += methodCount;
519548

520-
for (MethodWrapper method : methods.get(classname)) {
549+
DefaultMutableTreeNode classNode = new DefaultMutableTreeNode(new Pair<>(classname, methodCount));
550+
551+
ArrayList<MethodWrapper> sortedList = methods.get(classname);
552+
Collections.sort(sortedList);
553+
for (MethodWrapper method : sortedList) {
521554

522555
classNode.add(addCategoriesToNode(method));
523556
root.add(classNode);

0 commit comments

Comments
 (0)