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.uruma.rcp;
17  
18  import org.eclipse.equinox.app.IApplication;
19  import org.eclipse.equinox.app.IApplicationContext;
20  import org.eclipse.swt.widgets.Display;
21  import org.eclipse.swt.widgets.Shell;
22  import org.eclipse.ui.IWorkbench;
23  import org.eclipse.ui.PlatformUI;
24  import org.seasar.uruma.core.UrumaConstants;
25  import org.seasar.uruma.core.UrumaMessageCodes;
26  import org.seasar.uruma.log.UrumaLogger;
27  import org.seasar.uruma.rcp.core.UrumaBundleState;
28  import org.seasar.uruma.rcp.core.UrumaBundleState.BundleState;
29  import org.seasar.uruma.rcp.ui.UrumaWorkbenchAdvisor;
30  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
31  import org.seasar.uruma.ui.dialogs.UrumaErrorDialog;
32  import org.seasar.uruma.util.MessageUtil;
33  
34  /**
35   * RCP 環境での Uruma ブートストラップクラスです。<br />
36   * 
37   * @author y-komori
38   */
39  public class UrumaApplication implements IApplication, UrumaMessageCodes,
40          UrumaConstants {
41      private static final UrumaLogger logger = UrumaLogger
42              .getLogger(UrumaApplication.class);
43  
44      /*
45       * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
46       */
47      public Object start(final IApplicationContext context) throws Exception {
48          if (UrumaBundleState.getInstance().getUrumaBundleState() == BundleState.NOT_AVAILABLE) {
49              return IApplication.EXIT_OK;
50          }
51  
52          logger.log(URUMA_APPLICATION_STARTING);
53  
54          Display display = null;
55          try {
56              UrumaServiceUtil.getService().switchToAppClassLoader();
57  
58              display = PlatformUI.createDisplay();
59              int returnCode = PlatformUI.createAndRunWorkbench(display,
60                      new UrumaWorkbenchAdvisor());
61              if (returnCode == PlatformUI.RETURN_RESTART) {
62                  return IApplication.EXIT_RESTART;
63              } else {
64                  return IApplication.EXIT_OK;
65              }
66          } catch (Throwable ex) {
67              if (display == null) {
68                  display = new Display();
69              }
70              Shell shell = new Shell(display);
71              String msg = MessageUtil.getMessageWithBundleName(
72                      URUMA_MESSAGE_BASE, "RCP_EXCEPTION_OCCURED");
73              UrumaErrorDialog dialog = new UrumaErrorDialog(shell, "Uruma", msg,
74                      ex);
75              dialog.open();
76              shell.dispose();
77          } finally {
78              if ((display != null) && !display.isDisposed()) {
79                  display.dispose();
80              }
81          }
82          return IApplication.EXIT_OK;
83      }
84  
85      /*
86       * @see org.eclipse.equinox.app.IApplication#stop()
87       */
88      public void stop() {
89          logger.log(URUMA_APPLICATION_STOPPING);
90  
91          final IWorkbench workbench = PlatformUI.getWorkbench();
92          if (workbench == null)
93              return;
94          final Display display = workbench.getDisplay();
95          display.syncExec(new Runnable() {
96              public void run() {
97                  if (!display.isDisposed()) {
98                      workbench.close();
99                  }
100             }
101         });
102     }
103 }