瀏覽代碼

Add display properties to DisplayableEvents.

This will allow listeners to change how events are displayed,
for example setting the default FG colour to red when the
user is highlighted.

Depends-On: I21f9babe74a90b5cfea6daba815c08e7a4b5afa5
Change-Id: I0ac716ee335ed40531af698fe475d077aba2766d
Reviewed-on: http://gerrit.dmdirc.com/3833
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 9 年之前
父節點
當前提交
4784239044

+ 16
- 2
src/com/dmdirc/events/ChannelDisplayableEvent.java 查看文件

@@ -24,6 +24,8 @@ package com.dmdirc.events;
24 24
 
25 25
 import com.dmdirc.Channel;
26 26
 
27
+import com.google.common.base.Optional;
28
+
27 29
 import java.util.concurrent.atomic.AtomicReference;
28 30
 
29 31
 /**
@@ -33,6 +35,8 @@ public abstract class ChannelDisplayableEvent extends ChannelEvent implements Di
33 35
 
34 36
     /** The display format to use for this event. */
35 37
     private final AtomicReference<String> displayFormatRef = new AtomicReference<>("");
38
+    /** The properties associated with this event. */
39
+    private final DisplayPropertyMap properties = new DisplayPropertyMap();
36 40
 
37 41
     public ChannelDisplayableEvent(final long timestamp, final Channel channel) {
38 42
         super(timestamp, channel);
@@ -48,8 +52,18 @@ public abstract class ChannelDisplayableEvent extends ChannelEvent implements Di
48 52
     }
49 53
 
50 54
     @Override
51
-    public void setDisplayFormat(String format) {
52
-        this.displayFormatRef.set(format);
55
+    public void setDisplayFormat(final String format) {
56
+        displayFormatRef.set(format);
57
+    }
58
+
59
+    @Override
60
+    public <T> void setDisplayProperty(final DisplayProperty<T> property, final T value) {
61
+        properties.put(property, value);
62
+    }
63
+
64
+    @Override
65
+    public <T> Optional<T> getDisplayProperty(final DisplayProperty<T> property) {
66
+        return properties.get(property);
53 67
     }
54 68
 
55 69
 }

+ 37
- 0
src/com/dmdirc/events/DisplayProperty.java 查看文件

@@ -0,0 +1,37 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
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.events;
24
+
25
+import com.dmdirc.ui.Colour;
26
+
27
+/**
28
+ * Describes a property that may be set on a {@link DisplayableEvent} to affect its display.
29
+ */
30
+public final class DisplayProperty<T> {
31
+
32
+    /** The foreground colour of text relating to the event. */
33
+    public static DisplayProperty<Colour> FOREGROUND_COLOUR = new DisplayProperty<>();
34
+    /** The background colour of text relating to the event. */
35
+    public static DisplayProperty<Colour> BACKGROUND_COLOUR = new DisplayProperty<>();
36
+
37
+}

+ 60
- 0
src/com/dmdirc/events/DisplayPropertyMap.java 查看文件

@@ -0,0 +1,60 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
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.events;
24
+
25
+import com.google.common.base.Optional;
26
+
27
+import java.util.Map;
28
+import java.util.concurrent.ConcurrentSkipListMap;
29
+
30
+/**
31
+ * Provides a map of {@link DisplayProperty}s to values, maintaining type safety.
32
+ */
33
+public class DisplayPropertyMap {
34
+
35
+    private final Map<DisplayProperty<?>, Object> properties = new ConcurrentSkipListMap<>();
36
+
37
+    /**
38
+     * Gets the value of the specified property, if present.
39
+     *
40
+     * @param property The property to be retrieved.
41
+     * @param <T> The type of value the property takes.
42
+     * @return An optional containing the value of the property if it was present.
43
+     */
44
+    @SuppressWarnings("unchecked")
45
+    public <T> Optional<T> get(final DisplayProperty<T> property) {
46
+        return Optional.fromNullable((T) properties.get(property));
47
+    }
48
+
49
+    /**
50
+     * Adds a new value for the specified property. Any previous value will be replaced.
51
+     *
52
+     * @param property The property to set.
53
+     * @param value The new value of the property.
54
+     * @param <T> The type of value the property takes.
55
+     */
56
+    public <T> void put(final DisplayProperty<T> property, final T value) {
57
+        properties.put(property, value);
58
+    }
59
+
60
+}

+ 20
- 0
src/com/dmdirc/events/DisplayableEvent.java 查看文件

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.events;
24 24
 
25
+import com.google.common.base.Optional;
26
+
25 27
 /**
26 28
  * Describes an event which is rendered in the client to the user.
27 29
  */
@@ -41,4 +43,22 @@ public interface DisplayableEvent {
41 43
      */
42 44
     void setDisplayFormat(String format);
43 45
 
46
+    /**
47
+     * Sets a property relating to how this event should be displayed.
48
+     *
49
+     * @param property The property to be set
50
+     * @param value The value of the property
51
+     * @param <T> The type of value that the property takes.
52
+     */
53
+    <T> void setDisplayProperty(DisplayProperty<T> property, T value);
54
+
55
+    /**
56
+     * Retrieves a property relating to how this event should be displayed.
57
+     *
58
+     * @param property The property to be retrieved.
59
+     * @param <T> The type of value that the property takes.
60
+     * @return An optional value for the property.
61
+     */
62
+    <T> Optional<T> getDisplayProperty(DisplayProperty<T> property);
63
+
44 64
 }

+ 16
- 2
src/com/dmdirc/events/QueryDisplayableEvent.java 查看文件

@@ -24,6 +24,8 @@ package com.dmdirc.events;
24 24
 
25 25
 import com.dmdirc.Query;
26 26
 
27
+import com.google.common.base.Optional;
28
+
27 29
 import java.util.concurrent.atomic.AtomicReference;
28 30
 
29 31
 /**
@@ -34,6 +36,8 @@ public abstract class QueryDisplayableEvent extends QueryEvent implements
34 36
 
35 37
     /** The display format to use for this event. */
36 38
     private final AtomicReference<String> displayFormatRef = new AtomicReference<>("");
39
+    /** The properties associated with this event. */
40
+    private final DisplayPropertyMap properties = new DisplayPropertyMap();
37 41
 
38 42
     public QueryDisplayableEvent(final long timestamp, final Query query) {
39 43
         super(timestamp, query);
@@ -49,8 +53,18 @@ public abstract class QueryDisplayableEvent extends QueryEvent implements
49 53
     }
50 54
 
51 55
     @Override
52
-    public void setDisplayFormat(String format) {
53
-        this.displayFormatRef.set(format);
56
+    public void setDisplayFormat(final String format) {
57
+        displayFormatRef.set(format);
58
+    }
59
+
60
+    @Override
61
+    public <T> void setDisplayProperty(final DisplayProperty<T> property, final T value) {
62
+        properties.put(property, value);
63
+    }
64
+
65
+    @Override
66
+    public <T> Optional<T> getDisplayProperty(final DisplayProperty<T> property) {
67
+        return properties.get(property);
54 68
     }
55 69
 
56 70
 }

+ 17
- 2
src/com/dmdirc/events/ServerDisplayableEvent.java 查看文件

@@ -24,6 +24,8 @@ package com.dmdirc.events;
24 24
 
25 25
 import com.dmdirc.interfaces.Connection;
26 26
 
27
+import com.google.common.base.Optional;
28
+
27 29
 import java.util.concurrent.atomic.AtomicReference;
28 30
 
29 31
 
@@ -34,6 +36,8 @@ public abstract class ServerDisplayableEvent extends ServerEvent implements Disp
34 36
 
35 37
     /** The display format to use for this event. */
36 38
     private final AtomicReference<String> displayFormatRef = new AtomicReference<>("");
39
+    /** The properties associated with this event. */
40
+    private final DisplayPropertyMap properties = new DisplayPropertyMap();
37 41
 
38 42
     public ServerDisplayableEvent(final long timestamp, final Connection connection) {
39 43
         super(timestamp, connection);
@@ -49,7 +53,18 @@ public abstract class ServerDisplayableEvent extends ServerEvent implements Disp
49 53
     }
50 54
 
51 55
     @Override
52
-    public void setDisplayFormat(String format) {
53
-        this.displayFormatRef.set(format);
56
+    public void setDisplayFormat(final String format) {
57
+        displayFormatRef.set(format);
58
+    }
59
+
60
+    @Override
61
+    public <T> void setDisplayProperty(final DisplayProperty<T> property, final T value) {
62
+        properties.put(property, value);
54 63
     }
64
+
65
+    @Override
66
+    public <T> Optional<T> getDisplayProperty(final DisplayProperty<T> property) {
67
+        return properties.get(property);
68
+    }
69
+
55 70
 }

+ 16
- 2
src/com/dmdirc/events/UnknownCommandEvent.java 查看文件

@@ -24,6 +24,8 @@ package com.dmdirc.events;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26 26
 
27
+import com.google.common.base.Optional;
28
+
27 29
 import java.util.concurrent.atomic.AtomicReference;
28 30
 
29 31
 import javax.annotation.Nullable;
@@ -35,6 +37,8 @@ public class UnknownCommandEvent extends DMDircEvent implements DisplayableEvent
35 37
 
36 38
     /** The display format to use for this event. */
37 39
     private final AtomicReference<String> displayFormatRef = new AtomicReference<>("");
40
+    /** The properties associated with this event. */
41
+    private final DisplayPropertyMap properties = new DisplayPropertyMap();
38 42
     @Nullable private final FrameContainer source;
39 43
     private final String command;
40 44
     private final String[] arguments;
@@ -73,8 +77,18 @@ public class UnknownCommandEvent extends DMDircEvent implements DisplayableEvent
73 77
     }
74 78
 
75 79
     @Override
76
-    public void setDisplayFormat(String format) {
77
-        this.displayFormatRef.set(format);
80
+    public void setDisplayFormat(final String format) {
81
+        displayFormatRef.set(format);
82
+    }
83
+
84
+    @Override
85
+    public <T> void setDisplayProperty(final DisplayProperty<T> property, final T value) {
86
+        properties.put(property, value);
87
+    }
88
+
89
+    @Override
90
+    public <T> Optional<T> getDisplayProperty(final DisplayProperty<T> property) {
91
+        return properties.get(property);
78 92
     }
79 93
 
80 94
 }

Loading…
取消
儲存