소스 검색

Change DisplayLocation to using an interface and a manager.

pull/777/head
Shane Mc Cormack 7 년 전
부모
커밋
944531fdf5

+ 25
- 25
api/src/main/java/com/dmdirc/events/DisplayLocation.java 파일 보기

24
 
24
 
25
 import com.dmdirc.interfaces.WindowModel;
25
 import com.dmdirc.interfaces.WindowModel;
26
 
26
 
27
+import java.util.function.BiPredicate;
28
+
27
 /**
29
 /**
28
  * Valid values for the DISPLAY_LOCATION property and how to test for them.
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
     /** Event came from the same WindowModel. */
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
     /** Event came from a WindowModel that shares the same connection. */
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
             && model.getConnection().isPresent()
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
      * Test to see if this location is valid.
41
      * Test to see if this location is valid.
56
      *
42
      *
58
      * @param event Event we are wanting to display.
44
      * @param event Event we are wanting to display.
59
      * @return True if the event should be displayed here.
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 파일 보기

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

+ 5
- 2
src/main/java/com/dmdirc/ui/messages/BackBufferImpl.java 파일 보기

19
 
19
 
20
 import com.dmdirc.events.DisplayLocation;
20
 import com.dmdirc.events.DisplayLocation;
21
 import com.dmdirc.events.DisplayProperty;
21
 import com.dmdirc.events.DisplayProperty;
22
+import com.dmdirc.events.DisplayPropertyMap;
22
 import com.dmdirc.events.DisplayableEvent;
23
 import com.dmdirc.events.DisplayableEvent;
23
 import com.dmdirc.events.eventbus.EventBus;
24
 import com.dmdirc.events.eventbus.EventBus;
24
 import com.dmdirc.interfaces.WindowModel;
25
 import com.dmdirc.interfaces.WindowModel;
86
      * @return True if the event should be displayed, false otherwise.
87
      * @return True if the event should be displayed, false otherwise.
87
      */
88
      */
88
     private boolean shouldDisplay(final DisplayableEvent event) {
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
                 && !event.hasDisplayProperty(DisplayProperty.DO_NOT_DISPLAY);
94
                 && !event.hasDisplayProperty(DisplayProperty.DO_NOT_DISPLAY);
92
     }
95
     }
93
 
96
 

+ 52
- 0
src/main/java/com/dmdirc/ui/messages/DisplayLocationManager.java 파일 보기

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 파일 보기

155
         return res.toString();
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 파일 보기

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

+ 8
- 4
src/main/java/com/dmdirc/ui/messages/YamlEventFormatProvider.java 파일 보기

54
 
54
 
55
     private final Path path;
55
     private final Path path;
56
     private final ColourManager colourManager;
56
     private final ColourManager colourManager;
57
+    private final DisplayLocationManager displayLocationManager;
57
     private final Map<String, EventFormat> formats = new HashMap<>();
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
         this.path = path;
62
         this.path = path;
61
         this.colourManager = colourManager;
63
         this.colourManager = colourManager;
64
+        this.displayLocationManager = displayLocationManager;
62
     }
65
     }
63
 
66
 
64
     public void load() {
67
     public void load() {
124
         }
127
         }
125
         if (info.containsKey("displaywindow")) {
128
         if (info.containsKey("displaywindow")) {
126
             try {
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
             } catch (final IllegalArgumentException iae) {
132
             } catch (final IllegalArgumentException iae) {
130
                 LOG.info(USER_ERROR, "Invalid displaywindow specified for: {}.\nValid values are: {}",
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
         return map;
138
         return map;

Loading…
취소
저장