Quellcode durchsuchen

Make the UrlBuilder non-static.

Change-Id: I345b6199a4a56ecfd37a4aa23a6e522665fe6120
Reviewed-on: http://gerrit.dmdirc.com/2699
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith vor 10 Jahren
Ursprung
Commit
11b5f9b4ce
2 geänderte Dateien mit 156 neuen und 19 gelöschten Zeilen
  1. 19
    0
      src/com/dmdirc/ui/themes/ThemeManager.java
  2. 137
    19
      src/com/dmdirc/util/URLBuilder.java

+ 19
- 0
src/com/dmdirc/ui/themes/ThemeManager.java Datei anzeigen

@@ -32,6 +32,8 @@ import java.util.HashMap;
32 32
 import java.util.List;
33 33
 import java.util.Map;
34 34
 
35
+import javax.inject.Provider;
36
+
35 37
 /**
36 38
  * Manages available themes.
37 39
  */
@@ -222,4 +224,21 @@ public final class ThemeManager {
222 224
         instance = manager;
223 225
     }
224 226
 
227
+    /**
228
+     * Gets a provider of a theme manager for use in the future.
229
+     *
230
+     * @return A theme manager provider
231
+     * @deprecated Should be injected instead
232
+     */
233
+    @Deprecated
234
+    public static Provider<ThemeManager> getThemeManagerProvider() {
235
+        return new Provider<ThemeManager>() {
236
+            /** {@inheritDoc} */
237
+            @Override
238
+            public ThemeManager get() {
239
+                return instance;
240
+            }
241
+        };
242
+    }
243
+
225 244
 }

+ 137
- 19
src/com/dmdirc/util/URLBuilder.java Datei anzeigen

@@ -25,21 +25,40 @@ package com.dmdirc.util;
25 25
 import com.dmdirc.Main;
26 26
 import com.dmdirc.logger.ErrorLevel;
27 27
 import com.dmdirc.logger.Logger;
28
+import com.dmdirc.plugins.PluginManager;
28 29
 import com.dmdirc.ui.themes.ThemeManager;
29 30
 
30 31
 import java.net.MalformedURLException;
31 32
 import java.net.URL;
32 33
 
34
+import javax.inject.Provider;
35
+
33 36
 /**
34 37
  * Provides methods for building URLs to reference DMDirc resources.
35 38
  */
36 39
 public final class URLBuilder {
37 40
 
41
+    /** Singleton instance. */
42
+    @Deprecated
43
+    private static URLBuilder instance;
44
+
45
+    /** Provider to retrieve a plugin manager instance when needed. */
46
+    private final Provider<PluginManager> pluginManagerProvider;
47
+
48
+    /** Provider to retrieve a theme manager instance when needed. */
49
+    private final Provider<ThemeManager> themeManagerProvider;
50
+
38 51
     /**
39 52
      * Creates a new instance of URLBuilder.
53
+     *
54
+     * @param pluginManagerProvider Provider to retrieve a plugin manager instance when needed.
55
+     * @param themeManagerProvider Provider to retrieve a theme manager instance when needed.
40 56
      */
41
-    private URLBuilder() {
42
-        // Shouldn't be constructed
57
+    public URLBuilder(
58
+            final Provider<PluginManager> pluginManagerProvider,
59
+            final Provider<ThemeManager> themeManagerProvider) {
60
+        this.pluginManagerProvider = pluginManagerProvider;
61
+        this.themeManagerProvider = themeManagerProvider;
43 62
     }
44 63
 
45 64
     /**
@@ -47,8 +66,20 @@ public final class URLBuilder {
47 66
      *
48 67
      * @param path The path that the URL is for
49 68
      * @return An URL corresponding to the specified path, or null on failure
69
+     * @deprecated Use non-static methods
50 70
      */
71
+    @Deprecated
51 72
     public static URL buildFileURL(final String path) {
73
+        return getInstance().getUrlForFile(path);
74
+    }
75
+
76
+    /**
77
+     * Constructs an URL pointing to the specified resource on the file system.
78
+     *
79
+     * @param path The path that the URL is for
80
+     * @return An URL corresponding to the specified path, or null on failure
81
+     */
82
+    public URL getUrlForFile(final String path) {
52 83
         final String prefix = path.startsWith("file://") ? "" : "file://";
53 84
 
54 85
         try {
@@ -65,8 +96,21 @@ public final class URLBuilder {
65 96
      * @param jarFile Path to the jar file (including scheme)
66 97
      * @param path Path to the resource within the jar file
67 98
      * @return An URL corresponding to the specified resource, or null on failure
99
+     * @deprecated Use non-static methods.
68 100
      */
101
+    @Deprecated
69 102
     public static URL buildJarURL(final String jarFile, final String path) {
103
+        return getInstance().getUrlForJarFile(jarFile, path);
104
+    }
105
+
106
+    /**
107
+     * Constructs an URL pointing to the specified resource within a jar file.
108
+     *
109
+     * @param jarFile Path to the jar file (including scheme)
110
+     * @param path Path to the resource within the jar file
111
+     * @return An URL corresponding to the specified resource, or null on failure
112
+     */
113
+    public URL getUrlForJarFile(final String jarFile, final String path) {
70 114
         try {
71 115
             String url = "jar:" + buildURL(jarFile) + "!/" + path;
72 116
             if (url.startsWith("jar:file://")) {
@@ -85,8 +129,21 @@ public final class URLBuilder {
85 129
      *
86 130
      * @param resource The path to the resource
87 131
      * @return An URL corresponding to the specified resource
132
+     * @deprecated Use non-static methods
88 133
      */
134
+    @Deprecated
89 135
     public static URL buildDMDircURL(final String resource) {
136
+        return getInstance().getUrlForDMDircResource(resource);
137
+    }
138
+
139
+    /**
140
+     * Constructs an URL pointing to the specified resource within the DMDirc
141
+     * project.
142
+     *
143
+     * @param resource The path to the resource
144
+     * @return An URL corresponding to the specified resource
145
+     */
146
+    public URL getUrlForDMDircResource(final String resource) {
90 147
         return Thread.currentThread().getContextClassLoader().getResource(resource);
91 148
     }
92 149
 
@@ -96,9 +153,23 @@ public final class URLBuilder {
96 153
      * @param theme The theme which the resource is located in
97 154
      * @param path The path within the theme of the resource
98 155
      * @return An URL corresponding to the specified resource, or null on failure
156
+     * @deprecated Use non-static methods
99 157
      */
158
+    @Deprecated
100 159
     public static URL buildThemeURL(final String theme, final String path) {
101
-        return buildJarURL(ThemeManager.getThemeDirectory() + theme + ".zip", path);
160
+        return getInstance().getUrlForThemeResource(theme, path);
161
+    }
162
+
163
+    /**
164
+     * Builds an URL pointing to a resource within a DMDirc theme.
165
+     *
166
+     * @param theme The theme which the resource is located in
167
+     * @param path The path within the theme of the resource
168
+     * @return An URL corresponding to the specified resource, or null on failure
169
+     */
170
+    public URL getUrlForThemeResource(final String theme, final String path) {
171
+        return getUrlForJarFile(themeManagerProvider.get().getDirectory()
172
+                + theme + ".zip", path);
102 173
     }
103 174
 
104 175
     /**
@@ -107,19 +178,24 @@ public final class URLBuilder {
107 178
      * @param plugin The plugin which the resource is located in
108 179
      * @param path The path within the theme of the resource
109 180
      * @return An URL corresponding to the specified resource, or null on failure
181
+     * @deprecated Use non-static methods
110 182
      */
183
+    @Deprecated
111 184
     public static URL buildPluginURL(final String plugin, final String path) {
112
-        // TODO: Un hack this.
113
-        // Using the ActionManager to getMain() so that we can get the plugin
114
-        // manager is a horribe horrible hack.
115
-        // But making this method require an instance of Main means that
116
-        // BuildURL also need one, which breaks other things. Specifically
117
-        // IconManager, which currently is created new each time it is needed
118
-        // rather than being created once and passed around.
119
-        return buildJarURL(
120
-                Main.mainInstance.getPluginManager()
121
-                .getPluginInfoByName(plugin).getMetaData().getPluginUrl()
122
-                .getPath(), path);
185
+        return getInstance().getUrlForPluginResource(plugin, path);
186
+    }
187
+
188
+    /**
189
+     * Builds an URL pointing to a resource within a DMDirc plugin.
190
+     *
191
+     * @param plugin The plugin which the resource is located in
192
+     * @param path The path within the theme of the resource
193
+     * @return An URL corresponding to the specified resource, or null on failure
194
+     */
195
+    public URL getUrlForPluginResource(final String plugin, final String path) {
196
+        return getUrlForJarFile(
197
+                pluginManagerProvider.get().getPluginInfoByName(plugin)
198
+                .getMetaData().getPluginUrl().getPath(), path);
123 199
     }
124 200
 
125 201
     /**
@@ -136,10 +212,31 @@ public final class URLBuilder {
136 212
      * <li>[file://]/path/on/filesystem</ul>
137 213
      *
138 214
      * @return An URL corresponding to the specified resource, or null on failure
215
+     * @deprecated Use non-static methods
139 216
      */
217
+    @Deprecated
140 218
     public static URL buildURL(final String spec) {
219
+        return getInstance().getUrl(spec);
220
+    }
221
+
222
+    /**
223
+     * Constructs an URL corresponding to the described resource.
224
+     *
225
+     * @param spec The resource location. May take the form of: <ul>
226
+     * <li>dmdirc://com/dmdirc/etc/
227
+     * <li>jar://path/to/jarfile:path/inside/jarfile
228
+     * <li>zip://path/to/zipfile:path/inside/zipfile
229
+     * <li>theme://theme_name:file/inside/theme
230
+     * <li>plugin://plugin_name:file/inside/plugin
231
+     * <li>http://server/path
232
+     * <li>https://server/path
233
+     * <li>[file://]/path/on/filesystem</ul>
234
+     *
235
+     * @return An URL corresponding to the specified resource, or null on failure
236
+     */
237
+    public URL getUrl(final String spec) {
141 238
         if (spec.startsWith("dmdirc://")) {
142
-            return buildDMDircURL(spec.substring(9));
239
+            return getUrlForDMDircResource(spec.substring(9));
143 240
         } else if (spec.startsWith("jar://") || spec.startsWith("zip://")) {
144 241
             final int offset = spec.indexOf(':', 6);
145 242
 
@@ -147,7 +244,7 @@ public final class URLBuilder {
147 244
                 Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
148 245
                 return null;
149 246
             } else {
150
-                return buildJarURL(spec.substring(6, offset), spec.substring(offset + 1));
247
+                return getUrlForJarFile(spec.substring(6, offset), spec.substring(offset + 1));
151 248
             }
152 249
         } else if (spec.startsWith("plugin://")) {
153 250
             final int offset = spec.indexOf(':', 8);
@@ -156,7 +253,7 @@ public final class URLBuilder {
156 253
                 Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
157 254
                 return null;
158 255
             } else {
159
-                return buildPluginURL(spec.substring(9, offset), spec.substring(offset + 1));
256
+                return getUrlForPluginResource(spec.substring(9, offset), spec.substring(offset + 1));
160 257
             }
161 258
         } else if (spec.startsWith("theme://")) {
162 259
             final int offset = spec.indexOf(':', 8);
@@ -165,7 +262,7 @@ public final class URLBuilder {
165 262
                 Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
166 263
                 return null;
167 264
             } else {
168
-                return buildThemeURL(spec.substring(8, offset), spec.substring(offset + 1));
265
+                return getUrlForThemeResource(spec.substring(8, offset), spec.substring(offset + 1));
169 266
             }
170 267
         } else if (spec.startsWith("http://") || spec.startsWith("https://")) {
171 268
             try {
@@ -175,7 +272,28 @@ public final class URLBuilder {
175 272
                 return null;
176 273
             }
177 274
         } else {
178
-            return buildFileURL(spec);
275
+            return getUrlForFile(spec);
179 276
         }
180 277
     }
278
+
279
+    /**
280
+     * Gets a singleton static instance for use by deprecated static methods.
281
+     *
282
+     * @return A singleton instance of this class.
283
+     * @deprecated Can be deleted when static methods are gone.
284
+     */
285
+    @Deprecated
286
+    private static synchronized URLBuilder getInstance() {
287
+        if (instance == null) {
288
+            instance = new URLBuilder(new Provider<PluginManager>() {
289
+
290
+                @Override
291
+                public PluginManager get() {
292
+                    return Main.mainInstance.getPluginManager();
293
+                }
294
+            }, ThemeManager.getThemeManagerProvider());
295
+        }
296
+
297
+        return instance;
298
+    }
181 299
 }

Laden…
Abbrechen
Speichern