001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.application.support;
017
018 import java.util.Locale;
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022 import org.springframework.binding.convert.ConversionService;
023 import org.springframework.context.ApplicationContext;
024 import org.springframework.context.MessageSource;
025 import org.springframework.context.MessageSourceResolvable;
026 import org.springframework.context.support.MessageSourceAccessor;
027 import org.springframework.richclient.application.Application;
028 import org.springframework.richclient.application.ApplicationServices;
029 import org.springframework.richclient.application.ApplicationServicesLocator;
030 import org.springframework.richclient.application.ApplicationWindow;
031 import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
032 import org.springframework.richclient.command.config.CommandConfigurer;
033 import org.springframework.richclient.factory.ComponentFactory;
034 import org.springframework.richclient.image.IconSource;
035 import org.springframework.richclient.image.ImageSource;
036
037 /**
038 * @author Keith Donald
039 */
040 public class ApplicationServicesAccessor {
041 protected final Log logger = LogFactory.getLog(getClass());
042
043 protected String getApplicationName() {
044 return getApplication().getName();
045 }
046
047 protected Application getApplication() {
048 return Application.instance();
049 }
050
051 protected ApplicationServices getApplicationServices() {
052 return ApplicationServicesLocator.services();
053 }
054
055 protected Object getService(Class serviceType) {
056 return getApplicationServices().getService(serviceType);
057 }
058
059 protected ApplicationContext getApplicationContext() {
060 return getApplication().getApplicationContext();
061 }
062
063 protected ComponentFactory getComponentFactory() {
064 return (ComponentFactory)getService(ComponentFactory.class);
065 }
066
067 protected MessageSource getMessageSource() {
068 return getApplicationContext();
069 }
070
071 protected MessageSourceAccessor getMessages() {
072 return (MessageSourceAccessor)ApplicationServicesLocator.services().getService(MessageSourceAccessor.class);
073 }
074
075 protected ImageSource getImageSource() {
076 return (ImageSource)getService(ImageSource.class);
077 }
078
079 protected IconSource getIconSource() {
080 return (IconSource)getService(IconSource.class);
081 }
082
083 protected ApplicationObjectConfigurer getObjectConfigurer() {
084 return (ApplicationObjectConfigurer)getService(ApplicationObjectConfigurer.class);
085 }
086
087 protected CommandConfigurer getCommandConfigurer() {
088 return (CommandConfigurer)getService(CommandConfigurer.class);
089 }
090
091 protected ApplicationWindow getActiveWindow() {
092 return getApplication().getActiveWindow();
093 }
094
095 protected ConversionService getConversionService() {
096 return (ConversionService)getService(ConversionService.class);
097 }
098
099 protected String getMessage(String messageCode) {
100 return getApplicationContext().getMessage(messageCode, null, messageCode, Locale.getDefault());
101 }
102
103 protected String getMessage(final String[] messageCodes) {
104 MessageSourceResolvable resolvable = new MessageSourceResolvable() {
105 public String[] getCodes() {
106 return messageCodes;
107 }
108
109 public Object[] getArguments() {
110 return new Object[0];
111 }
112
113 public String getDefaultMessage() {
114 return messageCodes[0];
115 }
116 };
117 return getApplicationContext().getMessage(resolvable, Locale.getDefault());
118 }
119
120 protected String getMessage(String messageCode, Object[] args) {
121 return getApplicationContext().getMessage(messageCode, args, messageCode, Locale.getDefault());
122 }
123
124 protected String getMessage(final String[] messageCodes, final Object[] args) {
125 MessageSourceResolvable resolvable = new MessageSourceResolvable() {
126 public String[] getCodes() {
127 return messageCodes;
128 }
129
130 public Object[] getArguments() {
131 return args;
132 }
133
134 public String getDefaultMessage() {
135 return messageCodes[0];
136 }
137 };
138 return getApplicationContext().getMessage(resolvable, Locale.getDefault());
139 }
140
141 }