Browse Source

Allow specifying a window for DisplayableEvents in format.yml (Issue #459)

I'm sure this will be changed/redone, but this is at least a start by allowing
certain events to go to all windows (eg ServerNotices)

Ideally it would be possible for some events to only be displayed in the window
that generated them (eg if I do /notice foo bar the ">-{{target}}-> {{message}}"
message only appears in the window I ran the command in, not either just the
server window or all the windows, but this would need changes to a bunch of
events to make them include a source window.
pull/777/head
Shane Mc Cormack 7 years ago
parent
commit
a1d0ef7497

+ 68
- 0
api/src/main/java/com/dmdirc/events/DisplayLocation.java View File

@@ -0,0 +1,68 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.interfaces.WindowModel;
26
+
27
+/**
28
+ * Valid values for the DISPLAY_LOCATION property and how to test for them.
29
+ */
30
+public enum DisplayLocation {
31
+    /** Event came from the same WindowModel. */
32
+    SOURCE((model, event) -> event.getSource().equals(model)),
33
+
34
+    /** Event came from a WindowModel that shares the same connection. */
35
+    SAMECONNECTION((model, event) -> event.getSource().getConnection().isPresent()
36
+            && model.getConnection().isPresent()
37
+            && event.getSource().getConnection().get().equals(model.getConnection().get())),
38
+
39
+    /** Alias for SAMECONNECTION. */
40
+    SAMESERVER((model, event) -> SAMECONNECTION.shouldDisplay(model, event));
41
+
42
+    /** Function to test this DisplayLocation. */
43
+    private DisplayLocationTester locationTester;
44
+
45
+    /**
46
+     * Create a new DisplayLocation
47
+     *
48
+     * @param test Test function to run to see if this location is valid.
49
+     */
50
+    DisplayLocation(final DisplayLocationTester test) {
51
+        locationTester = test;
52
+    }
53
+
54
+    /**
55
+     * Test to see if this location is valid.
56
+     *
57
+     * @param model WindowModel we are wanting to display the event in.
58
+     * @param event Event we are wanting to display.
59
+     * @return True if the event should be displayed here.
60
+     */
61
+    public boolean shouldDisplay(final WindowModel model, final DisplayableEvent event) {
62
+        return locationTester.test(model, event);
63
+    }
64
+
65
+    interface DisplayLocationTester {
66
+        public boolean test(final WindowModel model, final DisplayableEvent event);
67
+    }
68
+};

+ 2
- 0
api/src/main/java/com/dmdirc/events/DisplayProperty.java View File

@@ -39,6 +39,8 @@ public interface DisplayProperty<T> {
39 39
     DisplayProperty<Boolean> DO_NOT_DISPLAY = new DisplayPropertyImpl<>();
40 40
     /** Whether to suppress timestamps for the event. */
41 41
     DisplayProperty<Boolean> NO_TIMESTAMPS = new DisplayPropertyImpl<>();
42
+    /** Where the event should be displayed. */
43
+    DisplayProperty<DisplayLocation> DISPLAY_LOCATION = new DisplayPropertyImpl<>();
42 44
     /** A user that the displayable is linked to. */
43 45
     DisplayProperty<User> LINK_USER = new DisplayPropertyImpl<>();
44 46
 

+ 4
- 1
src/main/java/com/dmdirc/ui/messages/BackBufferImpl.java View File

@@ -17,7 +17,9 @@
17 17
 
18 18
 package com.dmdirc.ui.messages;
19 19
 
20
+import com.dmdirc.events.DisplayLocation;
20 21
 import com.dmdirc.events.DisplayProperty;
22
+import com.dmdirc.events.DisplayPropertyMap;
21 23
 import com.dmdirc.events.DisplayableEvent;
22 24
 import com.dmdirc.events.eventbus.EventBus;
23 25
 import com.dmdirc.interfaces.WindowModel;
@@ -85,7 +87,8 @@ public class BackBufferImpl implements BackBuffer {
85 87
      * @return True if the event should be displayed, false otherwise.
86 88
      */
87 89
     private boolean shouldDisplay(final DisplayableEvent event) {
88
-        return event.getSource().equals(owner)
90
+        return formatter.getFormatDisplayableProperties(event).get(DisplayProperty.DISPLAY_LOCATION)
91
+                    .orElse(DisplayLocation.SOURCE).shouldDisplay(owner, event)
89 92
                 && !event.hasDisplayProperty(DisplayProperty.DO_NOT_DISPLAY);
90 93
     }
91 94
 

+ 11
- 0
src/main/java/com/dmdirc/ui/messages/EventFormatter.java View File

@@ -155,4 +155,15 @@ public class EventFormatter {
155 155
         return res.toString();
156 156
     }
157 157
 
158
+    /**
159
+     * Get the displayable properties map for this event according to the formatter.
160
+     *
161
+     * @param event Event to get properties for.
162
+     * @return DisplayPropertyMap from formatter.
163
+     */
164
+    public DisplayPropertyMap getFormatDisplayableProperties(final DisplayableEvent event) {
165
+        return formatProvider.getFormat(event.getClass()).isPresent()
166
+                ? formatProvider.getFormat(event.getClass()).get().getDisplayProperties()
167
+                : event.getDisplayProperties();
168
+    }
158 169
 }

+ 11
- 0
src/main/java/com/dmdirc/ui/messages/YamlEventFormatProvider.java View File

@@ -17,6 +17,7 @@
17 17
 
18 18
 package com.dmdirc.ui.messages;
19 19
 
20
+import com.dmdirc.events.DisplayLocation;
20 21
 import com.dmdirc.events.DisplayProperty;
21 22
 import com.dmdirc.events.DisplayPropertyMap;
22 23
 import com.dmdirc.events.DisplayableEvent;
@@ -27,6 +28,7 @@ import java.io.InputStream;
27 28
 import java.io.InputStreamReader;
28 29
 import java.nio.file.Files;
29 30
 import java.nio.file.Path;
31
+import java.util.Arrays;
30 32
 import java.util.HashMap;
31 33
 import java.util.Map;
32 34
 import java.util.Optional;
@@ -120,6 +122,15 @@ public class YamlEventFormatProvider implements EventFormatProvider {
120 122
             map.put(DisplayProperty.NO_TIMESTAMPS,
121 123
                     !info.get("timestamp").toString().toLowerCase().matches("y|yes|true|1|on"));
122 124
         }
125
+        if (info.containsKey("displaywindow")) {
126
+            try {
127
+                map.put(DisplayProperty.DISPLAY_LOCATION,
128
+                        DisplayLocation.valueOf(info.get("displaywindow").toString().toUpperCase()));
129
+            } catch (final IllegalArgumentException iae) {
130
+                LOG.info(USER_ERROR, "Invalid displaywindow specified for: {}.\nValid values are: {}",
131
+                        info.get("displaywindow").toString(), Arrays.toString(DisplayLocation.values()));
132
+            }
133
+        }
123 134
         return map;
124 135
     }
125 136
 

Loading…
Cancel
Save