Browse Source

Change DisplayLocation to using an interface and a manager.

pull/777/head
Shane Mc Cormack 7 years ago
parent
commit
944531fdf5

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

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

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

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

+ 5
- 2
src/main/java/com/dmdirc/ui/messages/BackBufferImpl.java View File

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

+ 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
+    public 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
+}

+ 2
- 10
src/main/java/com/dmdirc/ui/messages/EventFormatter.java View File

@@ -155,15 +155,7 @@ 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();
158
+    public EventFormatProvider getEventFormatProvider() {
159
+        return formatProvider;
168 160
     }
169 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
     }

+ 8
- 4
src/main/java/com/dmdirc/ui/messages/YamlEventFormatProvider.java View File

@@ -54,11 +54,14 @@ public class YamlEventFormatProvider implements EventFormatProvider {
54 54
 
55 55
     private final Path path;
56 56
     private final ColourManager colourManager;
57
+    private final DisplayLocationManager displayLocationManager;
57 58
     private final Map<String, EventFormat> formats = new HashMap<>();
58 59
 
59
-    public YamlEventFormatProvider(final Path path, final ColourManager colourManager) {
60
+    public YamlEventFormatProvider(final Path path, final ColourManager colourManager,
61
+                                   final DisplayLocationManager displayLocationManager) {
60 62
         this.path = path;
61 63
         this.colourManager = colourManager;
64
+        this.displayLocationManager = displayLocationManager;
62 65
     }
63 66
 
64 67
     public void load() {
@@ -124,11 +127,12 @@ public class YamlEventFormatProvider implements EventFormatProvider {
124 127
         }
125 128
         if (info.containsKey("displaywindow")) {
126 129
             try {
127
-                map.put(DisplayProperty.DISPLAY_LOCATION,
128
-                        DisplayLocation.valueOf(info.get("displaywindow").toString().toUpperCase()));
130
+                map.put(DisplayProperty.DISPLAY_LOCATION, displayLocationManager.getDisplayLocation(
131
+                        info.get("displaywindow").toString()).orElseThrow(() -> new IllegalArgumentException()));
129 132
             } catch (final IllegalArgumentException iae) {
130 133
                 LOG.info(USER_ERROR, "Invalid displaywindow specified for: {}.\nValid values are: {}",
131
-                        info.get("displaywindow").toString(), Arrays.toString(DisplayLocation.values()));
134
+                        info.get("displaywindow").toString(),
135
+                        Arrays.toString(displayLocationManager.getDisplayLocations().toArray()));
132 136
             }
133 137
         }
134 138
         return map;

Loading…
Cancel
Save