001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020:
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Shell;
023:
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.InvocationTargetException;
026:
027: /**
028: * Performs platform-specific class selection.
029: */
030: public class PlatformSpecific {
031:
032: /**
033: * All of these classes must extend BrowserWidget.
034: */
035: private static final String[] browserClassNames = new String[] {
036: "com.google.gwt.dev.shell.ie.BrowserWidgetIE6",
037: "com.google.gwt.dev.shell.moz.BrowserWidgetMoz",
038: "com.google.gwt.dev.shell.mac.BrowserWidgetSaf" };
039:
040: /**
041: * All of these classes must extend CheckForUpdates.
042: */
043: private static final String[] updaterClassNames = new String[] {
044: "com.google.gwt.dev.shell.ie.CheckForUpdatesIE6",
045: "com.google.gwt.dev.shell.moz.CheckForUpdatesMoz",
046: "com.google.gwt.dev.shell.mac.CheckForUpdatesSaf" };
047:
048: public static BrowserWidget createBrowserWidget(TreeLogger logger,
049: Composite parent, BrowserWidgetHost host)
050: throws UnableToCompleteException {
051: Throwable caught = null;
052: try {
053: for (int i = 0; i < browserClassNames.length; i++) {
054: Class<BrowserWidget> clazz = null;
055: try {
056: clazz = (Class<BrowserWidget>) Class
057: .forName(browserClassNames[i]);
058: Constructor<BrowserWidget> ctor = clazz
059: .getDeclaredConstructor(new Class[] {
060: Shell.class,
061: BrowserWidgetHost.class });
062: BrowserWidget bw = ctor.newInstance(new Object[] {
063: parent, host });
064: return bw;
065: } catch (ClassNotFoundException e) {
066: caught = e;
067: }
068: }
069: logger
070: .log(
071: TreeLogger.ERROR,
072: "No instantiable browser widget class could be found",
073: caught);
074: throw new UnableToCompleteException();
075: } catch (SecurityException e) {
076: caught = e;
077: } catch (NoSuchMethodException e) {
078: caught = e;
079: } catch (IllegalArgumentException e) {
080: caught = e;
081: } catch (InstantiationException e) {
082: caught = e;
083: } catch (IllegalAccessException e) {
084: caught = e;
085: } catch (InvocationTargetException e) {
086: caught = e.getTargetException();
087: } catch (ClassCastException e) {
088: caught = e;
089: }
090: logger.log(TreeLogger.ERROR,
091: "The browser widget class could not be instantiated",
092: caught);
093: throw new UnableToCompleteException();
094: }
095:
096: public static CheckForUpdates createUpdateChecker() {
097: try {
098: for (int i = 0; i < updaterClassNames.length; i++) {
099: try {
100: Class<CheckForUpdates> clazz = (Class<CheckForUpdates>) Class
101: .forName(updaterClassNames[i]);
102: Constructor<CheckForUpdates> ctor = clazz
103: .getDeclaredConstructor(new Class[] {});
104: CheckForUpdates checker = ctor
105: .newInstance(new Object[] {});
106: return checker;
107: } catch (ClassNotFoundException e) {
108: // keep trying
109: }
110: }
111: } catch (Throwable e) {
112: // silently ignore any errors
113: }
114: return null;
115: }
116: }
|