Переглянути джерело

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 роки тому
джерело
коміт
cc923143f0

+ 37
- 48
src/com/dmdirc/addons/ui_swing/components/statusbar/MessageLabel.java Переглянути файл

24
 
24
 
25
 import com.dmdirc.addons.ui_swing.UIUtilities;
25
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
 import com.dmdirc.config.IdentityManager;
26
 import com.dmdirc.config.IdentityManager;
27
-import com.dmdirc.interfaces.ConfigChangeListener;
28
 import com.dmdirc.ui.IconManager;
27
 import com.dmdirc.ui.IconManager;
29
 import com.dmdirc.ui.StatusMessage;
28
 import com.dmdirc.ui.StatusMessage;
30
 import com.dmdirc.ui.interfaces.StatusBarComponent;
29
 import com.dmdirc.ui.interfaces.StatusBarComponent;
32
 
31
 
33
 import java.awt.event.MouseEvent;
32
 import java.awt.event.MouseEvent;
34
 import java.awt.event.MouseListener;
33
 import java.awt.event.MouseListener;
34
+import java.util.ArrayList;
35
 import java.util.Date;
35
 import java.util.Date;
36
+import java.util.List;
36
 import java.util.Timer;
37
 import java.util.Timer;
37
 import java.util.TimerTask;
38
 import java.util.TimerTask;
38
 import java.util.concurrent.Semaphore;
39
 import java.util.concurrent.Semaphore;
44
 /**
45
 /**
45
  * Message label handles showing messages in the status bar.
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
      * A version number for this class. It should be changed whenever the class
52
      * A version number for this class. It should be changed whenever the class
55
     private static final long serialVersionUID = 1;
56
     private static final long serialVersionUID = 1;
56
     /** Default status bar message. */
57
     /** Default status bar message. */
57
     private final StatusMessage defaultMessage;
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
     /** Timer to clear the message. */
63
     /** Timer to clear the message. */
61
     private transient TimerTask messageTimer;
64
     private transient TimerTask messageTimer;
62
-    /** Message timeout. */
63
-    private int timeout;
64
     /** Set message synchronisation. */
65
     /** Set message synchronisation. */
65
     private final Semaphore semaphore;
66
     private final Semaphore semaphore;
66
 
67
 
69
      */
70
      */
70
     public MessageLabel() {
71
     public MessageLabel() {
71
         super();
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
         semaphore = new Semaphore(1);
77
         semaphore = new Semaphore(1);
74
         setText("Ready.");
78
         setText("Ready.");
75
         setBorder(BorderFactory.createEtchedBorder());
79
         setBorder(BorderFactory.createEtchedBorder());
76
         addMouseListener(this);
80
         addMouseListener(this);
77
-        setCachedSettings();
78
-        IdentityManager.getGlobalConfig().addChangeListener("ui",
79
-                "awayindicator", this);
80
     }
81
     }
81
 
82
 
82
     /**
83
     /**
88
      */
89
      */
89
     @Deprecated
90
     @Deprecated
90
     public void setMessage(final String newMessage) {
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
     @Deprecated
104
     @Deprecated
103
     public void setMessage(final String newMessage,
105
     public void setMessage(final String newMessage,
104
             final StatusMessageNotifier newNotifier) {
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
      */
118
      */
116
     @Deprecated
119
     @Deprecated
117
     public void setMessage(final String iconType, final String newMessage) {
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
     @Deprecated
134
     @Deprecated
131
     public void setMessage(final String iconType, final String newMessage,
135
     public void setMessage(final String iconType, final String newMessage,
132
             final StatusMessageNotifier newNotifier) {
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
     @Deprecated
150
     @Deprecated
147
     public void setMessage(final String newMessage,
151
     public void setMessage(final String newMessage,
148
             final StatusMessageNotifier newNotifier, final int timeout) {
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
      * @deprecated Should use {@link setMessage(StatusMessage)} instead
165
      * @deprecated Should use {@link setMessage(StatusMessage)} instead
161
      */
166
      */
162
     @Deprecated
167
     @Deprecated
163
-    public void setMessage(final String iconType, final String newMessage, 
168
+    public void setMessage(final String iconType, final String newMessage,
164
             final StatusMessageNotifier newNotifier, final int timeout) {
169
             final StatusMessageNotifier newNotifier, final int timeout) {
165
         setMessage(new StatusMessage(iconType, newMessage, newNotifier,
170
         setMessage(new StatusMessage(iconType, newMessage, newNotifier,
166
-                timeout));
171
+                timeout, IdentityManager.getGlobalConfig()));
167
     }
172
     }
168
 
173
 
169
     /**
174
     /**
173
      */
178
      */
174
     public void setMessage(final StatusMessage message) {
179
     public void setMessage(final StatusMessage message) {
175
         semaphore.acquireUninterruptibly();
180
         semaphore.acquireUninterruptibly();
176
-        this.messageNotifier = message.getMessageNotifier();
177
         SwingUtilities.invokeLater(new Runnable() {
181
         SwingUtilities.invokeLater(new Runnable() {
178
 
182
 
179
             /** {@inheritDoc} */
183
             /** {@inheritDoc} */
180
             @Override
184
             @Override
181
             public void run() {
185
             public void run() {
182
                 try {
186
                 try {
183
-                   if (message.getIconType() == null) {
187
+                    messages.add(message);
188
+                    currentMessage = messages.indexOf(message);
189
+                    if (message.getIconType() == null) {
184
                         setIcon(null);
190
                         setIcon(null);
185
                     } else {
191
                     } else {
186
                         setIcon(IconManager.getIconManager().getIcon(
192
                         setIcon(IconManager.getIconManager().getIcon(
188
                     }
194
                     }
189
                     setText(UIUtilities.clipStringifNeeded(MessageLabel.this,
195
                     setText(UIUtilities.clipStringifNeeded(MessageLabel.this,
190
                             message.getMessage(), getWidth()));
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
                         messageTimer.cancel();
200
                         messageTimer.cancel();
196
                     }
201
                     }
197
 
202
 
198
                     if (!defaultMessage.equals(message)) {
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
                         new Timer("SwingStatusBar messageTimer").schedule(
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
                 } finally {
210
                 } finally {
212
                     semaphore.release();
211
                     semaphore.release();
229
      */
228
      */
230
     @Override
229
     @Override
231
     public void mouseClicked(final MouseEvent e) {
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
     public void mouseExited(final MouseEvent e) {
274
     public void mouseExited(final MouseEvent e) {
274
         //Ignore
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 Переглянути файл

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 Переглянути файл

39
 
39
 
40
 import net.miginfocom.swing.MigLayout;
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
 public final class SwingStatusBar extends JPanel implements StatusBar {
43
 public final class SwingStatusBar extends JPanel implements StatusBar {
44
 
44
 
45
     /**
45
     /**
48
      * objects being unserialized with the new class).
48
      * objects being unserialized with the new class).
49
      */
49
      */
50
     private static final long serialVersionUID = 5;
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
     /** message label. */
54
     /** message label. */
52
     private final MessageLabel messageLabel;
55
     private final MessageLabel messageLabel;
53
     /** error panel. */
56
     /** error panel. */
63
      * @param controller Swing controller
66
      * @param controller Swing controller
64
      * @param mainFrame Main frame
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
         super();
71
         super();
68
 
72
 
69
         messageLabel = new MessageLabel();
73
         messageLabel = new MessageLabel();
74
         setLayout(new MigLayout("fill, ins 0, hidemode 3"));
78
         setLayout(new MigLayout("fill, ins 0, hidemode 3"));
75
 
79
 
76
         add(messageLabel, "growx, pushx, sgy components, hmax 20, hmin 20");
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
     /** {@inheritDoc}
86
     /** {@inheritDoc}
165
                     "instance of java.awt.component"));
169
                     "instance of java.awt.component"));
166
             return;
170
             return;
167
         }
171
         }
168
-        if (!Arrays.asList(getComponents()).contains(component)) {
172
+        if (!Arrays.asList(getComponents()).contains((Component) component)) {
169
             SwingUtilities.invokeLater(new Runnable() {
173
             SwingUtilities.invokeLater(new Runnable() {
170
 
174
 
171
                 /** {@inheritDoc} */
175
                 /** {@inheritDoc} */
174
                     remove(updateLabel);
178
                     remove(updateLabel);
175
                     remove(errorPanel);
179
                     remove(errorPanel);
176
                     remove(inviteLabel);
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
                     validate();
185
                     validate();
186
                 }
186
                 }
187
             });
187
             });
192
     @Override
192
     @Override
193
     public void removeComponent(final StatusBarComponent component) {
193
     public void removeComponent(final StatusBarComponent component) {
194
         if (!(component instanceof Component)) {
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
             return;
198
             return;
199
         }
199
         }
200
         SwingUtilities.invokeLater(new Runnable() {
200
         SwingUtilities.invokeLater(new Runnable() {
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
      * @return Message label component
216
      * @return Message label component
216
      */
217
      */

Завантаження…
Відмінити
Зберегти