Browse Source

Merge pull request #777 from ShaneMcC/displaywindowformat

Allow specifying a window for DisplayableEvents in format.yml (Issue #459)
pull/783/head
Shane Mc Cormack 7 years ago
parent
commit
f4cde27f58

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

@@ -0,0 +1,47 @@
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
+@FunctionalInterface
31
+public interface DisplayLocation {
32
+    /** Event came from the same WindowModel. */
33
+    DisplayLocation SOURCE = (model, event) -> event.getSource().equals(model);
34
+
35
+    /** Event came from a WindowModel that shares the same connection. */
36
+    DisplayLocation SAME_CONNECTION = (model, event) -> event.getSource().getConnection().isPresent()
37
+            && model.getConnection().isPresent()
38
+            && event.getSource().getConnection().get().equals(model.getConnection().get());
39
+    /**
40
+     * Test to see if this location is valid.
41
+     *
42
+     * @param model WindowModel we are wanting to display the event in.
43
+     * @param event Event we are wanting to display.
44
+     * @return True if the event should be displayed here.
45
+     */
46
+    boolean shouldDisplay(final WindowModel model, final DisplayableEvent event);
47
+};

+ 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
 

+ 7
- 3
src/main/java/com/dmdirc/plugins/PluginEventFormatManager.java View File

@@ -21,11 +21,12 @@ import com.dmdirc.config.GlobalConfig;
21 21
 import com.dmdirc.events.PluginLoadedEvent;
22 22
 import com.dmdirc.events.PluginUnloadedEvent;
23 23
 import com.dmdirc.events.eventbus.EventBus;
24
-import com.dmdirc.util.system.SystemLifecycleComponent;
25 24
 import com.dmdirc.ui.messages.ColourManager;
26 25
 import com.dmdirc.ui.messages.EventFormatProvider;
27 26
 import com.dmdirc.ui.messages.MultiEventFormatProvider;
28 27
 import com.dmdirc.ui.messages.YamlEventFormatProvider;
28
+import com.dmdirc.ui.messages.DisplayLocationManager;
29
+import com.dmdirc.util.system.SystemLifecycleComponent;
29 30
 import net.engio.mbassy.listener.Handler;
30 31
 
31 32
 import javax.inject.Inject;
@@ -48,15 +49,18 @@ public class PluginEventFormatManager implements SystemLifecycleComponent {
48 49
     private final MultiEventFormatProvider multiEventFormatProvider;
49 50
     private final Map<PluginInfo, EventFormatProvider> providers = new HashMap<>();
50 51
     private final ColourManager colourManager;
52
+    private final DisplayLocationManager displayLocationManager;
51 53
 
52 54
     @Inject
53 55
     public PluginEventFormatManager(
54 56
             final EventBus eventbus,
55 57
             final MultiEventFormatProvider multiEventFormatProvider,
56
-            @GlobalConfig final ColourManager colourManager) {
58
+            @GlobalConfig final ColourManager colourManager,
59
+            final DisplayLocationManager displayLocationManager) {
57 60
         this.eventbus = eventbus;
58 61
         this.multiEventFormatProvider = multiEventFormatProvider;
59 62
         this.colourManager = colourManager;
63
+        this.displayLocationManager = displayLocationManager;
60 64
     }
61 65
 
62 66
     @Override
@@ -74,7 +78,7 @@ public class PluginEventFormatManager implements SystemLifecycleComponent {
74 78
         final Path path = event.getPlugin().getPath("/META-INF/format.yml");
75 79
         if (Files.exists(path)) {
76 80
             final YamlEventFormatProvider provider =
77
-                    new YamlEventFormatProvider(path, colourManager);
81
+                    new YamlEventFormatProvider(path, colourManager, displayLocationManager);
78 82
             provider.load();
79 83
             providers.put(event.getPlugin(), provider);
80 84
             multiEventFormatProvider.addProvider(provider);

+ 5
- 1
src/main/java/com/dmdirc/ui/messages/BackBufferImpl.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.DisplayableEvent;
22 23
 import com.dmdirc.events.eventbus.EventBus;
@@ -85,7 +86,10 @@ public class BackBufferImpl implements BackBuffer {
85 86
      * @return True if the event should be displayed, false otherwise.
86 87
      */
87 88
     private boolean shouldDisplay(final DisplayableEvent event) {
88
-        return event.getSource().equals(owner)
89
+        return (formatter.getEventFormatProvider().getFormat(event.getClass()).isPresent() ?
90
+                formatter.getEventFormatProvider().getFormat(event.getClass()).get().getDisplayProperties()
91
+                    .get(DisplayProperty.DISPLAY_LOCATION).orElse(DisplayLocation.SOURCE).shouldDisplay(owner, event)
92
+                        : DisplayLocation.SOURCE.shouldDisplay(owner, event))
89 93
                 && !event.hasDisplayProperty(DisplayProperty.DO_NOT_DISPLAY);
90 94
     }
91 95
 

+ 52
- 0
src/main/java/com/dmdirc/ui/messages/DisplayLocationManager.java View File

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2017 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7
+ * permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ * Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14
+ * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ */
17
+
18
+package com.dmdirc.ui.messages;
19
+
20
+import com.dmdirc.events.DisplayLocation;
21
+
22
+import javax.inject.Inject;
23
+import javax.inject.Singleton;
24
+import java.util.HashMap;
25
+import java.util.Map;
26
+import java.util.Optional;
27
+import java.util.Set;
28
+
29
+@Singleton
30
+public class DisplayLocationManager {
31
+    private final Map<String, DisplayLocation> displayLocations = new HashMap<>();
32
+
33
+    @Inject
34
+    DisplayLocationManager() {
35
+        // Add Defaults
36
+        addDisplayLocation("source", DisplayLocation.SOURCE);
37
+        addDisplayLocation("sameconnection", DisplayLocation.SAME_CONNECTION);
38
+        addDisplayLocation("sameserver", DisplayLocation.SAME_CONNECTION);
39
+    }
40
+
41
+    public void addDisplayLocation(final String name, final DisplayLocation displayLocation) {
42
+        displayLocations.put(name.toLowerCase().replaceAll("[^a-z]", ""), displayLocation);
43
+    }
44
+
45
+    public Optional<DisplayLocation> getDisplayLocation(final String name) {
46
+        return Optional.ofNullable(displayLocations.get(name.toLowerCase().replaceAll("[^a-z]", "")));
47
+    }
48
+
49
+    public Set<String> getDisplayLocations() {
50
+        return displayLocations.keySet();
51
+    }
52
+}

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

@@ -155,4 +155,7 @@ public class EventFormatter {
155 155
         return res.toString();
156 156
     }
157 157
 
158
+    public EventFormatProvider getEventFormatProvider() {
159
+        return formatProvider;
160
+    }
158 161
 }

+ 3
- 2
src/main/java/com/dmdirc/ui/messages/UiMessagesModule.java View File

@@ -38,9 +38,10 @@ public class UiMessagesModule {
38 38
     @Singleton
39 39
     public MultiEventFormatProvider getTemplateProvider(
40 40
             @Directory(BASE) final Path directory,
41
-            @GlobalConfig final ColourManager colourManager) {
41
+            @GlobalConfig final ColourManager colourManager,
42
+            final DisplayLocationManager displayLocationManager) {
42 43
         final YamlEventFormatProvider yamlProvider =
43
-                new YamlEventFormatProvider(directory.resolve("format.yml"), colourManager);
44
+                new YamlEventFormatProvider(directory.resolve("format.yml"), colourManager, displayLocationManager);
44 45
         yamlProvider.load();
45 46
         return new MultiEventFormatProvider(yamlProvider);
46 47
     }

+ 14
- 1
src/main/java/com/dmdirc/ui/messages/YamlEventFormatProvider.java View File

@@ -52,11 +52,14 @@ public class YamlEventFormatProvider implements EventFormatProvider {
52 52
 
53 53
     private final Path path;
54 54
     private final ColourManager colourManager;
55
+    private final DisplayLocationManager displayLocationManager;
55 56
     private final Map<String, EventFormat> formats = new HashMap<>();
56 57
 
57
-    public YamlEventFormatProvider(final Path path, final ColourManager colourManager) {
58
+    public YamlEventFormatProvider(final Path path, final ColourManager colourManager,
59
+                                   final DisplayLocationManager displayLocationManager) {
58 60
         this.path = path;
59 61
         this.colourManager = colourManager;
62
+        this.displayLocationManager = displayLocationManager;
60 63
     }
61 64
 
62 65
     public void load() {
@@ -120,6 +123,16 @@ public class YamlEventFormatProvider implements EventFormatProvider {
120 123
             map.put(DisplayProperty.NO_TIMESTAMPS,
121 124
                     !info.get("timestamp").toString().toLowerCase().matches("y|yes|true|1|on"));
122 125
         }
126
+        if (info.containsKey("displaywindow")) {
127
+            try {
128
+                map.put(DisplayProperty.DISPLAY_LOCATION, displayLocationManager.getDisplayLocation(
129
+                        info.get("displaywindow").toString()).orElseThrow(() -> new IllegalArgumentException()));
130
+            } catch (final IllegalArgumentException iae) {
131
+                LOG.info(USER_ERROR, "Invalid displaywindow specified for: {}.\nValid values are: {}",
132
+                        info.get("displaywindow").toString(),
133
+                        displayLocationManager.getDisplayLocations().toString());
134
+            }
135
+        }
123 136
         return map;
124 137
     }
125 138
 

Loading…
Cancel
Save