Class AbstractDialogOptions<T,S extends AbstractDialogOptions<T,S>>
- java.lang.Object
-
- net.sf.jguiraffe.gui.dlg.AbstractDialogOptions<T,S>
-
- Type Parameters:
T
- the type of the dialog resultS
- the self type returned by fluent methods
- Direct Known Subclasses:
AbstractFileChooserOptions
,DirectoryChooserOptions
public abstract class AbstractDialogOptions<T,S extends AbstractDialogOptions<T,S>> extends Object
An abstract base class for options classes for standard dialogs.
This class defines a set of properties that are common for all kinds of dialogs. These are the following:
- A
DialogResultCallback
to be invoked with the result of the dialog. - A title for the dialog (either as string or resource ID).
- An optional
DialogCanceledCallback
to be invoked when the dialog is canceled.
The class offers a fluent interface for setting the single options defining the associated dialog. This can be extended by concrete sub classes. As updates of properties happen in-place, instances are not thread-safe. The use case assumed is that an instance is created, populated, and passed to a dialog service - all by the same thread.
When invoking callbacks - either for the dialog result or the cancel notification - it is possible to pass a data object with additional context information. This class manages the data object and provides functionality to invoke a callback without having it at hand. So implementation classes for dialog services do not have to bother.
- Since:
- 1.4
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractDialogOptions(DialogResultCallback<T,?> resultCallback)
Creates a new instance ofAbstractDialogOptions
and initializes it with the result callback.protected
AbstractDialogOptions(DialogResultCallback<T,D> resultCallback, D data)
Creates a new instance ofAbstractDialogOptions
and initializes it with the result callback and a data object to be passed to the callback when it is invoked.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Runnable
getCancelInvoker()
Returns aRunnable
that invokes the canceled callback assigned to this options instance.DialogResultCallback<T,Void>
getResultCallback()
Returns the callback to propagate the dialog result.protected abstract S
getSelf()
Returns a self reference to this object.String
getTitle()
Returns the title that has been set for the dialog.Object
getTitleResource()
Returns the title resource that has been set for the dialog.String
resolveTitle(ApplicationContext applicationContext)
Resolves the title for the dialog using the given application context.S
setCanceledCallback(DialogCanceledCallback<?> canceledCallback)
Sets a callback to be invoked when the dialog is canceled.<D> S
setCanceledCallback(DialogCanceledCallback<D> canceledCallback, D data)
Sets a callback to be invoked when the dialog is canceled with a context data object.S
setTitle(String title)
Allows setting a title for the dialog as string.S
setTitleResource(Object resource)
Allows setting the title for the dialog as a resource ID.
-
-
-
Constructor Detail
-
AbstractDialogOptions
protected AbstractDialogOptions(DialogResultCallback<T,D> resultCallback, D data)
Creates a new instance ofAbstractDialogOptions
and initializes it with the result callback and a data object to be passed to the callback when it is invoked.- Type Parameters:
D
- the type of the data object for the result callback- Parameters:
resultCallback
- the result callbackdata
- the context data for the result callback- Throws:
IllegalArgumentException
- if the result callback is null
-
AbstractDialogOptions
protected AbstractDialogOptions(DialogResultCallback<T,?> resultCallback)
Creates a new instance ofAbstractDialogOptions
and initializes it with the result callback. No data object will be passed to this callback when it is invoked. (It is assumed that the callback can handle a null data object.)- Parameters:
resultCallback
- the result callback- Throws:
IllegalArgumentException
- if the result callback is null
-
-
Method Detail
-
getResultCallback
public DialogResultCallback<T,Void> getResultCallback()
Returns the callback to propagate the dialog result. Note that the callback returned by this method is not the same as passed to the constructor. It is a callback that wraps the original callback and takes care that the correct data object is passed. Callers just have to provide the result and pass null for the data object; the implementation calls the underlying callback with the correct data object.- Returns:
- a callback for propagating the dialog result
-
setCanceledCallback
public <D> S setCanceledCallback(DialogCanceledCallback<D> canceledCallback, D data)
Sets a callback to be invoked when the dialog is canceled with a context data object. This object is passed to the callback when it is invoked.- Type Parameters:
D
- the type of the data object- Parameters:
canceledCallback
- the canceled callbackdata
- a data object to be passed to the callback- Returns:
- this object
-
setCanceledCallback
public S setCanceledCallback(DialogCanceledCallback<?> canceledCallback)
Sets a callback to be invoked when the dialog is canceled. The callback does not expect a data object; it will be therefore invoked with a null reference.- Parameters:
canceledCallback
- the canceled callback- Returns:
- this object
-
getCancelInvoker
public Runnable getCancelInvoker()
Returns aRunnable
that invokes the canceled callback assigned to this options instance. It is ensured that the correct data object is passed to the callback. If no canceled callback has been set, aRunnable
is returned whose invocation does not have any effect.- Returns:
- a
Runnable
to invoke the canceled callback
-
setTitle
public S setTitle(String title)
Allows setting a title for the dialog as string.- Parameters:
title
- the dialog title- Returns:
- this object
-
getTitle
public String getTitle()
Returns the title that has been set for the dialog. This method returns the string that has been set usingsetTitle(String)
or null if no title has been set.- Returns:
- the title string or null
-
setTitleResource
public S setTitleResource(Object resource)
Allows setting the title for the dialog as a resource ID. The passed in object is resolved usingApplicationContext.getResourceText(Object)
.- Parameters:
resource
- the title resource- Returns:
- this object
-
getTitleResource
public Object getTitleResource()
Returns the title resource that has been set for the dialog. This method returns the object that has been set usingsetTitleResource(Object)
or null if not title resource has been set.- Returns:
- the title resource or null
-
resolveTitle
public String resolveTitle(ApplicationContext applicationContext)
Resolves the title for the dialog using the given application context. If the title has been set directly using thesetTitle(String)
method, it is returned directly. If it has been set as a resource, the resource is resolved, and the result is returned. If no title has been specified, result is null.- Parameters:
applicationContext
- the application context- Returns:
- the resolved title (can be null)
-
getSelf
protected abstract S getSelf()
Returns a self reference to this object. This is used to support method chaining when setting multiple properties.- Returns:
- a self reference
-
-