Browse Source

Add YAML format store. Hook everything up.

pull/132/head
Chris Smith 9 years ago
parent
commit
b31e26c2a6

+ 2
- 0
src/com/dmdirc/ClientModule.java View File

@@ -45,6 +45,7 @@ import com.dmdirc.plugins.PluginModule;
45 45
 import com.dmdirc.ui.IconManager;
46 46
 import com.dmdirc.ui.messages.ColourManager;
47 47
 import com.dmdirc.ui.messages.ColourManagerFactory;
48
+import com.dmdirc.ui.messages.UiMessagesModule;
48 49
 import com.dmdirc.ui.themes.ThemeManager;
49 50
 import com.dmdirc.updater.UpdaterModule;
50 51
 import com.dmdirc.updater.manager.UpdateManager;
@@ -73,6 +74,7 @@ import dagger.Provides;
73 74
                 ConfigModule.class,
74 75
                 MessagesModule.class,
75 76
                 PluginModule.class,
77
+                UiMessagesModule.class,
76 78
                 UpdaterModule.class
77 79
         },
78 80
         library = true)

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

@@ -26,6 +26,9 @@ import com.dmdirc.events.DisplayableEvent;
26 26
 
27 27
 import java.util.Optional;
28 28
 
29
+import javax.inject.Inject;
30
+import javax.inject.Singleton;
31
+
29 32
 /**
30 33
  * Formats an event into a text string, based on a user-defined template.
31 34
  *
@@ -38,6 +41,7 @@ import java.util.Optional;
38 41
  *
39 42
  * <p>Properties and functions are case-insensitive.
40 43
  */
44
+@Singleton
41 45
 public class EventFormatter {
42 46
 
43 47
     private static final String ERROR_STRING = "<FormatError>";
@@ -45,6 +49,7 @@ public class EventFormatter {
45 49
     private final EventPropertyManager propertyManager;
46 50
     private final EventTemplateProvider templateProvider;
47 51
 
52
+    @Inject
48 53
     public EventFormatter(final EventPropertyManager propertyManager,
49 54
             final EventTemplateProvider templateProvider) {
50 55
         this.propertyManager = propertyManager;

+ 2
- 0
src/com/dmdirc/ui/messages/EventPropertyManager.java View File

@@ -33,7 +33,9 @@ import java.util.Optional;
33 33
 import java.util.function.Function;
34 34
 
35 35
 import javax.inject.Inject;
36
+import javax.inject.Singleton;
36 37
 
38
+@Singleton
37 39
 public class EventPropertyManager {
38 40
 
39 41
     private final DMDircMBassador eventBus;

+ 53
- 0
src/com/dmdirc/ui/messages/UiMessagesModule.java View File

@@ -0,0 +1,53 @@
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.ui.messages;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
27
+
28
+import java.nio.file.Path;
29
+
30
+import javax.inject.Singleton;
31
+
32
+import dagger.Module;
33
+import dagger.Provides;
34
+
35
+import static com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType.BASE;
36
+
37
+/**
38
+ * Dagger module for message related objects.
39
+ */
40
+@Module(library = true, complete = false)
41
+public class UiMessagesModule {
42
+
43
+    @Provides
44
+    @Singleton
45
+    public EventTemplateProvider getTemplateProvider(@Directory(BASE) final Path directory,
46
+            final DMDircMBassador eventBus) {
47
+        final YamlEventTemplateProvider provider =
48
+                new YamlEventTemplateProvider(directory.resolve("format.yml"), eventBus);
49
+        provider.load();
50
+        return provider;
51
+    }
52
+
53
+}

+ 98
- 0
src/com/dmdirc/ui/messages/YamlEventTemplateProvider.java View File

@@ -0,0 +1,98 @@
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.ui.messages;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.AppErrorEvent;
27
+import com.dmdirc.events.DisplayableEvent;
28
+import com.dmdirc.logger.ErrorLevel;
29
+
30
+import java.io.IOException;
31
+import java.io.InputStream;
32
+import java.io.InputStreamReader;
33
+import java.nio.file.Files;
34
+import java.nio.file.Path;
35
+import java.util.HashMap;
36
+import java.util.Map;
37
+import java.util.Optional;
38
+
39
+import com.esotericsoftware.yamlbeans.YamlReader;
40
+
41
+import static com.dmdirc.util.YamlReaderUtils.asMap;
42
+
43
+/**
44
+ * YAML-backed supplier of event templates.
45
+ */
46
+public class YamlEventTemplateProvider implements EventTemplateProvider {
47
+
48
+    /** The charset to use when reading and writing files. */
49
+    private static final String CHARSET = "UTF-8";
50
+
51
+    private final Path path;
52
+    private final DMDircMBassador eventBus;
53
+    private final Map<String, String> formats = new HashMap<>();
54
+
55
+    public YamlEventTemplateProvider(final Path path, final DMDircMBassador eventBus) {
56
+        this.path = path;
57
+        this.eventBus = eventBus;
58
+    }
59
+
60
+    public void load() {
61
+        formats.clear();
62
+
63
+        try (final InputStream stream = getClass().getResourceAsStream("format.yml")) {
64
+            load(stream);
65
+        } catch (IOException e) {
66
+            eventBus.publishAsync(new AppErrorEvent(ErrorLevel.LOW, e,
67
+                    "Unable to load default event templates", ""));
68
+        }
69
+
70
+        if (Files.exists(path)) {
71
+            try (final InputStream stream = Files.newInputStream(path)) {
72
+                load(stream);
73
+            } catch (IOException e) {
74
+                eventBus.publishAsync(new AppErrorEvent(ErrorLevel.LOW, e,
75
+                        "Unable to load event templates", ""));
76
+            }
77
+        }
78
+    }
79
+
80
+    private void load(final InputStream stream) {
81
+        try (final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
82
+            final YamlReader yamlReader = new YamlReader(reader);
83
+            final Object root = yamlReader.read();
84
+            final Map<Object, Object> entries = asMap(root);
85
+            entries.forEach((k, v) -> formats.put(k.toString(), v.toString()));
86
+            yamlReader.close();
87
+        } catch (IOException e) {
88
+            eventBus.publishAsync(new AppErrorEvent(ErrorLevel.LOW, e,
89
+                    "Unable to load event templates", ""));
90
+        }
91
+    }
92
+
93
+    @Override
94
+    public Optional<String> getTemplate(final Class<? extends DisplayableEvent> eventType) {
95
+        return Optional.ofNullable(formats.get(eventType.getSimpleName()));
96
+    }
97
+
98
+}

Loading…
Cancel
Save