View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.eclipse.common.util;
17  
18  import java.net.URL;
19  
20  import org.eclipse.ui.IWorkbench;
21  import org.eclipse.ui.IWorkbenchWindow;
22  import org.eclipse.ui.PlatformUI;
23  import org.eclipse.ui.browser.IWebBrowser;
24  import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
25  import org.seasar.eclipse.common.CommonPlugin;
26  import org.seasar.framework.util.StringUtil;
27  
28  /**
29   * @author taichi
30   * 
31   */
32  public class WorkbenchUtil {
33  
34      public static void openUrl(String url) {
35          try {
36              openUrl(new URL(url), false);
37          } catch (Exception e) {
38              CommonPlugin.log(e);
39          }
40      }
41  
42      public static void openUrl(URL url, boolean maybeInternal) {
43          openUrl(url, maybeInternal, "");
44      }
45  
46      public static void openUrl(URL url, boolean maybeInternal, String browserId) {
47          try {
48              IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
49                      .getBrowserSupport();
50              IWebBrowser browser = null;
51              if (maybeInternal && support.isInternalWebBrowserAvailable()) {
52                  int flag = IWorkbenchBrowserSupport.AS_EDITOR
53                          | IWorkbenchBrowserSupport.LOCATION_BAR
54                          | IWorkbenchBrowserSupport.NAVIGATION_BAR
55                          | IWorkbenchBrowserSupport.STATUS
56                          | IWorkbenchBrowserSupport.PERSISTENT;
57                  browser = support.createBrowser(flag, StringUtil
58                          .isEmpty(browserId) ? "" : browserId, null, null);
59              } else {
60                  browser = support.getExternalBrowser();
61              }
62              if (browser != null) {
63                  browser.openURL(url);
64              }
65          } catch (Exception e) {
66              CommonPlugin.log(e);
67          }
68      }
69  
70      public static IWorkbenchWindow getWorkbenchWindow() {
71          IWorkbench workbench = PlatformUI.getWorkbench();
72          IWorkbenchWindow result = workbench.getActiveWorkbenchWindow();
73          if (result == null && 0 < workbench.getWorkbenchWindowCount()) {
74              IWorkbenchWindow[] ws = workbench.getWorkbenchWindows();
75              result = ws[0];
76          }
77          return result;
78      }
79  
80  }