Browse Source

Move default timeout logic to StatusMessage.

Change-Id: I332549e037d94849a5af18e94e186cf65d3a033c
Depends-On: I7f8e95c440482310d1818cc50f389d9e7ec8dfa6
Reviewed-on: http://gerrit.dmdirc.com/1863
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 13 years ago
parent
commit
cc923143f0

+ 37
- 48
src/com/dmdirc/addons/ui_swing/components/statusbar/MessageLabel.java View File

@@ -24,7 +24,6 @@ package com.dmdirc.addons.ui_swing.components.statusbar;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
26 26
 import com.dmdirc.config.IdentityManager;
27
-import com.dmdirc.interfaces.ConfigChangeListener;
28 27
 import com.dmdirc.ui.IconManager;
29 28
 import com.dmdirc.ui.StatusMessage;
30 29
 import com.dmdirc.ui.interfaces.StatusBarComponent;
@@ -32,7 +31,9 @@ import com.dmdirc.ui.interfaces.StatusMessageNotifier;
32 31
 
33 32
 import java.awt.event.MouseEvent;
34 33
 import java.awt.event.MouseListener;
34
+import java.util.ArrayList;
35 35
 import java.util.Date;
36
+import java.util.List;
36 37
 import java.util.Timer;
37 38
 import java.util.TimerTask;
38 39
 import java.util.concurrent.Semaphore;
@@ -44,8 +45,8 @@ import javax.swing.SwingUtilities;
44 45
 /**
45 46
  * Message label handles showing messages in the status bar.
46 47
  */
47
-public class MessageLabel extends JLabel implements StatusBarComponent, 
48
-        MouseListener, ConfigChangeListener {
48
+public class MessageLabel extends JLabel implements StatusBarComponent,
49
+        MouseListener {
49 50
 
50 51
     /**
51 52
      * A version number for this class. It should be changed whenever the class
@@ -55,12 +56,12 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
55 56
     private static final long serialVersionUID = 1;
56 57
     /** Default status bar message. */
57 58
     private final StatusMessage defaultMessage;
58
-    /** current status bar message notifier. */
59
-    private transient StatusMessageNotifier messageNotifier;
59
+    /** Message queue. */
60
+    private final List<StatusMessage> messages;
61
+    /** Current status messsage. */
62
+    private int currentMessage;
60 63
     /** Timer to clear the message. */
61 64
     private transient TimerTask messageTimer;
62
-    /** Message timeout. */
63
-    private int timeout;
64 65
     /** Set message synchronisation. */
65 66
     private final Semaphore semaphore;
66 67
 
@@ -69,14 +70,14 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
69 70
      */
70 71
     public MessageLabel() {
71 72
         super();
72
-        defaultMessage = new StatusMessage(null, "Ready.", null, -1);
73
+        currentMessage = -1;
74
+        messages = new ArrayList<StatusMessage>();
75
+        defaultMessage = new StatusMessage(null, "Ready.", null, -1,
76
+                IdentityManager.getGlobalConfig());
73 77
         semaphore = new Semaphore(1);
74 78
         setText("Ready.");
75 79
         setBorder(BorderFactory.createEtchedBorder());
76 80
         addMouseListener(this);
77
-        setCachedSettings();
78
-        IdentityManager.getGlobalConfig().addChangeListener("ui",
79
-                "awayindicator", this);
80 81
     }
81 82
 
82 83
     /**
@@ -88,7 +89,8 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
88 89
      */
89 90
     @Deprecated
90 91
     public void setMessage(final String newMessage) {
91
-        setMessage(new StatusMessage(null, newMessage, null, timeout));
92
+        setMessage(new StatusMessage(null, newMessage, null, -1,
93
+                IdentityManager.getGlobalConfig()));
92 94
     }
93 95
 
94 96
     /**
@@ -102,7 +104,8 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
102 104
     @Deprecated
103 105
     public void setMessage(final String newMessage,
104 106
             final StatusMessageNotifier newNotifier) {
105
-        setMessage(new StatusMessage(null, newMessage, newNotifier, timeout));
107
+        setMessage(new StatusMessage(null, newMessage, newNotifier, -1,
108
+                IdentityManager.getGlobalConfig()));
106 109
     }
107 110
 
108 111
     /**
@@ -115,7 +118,8 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
115 118
      */
116 119
     @Deprecated
117 120
     public void setMessage(final String iconType, final String newMessage) {
118
-        setMessage(new StatusMessage(iconType, newMessage, null, timeout));
121
+        setMessage(new StatusMessage(iconType, newMessage, null, -1,
122
+                IdentityManager.getGlobalConfig()));
119 123
     }
120 124
 
121 125
     /**
@@ -130,8 +134,8 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
130 134
     @Deprecated
131 135
     public void setMessage(final String iconType, final String newMessage,
132 136
             final StatusMessageNotifier newNotifier) {
133
-        setMessage(new StatusMessage(iconType, newMessage, newNotifier,
134
-                timeout));
137
+        setMessage(new StatusMessage(iconType, newMessage, newNotifier, -1,
138
+                IdentityManager.getGlobalConfig()));
135 139
     }
136 140
 
137 141
     /**
@@ -146,7 +150,8 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
146 150
     @Deprecated
147 151
     public void setMessage(final String newMessage,
148 152
             final StatusMessageNotifier newNotifier, final int timeout) {
149
-        setMessage(new StatusMessage(null, newMessage, newNotifier, timeout));
153
+        setMessage(new StatusMessage(null, newMessage, newNotifier, timeout,
154
+                IdentityManager.getGlobalConfig()));
150 155
     }
151 156
 
152 157
     /**
@@ -160,10 +165,10 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
160 165
      * @deprecated Should use {@link setMessage(StatusMessage)} instead
161 166
      */
162 167
     @Deprecated
163
-    public void setMessage(final String iconType, final String newMessage, 
168
+    public void setMessage(final String iconType, final String newMessage,
164 169
             final StatusMessageNotifier newNotifier, final int timeout) {
165 170
         setMessage(new StatusMessage(iconType, newMessage, newNotifier,
166
-                timeout));
171
+                timeout, IdentityManager.getGlobalConfig()));
167 172
     }
168 173
 
169 174
     /**
@@ -173,14 +178,15 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
173 178
      */
174 179
     public void setMessage(final StatusMessage message) {
175 180
         semaphore.acquireUninterruptibly();
176
-        this.messageNotifier = message.getMessageNotifier();
177 181
         SwingUtilities.invokeLater(new Runnable() {
178 182
 
179 183
             /** {@inheritDoc} */
180 184
             @Override
181 185
             public void run() {
182 186
                 try {
183
-                   if (message.getIconType() == null) {
187
+                    messages.add(message);
188
+                    currentMessage = messages.indexOf(message);
189
+                    if (message.getIconType() == null) {
184 190
                         setIcon(null);
185 191
                     } else {
186 192
                         setIcon(IconManager.getIconManager().getIcon(
@@ -188,25 +194,18 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
188 194
                     }
189 195
                     setText(UIUtilities.clipStringifNeeded(MessageLabel.this,
190 196
                             message.getMessage(), getWidth()));
191
-                    messageNotifier = message.getMessageNotifier();
192 197
 
193
-                    if (messageTimer != null && (System.currentTimeMillis() -
194
-                            messageTimer.scheduledExecutionTime()) <= 0) {
198
+                    if (messageTimer != null && (System.currentTimeMillis()
199
+                            - messageTimer.scheduledExecutionTime()) <= 0) {
195 200
                         messageTimer.cancel();
196 201
                     }
197 202
 
198 203
                     if (!defaultMessage.equals(message)) {
199
-                        messageTimer = new TimerTask() {
200
-
201
-                            /** {@inheritDoc} */
202
-                            @Override
203
-                            public void run() {
204
-                                clearMessage();
205
-                            }
206
-                        };
204
+                        messageTimer = new MessageTimerTask(MessageLabel.this);
207 205
                         new Timer("SwingStatusBar messageTimer").schedule(
208
-                                messageTimer, new Date(System.currentTimeMillis()
209
-                                + 250 + timeout * 1000L));
206
+                                messageTimer, new Date(
207
+                                System.currentTimeMillis() + 250
208
+                                + message.getTimeout() * 1000L));
210 209
                     }
211 210
                 } finally {
212 211
                     semaphore.release();
@@ -229,8 +228,10 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
229 228
      */
230 229
     @Override
231 230
     public void mouseClicked(final MouseEvent e) {
232
-        if (messageNotifier != null) {
233
-            messageNotifier.clickReceived(e.getButton(), e.getClickCount());
231
+        if (currentMessage != -1 && messages.size() > currentMessage
232
+                && messages.get(currentMessage).getMessageNotifier() != null) {
233
+            messages.get(currentMessage).getMessageNotifier().clickReceived(
234
+                    e.getButton(), e.getClickCount());
234 235
         }
235 236
     }
236 237
 
@@ -273,16 +274,4 @@ public class MessageLabel extends JLabel implements StatusBarComponent,
273 274
     public void mouseExited(final MouseEvent e) {
274 275
         //Ignore
275 276
     }
276
-
277
-    /** Set cached options. */
278
-    private void setCachedSettings() {
279
-        timeout = IdentityManager.getGlobalConfig().getOptionInt("statusBar",
280
-                "messageDisplayLength");
281
-    }
282
-
283
-    /** {@inheritDoc} */
284
-    @Override
285
-    public void configChanged(final String domain, final String key) {
286
-        setCachedSettings();
287
-    }
288 277
 }

+ 51
- 0
src/com/dmdirc/addons/ui_swing/components/statusbar/MessageTimerTask.java View File

@@ -0,0 +1,51 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.components.statusbar;
24
+
25
+import java.util.TimerTask;
26
+
27
+/**
28
+ * Timer task that clears a message label on completion.
29
+ */
30
+public class MessageTimerTask extends TimerTask {
31
+
32
+    /** Message label. */
33
+    private final MessageLabel messageLabel;
34
+
35
+    /**
36
+     * Creates a new message timer task that clears a message label on
37
+     * completion.
38
+     *
39
+     * @param messageLabel Message label to clear on completion
40
+     */
41
+    public MessageTimerTask(final MessageLabel messageLabel) {
42
+        super();
43
+        this.messageLabel = messageLabel;
44
+    }
45
+
46
+    /** {@inheritDoc} */
47
+    @Override
48
+    public void run() {
49
+        messageLabel.clearMessage();
50
+    }
51
+}

+ 20
- 19
src/com/dmdirc/addons/ui_swing/components/statusbar/SwingStatusBar.java View File

@@ -39,7 +39,7 @@ import javax.swing.SwingUtilities;
39 39
 
40 40
 import net.miginfocom.swing.MigLayout;
41 41
 
42
-/** Status bar, shows message and info on the gui. */
42
+/** Status bar, shows message and info on the GUI. */
43 43
 public final class SwingStatusBar extends JPanel implements StatusBar {
44 44
 
45 45
     /**
@@ -48,6 +48,9 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
48 48
      * objects being unserialized with the new class).
49 49
      */
50 50
     private static final long serialVersionUID = 5;
51
+    /** Mig layout component restraints. */
52
+    private static final String COMPONENT_CONSTRAINTS
53
+            = "sgy components, hmax 20, hmin 20, wmin 20, shrink 0";
51 54
     /** message label. */
52 55
     private final MessageLabel messageLabel;
53 56
     /** error panel. */
@@ -63,7 +66,8 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
63 66
      * @param controller Swing controller
64 67
      * @param mainFrame Main frame
65 68
      */
66
-    public SwingStatusBar(final SwingController controller, final MainFrame mainFrame) {
69
+    public SwingStatusBar(final SwingController controller,
70
+            final MainFrame mainFrame) {
67 71
         super();
68 72
 
69 73
         messageLabel = new MessageLabel();
@@ -74,9 +78,9 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
74 78
         setLayout(new MigLayout("fill, ins 0, hidemode 3"));
75 79
 
76 80
         add(messageLabel, "growx, pushx, sgy components, hmax 20, hmin 20");
77
-        add(updateLabel, "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
78
-        add(errorPanel, "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
79
-        add(inviteLabel, "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
81
+        add(updateLabel, COMPONENT_CONSTRAINTS);
82
+        add(errorPanel, COMPONENT_CONSTRAINTS);
83
+        add(inviteLabel, COMPONENT_CONSTRAINTS);
80 84
     }
81 85
 
82 86
     /** {@inheritDoc}
@@ -165,7 +169,7 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
165 169
                     "instance of java.awt.component"));
166 170
             return;
167 171
         }
168
-        if (!Arrays.asList(getComponents()).contains(component)) {
172
+        if (!Arrays.asList(getComponents()).contains((Component) component)) {
169 173
             SwingUtilities.invokeLater(new Runnable() {
170 174
 
171 175
                 /** {@inheritDoc} */
@@ -174,14 +178,10 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
174 178
                     remove(updateLabel);
175 179
                     remove(errorPanel);
176 180
                     remove(inviteLabel);
177
-                    add((Component) component,
178
-                            "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
179
-                    add(updateLabel,
180
-                            "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
181
-                    add(inviteLabel,
182
-                            "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
183
-                    add(errorPanel,
184
-                            "sgy components, hmax 20, hmin 20, wmin 20, shrink 0");
181
+                    add((Component) component, COMPONENT_CONSTRAINTS);
182
+                    add(updateLabel, COMPONENT_CONSTRAINTS);
183
+                    add(inviteLabel, COMPONENT_CONSTRAINTS);
184
+                    add(errorPanel, COMPONENT_CONSTRAINTS);
185 185
                     validate();
186 186
                 }
187 187
             });
@@ -192,9 +192,9 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
192 192
     @Override
193 193
     public void removeComponent(final StatusBarComponent component) {
194 194
         if (!(component instanceof Component)) {
195
-            Logger.appError(ErrorLevel.HIGH, "Error adding status bar component", 
196
-                    new IllegalArgumentException("Component must be an " +
197
-                    "instance of java.awt.component"));
195
+            Logger.appError(ErrorLevel.HIGH, "Error removing status bar "
196
+                    + "component", new IllegalArgumentException("Component "
197
+                    + "must be an instance of java.awt.component"));
198 198
             return;
199 199
         }
200 200
         SwingUtilities.invokeLater(new Runnable() {
@@ -209,8 +209,9 @@ public final class SwingStatusBar extends JPanel implements StatusBar {
209 209
     }
210 210
 
211 211
     /**
212
-     * Returns the message label for this status bar. This is intended to be used
213
-     * for advanced plugins that wish to do compliated things with messages.
212
+     * Returns the message label for this status bar. This is intended to be 
213
+     * used for advanced plugins that wish to do compliated things with
214
+     * messages.
214 215
      *
215 216
      * @return Message label component
216 217
      */

Loading…
Cancel
Save