|
@@ -22,38 +22,36 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.addons.ui_swing.components.statusbar;
|
24
|
24
|
|
|
25
|
+import com.dmdirc.DMDircMBassador;
|
|
26
|
+import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
|
|
27
|
+import com.dmdirc.addons.ui_swing.components.menubar.JMenuItemBuilder;
|
25
|
28
|
import com.dmdirc.addons.ui_swing.dialogs.errors.ErrorsDialog;
|
26
|
29
|
import com.dmdirc.addons.ui_swing.injection.DialogProvider;
|
27
|
30
|
import com.dmdirc.addons.ui_swing.injection.MainWindow;
|
|
31
|
+import com.dmdirc.events.NonFatalProgramErrorEvent;
|
|
32
|
+import com.dmdirc.events.ProgramErrorDeletedEvent;
|
28
|
33
|
import com.dmdirc.logger.ErrorLevel;
|
29
|
|
-import com.dmdirc.logger.ErrorListener;
|
30
|
34
|
import com.dmdirc.logger.ErrorManager;
|
31
|
35
|
import com.dmdirc.logger.ProgramError;
|
32
|
36
|
import com.dmdirc.ui.IconManager;
|
33
|
37
|
|
34
|
38
|
import java.awt.Window;
|
35
|
|
-import java.awt.event.ActionEvent;
|
36
|
|
-import java.awt.event.ActionListener;
|
37
|
39
|
import java.awt.event.MouseEvent;
|
38
|
40
|
import java.util.Set;
|
39
|
41
|
|
40
|
42
|
import javax.inject.Inject;
|
41
|
|
-import javax.inject.Provider;
|
42
|
43
|
import javax.inject.Singleton;
|
43
|
44
|
import javax.swing.Icon;
|
44
|
45
|
import javax.swing.JLabel;
|
45
|
|
-import javax.swing.JMenuItem;
|
46
|
46
|
import javax.swing.JPopupMenu;
|
47
|
|
-import javax.swing.SwingUtilities;
|
|
47
|
+
|
|
48
|
+import net.engio.mbassy.listener.Handler;
|
48
|
49
|
|
49
|
50
|
/**
|
50
|
51
|
* Shows error status in the status bar.
|
51
|
|
- *
|
52
|
|
- * @since 0.6.3m1
|
53
|
52
|
*/
|
54
|
53
|
@Singleton
|
55
|
|
-public class ErrorPanel extends StatusbarPopupPanel<JLabel> implements
|
56
|
|
- ErrorListener, ActionListener {
|
|
54
|
+public class ErrorPanel extends StatusbarPopupPanel<JLabel> {
|
57
|
55
|
|
58
|
56
|
/** Serial version UID. */
|
59
|
57
|
private static final long serialVersionUID = 2;
|
|
@@ -61,55 +59,51 @@ public class ErrorPanel extends StatusbarPopupPanel<JLabel> implements
|
61
|
59
|
private final Icon defaultIcon;
|
62
|
60
|
/** Parent window that will own popups. */
|
63
|
61
|
private final Window parentWindow;
|
64
|
|
- /** Swing status bar. */
|
65
|
|
- private final Provider<SwingStatusBar> statusBar;
|
66
|
62
|
/** The manager to use to retrieve icons. */
|
67
|
63
|
private final IconManager iconManager;
|
68
|
64
|
/** Error manager. */
|
69
|
65
|
private final ErrorManager errorManager;
|
70
|
66
|
/** Dismiss menu. */
|
71
|
67
|
private final JPopupMenu menu;
|
72
|
|
- /** Show menu item. */
|
73
|
|
- private final JMenuItem show;
|
74
|
68
|
/** Error list dialog provider. */
|
75
|
69
|
private final DialogProvider<ErrorsDialog> errorListDialogProvider;
|
|
70
|
+ /** The event bus to listen to error changes on .*/
|
|
71
|
+ private final DMDircMBassador eventBus;
|
76
|
72
|
/** Currently showing error level. */
|
77
|
73
|
private ErrorLevel errorLevel;
|
|
74
|
+ private boolean hasBeenVisible;
|
78
|
75
|
|
79
|
76
|
/**
|
80
|
77
|
* Creates a new ErrorPanel for the specified status bar.
|
81
|
78
|
*
|
82
|
79
|
* @param iconManager The manager to use to retrieve icons.
|
83
|
80
|
* @param parentWindow Main frame
|
84
|
|
- * @param statusBar Status bar
|
85
|
81
|
* @param errorListDialogProvider Error list dialog provider.
|
86
|
82
|
*/
|
87
|
83
|
@Inject
|
88
|
84
|
public ErrorPanel(
|
89
|
85
|
final IconManager iconManager,
|
90
|
86
|
@MainWindow final Window parentWindow,
|
91
|
|
- final Provider<SwingStatusBar> statusBar,
|
92
|
87
|
final DialogProvider<ErrorsDialog> errorListDialogProvider,
|
93
|
|
- final ErrorManager errorManager) {
|
|
88
|
+ final ErrorManager errorManager,
|
|
89
|
+ final DMDircMBassador eventBus) {
|
94
|
90
|
super(new JLabel());
|
95
|
|
-
|
96
|
91
|
this.parentWindow = parentWindow;
|
97
|
|
- this.statusBar = statusBar;
|
98
|
92
|
this.iconManager = iconManager;
|
99
|
93
|
this.errorListDialogProvider = errorListDialogProvider;
|
100
|
94
|
this.errorManager = errorManager;
|
|
95
|
+ this.eventBus = eventBus;
|
101
|
96
|
defaultIcon = iconManager.getIcon("normal");
|
102
|
97
|
|
103
|
98
|
menu = new JPopupMenu();
|
104
|
|
- final JMenuItem dismiss = new JMenuItem("Clear All");
|
105
|
|
- show = new JMenuItem("Open");
|
106
|
99
|
label.setIcon(defaultIcon);
|
107
|
100
|
setVisible(errorManager.getErrorCount() > 0);
|
108
|
|
- menu.add(show);
|
109
|
|
- menu.add(dismiss);
|
110
|
|
- errorManager.addErrorListener(this);
|
111
|
|
- dismiss.addActionListener(this);
|
112
|
|
- show.addActionListener(this);
|
|
101
|
+ menu.add(JMenuItemBuilder.create().setText("Open")
|
|
102
|
+ .addActionListener(e -> errorListDialogProvider.displayOrRequestFocus()).build());
|
|
103
|
+ menu.add(JMenuItemBuilder.create()
|
|
104
|
+ .setText("Clear All")
|
|
105
|
+ .addActionListener(e -> errorManager.getErrors().forEach(errorManager::deleteError))
|
|
106
|
+ .build());
|
113
|
107
|
checkErrors();
|
114
|
108
|
}
|
115
|
109
|
|
|
@@ -118,49 +112,41 @@ public class ErrorPanel extends StatusbarPopupPanel<JLabel> implements
|
118
|
112
|
return new ErrorPopup(errorManager, iconManager, this, parentWindow);
|
119
|
113
|
}
|
120
|
114
|
|
121
|
|
- /** Clears the error. */
|
122
|
|
- public void clearError() {
|
123
|
|
- label.setIcon(defaultIcon);
|
124
|
|
- errorLevel = null;
|
125
|
|
- }
|
126
|
|
-
|
127
|
115
|
@Override
|
128
|
|
- public void errorAdded(final ProgramError error) {
|
129
|
|
- checkErrors();
|
|
116
|
+ public void setVisible(final boolean visible) {
|
|
117
|
+ if (!hasBeenVisible) {
|
|
118
|
+ hasBeenVisible = true;
|
|
119
|
+ eventBus.subscribe(this);
|
|
120
|
+ }
|
|
121
|
+ super.setVisible(visible);
|
130
|
122
|
}
|
131
|
123
|
|
132
|
|
- @Override
|
133
|
|
- public void errorDeleted(final ProgramError error) {
|
|
124
|
+ @Handler(invocation = EdtHandlerInvocation.class)
|
|
125
|
+ public void errorAdded(final NonFatalProgramErrorEvent event) {
|
134
|
126
|
checkErrors();
|
135
|
127
|
}
|
136
|
128
|
|
137
|
|
- @Override
|
138
|
|
- public void errorStatusChanged(final ProgramError error) {
|
139
|
|
- //Ignore
|
|
129
|
+ @Handler(invocation = EdtHandlerInvocation.class)
|
|
130
|
+ public void errorDeleted(final ProgramErrorDeletedEvent event) {
|
|
131
|
+ checkErrors();
|
140
|
132
|
}
|
141
|
133
|
|
142
|
134
|
/** Checks all the errors for the most significant error. */
|
143
|
135
|
private void checkErrors() {
|
144
|
|
- SwingUtilities.invokeLater(() -> {
|
145
|
|
- clearError();
|
146
|
|
- final Set<ProgramError> errors = errorManager.getErrors();
|
147
|
|
-
|
148
|
|
- if (errors.isEmpty()) {
|
149
|
|
- setVisible(false);
|
150
|
|
- } else {
|
151
|
|
- errors.stream().filter(error -> errorLevel == null ||
|
152
|
|
- !error.getLevel().moreImportant(errorLevel)).forEach(error -> {
|
153
|
|
- errorLevel = error.getLevel();
|
154
|
|
- label.setIcon(iconManager.getIcon(errorLevel.getIcon()));
|
155
|
|
- });
|
156
|
|
- setVisible(true);
|
157
|
|
- }
|
158
|
|
- });
|
159
|
|
- }
|
|
136
|
+ label.setIcon(defaultIcon);
|
|
137
|
+ errorLevel = null;
|
|
138
|
+ final Set<ProgramError> errors = errorManager.getErrors();
|
160
|
139
|
|
161
|
|
- @Override
|
162
|
|
- public boolean isReady() {
|
163
|
|
- return statusBar.get().isValid();
|
|
140
|
+ if (errors.isEmpty()) {
|
|
141
|
+ setVisible(false);
|
|
142
|
+ } else {
|
|
143
|
+ errors.stream().filter(error -> errorLevel == null ||
|
|
144
|
+ !error.getLevel().moreImportant(errorLevel)).forEach(error -> {
|
|
145
|
+ errorLevel = error.getLevel();
|
|
146
|
+ label.setIcon(iconManager.getIcon(errorLevel.getIcon()));
|
|
147
|
+ });
|
|
148
|
+ setVisible(true);
|
|
149
|
+ }
|
164
|
150
|
}
|
165
|
151
|
|
166
|
152
|
@Override
|
|
@@ -207,13 +193,4 @@ public class ErrorPanel extends StatusbarPopupPanel<JLabel> implements
|
207
|
193
|
}
|
208
|
194
|
}
|
209
|
195
|
|
210
|
|
- @Override
|
211
|
|
- public void actionPerformed(final ActionEvent e) {
|
212
|
|
- if (e.getSource() == show) {
|
213
|
|
- errorListDialogProvider.displayOrRequestFocus();
|
214
|
|
- } else {
|
215
|
|
- errorManager.getErrors().forEach(errorManager::deleteError);
|
216
|
|
- }
|
217
|
|
- }
|
218
|
|
-
|
219
|
196
|
}
|