Skip to content

Commit 5655f65

Browse files
committed
Refactor dialogs to use DialogWrapper
1 parent c4ef4c4 commit 5655f65

File tree

10 files changed

+188
-452
lines changed

10 files changed

+188
-452
lines changed

src/de/fraunhofer/iem/mois/assist/ui/MethodListTree.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package de.fraunhofer.iem.mois.assist.ui;
22

33
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
4+
import com.intellij.notification.Notification;
5+
import com.intellij.notification.NotificationListener;
6+
import com.intellij.notification.NotificationType;
7+
import com.intellij.notification.Notifications;
48
import com.intellij.openapi.actionSystem.ActionManager;
59
import com.intellij.openapi.actionSystem.ActionPopupMenu;
610
import com.intellij.openapi.editor.Document;
@@ -9,7 +13,9 @@
913
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
1014
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
1115
import com.intellij.openapi.project.Project;
12-
import com.intellij.openapi.ui.Messages;
16+
import com.intellij.openapi.ui.MessageType;
17+
import com.intellij.openapi.ui.popup.Balloon;
18+
import com.intellij.openapi.ui.popup.JBPopupFactory;
1319
import com.intellij.openapi.vfs.VirtualFile;
1420
import com.intellij.psi.PsiClass;
1521
import com.intellij.psi.PsiFile;
@@ -32,16 +38,14 @@
3238
import de.fraunhofer.iem.mois.assist.util.Formatter;
3339
import de.fraunhofer.iem.mois.assist.util.PsiTraversal;
3440
import de.fraunhofer.iem.mois.data.Category;
35-
import icons.PluginIcons;
3641
import org.jetbrains.annotations.NotNull;
3742

3843
import javax.swing.*;
44+
import javax.swing.event.HyperlinkEvent;
3945
import javax.swing.event.TreeSelectionEvent;
4046
import javax.swing.event.TreeSelectionListener;
4147
import javax.swing.tree.DefaultMutableTreeNode;
4248
import javax.swing.tree.DefaultTreeModel;
43-
import java.awt.event.ActionEvent;
44-
import java.awt.event.ActionListener;
4549
import java.awt.event.MouseAdapter;
4650
import java.awt.event.MouseEvent;
4751
import java.util.ArrayList;
@@ -156,8 +160,11 @@ public void mouseClicked(MouseEvent e) {
156160
}
157161
}
158162

159-
if (!methodFound)
160-
Messages.showMessageDialog(Constants.METHOD_NOT_FOUND_IN_EDITOR, "Method Not Found", Messages.getInformationIcon());
163+
if (!methodFound) {
164+
JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(Constants.METHOD_NOT_FOUND_IN_EDITOR, MessageType.ERROR, null)
165+
.createBalloon()
166+
.show(JBPopupFactory.getInstance().guessBestPopupLocation((JComponent) e.getComponent()), Balloon.Position.below);
167+
}
161168
}
162169
}
163170
}
@@ -231,9 +238,9 @@ public void afterAction(MethodWrapper newMethod) {
231238

232239
DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();
233240

234-
if(node==null){
241+
if (node == null) {
235242
node = searchNode((DefaultMutableTreeNode) treeModel.getRoot(), newMethod.getSignature(true));
236-
}
243+
}
237244

238245
treeModel.removeNodeFromParent(node);
239246
JSONFileLoader.removeMethod(newMethod);
@@ -246,11 +253,8 @@ public void afterAction(MethodWrapper newMethod) {
246253
@Override
247254
public void launchMois(HashMap<String, String> values) {
248255

249-
JLabel notificationMessage = (JLabel) notificationPanel.getComponent(0);
250-
notificationMessage.setText(Constants.NOTIFICATION_START_MOIS);
251-
252-
//show progress bar
253-
notificationPanel.getComponent(1).setVisible(true);
256+
JSONFileLoader.setReloading(true);
257+
Notifications.Bus.notify(new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "Reloading MOIS", Constants.NOTIFICATION_START_MOIS, NotificationType.INFORMATION));
254258
}
255259
});
256260

@@ -259,27 +263,32 @@ public void launchMois(HashMap<String, String> values) {
259263
@Override
260264
public void launchMois(HashMap<String, String> values) {
261265

262-
JLabel label = (JLabel) notificationPanel.getComponent(0);
263-
label.setText(values.get(Constants.MOIS_OUTPUT_MESSAGE));
264-
265-
//remove progress bar
266-
notificationPanel.getComponent(1).setVisible(false);
266+
JSONFileLoader.setReloading(false);
267+
NotificationType notificationType = NotificationType.INFORMATION;
267268

268-
JButton viewResults = (JButton) notificationPanel.getComponent(2);
269-
viewResults.setIcon(PluginIcons.NOTIFICATION_NEW);
270-
viewResults.setToolTipText(Constants.NOTIFICATION_MOIS);
269+
if (!values.get(Constants.MOIS_OUTPUT_MESSAGE).equals(Constants.NOTIFICATION_END_MOIS_SUCCESS)) {
270+
notificationType = NotificationType.ERROR;
271+
}
271272

272-
viewResults.addActionListener(new ActionListener() {
273+
String message = "<html>" + values.get(Constants.MOIS_OUTPUT_MESSAGE) + "<br><a href='logs'>View Logs</a> or <a href='load'>Load Changes</a></html>";
274+
Notifications.Bus.notify(new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "Completed", message, notificationType, new NotificationListener() {
273275
@Override
274-
public void actionPerformed(ActionEvent e) {
276+
public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent hyperlinkEvent) {
275277

276-
MoisResultsDialog resultsDialog = new MoisResultsDialog(project, values);
277-
resultsDialog.pack();
278-
resultsDialog.setSize(550, 350);
279-
resultsDialog.setLocationRelativeTo(null);
280-
resultsDialog.setVisible(true);
278+
if (hyperlinkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
279+
280+
if (hyperlinkEvent.getDescription().equals("logs")) {
281+
282+
MoisResultsDialog resultsDialog = new MoisResultsDialog(project, values);
283+
resultsDialog.show();
284+
285+
} else if (hyperlinkEvent.getDescription().equals("load")){
286+
JSONFileLoader.loadUpdatedFile(values.get(Constants.MOIS_OUTPUT_FILE));
287+
loadMethods();
288+
}
289+
}
281290
}
282-
});
291+
}));
283292
}
284293
});
285294

src/de/fraunhofer/iem/mois/assist/ui/SummaryToolWindow.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
public class SummaryToolWindow implements ToolWindowFactory {
3636

3737
private String currentFile;
38-
private JBPanel notificationPanel;
3938
public static ArrayList<String> TREE_FILTERS;
4039
public static boolean CURRENT_FILE_FILTER;
4140
public static boolean RESTORE_METHOD;
@@ -64,31 +63,6 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
6463
//Add method list tree to tool window
6564
toolPanel.add(new JBScrollPane(new MethodListTree(project)), BorderLayout.CENTER);
6665

67-
//Tool Window Footer
68-
notificationPanel = new JBPanel(new BorderLayout());
69-
notificationPanel.setBorder(JBUI.Borders.empty(4));
70-
71-
JLabel eventDescription = new JLabel();
72-
eventDescription.setFont(new Font(eventDescription.getFont().getName(), Font.PLAIN, 11));
73-
notificationPanel.add(eventDescription, BorderLayout.WEST);
74-
75-
JProgressBar progressBar = new JProgressBar();
76-
progressBar.setIndeterminate(true);
77-
78-
JPanel wrappingPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
79-
wrappingPanel.setVisible(false);
80-
wrappingPanel.add(progressBar);
81-
notificationPanel.add(wrappingPanel);
82-
83-
JButton eventNotification = new JButton();
84-
eventNotification.setIcon(PluginIcons.NOTIFICATION_NONE);
85-
eventNotification.setOpaque(false);
86-
eventNotification.setBorder(null);
87-
eventNotification.setToolTipText(Constants.NOTIFICATION_NONE);
88-
notificationPanel.add(eventNotification, BorderLayout.EAST);
89-
90-
toolPanel.add(notificationPanel, BorderLayout.PAGE_END);
91-
9266
//Add Content to ToolWindow
9367
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
9468
Content toolContent = contentFactory.createContent(toolPanel, "", false);

src/de/fraunhofer/iem/mois/assist/ui/dialog/MethodDialog.form

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.fraunhofer.iem.mois.assist.ui.dialog.MethodDialog">
3-
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="7" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
66
<xy x="8" y="54" width="570" height="404"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
1010
<children>
11-
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
12-
<margin top="0" left="0" bottom="0" right="0"/>
13-
<constraints>
14-
<grid row="6" column="0" row-span="1" col-span="5" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
15-
</constraints>
16-
<properties/>
17-
<border type="none"/>
18-
<children>
19-
<hspacer id="98af6">
20-
<constraints>
21-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
22-
</constraints>
23-
</hspacer>
24-
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
25-
<margin top="0" left="0" bottom="0" right="0"/>
26-
<constraints>
27-
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
28-
</constraints>
29-
<properties/>
30-
<border type="none"/>
31-
<children>
32-
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
33-
<constraints>
34-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
35-
</constraints>
36-
<properties>
37-
<text value="OK"/>
38-
</properties>
39-
</component>
40-
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
41-
<constraints>
42-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
43-
</constraints>
44-
<properties>
45-
<text value="Cancel"/>
46-
</properties>
47-
</component>
48-
</children>
49-
</grid>
50-
<component id="d0bdc" class="javax.swing.JButton" binding="buttonProperty">
51-
<constraints>
52-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
53-
</constraints>
54-
<properties>
55-
<text value="Properties"/>
56-
</properties>
57-
</component>
58-
</children>
59-
</grid>
6011
<grid id="e3588" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
6112
<margin top="0" left="0" bottom="0" right="0"/>
6213
<constraints>

src/de/fraunhofer/iem/mois/assist/ui/dialog/MethodDialog.java

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package de.fraunhofer.iem.mois.assist.ui.dialog;
22

3+
import com.intellij.notification.Notification;
4+
import com.intellij.notification.NotificationType;
5+
import com.intellij.notification.Notifications;
6+
import com.intellij.openapi.actionSystem.ActionManager;
7+
import com.intellij.openapi.actionSystem.AnAction;
38
import com.intellij.openapi.project.Project;
9+
import com.intellij.openapi.ui.DialogWrapper;
10+
import com.intellij.openapi.ui.MessageType;
411
import com.intellij.openapi.ui.Messages;
12+
import com.intellij.openapi.ui.popup.Balloon;
13+
import com.intellij.openapi.ui.popup.JBPopupFactory;
514
import com.intellij.util.messages.MessageBus;
15+
import de.fraunhofer.iem.mois.assist.actions.method.MethodPropertiesAction;
616
import de.fraunhofer.iem.mois.assist.comm.MethodNotifier;
717
import de.fraunhofer.iem.mois.assist.data.MethodWrapper;
818
import de.fraunhofer.iem.mois.assist.ui.CategoryRenderer;
919
import de.fraunhofer.iem.mois.assist.util.Constants;
1020
import de.fraunhofer.iem.mois.data.Category;
1121
import org.apache.commons.lang.StringUtils;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
1224

1325
import javax.swing.*;
1426
import java.awt.event.*;
@@ -19,11 +31,9 @@
1931
*
2032
* @author Oshando Johnson
2133
*/
22-
public class MethodDialog extends JDialog {
34+
public class MethodDialog extends DialogWrapper {
2335

2436
private JPanel contentPane;
25-
private JButton buttonOK;
26-
private JButton buttonCancel;
2737
private JList selectedList;
2838
private JList availableList;
2939
private JRadioButton cweRadioButton;
@@ -39,16 +49,21 @@ public class MethodDialog extends JDialog {
3949

4050
public MethodDialog(MethodWrapper m, Project project, Set<Category> availableCategories) {
4151

42-
method = m;
52+
super(project);
4353

54+
method = m;
4455
this.project = project;
4556

46-
typeRadioButton.setSelected(true);
57+
if (method.isNewMethod())
58+
setTitle(Constants.TITLE_ADD_METHOD);
59+
else
60+
setTitle(Constants.TITLE_UPDATE_METHOD);
4761

4862
methodSignature.setText(method.getSignature(false));
4963
methodSignature.setToolTipText(method.getSignature(true));
5064
methodTypes.setText(StringUtils.join(method.getTypesList(true), ", "));
5165
methodCwes.setText(StringUtils.join(method.getCWEList(), ", "));
66+
typeRadioButton.setSelected(true);
5267

5368
for (Category category : method.getCategories()) {
5469

@@ -65,9 +80,9 @@ public MethodDialog(MethodWrapper m, Project project, Set<Category> availableCat
6580
availableList.setCellRenderer(new CategoryRenderer());
6681
availableList.setModel(availableModel);
6782

68-
setContentPane(contentPane);
6983
setModal(true);
70-
getRootPane().setDefaultButton(buttonOK);
84+
setSize(550, 350);
85+
init();
7186

7287
typeRadioButton.addActionListener(new ActionListener() {
7388
@Override
@@ -142,20 +157,7 @@ public void mouseClicked(MouseEvent e) {
142157
});
143158

144159

145-
buttonOK.addActionListener(new ActionListener() {
146-
public void actionPerformed(ActionEvent e) {
147-
148-
onOK();
149-
}
150-
});
151-
152-
buttonCancel.addActionListener(new ActionListener() {
153-
public void actionPerformed(ActionEvent e) {
154-
onCancel();
155-
}
156-
});
157-
158-
buttonProperty.addActionListener(new ActionListener() {
160+
/* buttonProperty.addActionListener(new ActionListener() {
159161
@Override
160162
public void actionPerformed(ActionEvent e) {
161163
@@ -166,45 +168,36 @@ public void actionPerformed(ActionEvent e) {
166168
detailsDialog.setLocationRelativeTo(null);
167169
detailsDialog.setVisible(true);
168170
}
169-
});
170-
171-
// call onCancel() when cross is clicked
172-
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
173-
addWindowListener(new WindowAdapter() {
174-
public void windowClosing(WindowEvent e) {
175-
onCancel();
176-
}
177-
});
178-
179-
// call onCancel() on ESCAPE
180-
contentPane.registerKeyboardAction(new ActionListener() {
181-
public void actionPerformed(ActionEvent e) {
182-
onCancel();
183-
}
184-
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
171+
});*/
185172
}
186173

187-
private void onOK() {
188-
189-
if (method.getCategories().size() == 0) {
174+
@Override
175+
protected void doOKAction() {
190176

191-
Messages.showMessageDialog(Constants.NO_CATEGORY_SELECTED, "Category Selection", Messages.getInformationIcon());
192-
} else {
193-
//Notify Summary Tool window that new method was added
194-
MessageBus messageBus = project.getMessageBus();
177+
if (isOKActionEnabled()) {
178+
if (method.getCategories().size() == 0) {
179+
JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(Constants.NO_CATEGORY_SELECTED, MessageType.ERROR, null)
180+
.createBalloon()
181+
.show(JBPopupFactory.getInstance().guessBestPopupLocation((JComponent) selectedList), Balloon.Position.below);
182+
} else {
183+
//Notify Summary Tool window that new method was added
184+
MessageBus messageBus = project.getMessageBus();
195185

196-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
197-
publisher.afterAction(method);
186+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
187+
publisher.afterAction(method);
198188

199-
dispose();
189+
dispose();
190+
}
200191
}
201192
}
202193

203-
private void onCancel() {
204-
// add your code here if necessary
205-
dispose();
194+
@Nullable
195+
@Override
196+
protected JComponent createCenterPanel() {
197+
return contentPane;
206198
}
207199

200+
208201
//Add categories to the List model
209202
private DefaultListModel<Category> addCategoriesToModel(Set<Category> categories, boolean showCwe) {
210203

0 commit comments

Comments
 (0)