Project: SWT
Bug 317163 Browser.java dispatching to platform specific implementation has room for improvment
Reported: 2010-06-17 08:15:46 | Status: resolved fixed

Currently the Browser class has a gigantic "if" to detect the Browser class of choice that connects the actual platform specific Browser implementation: if ((style & SWT.MOZILLA) != 0) { className = "org.eclipse.swt.browser.Mozilla"; //$NON-NLS-1$ } else { if ("win32".equals (platform) || "wpf".equals (platform)) { //$NON-NLS-1$ $NON-NLS-2$ className = "org.eclipse.swt.browser.IE"; //$NON-NLS-1$ } else if ("motif".equals (platform)) { //$NON-NLS-1$ className = "org.eclipse.swt.browser.Mozilla"; //$NON-NLS-1$ } else if ("gtk".equals (platform)) { //$NON-NLS-1$ className = "org.eclipse.swt.browser.Mozilla"; //$NON-NLS-1$ } else if ("carbon".equals (platform) || "cocoa".equals (platform)) { //$NON-NLS-1$ className = "org.eclipse.swt.browser.Safari"; //$NON-NLS-1$ } else if ("photon".equals (platform)) { //$NON-NLS-1$ className = "org.eclipse.swt.browser.Voyager"; //$NON-NLS-1$ } else { dispose (); SWT.error (SWT.ERROR_NO_HANDLES); } } This design makes it unbearable hard for new platforms to add their implementation (and its a pretty bad design anyway). Also using strings and reflection to find a class is the second bad idea that I see here. I propose to use a class BrowserFactory with a single static method createBrowser() which returns an instance of of WebBrowser and then every platform can easily implement its own version of BrowserFactory to retrieve the implementation.

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT Browser/carbon/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/cocoa/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java bundles/org.eclipse.swt/Eclipse SWT Browser/forms/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/forms/org/eclipse/swt/browser/IE.java bundles/org.eclipse.swt/Eclipse SWT Browser/gtk/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/motif/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/photon/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/photon/org/eclipse/swt/browser/Voyager.java bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java bundles/org.eclipse.swt/Eclipse SWT Browser/wpf/org/eclipse/swt/browser/BrowserFactory.java bundles/org.eclipse.swt/Eclipse SWT Browser/wpf/org/eclipse/swt/browser/IE.java bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java bundles/org.eclipse.swt/Eclipse SWT WebKit/carbon/org/eclipse/swt/browser/WebKit.java bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java