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
ApplicationContext
interface.This class is used by the
Application
class to store global information that is needed by different components of an application. Because anApplicationContext
implementation also implements theTransformerContext
interface 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 ofApplicationContextImpl
and sets the current locale.ApplicationContextImpl(Locale locale, ResourceManager resMan)
Creates a new instance ofApplicationContextImpl
and sets the current locale and the resource manager to use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BindingStrategy
fetchBindingStrategy()
Obtains theBindingStrategy
for a builder operation.ActionStore
getActionStore()
Returns the application'sActionStore
.BeanContext
getBeanContext()
Returns the global bean context.ClassLoaderProvider
getClassLoaderProvider()
Returns theClassLoaderProvider
.org.apache.commons.configuration.Configuration
getConfiguration()
Returns a reference to the global configuration.GUISynchronizer
getGUISynchronizer()
Returns theGUISynchronizer
.Locale
getLocale()
Returns the current locale.Window
getMainWindow()
Returns the application's main window.MessageOutput
getMessageOutput()
Returns a reference to the object for displaying messages.Object
getResource(Object resID)
Convenience method for looking up a resource.Object
getResource(Object groupID, Object resID)
Convenience method for looking up a resource specified as group and resource ID.Object
getResource(Message msg)
Convenience method for looking up a resource that is specified as aMessage
object.ResourceManager
getResourceManager()
Returns the actual resource manager.String
getResourceText(Object resID)
Convenience method for looking up the text of a specified resource.String
getResourceText(Object groupID, Object resID)
Convenience method for looking up the text of a resource specified as group and resource ID.String
getResourceText(Message msg)
Convenience method for looking up the text of a resource specified as aMessage
object.<T> T
getTypedProperty(Class<T> propCls)
Returns the value of the specified typed property or null if it cannot be found.ValidationMessageHandler
getValidationMessageHandler()
Returns theValidationMessageHandler
.ApplicationBuilderData
initBuilderData()
Returns an initializedApplicationBuilderData
object that can be used for calling the GUI builder.int
messageBox(Object resMsg, Object resTitle, int msgType, int btnType)
A convenience method for displaying a message box.Builder
newBuilder()
Returns a new
instance.Builder
Map<String,Object>
properties()
Returns a map with properties maintained by this context.void
setActionStore(ActionStore actionStore)
Sets the application'sActionStore
.void
setBeanContext(BeanContext beanContext)
Sets the global bean context.void
setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
.void
setConfiguration(org.apache.commons.configuration.Configuration configuration)
Sets the global configuration.void
setLocale(Locale locale)
Sets the current locale.void
setMainWindow(Window mainWindow)
Allows to set the application's main window.void
setMessageOutput(MessageOutput msg)
Sets the message output object to be used by this application.void
setResourceManager(ResourceManager resourceManager)
Sets the resource manager.<T> void
setTypedProperty(Class<T> propCls, T value)
Sets the value of the given typed property.void
setValidationMessageHandler(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 ofApplicationContextImpl
and sets the current locale.- Parameters:
locale
- theLocale
-
ApplicationContextImpl
public ApplicationContextImpl(Locale locale, ResourceManager resMan)
Creates a new instance ofApplicationContextImpl
and sets the current locale and the resource manager to use.- Parameters:
locale
- theLocale
resMan
- theResourceManager
to 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:
getLocale
in 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:
setLocale
in interfaceApplicationContext
- Parameters:
locale
- the newLocale
-
getResourceManager
public ResourceManager getResourceManager()
Returns the actual resource manager.- Specified by:
getResourceManager
in interfaceTransformerContext
- Returns:
- the
ResourceManager
for accessing resources
-
setResourceManager
public void setResourceManager(ResourceManager resourceManager)
Sets the resource manager. Access to system resources will be handled by this object.- Specified by:
setResourceManager
in interfaceApplicationContext
- Parameters:
resourceManager
- theResourceManager
-
properties
public Map<String,Object> properties()
Returns a map with properties maintained by this context.- Specified by:
properties
in interfaceTransformerContext
- Returns:
- a map with properties
-
getConfiguration
public org.apache.commons.configuration.Configuration getConfiguration()
Returns a reference to the global configuration.- Specified by:
getConfiguration
in interfaceApplicationContext
- Returns:
- the configuration object
-
setConfiguration
public void setConfiguration(org.apache.commons.configuration.Configuration configuration)
Sets the global configuration.- Specified by:
setConfiguration
in interfaceApplicationContext
- Parameters:
configuration
- the configuration object
-
getBeanContext
public BeanContext getBeanContext()
Returns the global bean context.- Specified by:
getBeanContext
in 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 theClassLoaderProvider
managed by the currentBeanContext
.- Specified by:
getClassLoaderProvider
in interfaceApplicationContext
- Returns:
- the
ClassLoaderProvider
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
. This implementation does not store theClassLoaderProvider
in a separate member field. Rather, it is passed to the currentBeanContext
. To avoid inconsistencies when loading classes there must be only a singleClassLoaderProvider
instance. If this method is called before aBeanContext
was set, anIllegalStateException
exception is thrown.- Specified by:
setClassLoaderProvider
in interfaceApplicationContext
- Parameters:
classLoaderProvider
- the new class loader provider- Throws:
IllegalStateException
- if noBeanContext
has been set
-
getValidationMessageHandler
public ValidationMessageHandler getValidationMessageHandler()
Returns theValidationMessageHandler
.- Specified by:
getValidationMessageHandler
in 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:
getMessageOutput
in 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:
setMessageOutput
in interfaceApplicationContext
- Parameters:
msg
- the newMessageOutput
object
-
getResource
public Object getResource(Object groupID, Object resID)
Convenience method for looking up a resource specified as group and resource ID.- Specified by:
getResource
in 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 aMessage
object.- Specified by:
getResource
in 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:
getResource
in 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:
getResourceText
in 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 aMessage
object.- Specified by:
getResourceText
in 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:
getResourceText
in 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 associatedMessageOutput
object. Before that the passed in resource IDs (which can be either resource IDs or instances of theMessage
class) will be resolved.- Specified by:
messageBox
in 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_XXX
constants ofMessageOutput
)btnType
- the button type (one of theBTN_XXX
constants ofMessageOutput
)- Returns:
- the message box's return value (one of the
RET_XXX
constants ofMessageOutput
) - See Also:
MessageOutput
-
getGUISynchronizer
public GUISynchronizer getGUISynchronizer()
Returns theGUISynchronizer
. This implementation obtains the synchronizer from the bean context.- Specified by:
getGUISynchronizer
in interfaceApplicationContext
- Returns:
- the
GUISynchronizer
-
getMainWindow
public Window getMainWindow()
Returns the application's main window.- Specified by:
getMainWindow
in interfaceApplicationContext
- Returns:
- the main window of this application
-
setMainWindow
public void setMainWindow(Window mainWindow)
Allows to set the application's main window.- Specified by:
setMainWindow
in interfaceApplicationContext
- Parameters:
mainWindow
- the new main window
-
getActionStore
public ActionStore getActionStore()
Returns the application'sActionStore
.- Specified by:
getActionStore
in 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:
setActionStore
in interfaceApplicationContext
- Parameters:
actionStore
- the new action store
-
newBuilder
public Builder newBuilder()
Returns a new
instance. This implementation obtains the builder instance from the global bean context.Builder
- Specified by:
newBuilder
in interfaceApplicationContext
- Returns:
- the new
Builder
instance
-
initBuilderData
public ApplicationBuilderData initBuilderData()
Returns an initializedApplicationBuilderData
object 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
ActionStore
managed by this object is empty, it is directly used. Otherwise a newActionStore
instance is created with the managedActionStore
as parent. This has the effect that the first builder script will populate the global action store. Further scripts use their own store. - the
MessageOutput
object - the
CommandQueue
- the
BindingStrategy
- a
BeanCreationListener
is set that can inject a reference to the centralApplication
object into beans implementing theApplicationClient
interface
- Specified by:
initBuilderData
in 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:
getTypedProperty
in 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:
setTypedProperty
in 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 theBindingStrategy
for a builder operation. This method is invoked byinitBuilderData()
for populating thebindingStrategy
property of theApplicationBuilderData
object. The default algorithm looks up theBindingStrategy
from the globalBeanContext
.- Returns:
- the
BindingStrategy
-
-