Skip to content

Commit 818578b

Browse files
committed
Refactor filter implementation and add new filters
1 parent 9d12f4a commit 818578b

File tree

8 files changed

+128
-110
lines changed

8 files changed

+128
-110
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
4040
for (Category category : JSONFileLoader.getCategories()) {
4141

4242
if (category.isCwe())
43-
cweFilter.add(new FilterAction(new Pair<>(Constants.FILTER_CWE, Formatter.capitalizeFirstCharacter(category.toString()))));
43+
cweFilter.add(new FilterAction(new Pair<>(Constants.FILTER_CWE, Formatter.toTitleCase(category.toString()))));
4444
else
45-
typeFilter.add(new FilterAction(new Pair<>(Constants.FILTER_TYPE, Formatter.capitalizeFirstCharacter(category.toString()))));
45+
typeFilter.add(new FilterAction(new Pair<>(Constants.FILTER_TYPE, Formatter.toTitleCase(category.toString()))));
4646
}
4747
}
4848

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import com.intellij.openapi.project.Project;
88
import com.intellij.util.messages.MessageBus;
99
import de.fraunhofer.iem.swan.assist.comm.FilterNotifier;
10-
import de.fraunhofer.iem.swan.assist.ui.SummaryToolWindow;
11-
import de.fraunhofer.iem.swan.assist.util.Constants;
10+
import de.fraunhofer.iem.swan.assist.ui.MethodListTree;
1211
import icons.PluginIcons;
1312
import javafx.util.Pair;
1413

@@ -38,40 +37,16 @@ public void actionPerformed(AnActionEvent e) {
3837
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
3938
MessageBus messageBus = project.getMessageBus();
4039

41-
switch (filterPair.getKey()) {
42-
43-
case Constants.FILTER_CURRENT_FILE_KEY:
44-
45-
SummaryToolWindow.CURRENT_FILE_FILTER = !SummaryToolWindow.CURRENT_FILE_FILTER;
46-
SummaryToolWindow.CURRENT_PROJECT_FILTER = false;
47-
break;
48-
case Constants.FILTER_CURRENT_PROJECT_KEY:
49-
SummaryToolWindow.CURRENT_FILE_FILTER = false;
50-
SummaryToolWindow.CURRENT_PROJECT_FILTER = !SummaryToolWindow.CURRENT_PROJECT_FILTER;
51-
break;
52-
case Constants.FILTER_CLEAR_KEY:
53-
54-
SummaryToolWindow.TREE_FILTERS.clear();
55-
break;
56-
default:
57-
58-
if (SummaryToolWindow.TREE_FILTERS.contains(filterPair.getValue()))
59-
SummaryToolWindow.TREE_FILTERS.remove(filterPair.getValue());
60-
else
61-
SummaryToolWindow.TREE_FILTERS.add(filterPair.getValue());
62-
break;
63-
}
64-
6540
FilterNotifier filterNotifier = messageBus.syncPublisher(FilterNotifier.FILTER_SELECTED_TOPIC);
66-
filterNotifier.updateFilter(filterPair.getValue());
41+
filterNotifier.updateFilter(filterPair);
6742
}
6843

6944

7045
@Override
7146
public void update(AnActionEvent event) {
7247

7348
//Set/unset icon for filters
74-
if (SummaryToolWindow.TREE_FILTERS.contains(filterPair.getValue()) || (filterPair.getKey().equals(Constants.FILTER_CURRENT_FILE_KEY) && SummaryToolWindow.CURRENT_FILE_FILTER) || (filterPair.getKey().equals(Constants.FILTER_CURRENT_PROJECT_KEY) && SummaryToolWindow.CURRENT_PROJECT_FILTER))
49+
if ( MethodListTree.TREE_FILTERS.contains(filterPair))
7550
event.getPresentation().setIcon(PluginIcons.SELECTED);
7651
else
7752
event.getPresentation().setIcon(null);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ public class FilterActionGroup extends ActionGroup {
2222
public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
2323

2424
return new AnAction[]{
25-
new FilterAction(new Pair<>(Constants.FILTER_CURRENT_FILE_KEY, Constants.FILTER_CURRENT_FILE_VALUE)),
26-
new FilterAction(new Pair<>(Constants.FILTER_CURRENT_PROJECT_KEY, Constants.FILTER_CURRENT_PROJECT_VALUE)),
25+
new FilterAction(Constants.FILE_FILTER),
2726
new Separator(),
2827
new CategoryActionGroup(Constants.FILTER_TYPE, true),
2928
new CategoryActionGroup(Constants.FILTER_CWE, true),
3029
new Separator(),
31-
new FilterAction(new Pair<>(Constants.FILTER_CLEAR_KEY, Constants.FILTER_CLEAR_VALUE))};
30+
new FilterAction(Constants.TRAIN_FILTER),
31+
new FilterAction(Constants.DELETED_FILTER),
32+
new Separator(),
33+
new FilterAction(Constants.CLEAR_FILTER)};
3234
}
3335

3436
@Override

swan_assist/src/de/fraunhofer/iem/swan/assist/data/JSONFileLoader.java

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
import com.intellij.psi.JavaPsiFacade;
55
import com.intellij.psi.PsiClass;
66
import com.intellij.psi.search.GlobalSearchScope;
7+
import de.fraunhofer.iem.swan.assist.util.Constants;
78
import de.fraunhofer.iem.swan.assist.util.Formatter;
89
import de.fraunhofer.iem.swan.data.Category;
10+
import javafx.util.Pair;
911

10-
import java.util.*;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.HashSet;
15+
import java.util.Set;
1116

1217
public class JSONFileLoader {
1318

1419
static private HashMap<String, MethodWrapper> methods;
1520
static private String congFile = "";
1621
static public final int NEW_METHOD = 0;
1722
static public final int EXISTING_METHOD = 1;
18-
19-
20-
23+
static public final int RESTORED_METHOD = 2;
2124
static private boolean reloadingSwan = false;
2225

2326
//Get configuration file location
@@ -32,8 +35,7 @@ public static String getConfigurationFile(boolean path) {
3235
if (path)
3336
return congFile;
3437
else
35-
return congFile.substring(congFile.lastIndexOf("/") + 1, congFile.length());
36-
38+
return congFile.substring(congFile.lastIndexOf("/") + 1);
3739
}
3840

3941
//Returns whether or not a configuration file was selected
@@ -63,43 +65,74 @@ public static ArrayList<MethodWrapper> getMethods() {
6365
return new ArrayList<>(methods.values());
6466
}
6567

68+
//Return list of methods as an array
69+
public static HashMap<String, MethodWrapper> getAllMethods() {
70+
71+
return methods;
72+
}
6673

6774
//Return list of methods as an array using categories
68-
public static ArrayList<MethodWrapper> getMethods(ArrayList<String> filters, String currentFile, boolean currentFileMode, boolean currentProjectMode, Project project) {
75+
public static ArrayList<MethodWrapper> getMethods(ArrayList<Pair<String, String>> filters, String currentFile, Project project) {
6976

70-
if (filters.size() == 0 && (currentFileMode || currentProjectMode)) {
77+
if (filters.size() > 0) {
7178

72-
ArrayList<MethodWrapper> filteredList = new ArrayList<>();
79+
//case where file selected but no categories .
80+
if (filters.size() == 1 && filters.contains(Constants.FILE_FILTER)) {
7381

74-
for (String methodSignature : methods.keySet()) {
82+
ArrayList<MethodWrapper> filteredList = new ArrayList<>();
7583

76-
if ((methodSignature.contains(currentFile) && currentFileMode) || (inProject(methods.get(methodSignature).getClassName(true), project) && currentProjectMode)) {
77-
filteredList.add(methods.get(methodSignature));
84+
for (String methodSignature : methods.keySet()) {
85+
if (methodSignature.contains(currentFile)) {
86+
filteredList.add(methods.get(methodSignature));
87+
}
7888
}
89+
return filteredList;
90+
} else {
91+
//case where file is selected and categories
92+
return filterList(filters, currentFile);
7993
}
80-
return filteredList;
81-
} else if (filters.size() > 0) {
82-
94+
} else {
8395
ArrayList<MethodWrapper> filteredList = new ArrayList<>();
8496

85-
for (String methodSignature : methods.keySet()) {
97+
for (MethodWrapper method : methods.values()) {
8698

87-
if ((!methodSignature.contains(currentFile) && currentFileMode) || (!inProject(methods.get(methodSignature).getClassName(true), project) && currentProjectMode))
99+
100+
if (method.getUpdateOperation().equals(Constants.METHOD_DELETED) || method.isTrainingMethod())
88101
continue;
89102

90-
for (Category category : methods.get(methodSignature).getCategories()) {
103+
System.out.println(method.getSignature(true));
104+
filteredList.add(method);
105+
}
106+
return filteredList;
107+
}
108+
}
109+
110+
private static ArrayList<MethodWrapper> filterList(ArrayList<Pair<String, String>> filters, String currentFile) {
111+
ArrayList<MethodWrapper> filteredList = new ArrayList<>();
91112

92-
if (filters.contains(Formatter.capitalizeFirstCharacter(category.toString()))) {
93-
filteredList.add(methods.get(methodSignature));
94-
break;
95-
}
113+
for (String methodSignature : methods.keySet()) {
114+
115+
if ((filters.contains(Constants.FILE_FILTER) && !methodSignature.contains(currentFile))
116+
|| (!filters.contains(Constants.DELETED_FILTER) && methods.get(methodSignature).getUpdateOperation().equals(Constants.METHOD_DELETED))
117+
|| (methods.get(methodSignature).isTrainingMethod() && !filters.contains(Constants.TRAIN_FILTER)))
118+
continue;
119+
120+
for (Category category : methods.get(methodSignature).getCategories()) {
121+
122+
if ((filters.contains(Constants.DELETED_FILTER) && methods.get(methodSignature).getUpdateOperation().equals(Constants.METHOD_DELETED))
123+
|| filters.contains(new Pair<>(Constants.FILTER_TYPE, Formatter.toTitleCase(category.toString())))
124+
|| filters.contains(new Pair<>(Constants.FILTER_CWE, Formatter.toTitleCase(category.toString())))
125+
|| (methods.get(methodSignature).isTrainingMethod() && filters.contains(Constants.TRAIN_FILTER))) {
126+
127+
filteredList.add(methods.get(methodSignature));
128+
break;
96129
}
97130
}
98-
return filteredList;
99-
} else
100-
return new ArrayList<>(methods.values());
131+
}
132+
return filteredList;
101133
}
102134

135+
103136
//Return list of categories as a set
104137
public static Set<Category> getCategories() {
105138

@@ -127,13 +160,13 @@ public static int addMethod(MethodWrapper method) {
127160
}
128161
}
129162

130-
//Add new method to the list
163+
//Check if method exists in list
131164
public static boolean methodExists(String methodSignature) {
132165

133166
return methods.containsKey(methodSignature);
134167
}
135168

136-
//Add new method to the list
169+
//Returns method for the specified signature
137170
public static MethodWrapper getMethod(String methodSignature) {
138171

139172
return methods.get(methodSignature);

swan_assist/src/de/fraunhofer/iem/swan/assist/data/JSONFileParser.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class JSONFileParser {
1313

1414
private String congFilePath;
1515

16-
JSONFileParser(String path) {
16+
public JSONFileParser(String path) {
1717
congFilePath = path;
1818
}
1919

@@ -35,7 +35,13 @@ public HashMap<String, MethodWrapper> parseJSONFileMap() {
3535
for (Method method : parser.parseFile(congFilePath)) {
3636

3737
MethodWrapper methodWrapper = new MethodWrapper(method);
38-
methods.put(methodWrapper.getSignature(true),methodWrapper );
38+
39+
if(method.getDiscovery().equals(Constants.TRAINING_METHOD))
40+
methodWrapper.setTrainingMethod(true);
41+
else
42+
methodWrapper.setTrainingMethod(false);
43+
44+
methods.put(methodWrapper.getSignature(true), methodWrapper);
3945
}
4046
} catch (Exception e) {
4147
Notifications.Bus.notify(new Notification("SWAN_Assist", "File Load Error", Constants.FILE_LOAD_ERROR, NotificationType.ERROR));

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import de.fraunhofer.iem.swan.data.Category;
55
import org.apache.commons.lang3.StringUtils;
66
import de.fraunhofer.iem.swan.data.Method;
7-
87
import java.util.*;
98

109
/**
@@ -19,6 +18,7 @@ public class MethodWrapper {
1918
private List<String> parameters = new ArrayList<String>();
2019
private String icon;
2120
private boolean isNewMethod;
21+
private boolean isTrainingMethod;
2222
private String updateOperation = "";
2323
private String markerMessage;
2424

@@ -122,7 +122,7 @@ public ArrayList<String> getTypesList(boolean capitalize) {
122122
if (!category.isCwe() && !capitalize) {
123123
typesList.add(category.toString());
124124
} else if (!category.isCwe())
125-
typesList.add(Formatter.capitalizeFirstCharacter(category.toString()));
125+
typesList.add(Formatter.toTitleCase(category.toString()));
126126
}
127127

128128
return typesList;
@@ -157,6 +157,13 @@ public String getUpdateOperation() {
157157
return updateOperation;
158158
}
159159

160+
public boolean isTrainingMethod() {
161+
return isTrainingMethod;
162+
}
163+
164+
public void setTrainingMethod(boolean trainingMethod) {
165+
isTrainingMethod = trainingMethod;
166+
}
160167
public void setUpdateOperation(String updateOperation) {
161168
this.updateOperation = updateOperation;
162169
}

0 commit comments

Comments
 (0)