Skip to content

Commit bf87dce

Browse files
committed
Extend editor popup method to show additional actions
1 parent 2d14340 commit bf87dce

13 files changed

+673
-418
lines changed

src/de/fraunhofer/iem/mois/assist/actions/filter/FilterActionGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.openapi.actionSystem.AnAction;
55
import com.intellij.openapi.actionSystem.AnActionEvent;
66
import com.intellij.openapi.actionSystem.Separator;
7+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
78
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
89
import de.fraunhofer.iem.mois.assist.util.Constants;
910
import javafx.util.Pair;
@@ -40,7 +41,7 @@ public boolean isPopup() {
4041
public void update(AnActionEvent event) {
4142

4243
//Disable/Enable action button
43-
if (SummaryToolWindow.CONFIG_FILE_SELECTED)
44+
if (JSONFileLoader.isFileSelected())
4445
event.getPresentation().setEnabled(true);
4546
else
4647
event.getPresentation().setEnabled(false);

src/de/fraunhofer/iem/mois/assist/actions/method/AddMethodAction.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package de.fraunhofer.iem.mois.assist.actions.method;
22

3-
import com.intellij.openapi.actionSystem.*;
4-
import com.intellij.openapi.editor.Editor;
3+
import com.intellij.openapi.actionSystem.ActionManager;
4+
import com.intellij.openapi.actionSystem.AnAction;
5+
import com.intellij.openapi.actionSystem.AnActionEvent;
56
import com.intellij.openapi.project.Project;
67
import com.intellij.openapi.ui.Messages;
7-
import com.intellij.psi.*;
8-
import com.intellij.psi.util.PsiTreeUtil;
9-
import com.intellij.psi.util.PsiTypesUtil;
108
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
119
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
12-
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
1310
import de.fraunhofer.iem.mois.assist.util.Constants;
14-
15-
import java.util.ArrayList;
16-
import java.util.List;
11+
import de.fraunhofer.iem.mois.assist.util.PsiTraversal;
1712

1813
/**
1914
* Action to add a new method by selecting a class\category.
@@ -27,79 +22,23 @@ public class AddMethodAction extends AnAction {
2722
public void actionPerformed(AnActionEvent e) {
2823

2924
//Get all the required data from data keys
30-
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
31-
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
32-
33-
//Get offset for element
34-
int offset = editor.getCaretModel().getOffset();
35-
36-
PsiJavaFile psiFile;
37-
38-
if (e.getData(LangDataKeys.PSI_FILE) instanceof PsiJavaFile) {
39-
40-
psiFile = (PsiJavaFile) e.getData(LangDataKeys.PSI_FILE);
41-
42-
if (psiFile != null) {
43-
44-
PsiElement element = psiFile.findElementAt(offset);
45-
46-
if (element != null) {
47-
48-
PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
49-
50-
if (psiMethod != null) {
51-
52-
//Determine method return type
53-
String returnType = psiMethod.getReturnType().getCanonicalText();
54-
55-
//Obtain parameters
56-
List<String> parameters = new ArrayList<String>();
57-
for (PsiParameter psiParameter : psiMethod.getParameterList().getParameters()) {
58-
59-
PsiClass psiClass = PsiTypesUtil.getPsiClass(psiParameter.getType());
60-
61-
if (psiClass != null) {
62-
parameters.add(psiClass.getQualifiedName());
63-
} else if (psiParameter.getType() instanceof PsiArrayType) {
64-
65-
66-
PsiArrayType psiArrayType = (PsiArrayType) psiParameter.getType();
67-
parameters.add(psiArrayType.getCanonicalText());
68-
} else
69-
parameters.add(psiParameter.getType().getCanonicalText());
70-
71-
}
72-
73-
//Get class
74-
PsiClass psiClass = PsiTreeUtil.getParentOfType(psiMethod, PsiClass.class);
75-
76-
MethodWrapper method = new MethodWrapper(psiMethod.getName(), parameters, returnType, psiClass.getQualifiedName());
25+
final Project project = e.getProject();
7726

78-
if (!JSONFileLoader.methodExists(method.getSignature(true))) {
79-
method.setNewMethod(true);
80-
} else {
81-
method = JSONFileLoader.getMethod(method.getSignature(true));
82-
}
27+
MethodWrapper method = PsiTraversal.getMethodAtOffset(e, true);
8328

84-
ActionManager.getInstance().tryToExecute(new UpdateMethodAction(method), e.getInputEvent(), null, "Add Method", false);
29+
if (method != null) {
8530

86-
} else {
87-
Messages.showMessageDialog(project, Constants.METHOD_NOT_FOUND, "Method Selection", Messages.getInformationIcon());
88-
}
89-
} else {
90-
Messages.showMessageDialog(project, Constants.ELEMENT_NOT_SELECTED, "Method Selection", Messages.getInformationIcon());
91-
}
92-
}
31+
ActionManager.getInstance().tryToExecute(new UpdateMethodAction(method), e.getInputEvent(), null, "Add Method", false);
9332
} else {
94-
Messages.showMessageDialog(project, Constants.NOT_JAVA_FILE, "Incorrect File", Messages.getInformationIcon());
33+
Messages.showMessageDialog(project, Constants.ELEMENT_NOT_SELECTED, "Method Selection", Messages.getInformationIcon());
9534
}
9635
}
9736

9837
@Override
9938
public void update(AnActionEvent event) {
10039

10140
//Disable/Enable action button
102-
if (SummaryToolWindow.CONFIG_FILE_SELECTED)
41+
if (JSONFileLoader.isFileSelected())
10342
event.getPresentation().setEnabled(true);
10443
else
10544
event.getPresentation().setEnabled(false);

src/de/fraunhofer/iem/mois/assist/actions/method/DeleteMethodAction.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
import com.intellij.openapi.actionSystem.AnAction;
44
import com.intellij.openapi.actionSystem.AnActionEvent;
5-
import com.intellij.openapi.actionSystem.CommonDataKeys;
65
import com.intellij.openapi.project.Project;
6+
import com.intellij.openapi.ui.Messages;
77
import com.intellij.util.messages.MessageBus;
88
import de.fraunhofer.iem.mois.assist.comm.MethodNotifier;
9+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
910
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
1011
import de.fraunhofer.iem.mois.assist.util.Constants;
12+
import de.fraunhofer.iem.mois.assist.util.PsiTraversal;
1113

1214
import javax.swing.*;
1315

1416
/**
1517
* Action to delete a method.
18+
*
1619
* @author Oshando Johnson
1720
*/
1821

@@ -21,23 +24,44 @@ public class DeleteMethodAction extends AnAction {
2124

2225
private MethodWrapper deleteMethod;
2326

24-
DeleteMethodAction(MethodWrapper method){
27+
public DeleteMethodAction() {
28+
super("Delete");
29+
deleteMethod = null;
30+
}
31+
32+
public DeleteMethodAction(MethodWrapper method) {
2533
super("Delete");
2634
deleteMethod = method;
2735
}
2836

2937
@Override
3038
public void actionPerformed(AnActionEvent anActionEvent) {
3139

32-
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
40+
final Project project = anActionEvent.getProject();
41+
42+
if (PsiTraversal.isFromEditor(anActionEvent))
43+
deleteMethod = PsiTraversal.getMethodAtOffset(anActionEvent, false);
3344

34-
int confirmation = JOptionPane.showConfirmDialog(null, Constants.CONFIRM_METHOD_DELETION, Constants.TITLE_DELETE_METHOD, JOptionPane.YES_NO_OPTION);
45+
if (deleteMethod != null) {
46+
int confirmation = JOptionPane.showConfirmDialog(null, Constants.CONFIRM_METHOD_DELETION, Constants.TITLE_DELETE_METHOD, JOptionPane.YES_NO_OPTION);
3547

36-
if (confirmation == JOptionPane.YES_OPTION) {
48+
if (confirmation == JOptionPane.YES_OPTION) {
49+
50+
MessageBus messageBus = project.getMessageBus();
51+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_REMOVED_TOPIC);
52+
publisher.afterAction(deleteMethod);
53+
}
54+
} else
55+
Messages.showMessageDialog(project, Constants.METHOD_NOT_FOUND, "Method Selection", Messages.getInformationIcon());
56+
}
57+
58+
@Override
59+
public void update(AnActionEvent event) {
3760

38-
MessageBus messageBus = project.getMessageBus();
39-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_REMOVED_TOPIC);
40-
publisher.afterAction(deleteMethod);
41-
}
61+
//Disable/Enable action button
62+
if (JSONFileLoader.isFileSelected())
63+
event.getPresentation().setEnabled(true);
64+
else
65+
event.getPresentation().setEnabled(false);
4266
}
4367
}

src/de/fraunhofer/iem/mois/assist/actions/method/MethodActionGroup.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.openapi.actionSystem.AnAction;
55
import com.intellij.openapi.actionSystem.AnActionEvent;
66
import com.intellij.openapi.actionSystem.Separator;
7+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
78
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
89
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
910
import org.jetbrains.annotations.NotNull;
@@ -35,7 +36,7 @@ public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) {
3536
new RestoreMethodAction(methodWrapper),
3637
new DeleteMethodAction(methodWrapper),
3738
new Separator(),
38-
new MethodPropertiesAction(methodWrapper),};
39+
new MethodPropertiesAction(methodWrapper)};
3940
}
4041

4142
@Override
@@ -47,7 +48,7 @@ public boolean isPopup() {
4748
public void update(AnActionEvent event) {
4849

4950
//Disable/Enable action button
50-
if (SummaryToolWindow.CONFIG_FILE_SELECTED)
51+
if (JSONFileLoader.isFileSelected())
5152
event.getPresentation().setEnabled(true);
5253
else
5354
event.getPresentation().setEnabled(false);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.fraunhofer.iem.mois.assist.actions.method;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.project.Project;
6+
import com.intellij.openapi.wm.ToolWindowManager;
7+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
8+
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
9+
10+
/**
11+
* @author Oshando Johnson on 14.12.18
12+
*/
13+
public class MethodListAction extends AnAction {
14+
@Override
15+
public void actionPerformed(AnActionEvent anActionEvent) {
16+
17+
final Project project = anActionEvent.getProject();
18+
19+
ToolWindowManager.getInstance(project).getToolWindow("AssistMOIS").show(null);
20+
}
21+
22+
@Override
23+
public void update(AnActionEvent event) {
24+
25+
//Disable/Enable action button
26+
if (JSONFileLoader.isFileSelected())
27+
event.getPresentation().setEnabled(true);
28+
else
29+
event.getPresentation().setEnabled(false);
30+
}
31+
}

src/de/fraunhofer/iem/mois/assist/actions/method/MethodPropertiesAction.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,69 @@
22

33
import com.intellij.openapi.actionSystem.AnAction;
44
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.actionSystem.CommonDataKeys;
6+
import com.intellij.openapi.actionSystem.LangDataKeys;
7+
import com.intellij.openapi.actionSystem.impl.ActionMenuItem;
8+
import com.intellij.openapi.editor.Editor;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.ui.Messages;
11+
import com.intellij.psi.PsiJavaFile;
12+
import com.intellij.util.EditorPopupHandler;
13+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
514
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
6-
import de.fraunhofer.iem.mois.assist.ui.MethodPropertiesDialog;
15+
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
16+
import de.fraunhofer.iem.mois.assist.ui.dialog.MethodPropertiesDialog;
717
import de.fraunhofer.iem.mois.assist.util.Constants;
18+
import de.fraunhofer.iem.mois.assist.util.PsiTraversal;
19+
import org.jetbrains.jsonProtocol.JsonField;
820

921
/**
1022
* Action to show additional properties for a method.
23+
*
1124
* @author Oshando Johnson
1225
*/
1326

14-
public class MethodPropertiesAction extends AnAction{
27+
public class MethodPropertiesAction extends AnAction {
1528

1629
private MethodWrapper method;
1730

18-
MethodPropertiesAction(MethodWrapper method){
31+
public MethodPropertiesAction() {
32+
this.method = null;
33+
}
34+
35+
public MethodPropertiesAction(MethodWrapper method) {
1936
super("Properties");
2037
this.method = method;
2138
}
2239

2340
@Override
2441
public void actionPerformed(AnActionEvent anActionEvent) {
2542

26-
MethodPropertiesDialog detailsDialog = new MethodPropertiesDialog(method);
27-
detailsDialog.setTitle(Constants.TITLE_METHOD_PROPERTIES);
28-
detailsDialog.pack();
29-
detailsDialog.setSize(550, 350);
30-
detailsDialog.setLocationRelativeTo(null);
31-
detailsDialog.setVisible(true);
43+
//Get all the required data from data keys
44+
final Project project = anActionEvent.getProject();
45+
46+
if (PsiTraversal.isFromEditor(anActionEvent))
47+
method = PsiTraversal.getMethodAtOffset(anActionEvent, false);
48+
49+
if (method != null) {
50+
MethodPropertiesDialog detailsDialog = new MethodPropertiesDialog(method);
51+
detailsDialog.setTitle(Constants.TITLE_METHOD_PROPERTIES);
52+
detailsDialog.pack();
53+
detailsDialog.setSize(550, 350);
54+
detailsDialog.setLocationRelativeTo(null);
55+
detailsDialog.setVisible(true);
56+
} else {
57+
Messages.showMessageDialog(project, Constants.METHOD_NOT_FOUND, "Method Selection", Messages.getInformationIcon());
58+
}
59+
}
60+
61+
@Override
62+
public void update(AnActionEvent event) {
63+
64+
//Disable/Enable action button
65+
if (JSONFileLoader.isFileSelected())
66+
event.getPresentation().setEnabled(true);
67+
else
68+
event.getPresentation().setEnabled(false);
3269
}
3370
}

src/de/fraunhofer/iem/mois/assist/actions/method/RestoreMethodAction.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import com.intellij.openapi.ui.Messages;
88
import com.intellij.util.messages.MessageBus;
99
import de.fraunhofer.iem.mois.assist.comm.MethodNotifier;
10+
import de.fraunhofer.iem.mois.assist.data.JSONFileLoader;
1011
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
11-
import de.fraunhofer.iem.mois.data.Method;
1212
import de.fraunhofer.iem.mois.assist.ui.SummaryToolWindow;
1313
import de.fraunhofer.iem.mois.assist.util.Constants;
14+
import de.fraunhofer.iem.mois.assist.util.PsiTraversal;
1415

1516
/**
1617
* Action to restore a method that was deleted after rerunning MOIS.
@@ -22,7 +23,11 @@ public class RestoreMethodAction extends AnAction {
2223

2324
private MethodWrapper method;
2425

25-
RestoreMethodAction(MethodWrapper method) {
26+
27+
public RestoreMethodAction() {
28+
}
29+
30+
public RestoreMethodAction(MethodWrapper method) {
2631
super("Restore");
2732
this.method = method;
2833
}
@@ -32,18 +37,24 @@ public void actionPerformed(AnActionEvent anActionEvent) {
3237

3338
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
3439

35-
//Notify Summary Tool window that new method was restored
36-
MessageBus messageBus = project.getMessageBus();
37-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
38-
publisher.afterAction(method);
40+
if (PsiTraversal.isFromEditor(anActionEvent))
41+
method = PsiTraversal.getMethodAtOffset(anActionEvent, false);
42+
43+
if (method != null) {
44+
//Notify Summary Tool window that new method was restored
45+
MessageBus messageBus = project.getMessageBus();
46+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
47+
publisher.afterAction(method);
3948

40-
Messages.showMessageDialog(project, Constants.MSG_METHOD_RESTORED, Constants.TITLE_RESTORE_METHOD, Messages.getInformationIcon());
49+
Messages.showMessageDialog(project, Constants.MSG_METHOD_RESTORED, Constants.TITLE_RESTORE_METHOD, Messages.getInformationIcon());
50+
} else
51+
Messages.showMessageDialog(project, Constants.METHOD_NOT_FOUND, "Method Selection", Messages.getInformationIcon());
4152
}
4253

4354
@Override
4455
public void update(AnActionEvent event) {
4556

46-
if (SummaryToolWindow.RESTORE_METHOD)
57+
if (SummaryToolWindow.RESTORE_METHOD && JSONFileLoader.isFileSelected())
4758
event.getPresentation().setEnabled(true);
4859
else
4960
event.getPresentation().setEnabled(false);

0 commit comments

Comments
 (0)