Package net.sf.jguiraffe.gui.app
Class ApplicationContextImpl
- java.lang.Object
-
- net.sf.jguiraffe.gui.app.ApplicationContextImpl
-
- All Implemented Interfaces:
ApplicationContext,TransformerContext
public class ApplicationContextImpl extends Object implements ApplicationContext
A default implementation of the
ApplicationContextinterface.This class is used by the
Applicationclass to store global information that is needed by different components of an application. Because anApplicationContextimplementation also implements theTransformerContextinterface instances can also be passed to transformer objects.Note that this implementation is not thread-safe. The intended use case is that an instance is created at application startup and initialized with the central helper objects managed by the instance. Later these objects should not be changed any more. If used in this way, the instance can be used from both the event dispatch thread and from commands executing in background threads.
- Version:
- $Id: ApplicationContextImpl.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
- See Also:
TransformerContext
-
-
Constructor Summary
Constructors Constructor Description ApplicationContextImpl()Creates a new instance ofApplicationContextImpl.ApplicationContextImpl(Locale locale)Creates a new instance ofApplicationContextImpland sets the current locale.ApplicationContextImpl(Locale locale, ResourceManager resMan)Creates a new instance ofApplicationContextImpland sets the current locale and the resource manager to use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BindingStrategyfetchBindingStrategy()Obtains theBindingStrategyfor a builder operation.ActionStoregetActionStore()Returns the application'sActionStore.BeanContextgetBeanContext()Returns the global bean context.ClassLoaderProvidergetClassLoaderProvider()Returns theClassLoaderProvider.org.apache.commons.configuration.ConfigurationgetConfiguration()Returns a reference to the global configuration.GUISynchronizergetGUISynchronizer()Returns theGUISynchronizer.LocalegetLocale()Returns the current locale.WindowgetMainWindow()Returns the application's main window.MessageOutputgetMessageOutput()Returns a reference to the object for displaying messages.ObjectgetResource(Object resID)Convenience method for looking up a resource.ObjectgetResource(Object groupID, Object resID)Convenience method for looking up a resource specified as group and resource ID.ObjectgetResource(Message msg)Convenience method for looking up a resource that is specified as aMessageobject.ResourceManagergetResourceManager()Returns the actual resource manager.StringgetResourceText(Object resID)Convenience method for looking up the text of a specified resource.StringgetResourceText(Object groupID, Object resID)Convenience method for looking up the text of a resource specified as group and resource ID.StringgetResourceText(Message msg)Convenience method for looking up the text of a resource specified as aMessageobject.<T> TgetTypedProperty(Class<T> propCls)Returns the value of the specified typed property or null if it cannot be found.ValidationMessageHandlergetValidationMessageHandler()Returns theValidationMessageHandler.ApplicationBuilderDatainitBuilderData()Returns an initializedApplicationBuilderDataobject that can be used for calling the GUI builder.intmessageBox(Object resMsg, Object resTitle, int msgType, int btnType)A convenience method for displaying a message box.BuildernewBuilder()Returns a newinstance.BuilderMap<String,Object>properties()Returns a map with properties maintained by this context.voidsetActionStore(ActionStore actionStore)Sets the application'sActionStore.voidsetBeanContext(BeanContext beanContext)Sets the global bean context.voidsetClassLoaderProvider(ClassLoaderProvider classLoaderProvider)Sets theClassLoaderProvider.voidsetConfiguration(org.apache.commons.configuration.Configuration configuration)Sets the global configuration.voidsetLocale(Locale locale)Sets the current locale.voidsetMainWindow(Window mainWindow)Allows to set the application's main window.voidsetMessageOutput(MessageOutput msg)Sets the message output object to be used by this application.voidsetResourceManager(ResourceManager resourceManager)Sets the resource manager.<T> voidsetTypedProperty(Class<T> propCls, T value)Sets the value of the given typed property.voidsetValidationMessageHandler(ValidationMessageHandler validationMessageHandler)Sets theValidationMessageHandler.
-
-
-
Constructor Detail
-
ApplicationContextImpl
public ApplicationContextImpl()
Creates a new instance ofApplicationContextImpl. The instance is not yet initialized.
-
ApplicationContextImpl
public ApplicationContextImpl(Locale locale)
Creates a new instance ofApplicationContextImpland sets the current locale.- Parameters:
locale- theLocale
-
ApplicationContextImpl
public ApplicationContextImpl(Locale locale, ResourceManager resMan)
Creates a new instance ofApplicationContextImpland sets the current locale and the resource manager to use.- Parameters:
locale- theLocaleresMan- theResourceManagerto use
-
-
Method Detail
-
getLocale
public Locale getLocale()
Returns the current locale. If no specific locale has been set yet, the system's default locale will be used.- Specified by:
getLocalein interfaceTransformerContext- Returns:
- the
Locale
-
setLocale
public void setLocale(Locale locale)
Sets the current locale. Note: This implementation is thread-safe; the newly set locale is visible for other threads, too.- Specified by:
setLocalein interfaceApplicationContext- Parameters:
locale- the newLocale
-
getResourceManager
public ResourceManager getResourceManager()
Returns the actual resource manager.- Specified by:
getResourceManagerin interfaceTransformerContext- Returns:
- the
ResourceManagerfor accessing resources
-
setResourceManager
public void setResourceManager(ResourceManager resourceManager)
Sets the resource manager. Access to system resources will be handled by this object.- Specified by:
setResourceManagerin interfaceApplicationContext- Parameters:
resourceManager- theResourceManager
-
properties
public Map<String,Object> properties()
Returns a map with properties maintained by this context.- Specified by:
propertiesin interfaceTransformerContext- Returns:
- a map with properties
-
getConfiguration
public org.apache.commons.configuration.Configuration getConfiguration()
Returns a reference to the global configuration.- Specified by:
getConfigurationin interfaceApplicationContext- Returns:
- the configuration object
-
setConfiguration
public void setConfiguration(org.apache.commons.configuration.Configuration configuration)
Sets the global configuration.- Specified by:
setConfigurationin interfaceApplicationContext- Parameters:
configuration- the configuration object
-
getBeanContext
public BeanContext getBeanContext()
Returns the global bean context.- Specified by:
getBeanContextin interfaceApplicationContext- Returns:
- the bean context
-
setBeanContext
public void setBeanContext(BeanContext beanContext)
Sets the global bean context.- Parameters:
beanContext- the new bean context
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProvider. This implementation always returns theClassLoaderProvidermanaged by the currentBeanContext.- Specified by:
getClassLoaderProviderin interfaceApplicationContext- Returns:
- the
ClassLoaderProvider
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider. This implementation does not store theClassLoaderProviderin a separate member field. Rather, it is passed to the currentBeanContext. To avoid inconsistencies when loading classes there must be only a singleClassLoaderProviderinstance. If this method is called before aBeanContextwas set, anIllegalStateExceptionexception is thrown.- Specified by:
setClassLoaderProviderin interfaceApplicationContext- Parameters:
classLoaderProvider- the new class loader provider- Throws:
IllegalStateException- if noBeanContexthas been set
-
getValidationMessageHandler
public ValidationMessageHandler getValidationMessageHandler()
Returns theValidationMessageHandler.- Specified by:
getValidationMessageHandlerin interfaceTransformerContext- Returns:
- the
ValidationMessageHandler
-
setValidationMessageHandler
public void setValidationMessageHandler(ValidationMessageHandler validationMessageHandler)
Sets theValidationMessageHandler. This object can be queried by validators to obtain a specific validation message.- Parameters:
validationMessageHandler- the newValidationMessageHandler(must not be null)- Throws:
IllegalArgumentException- if the passed validation message handler is null
-
getMessageOutput
public MessageOutput getMessageOutput()
Returns a reference to the object for displaying messages. This object can be used to create message boxes.- Specified by:
getMessageOutputin interfaceApplicationContext- Returns:
- the object for displaying messages
-
setMessageOutput
public void setMessageOutput(MessageOutput msg)
Sets the message output object to be used by this application.- Specified by:
setMessageOutputin interfaceApplicationContext- Parameters:
msg- the newMessageOutputobject
-
getResource
public Object getResource(Object groupID, Object resID)
Convenience method for looking up a resource specified as group and resource ID.- Specified by:
getResourcein interfaceApplicationContext- Parameters:
groupID- the resource group IDresID- the resource ID- Returns:
- the found resource
- Throws:
MissingResourceException- if the resource cannot be found
-
getResource
public Object getResource(Message msg)
Convenience method for looking up a resource that is specified as aMessageobject.- Specified by:
getResourcein interfaceApplicationContext- Parameters:
msg- the resource definition (must not be null)- Returns:
- the found resource
- Throws:
MissingResourceException- if the resource cannot be foundIllegalArgumentException- if then message is undefined
-
getResource
public Object getResource(Object resID)
Convenience method for looking up a resource. The passed in object is checked to be an instance of. If this is the case, the resource group and the resource ID are extracted from this object. Otherwise the passed in object is interpreted as resource ID and the default resource group will be used.Message- Specified by:
getResourcein interfaceApplicationContext- Parameters:
resID- the resource ID- Returns:
- the found resource
- Throws:
MissingResourceException- if the resource cannot be found
-
getResourceText
public String getResourceText(Object groupID, Object resID)
Convenience method for looking up the text of a resource specified as group and resource ID.- Specified by:
getResourceTextin interfaceApplicationContext- Parameters:
groupID- the resource group IDresID- the resource ID- Returns:
- the found resource text
- Throws:
MissingResourceException- if the resource cannot be found
-
getResourceText
public String getResourceText(Message msg)
Convenience method for looking up the text of a resource specified as aMessageobject.- Specified by:
getResourceTextin interfaceApplicationContext- Parameters:
msg- defines the resource (must not be null)- Returns:
- the found resource
- Throws:
MissingResourceException- if the resource cannot be foundIllegalArgumentException- if the message is undefined
-
getResourceText
public String getResourceText(Object resID)
Convenience method for looking up the text of a specified resource. This method works analogous togetResourceText(Object), especially the passed in object can be an instance ofMessage.- Specified by:
getResourceTextin interfaceApplicationContext- Parameters:
resID- defines the requested resource- Returns:
- the found resource
- Throws:
MissingResourceException- if the resource cannot be found
-
messageBox
public int messageBox(Object resMsg, Object resTitle, int msgType, int btnType)
A convenience method for displaying a message box. This method invokes the application's associatedMessageOutputobject. Before that the passed in resource IDs (which can be either resource IDs or instances of theMessageclass) will be resolved.- Specified by:
messageBoxin interfaceApplicationContext- Parameters:
resMsg- the resource defining the message to be displayedresTitle- the resource defining the message box's title (can be null)msgType- the message type (one of theMESSAGE_XXXconstants ofMessageOutput)btnType- the button type (one of theBTN_XXXconstants ofMessageOutput)- Returns:
- the message box's return value (one of the
RET_XXXconstants ofMessageOutput) - See Also:
MessageOutput
-
getGUISynchronizer
public GUISynchronizer getGUISynchronizer()
Returns theGUISynchronizer. This implementation obtains the synchronizer from the bean context.- Specified by:
getGUISynchronizerin interfaceApplicationContext- Returns:
- the
GUISynchronizer
-
getMainWindow
public Window getMainWindow()
Returns the application's main window.- Specified by:
getMainWindowin interfaceApplicationContext- Returns:
- the main window of this application
-
setMainWindow
public void setMainWindow(Window mainWindow)
Allows to set the application's main window.- Specified by:
setMainWindowin interfaceApplicationContext- Parameters:
mainWindow- the new main window
-
getActionStore
public ActionStore getActionStore()
Returns the application'sActionStore.- Specified by:
getActionStorein interfaceApplicationContext- Returns:
- the application's action store
-
setActionStore
public void setActionStore(ActionStore actionStore)
Sets the application'sActionStore. This object contains the definitions for all top level actions known to the application.- Specified by:
setActionStorein interfaceApplicationContext- Parameters:
actionStore- the new action store
-
newBuilder
public Builder newBuilder()
Returns a newinstance. This implementation obtains the builder instance from the global bean context.Builder- Specified by:
newBuilderin interfaceApplicationContext- Returns:
- the new
Builderinstance
-
initBuilderData
public ApplicationBuilderData initBuilderData()
Returns an initializedApplicationBuilderDataobject that can be used for calling the GUI builder. Most of the properties of the returned object are already set to default values, so only specific settings must be performed. The following properties have already been initialized with information available directly in this object or from the configuration:- the parent
BeanContext - the default resource group
- the menu icon flag
- the tool bar text flag
- the transformer context
- the parent window
- the action store: Here the following strategy is used: if the
ActionStoremanaged by this object is empty, it is directly used. Otherwise a newActionStoreinstance is created with the managedActionStoreas parent. This has the effect that the first builder script will populate the global action store. Further scripts use their own store. - the
MessageOutputobject - the
CommandQueue - the
BindingStrategy - a
BeanCreationListeneris set that can inject a reference to the centralApplicationobject into beans implementing theApplicationClientinterface
- Specified by:
initBuilderDatain interfaceApplicationContext- Returns:
- an initialized GUI builder parameter object
- the parent
-
getTypedProperty
public <T> T getTypedProperty(Class<T> propCls)
Returns the value of the specified typed property or null if it cannot be found.- Specified by:
getTypedPropertyin interfaceTransformerContext- Type Parameters:
T- the type of the property- Parameters:
propCls- the property class- Returns:
- the value of this typed property
-
setTypedProperty
public <T> void setTypedProperty(Class<T> propCls, T value)
Sets the value of the given typed property. Note: This method is thread-safe. It ensures proper synchronization so that the property is visible to other threads, too.- Specified by:
setTypedPropertyin interfaceApplicationContext- Type Parameters:
T- the type of the property- Parameters:
propCls- the property class (must not be null)value- the new value (null for clearing this property)- Throws:
IllegalArgumentException- if the property class is null
-
fetchBindingStrategy
protected BindingStrategy fetchBindingStrategy()
Obtains theBindingStrategyfor a builder operation. This method is invoked byinitBuilderData()for populating thebindingStrategyproperty of theApplicationBuilderDataobject. The default algorithm looks up theBindingStrategyfrom the globalBeanContext.- Returns:
- the
BindingStrategy
-
-