Browse Source

URLHandler is no longer a singleton

CORE-11

Change-Id: I268aa593dd81f38c94fc11e625acb4ff4aed61d1
Depends-on: Icc74ece86b012aa92ef999eb68df727e372e203e
Reviewed-on: http://gerrit.dmdirc.com/1522
Automatic-Compile: Greg Holmes <greg@dmdirc.com>
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.6.5b1
Chris Smith 13 years ago
parent
commit
5eb04e53fc

+ 26
- 21
src/com/dmdirc/ui/core/util/URLHandler.java View File

29
 import com.dmdirc.logger.ErrorLevel;
29
 import com.dmdirc.logger.ErrorLevel;
30
 import com.dmdirc.logger.Logger;
30
 import com.dmdirc.logger.Logger;
31
 import com.dmdirc.ui.core.components.StatusBarManager;
31
 import com.dmdirc.ui.core.components.StatusBarManager;
32
+import com.dmdirc.ui.interfaces.UIController;
32
 
33
 
33
 import java.awt.Desktop;
34
 import java.awt.Desktop;
34
 import java.io.IOException;
35
 import java.io.IOException;
42
 /** Handles URLs. */
43
 /** Handles URLs. */
43
 public class URLHandler {
44
 public class URLHandler {
44
 
45
 
46
+    /** The UI Controller that owns this handler. */
47
+    private final UIController controller;
45
     /** Config manager. */
48
     /** Config manager. */
46
     private final ConfigManager config;
49
     private final ConfigManager config;
47
-    /** Singleton instance. */
48
-    private static final URLHandler ME = new URLHandler();
49
     /** Desktop handler. */
50
     /** Desktop handler. */
50
     private final Desktop desktop;
51
     private final Desktop desktop;
51
     /** The time a browser was last launched. */
52
     /** The time a browser was last launched. */
52
     private static Date lastLaunch;
53
     private static Date lastLaunch;
53
 
54
 
54
-    /** Instantiates a new URL Handler. */
55
-    private URLHandler() {
56
-        config = IdentityManager.getGlobalConfig();
57
-        if (Desktop.isDesktopSupported()) {
58
-            desktop = Desktop.getDesktop();
59
-        } else {
60
-            desktop = null;
61
-        }
55
+    /**
56
+     * Instantiates a new URL Handler.
57
+     *
58
+     * @param controller The UI controller to show dialogs etc on
59
+     */
60
+    public URLHandler(final UIController controller) {
61
+        this.controller = controller;
62
+        this.config = IdentityManager.getGlobalConfig();
63
+        this.desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
62
     }
64
     }
63
 
65
 
64
     /**
66
     /**
65
      * Gets an instance of URLHandler.
67
      * Gets an instance of URLHandler.
66
      *
68
      *
67
      * @return URLHandler instance
69
      * @return URLHandler instance
70
+     * @deprecated This will return a URLHandler for a random UI which may not
71
+     * implement the required methods. It will probably break. Users of this
72
+     * class must create instances of URLHandler directly with an appropriate
73
+     * UIController.
68
      */
74
      */
75
+    @Deprecated
69
     public static URLHandler getURLHander() {
76
     public static URLHandler getURLHander() {
70
-        synchronized (ME) {
71
-            return ME;
72
-        }
77
+        return new URLHandler(Main.getUI());
73
     }
78
     }
74
 
79
 
75
     /**
80
     /**
132
         }
137
         }
133
 
138
 
134
         if (!config.hasOptionString("protocol", uri.getScheme().toLowerCase())) {
139
         if (!config.hasOptionString("protocol", uri.getScheme().toLowerCase())) {
135
-            Main.getUI().showURLDialog(uri);
140
+            controller.showURLDialog(uri);
136
             return;
141
             return;
137
         }
142
         }
138
 
143
 
161
      *
166
      *
162
      * @return Substituted command
167
      * @return Substituted command
163
      */
168
      */
164
-    public String substituteParams(final URI url, final String command) {
169
+    public static String substituteParams(final URI url, final String command) {
165
         final String userInfo = url.getUserInfo();
170
         final String userInfo = url.getUserInfo();
166
         String fragment = "";
171
         String fragment = "";
167
         String host = "";
172
         String host = "";
192
         if (url.getQuery() != null) {
197
         if (url.getQuery() != null) {
193
             query = url.getQuery();
198
             query = url.getQuery();
194
         }
199
         }
195
-        
200
+
196
         if (url.getPort() > 0) {
201
         if (url.getPort() > 0) {
197
             port = String.valueOf(url.getPort());
202
             port = String.valueOf(url.getPort());
198
         }
203
         }
233
                     ex.getMessage());
238
                     ex.getMessage());
234
         }
239
         }
235
     }
240
     }
236
-    
241
+
237
     /**
242
     /**
238
      * Parses the specified command into an array of arguments. Arguments are
243
      * Parses the specified command into an array of arguments. Arguments are
239
      * separated by spaces. Multi-word arguments may be specified by starting
244
      * separated by spaces. Multi-word arguments may be specified by starting
240
      * the argument with a quote (") and finishing it with a quote (").
245
      * the argument with a quote (") and finishing it with a quote (").
241
-     * 
246
+     *
242
      * @param command The command to parse
247
      * @param command The command to parse
243
      * @return An array of arguments corresponding to the command
248
      * @return An array of arguments corresponding to the command
244
      */
249
      */
246
         final List<String> args = new ArrayList<String>();
251
         final List<String> args = new ArrayList<String>();
247
         final StringBuilder builder = new StringBuilder();
252
         final StringBuilder builder = new StringBuilder();
248
         boolean inquote = false;
253
         boolean inquote = false;
249
-        
254
+
250
         for (String word : command.split(" ")) {
255
         for (String word : command.split(" ")) {
251
             if (word.endsWith("\"") && inquote) {
256
             if (word.endsWith("\"") && inquote) {
252
                 args.add(builder.toString() + ' ' + word.substring(0, word.length() - 1));
257
                 args.add(builder.toString() + ' ' + word.substring(0, word.length() - 1));
256
                 if (builder.length() > 0) {
261
                 if (builder.length() > 0) {
257
                     builder.append(' ');
262
                     builder.append(' ');
258
                 }
263
                 }
259
-                
264
+
260
                 builder.append(word);
265
                 builder.append(word);
261
             } else if (word.startsWith("\"") && !word.endsWith("\"")) {
266
             } else if (word.startsWith("\"") && !word.endsWith("\"")) {
262
                 inquote = true;
267
                 inquote = true;
271
                 args.add(word);
276
                 args.add(word);
272
             }
277
             }
273
         }
278
         }
274
-        
279
+
275
         return args.toArray(new String[args.size()]);
280
         return args.toArray(new String[args.size()]);
276
     }
281
     }
277
 
282
 

+ 1
- 1
test/com/dmdirc/ui/core/util/URLHandlerTest.java View File

65
         };
65
         };
66
         
66
         
67
         for (Object[] test : tests) {
67
         for (Object[] test : tests) {
68
-            final String result = URLHandler.getURLHander().substituteParams((URI) test[0],
68
+            final String result = URLHandler.substituteParams((URI) test[0],
69
                     (String) test[1]);
69
                     (String) test[1]);
70
             assertEquals(test[0].toString() + " + " + test[1].toString() + " ==> " + result,
70
             assertEquals(test[0].toString() + " + " + test[1].toString() + " ==> " + result,
71
                     (String) test[2], result);
71
                     (String) test[2], result);

Loading…
Cancel
Save