1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
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
51
52
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
70
71
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
98
99
100
101
102
103
104
105 public static void setConfigPath(final String configPath) {
106 SingletonS2ContainerFactory.setConfigPath(configPath);
107 }
108
109
110
111
112
113
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
169
170
171
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 }