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.ui;
17  
18  import java.util.ResourceBundle;
19  
20  import org.eclipse.swt.widgets.Display;
21  import org.eclipse.swt.widgets.Shell;
22  import org.eclipse.ui.application.IWorkbenchConfigurer;
23  import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
24  import org.eclipse.ui.application.WorkbenchAdvisor;
25  import org.eclipse.ui.application.WorkbenchWindowAdvisor;
26  import org.seasar.eclipse.common.util.ImageManager;
27  import org.seasar.framework.util.StringUtil;
28  import org.seasar.uruma.component.rcp.WorkbenchComponent;
29  import org.seasar.uruma.core.UrumaConstants;
30  import org.seasar.uruma.core.UrumaMessageCodes;
31  import org.seasar.uruma.exception.NotFoundException;
32  import org.seasar.uruma.log.UrumaLogger;
33  import org.seasar.uruma.rcp.UrumaService;
34  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
35  import org.seasar.uruma.ui.dialogs.UrumaErrorDialog;
36  import org.seasar.uruma.util.MessageUtil;
37  
38  /**
39   * Uruma における {@link WorkbenchAdvisor} です。<br />
40   * 
41   * @author y-komori
42   */
43  public class UrumaWorkbenchAdvisor extends WorkbenchAdvisor implements
44          UrumaMessageCodes, UrumaConstants {
45      private static final UrumaLogger logger = UrumaLogger
46              .getLogger(UrumaWorkbenchAdvisor.class);
47  
48      /*
49       * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
50       */
51      @Override
52      public void initialize(final IWorkbenchConfigurer configurer) {
53          UrumaService service = UrumaServiceUtil.getService();
54  
55          service.getContainer().register(configurer);
56  
57          // ウィンドウの位置・サイズの保存可否を指定します。
58          WorkbenchComponent workbench = service.getWorkbenchComponent();
59          boolean flg = Boolean.parseBoolean(workbench.saveAndRestore);
60          configurer.setSaveAndRestore(flg);
61  
62          ResourceBundle imageBundle = service.getImageBundle();
63          if (imageBundle != null) {
64              logger.log(UrumaMessageCodes.LOADING_IMAGE_BUNDLE, imageBundle);
65              ImageManager.loadImages(imageBundle);
66          }
67      }
68  
69      /*
70       * @see org.eclipse.ui.application.WorkbenchAdvisor#preShutdown()
71       */
72      @Override
73      public boolean preShutdown() {
74          ImageManager.dispose();
75          return true;
76      }
77  
78      /*
79       * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
80       */
81      @Override
82      public String getInitialWindowPerspectiveId() {
83          UrumaService service = UrumaServiceUtil.getService();
84  
85          String perspectiveId = service.getWorkbenchComponent().initialPerspectiveId;
86  
87          if (StringUtil.isNotBlank(perspectiveId)) {
88              WorkbenchComponent workbench = UrumaServiceUtil.getService()
89                      .getWorkbenchComponent();
90              if (workbench.findPerspective(perspectiveId) != null) {
91                  return service.createRcpId(perspectiveId);
92              } else {
93                  throw new NotFoundException(
94                          UrumaMessageCodes.PERSPECTIVE_NOT_FOUND, perspectiveId);
95              }
96          } else {
97              return service.createRcpId(UrumaConstants.DEFAULT_PERSPECTIVE_ID);
98          }
99      }
100 
101     /*
102      * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
103      */
104     @Override
105     public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
106             final IWorkbenchWindowConfigurer configurer) {
107         return new UrumaWorkbenchWindowAdvisor(configurer);
108     }
109 
110     /*
111      * @see org.eclipse.ui.application.WorkbenchAdvisor#eventLoopException(java.lang.Throwable)
112      */
113     @Override
114     public void eventLoopException(final Throwable throwable) {
115         logger.log(EXCEPTION_OCCURED_WITH_REASON, throwable, throwable
116                 .getMessage());
117 
118         Display display = getWorkbenchConfigurer().getWorkbench().getDisplay();
119         boolean displayCreated = false;
120         if (display == null) {
121             display = Display.getCurrent();
122 
123             if (display == null) {
124                 display = new Display();
125                 displayCreated = true;
126             }
127         }
128         Shell shell = new Shell(display);
129         String msg = MessageUtil.getMessageWithBundleName(URUMA_MESSAGE_BASE,
130                 "RCP_EXCEPTION_OCCURED");
131         UrumaErrorDialog dialog = new UrumaErrorDialog(shell, "Uruma", msg,
132                 throwable);
133         dialog.open();
134         shell.dispose();
135 
136         if (displayCreated) {
137             display.dispose();
138         }
139     }
140 }