1 package cz
.cvut
.promod
.services
;
3 import cz
.cvut
.promod
.services
.componentFactoryService
.ComponentFactoryService
;
4 import cz
.cvut
.promod
.services
.userService
.UserService
;
5 import cz
.cvut
.promod
.services
.actionService
.ActionControlService
;
6 import cz
.cvut
.promod
.services
.actionService
.ActionService
;
7 import cz
.cvut
.promod
.services
.projectService
.ProjectControlService
;
8 import cz
.cvut
.promod
.services
.projectService
.ProjectService
;
9 import cz
.cvut
.promod
.services
.menuService
.MenuService
;
10 import cz
.cvut
.promod
.services
.notationService
.NotationService
;
11 import cz
.cvut
.promod
.services
.toolBarService
.ToolBarService
;
12 import cz
.cvut
.promod
.services
.toolBarService
.ToolBarControlService
;
13 import cz
.cvut
.promod
.services
.statusBarService
.StatusBarControlService
;
14 import cz
.cvut
.promod
.services
.statusBarService
.StatusBarService
;
15 import cz
.cvut
.promod
.services
.extensionService
.ExtensionService
;
16 import cz
.cvut
.promod
.gui
.ModelerModel
;
17 import org
.apache
.log4j
.Logger
;
20 import java
.util
.ResourceBundle
;
23 * ProMod, master thesis project
24 * User: Petr Zverina, petr.zverina@gmail.com
25 * Date: 16:10:56, 10.10.2009
29 * ModelerSession allows access to all services and shared resources.
31 * It is supposed to be an entry point for plugins when need to use services.
33 * IMPORTANT NOTE: One is always supposed to use all services and other resources only in Event Dispatcher Thread
34 * or Main Thread (during application start).
36 * Usage of services and their resources form another thread than Event Dispatcher Thread or Main Thread can cause a
37 * unpredictable inconsistency of application. One is supposed to always avoid using services and their
38 * resources out of the Event Dispatcher Thread (when the GUI is shown)!
40 * @see cz.cvut.promod.services.Service
41 * @see javax.swing.SwingUtilities
43 public final class ModelerSession
{
45 private static final Logger LOG
= Logger
.getLogger(ModelerSession
.class);
47 /** testing purposes */
48 public static final String NULL_METHOD_NAME
= "nullServices";
50 private static final ModelerSession MODELER_SESSION_INSTANCE
= new ModelerSession();
52 private static ResourceBundle commonResourceBundle
= null;
54 private static JFrame frame
= null;
56 private ComponentFactoryService componentFactoryService
= null;
57 private UserService userService
= null;
58 private ActionControlService actionControlService
= null;
59 private ProjectControlService projectControlService
= null;
60 private MenuService menuService
= null;
61 private NotationService notationService
= null;
62 private ToolBarControlService toolBarControlService
= null;
63 private StatusBarControlService statusBarControlService
= null;
64 private ExtensionService extensionsService
= null;
67 * @return every-time the same instance of ModelerSession (singleton)
69 public static ModelerSession
getModelerSession() {
70 return MODELER_SESSION_INSTANCE
;
74 * Returns the common resource bundle - common translations.
76 * @return the common resource bundle - common translations
78 public static ResourceBundle
getCommonResourceBundle() {
79 return commonResourceBundle
;
83 * Sets the resource bundle object. It can be set only once. All next attempts to set it again will
86 * @param commonResourceBundle the resource bundle holding translations
88 public static void setCommonResourceBundle(final ResourceBundle commonResourceBundle
) {
89 if(ModelerSession
.commonResourceBundle
== null){
90 ModelerSession
.commonResourceBundle
= commonResourceBundle
;
95 * Usage of this method is a way how to get reference to the JFrame object of Modeler.
96 * One should always check whether return value is not null before actual use.
98 * @return an instance of the modeler frame
100 public static JFrame
getFrame() {
105 * Allows to set a title for the Modeler frame.
107 * @param text is the text to be shown
109 public static void setFrameTitleText(final String text
){
111 final StringBuffer stringBuffer
= new StringBuffer();
112 stringBuffer
.append(ModelerModel
.APPLICATION_NAME
);
114 if(text
!= null && !text
.isEmpty()){
115 stringBuffer
.append(" - [");
116 stringBuffer
.append(text
);
117 stringBuffer
.append("]");
120 frame
.setTitle(stringBuffer
.toString());
125 * Sets the title of Modeler frame to initial text.
127 public static void clearFrameTitleText(){
129 frame
.setTitle(ModelerModel
.APPLICATION_NAME
);
134 * Sets the application frame object. It can be set only once. All next attempts to set it again will
137 * @param frame is the application frame
139 public static void setFrame(final JFrame frame
) {
140 if(ModelerSession
.frame
== null){
141 ModelerSession
.frame
= frame
;
146 * Returns the ComponentFactoryService object.
148 * @return every-time the same ComponentFactoryService object
150 public static ComponentFactoryService
getComponentFactoryService() {
151 final ComponentFactoryService componentFactoryService
= MODELER_SESSION_INSTANCE
.componentFactoryService
;
153 if(componentFactoryService
== null){
154 LOG
.error("ComponentFactoryService has not been set.");
155 throw new NullPointerException("ComponentFactoryService is not available.");
158 return componentFactoryService
;
162 * Sets the ComponentFactoryService object. It can be set only once. All next attempts to set it again will
165 * @param componentFactoryService is the ComponentFactoryService object
167 public static void setComponentFactoryService(final ComponentFactoryService componentFactoryService
) {
168 MODELER_SESSION_INSTANCE
.componentFactoryService
= componentFactoryService
;
172 * Returns the UserService object.
174 * @return every-time the same UserService object
176 public static UserService
getUserService() {
177 return MODELER_SESSION_INSTANCE
.userService
;
181 * Sets the UserService object. It can be set only once. All next attempts to set it again will
184 * @param userService is the UserService object
186 public static void setUserService(final UserService userService
) {
187 MODELER_SESSION_INSTANCE
.userService
= userService
;
190 public static ActionControlService
getActionControlService() {
191 final ActionControlService actionControlService
= MODELER_SESSION_INSTANCE
.actionControlService
;
193 if(actionControlService
== null){
194 LOG
.error("ActionControlService has not been set.");
195 throw new NullPointerException("ActionControlService is not available.");
198 return actionControlService
;
201 public static ActionService
getActionService() {
202 final ActionService actionService
= MODELER_SESSION_INSTANCE
.actionControlService
;
204 if(actionService
== null){
205 LOG
.error("ActionService has not been set.");
206 throw new NullPointerException("ActionService is not available.");
209 return actionService
;
213 * Sets the ActionControlService object. It can be set only once. All next attempts to set it again will
216 * @param actionControlService is the ActionControlService object
218 public static void setActionControlService(final ActionControlService actionControlService
) {
219 if(MODELER_SESSION_INSTANCE
.actionControlService
== null){
220 MODELER_SESSION_INSTANCE
.actionControlService
= actionControlService
;
225 * Returns the ProjectControlService object.
227 * @return every-time the same ProjectControlService object
229 public static ProjectControlService
getProjectControlService() {
230 final ProjectControlService projectControlService
= MODELER_SESSION_INSTANCE
.projectControlService
;
232 if(projectControlService
== null){
233 LOG
.error("ProjectControlService has not been set.");
234 throw new NullPointerException("ProjectControlService is not available.");
237 return projectControlService
;
241 * Returns the ProjectService object.
243 * @return every-time the same ProjectService object
245 public static ProjectService
getProjectService() {
246 final ProjectService projectService
= MODELER_SESSION_INSTANCE
.projectControlService
;
248 if(projectService
== null){
249 LOG
.error("ProjectService has not been set.");
250 throw new NullPointerException("ProjectService is not available.");
253 return projectService
;
257 * Sets the ProjectControlService object. It can be set only once. All next attempts to set it again will
260 * @param projectControlService is the ProjectControlService object
262 public static void setProjectControlService(final ProjectControlService projectControlService
) {
263 if(MODELER_SESSION_INSTANCE
.projectControlService
== null){
264 MODELER_SESSION_INSTANCE
.projectControlService
= projectControlService
;
269 * Returns the MenuService object.
271 * @return every-time the same MenuService object
273 public static MenuService
getMenuService() {
274 final MenuService menuService
= MODELER_SESSION_INSTANCE
.menuService
;
276 if(menuService
== null){
277 LOG
.error("MenuService has not been set.");
278 throw new NullPointerException("MenuService is not available.");
285 * Sets the MenuService object. It can be set only once. All next attempts to set it again will
288 * @param menuService is the MenuService object
290 public static void setMainMenuService(final MenuService menuService
) {
291 if(MODELER_SESSION_INSTANCE
.menuService
== null){
292 MODELER_SESSION_INSTANCE
.menuService
= menuService
;
297 * Returns the NotationService object.
299 * @return every-time the same NotationService object
301 public static NotationService
getNotationService() {
302 final NotationService notationService
= MODELER_SESSION_INSTANCE
.notationService
;
304 if(notationService
== null){
305 LOG
.error("NotationService has not been set.");
306 throw new NullPointerException("NotationService is not available.");
309 return notationService
;
313 * Sets the NotationService object. It can be set only once. All next attempts to set it again will
316 * @param notationService is the NotationService object
318 public static void setNotationService(final NotationService notationService
) {
319 if(MODELER_SESSION_INSTANCE
.notationService
== null){
320 MODELER_SESSION_INSTANCE
.notationService
= notationService
;
325 * Returns the ToolBarService object.
327 * @return every-time the same ToolBarService object
329 public static ToolBarService
getToolBarService() {
330 final ToolBarService toolBarService
= MODELER_SESSION_INSTANCE
.toolBarControlService
;
332 if(toolBarService
== null){
333 LOG
.error("ToolBarService has not been set.");
334 throw new NullPointerException("ToolBarService is not available.");
337 return toolBarService
;
341 * Returns the ToolBarControlService object.
343 * @return every-time the same ToolBarControlService object
345 public static ToolBarControlService
getToolBarControlService() {
346 final ToolBarControlService toolBarControlService
= MODELER_SESSION_INSTANCE
.toolBarControlService
;
348 if(toolBarControlService
== null){
349 LOG
.error("ToolBarControlService has not been set.");
350 throw new NullPointerException("ToolBarControlService is not available.");
353 return toolBarControlService
;
357 * Sets the ToolBarControlService object. It can be set only once. All next attempts to set it again will
360 * @param toolBarService is the ToolBarControlService object
362 public static void setToolBarControlService(final ToolBarControlService toolBarService
) {
363 if(MODELER_SESSION_INSTANCE
.toolBarControlService
== null){
364 MODELER_SESSION_INSTANCE
.toolBarControlService
= toolBarService
;
369 * Returns the StatusBarControlService object.
371 * @return every-time the same StatusBarControlService object
373 public static StatusBarControlService
getStatusBarControlService() {
374 final StatusBarControlService statusBarControlService
= MODELER_SESSION_INSTANCE
.statusBarControlService
;
376 if(statusBarControlService
== null){
377 LOG
.error("statusBarControlService has not been set.");
378 throw new NullPointerException("statusBarControlService is not available.");
381 return statusBarControlService
;
385 * Returns the StatusBarService object.
387 * @return every-time the same StatusBarService object
389 public static StatusBarService
getStatusBarService() {
390 final StatusBarService statusBarService
= MODELER_SESSION_INSTANCE
.statusBarControlService
;
392 if(statusBarService
== null){
393 LOG
.error("statusBarService has not been set.");
394 throw new NullPointerException("statusBarService is not available.");
397 return statusBarService
;
401 * Sets the StatusBarControlService object. It can be set only once. All next attempts to set it again will
404 * @param statusBarControlService is the StatusBarControlService object
406 public static void setStatusBarControlService(final StatusBarControlService statusBarControlService
) {
407 if(MODELER_SESSION_INSTANCE
.statusBarControlService
== null){
408 MODELER_SESSION_INSTANCE
.statusBarControlService
= statusBarControlService
;
413 * Returns the ExtensionService object.
415 * @return every-time the same ExtensionService object
417 public static ExtensionService
getExtensionService() {
418 final ExtensionService extensionService
= MODELER_SESSION_INSTANCE
.extensionsService
;
420 if(extensionService
== null){
421 LOG
.error("extension service has not been set.");
422 throw new NullPointerException("extension service is not available.");
425 return extensionService
;
429 * Sets the ExtensionService object. It can be set only once. All next attempts to set it again will
432 * @param extensionService is the ExtensionService object
434 public static void setExtensionService(final ExtensionService extensionService
) {
435 if(MODELER_SESSION_INSTANCE
.extensionsService
== null){
436 MODELER_SESSION_INSTANCE
.extensionsService
= extensionService
;
441 * Private static method to null all services. Used in automatic tests - JUnit tests to null services.
442 * None is supposed to use this method for non-testing purposes.
444 * Tests use this method through reflection.
446 private static void nullServices(){
447 MODELER_SESSION_INSTANCE
.componentFactoryService
= null;
448 MODELER_SESSION_INSTANCE
.userService
= null;
449 MODELER_SESSION_INSTANCE
.actionControlService
= null;
450 MODELER_SESSION_INSTANCE
.projectControlService
= null;
451 MODELER_SESSION_INSTANCE
.menuService
= null;
452 MODELER_SESSION_INSTANCE
.notationService
= null;
453 MODELER_SESSION_INSTANCE
.toolBarControlService
= null;
454 MODELER_SESSION_INSTANCE
.statusBarControlService
= null;
455 MODELER_SESSION_INSTANCE
.extensionsService
= null;