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.core;
17  
18  import java.util.MissingResourceException;
19  
20  import org.eclipse.swt.widgets.Display;
21  import org.eclipse.swt.widgets.Shell;
22  import org.seasar.eclipse.common.util.ImageManager;
23  import org.seasar.framework.container.S2Container;
24  import org.seasar.framework.container.factory.S2ContainerFactory;
25  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
26  import org.seasar.framework.exception.ResourceNotFoundRuntimeException;
27  import org.seasar.uruma.log.UrumaLogger;
28  import org.seasar.uruma.ui.dialogs.UrumaErrorDialog;
29  import org.seasar.uruma.util.MessageUtil;
30  
31  /**
32   * RCP を利用せずに単独でウィンドウを開くアプリケーションのためのスタートアップクラスです。<br />
33   * 
34   * @author y-komori
35   */
36  public class StandAloneUrumaStarter implements UrumaMessageCodes,
37          UrumaConstants {
38      private final UrumaLogger logger = UrumaLogger
39              .getLogger(StandAloneUrumaStarter.class);
40  
41      private static StandAloneUrumaStarter instance;
42  
43      protected S2Container container;
44  
45      private Display display;
46  
47      private String imageBundleName = DEFAULT_IMAGE_BUNDLE_PATH;
48  
49      /**
50       * アプリケーションを開始します。<br />
51       * 
52       * @param args
53       *            コマンドライン引数
54       */
55      public static void main(final String[] args) {
56          if (args.length >= 1) {
57              String templatePath = args[0];
58              StandAloneUrumaStarter starter = StandAloneUrumaStarter
59                      .getInstance();
60              starter.openWindow(templatePath);
61          } else {
62              String msg = MessageUtil.getMessageWithBundleName(
63                      URUMA_MESSAGE_BASE, "STANDALONE_USAGE");
64              System.err.println(msg);
65          }
66      }
67  
68      /**
69       * 本クラスのインスタンスを取得します。<br />
70       * 
71       * @return 本クラスのインスタンス
72       */
73      public synchronized static StandAloneUrumaStarter getInstance() {
74          if (instance == null) {
75              instance = new StandAloneUrumaStarter();
76          }
77          return instance;
78      }
79  
80      private StandAloneUrumaStarter() {
81          try {
82              logger.log(STAND_ALONE_URUMA_STARTER_INIT);
83              initS2Container();
84          } catch (Throwable ex) {
85              Display display = new Display();
86              Shell shell = new Shell(display);
87              String msg = MessageUtil.getMessageWithBundleName(
88                      URUMA_MESSAGE_BASE, "STANDALONE_START_FAILED");
89              UrumaErrorDialog dialog = new UrumaErrorDialog(shell, "Uruma", msg,
90                      ex);
91              dialog.open();
92              shell.dispose();
93          }
94      }
95  
96      /**
97       * dicon ファイルのパスを設定します。<br />
98       * デフォルトでは、<code>app.dicon</code> が使用されます。<br />
99       * 本メソッドは、 {@link StandAloneUrumaStarter#getInstance() getInstance()}
100      * メソッドを最初に呼び出す前に呼び出してください。
101      * 
102      * @param configPath
103      *            Dicon ファイルのパス
104      */
105     public static void setConfigPath(final String configPath) {
106         SingletonS2ContainerFactory.setConfigPath(configPath);
107     }
108 
109     /**
110      * 指定された画面定義 XML を読み込み、画面を表示します。<br />
111      * 
112      * @param templatePath
113      *            画面定義XMLのパス
114      */
115     public void openWindow(final String templatePath) {
116         try {
117             display = Display.getCurrent();
118             if (display == null) {
119                 display = new Display();
120                 setupImageManager(display);
121             }
122 
123             UrumaWindowManager windowManager = (UrumaWindowManager) container
124                     .getComponent(UrumaWindowManager.class);
125             windowManager.openWindow(templatePath, true);
126         } catch (Throwable ex) {
127             logger.log(EXCEPTION_OCCURED_WITH_REASON, ex, ex.getMessage());
128 
129             if (display == null) {
130                 display = new Display();
131             }
132             Shell shell = new Shell(display);
133             String msg = MessageUtil.getMessageWithBundleName(
134                     URUMA_MESSAGE_BASE, "STANDALONE_EXCEPTION_OCCURED");
135             UrumaErrorDialog dialog = new UrumaErrorDialog(shell, "Uruma", msg,
136                     ex);
137             dialog.open();
138             shell.dispose();
139         } finally {
140             try {
141                 dispose();
142             } catch (Throwable ex) {
143                 logger.log(EXCEPTION_OCCURED_WITH_REASON, ex, ex.getMessage());
144             }
145         }
146     }
147 
148     protected void initS2Container() {
149         S2Container urumaContainer = S2ContainerFactory
150                 .create(UrumaConstants.URUMA_DICON_PATH);
151         String configPath = SingletonS2ContainerFactory.getConfigPath();
152 
153         try {
154             container = S2ContainerFactory.create(configPath);
155         } catch (ResourceNotFoundRuntimeException ex) {
156             logger.log(DICON_FILE_NOT_FOUND, configPath);
157             container = S2ContainerFactory.create();
158         }
159         container.include(urumaContainer);
160 
161         container.init();
162         SingletonS2ContainerFactory.setContainer(container);
163 
164         ComponentUtil.setS2Container(container);
165     }
166 
167     /**
168      * イメージ設定用リソースバンドル名を指定します。<br />
169      * デフォルトでは、 <code>urumaImages</code> が使用されます。
170      * 
171      * @param imageBundleName
172      *            リソースバンドル名
173      */
174     public void setImageBundleName(final String imageBundleName) {
175         this.imageBundleName = imageBundleName;
176     }
177 
178     protected void setupImageManager(final Display display) {
179         ImageManager.init(display);
180         logger.log(LOADING_IMAGE_BUNDLE, imageBundleName);
181 
182         try {
183             ImageManager.loadImages(imageBundleName);
184         } catch (MissingResourceException ex) {
185             logger.log(IMAGE_DEF_BUNDLE_NOT_FOUND, imageBundleName);
186         }
187     }
188 
189     protected void dispose() {
190         ImageManager.dispose();
191         if (display != null) {
192             display.dispose();
193             display = null;
194         }
195     }
196 
197     /**
198      * 本クラスのインスタンスを破棄します。
199      */
200     public static void destroy() {
201         if (instance != null) {
202             instance.logger.log(STAND_ALONE_URUMA_STARTER_STOP);
203             instance.dispose();
204             instance = null;
205         }
206     }
207 }