qflib 0.98.1

de.qfs.lib.gui
Class ModalDialog

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--java.awt.Dialog
                          |
                          +--javax.swing.JDialog
                                |
                                +--de.qfs.lib.gui.ModalDialog
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.event.ActionListener, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, javax.swing.RootPaneContainer, java.io.Serializable, javax.swing.WindowConstants, java.awt.event.WindowListener
Direct Known Subclasses:
OptionDialog

public class ModalDialog
extends javax.swing.JDialog
implements java.awt.event.ActionListener, java.awt.event.WindowListener

This is a very useful base class for modal dialogs that display one large component - or several components grouped into a large one - above a row of buttons. ModalDialog reads the number and labels of the buttons as well as some of their characteristics from a set of resources, simplifying localization together with layout, default actions, and return value definition.

The following resources are used by default. These may be extended or ignored by derived classes.

Resource name Usage
Dialog resources
dialog.name.title The title of the dialog (optional, no default).
dialog.name.name The name of the dialog (optional, default ModalDialog-name).
dialog.name.width The minimum width of the dialog (optional, no default).
dialog.name.height The minimum height of the dialog (optional, no default).
dialog.name.configure If true, create a ConfigWrapper for the dialog and register it under the name name.dialog (optional, default false).
dialog.name.buttons The resource names of the buttons for the dialog (optional, default "ok cancel" ).
Button resources
dialog.button.name.label The label of the button (required).
dialog.button.name.mnemonic Keyboard accelerator (optional, default none).
dialog.button.name.name The name of the button set with setName (optional, default "Button-name").
dialog.button.name.close Whether to close the dialog when the button is pressed (optional, default true).
dialog.button.name.retval The return value for doModal when the dialog is closed via this button. Ignored unless close is true (optional, default is the index of the button, starting from 0) .
dialog.button.name.default Whether the button is default capable. The first button with default set to true will be the initial default button (optional, default false) .
dialog.button.name.disabled If true, the button will be disabled (optional, default false) .

Default values for the buttons ok, yes, no and cancel are defined in the resource bundle /de/qfs/lib/resources/properties/qflib in qflib.jar. You should include these in the resources you pass to init.

Since:
0.98.0
Version:
$Revision: 1.12 $
Author:
Gregor Schmid
See Also:
Serialized Form

Inner Class Summary
static class ModalDialog.ButtonData
          A structure that encapsulates the values for a button.
 
Inner classes inherited from class javax.swing.JDialog
javax.swing.JDialog.AccessibleJDialog
 
Inner classes inherited from class java.awt.Dialog
java.awt.Dialog.AccessibleAWTDialog
 
Inner classes inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Field Summary
protected  ModalDialog.ButtonData[] buttons
          Data for the buttons of the dialog.
static int CANCEL_OPTION
          Return value for the cancel button.
static int CLOSED_OPTION
          Retun value when the dialog is closed.
protected static java.lang.String[] defaultButtons
          The default button names.
protected  java.awt.GridBagLayout layout
          The layout for the dialog.
protected  java.awt.Component mainComponent
          The main component of the dialog.
protected  javax.swing.JPanel mainPanel
          The panel for the main component.
protected  int minHeight
          The minimum height of the dialog.
protected  int minWidth
          The minimum width of the dialog.
protected  java.lang.String name
          The resource name of the dialog.
static int NO_OPTION
          Return value for the no button.
static int OK_OPTION
          Return value for the ok button.
protected  javax.swing.JPanel panel
          The content pane of the dialog.
protected  int retval
          The return code from the last call to doModal.
static int YES_OPTION
          Return value for the yes button.
 
Fields inherited from class javax.swing.JDialog
accessibleContext, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ModalDialog(java.lang.String name, java.awt.Dialog parent)
          Create a new ModalDialog.
ModalDialog(java.lang.String name, java.awt.Dialog parent, java.awt.Component com)
          Create a new ModalDialog.
ModalDialog(java.lang.String name, java.awt.Frame parent)
          Create a new ModalDialog.
ModalDialog(java.lang.String name, java.awt.Frame parent, java.awt.Component com)
          Create a new ModalDialog.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Handler for a button press.
static java.lang.String buttonResName(java.lang.String button, java.lang.String res)
          Helper method that uses a StringBuffer to build a resource name for one of the dialog's buttons.
 int doModal(java.awt.Component parent, boolean resetFocus)
          Bring up the dialog and wait for the user to finish editing.
protected  int findButton(javax.swing.JButton button)
          Get the index of a button.
protected  void fixSize()
          Pack the dialog and enlarge it if necessary for the minimum width and height.
 javax.swing.JButton getButton(int index)
          Get a button of the dialog.
 javax.swing.JButton getButton(java.lang.String name)
          Get a button of the dialog.
 int getButtonCount()
          Get the number of buttons of the dialog.
 void init(MapResourceBundle rb)
          Initialize the dialog and the buttons.
protected  ModalDialog.ButtonData initButton(java.lang.String bname, MapResourceBundle rb)
          Initialize one button.
protected  java.lang.String[] initButtonNames(MapResourceBundle rb)
          Get the names of the dialog's buttons.
protected  void initComponent()
          This method can be overridden in a derived class to initialize the mainComponent member of the dialog.
 java.lang.String resName(java.lang.String res)
          Helper method that uses a StringBuffer to build a resource name for the dialog.
 void setMainComponent(java.awt.Component com)
          Set the main component of the dialog.
 void windowActivated(java.awt.event.WindowEvent e)
          Noop.
 void windowClosed(java.awt.event.WindowEvent e)
          Noop.
 void windowClosing(java.awt.event.WindowEvent e)
          Cancellation handler.
 void windowDeactivated(java.awt.event.WindowEvent e)
          Noop.
 void windowDeiconified(java.awt.event.WindowEvent e)
          Noop.
 void windowIconified(java.awt.event.WindowEvent e)
          Noop.
 void windowOpened(java.awt.event.WindowEvent e)
          Noop.
 
Methods inherited from class javax.swing.JDialog
addImpl, createRootPane, dialogInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, processKeyEvent, processWindowEvent, remove, setContentPane, setDefaultCloseOperation, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setLocationRelativeTo, setRootPane, setRootPaneCheckingEnabled, update
 
Methods inherited from class java.awt.Dialog
addNotify, dispose, getTitle, hide, isModal, isResizable, setModal, setResizable, setTitle, show
 
Methods inherited from class java.awt.Window
addWindowListener, applyResourceBundle, applyResourceBundle, finalize, getFocusOwner, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getOwnedWindows, getOwner, getToolkit, getWarningString, isShowing, pack, postEvent, processEvent, removeWindowListener, setCursor, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, removeNotify, setFont, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputMethodRequests, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CLOSED_OPTION

public static final int CLOSED_OPTION
Retun value when the dialog is closed.

OK_OPTION

public static final int OK_OPTION
Return value for the ok button.

YES_OPTION

public static final int YES_OPTION
Return value for the yes button.

NO_OPTION

public static final int NO_OPTION
Return value for the no button.

CANCEL_OPTION

public static final int CANCEL_OPTION
Return value for the cancel button.

defaultButtons

protected static final java.lang.String[] defaultButtons
The default button names.

name

protected java.lang.String name
The resource name of the dialog.

retval

protected int retval
The return code from the last call to doModal.

mainComponent

protected java.awt.Component mainComponent
The main component of the dialog.

mainPanel

protected javax.swing.JPanel mainPanel
The panel for the main component.

buttons

protected ModalDialog.ButtonData[] buttons
Data for the buttons of the dialog.

layout

protected java.awt.GridBagLayout layout
The layout for the dialog.

panel

protected javax.swing.JPanel panel
The content pane of the dialog.

minWidth

protected int minWidth
The minimum width of the dialog.

minHeight

protected int minHeight
The minimum height of the dialog.
Constructor Detail

ModalDialog

public ModalDialog(java.lang.String name,
                   java.awt.Frame parent)
Create a new ModalDialog. Don't forget to call init afterwards.
Parameters:
name - The resource name of the dialog.
parent - The dialog's owner.

ModalDialog

public ModalDialog(java.lang.String name,
                   java.awt.Frame parent,
                   java.awt.Component com)
Create a new ModalDialog. Don't forget to call init afterwards.
Parameters:
name - The resource name of the dialog.
parent - The dialog's owner.
com - The main component for the dialog.

ModalDialog

public ModalDialog(java.lang.String name,
                   java.awt.Dialog parent)
Create a new ModalDialog. This constructor is only available for JDK 1.2 and above. Don't forget to call init afterwards.
Parameters:
name - The resource name of the dialog.
parent - The dialog's owner.

ModalDialog

public ModalDialog(java.lang.String name,
                   java.awt.Dialog parent,
                   java.awt.Component com)
Create a new ModalDialog. This constructor is only available for JDK 1.2 and above. Don't forget to call init afterwards.
Parameters:
name - The resource name of the dialog.
parent - The dialog's owner.
com - The main component for the dialog.
Method Detail

init

public void init(MapResourceBundle rb)
Initialize the dialog and the buttons. This method is not called directly by the constructor to give the constructor of a derived class the chance to run first.
Parameters:
rb - The resources to use.

setMainComponent

public void setMainComponent(java.awt.Component com)
Set the main component of the dialog.
Parameters:
com - The new main component.

doModal

public int doModal(java.awt.Component parent,
                   boolean resetFocus)
Bring up the dialog and wait for the user to finish editing. If the user selects a button with the close property set, the dialog will be closed.
Parameters:
parent - Place the dialog relative to this component if non-null.
resetFocus - Whether to reset the focus to the first child component of the dialog.
Returns:
-1 if the dialog was canceled, 0 for the first button, 1 for the second button and so on. If the selected button's return code is specified directly by a resource, that value is returned instead.

getButtonCount

public int getButtonCount()
Get the number of buttons of the dialog.
Returns:
The number of buttons.

getButton

public javax.swing.JButton getButton(int index)
                              throws java.lang.IndexOutOfBoundsException
Get a button of the dialog.
Parameters:
index - The index of the button.
Returns:
The button at the specified index.
Throws:
java.lang.IndexOutOfBoundsException - If there is no button for that index.

getButton

public javax.swing.JButton getButton(java.lang.String name)
Get a button of the dialog.
Parameters:
name - The resource name of the button.
Returns:
The button with the given name or null if none is found.

initButtonNames

protected java.lang.String[] initButtonNames(MapResourceBundle rb)
Get the names of the dialog's buttons. The default action is to read the names from the resources.
Parameters:
rb - The resources to use.
Returns:
The button names.

initButton

protected ModalDialog.ButtonData initButton(java.lang.String bname,
                                            MapResourceBundle rb)
Initialize one button.
Parameters:
bname - The resource name of the button.
rb - The resources to use.
Returns:
The initialized button.

initComponent

protected void initComponent()
This method can be overridden in a derived class to initialize the mainComponent member of the dialog. It will be placed a small distance above the buttons, filling all the remaining space.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Handler for a button press. If the button's close property is set, its return value is determined and the dialog is closed.
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - The event containing the details.

windowClosing

public void windowClosing(java.awt.event.WindowEvent e)
Cancellation handler. The return value is set to -1 and the dialog is closed.
Specified by:
windowClosing in interface java.awt.event.WindowListener
Parameters:
e - The event containing the details.

windowOpened

public void windowOpened(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowOpened in interface java.awt.event.WindowListener

windowClosed

public void windowClosed(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowClosed in interface java.awt.event.WindowListener

windowActivated

public void windowActivated(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowActivated in interface java.awt.event.WindowListener

windowDeactivated

public void windowDeactivated(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowDeactivated in interface java.awt.event.WindowListener

windowIconified

public void windowIconified(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowIconified in interface java.awt.event.WindowListener

windowDeiconified

public void windowDeiconified(java.awt.event.WindowEvent e)
Noop.
Specified by:
windowDeiconified in interface java.awt.event.WindowListener

fixSize

protected void fixSize()
Pack the dialog and enlarge it if necessary for the minimum width and height.

resName

public java.lang.String resName(java.lang.String res)
Helper method that uses a StringBuffer to build a resource name for the dialog. This method should be protected but is public due to a compiler bug in javac.
Parameters:
res - The resource needed.
Returns:
The whole resource name: "dialog.name.res".

buttonResName

public static java.lang.String buttonResName(java.lang.String button,
                                             java.lang.String res)
Helper method that uses a StringBuffer to build a resource name for one of the dialog's buttons. This method should be protected but is public due to a compiler bug in javac.
Parameters:
button - The resource name of the button.
res - The resource needed.
Returns:
The whole resource name: "dialog.button.button.res".

findButton

protected int findButton(javax.swing.JButton button)
Get the index of a button.
Parameters:
button - The button to look for.
Returns:
The index of the button or -1 if not found.

qflib 0.98.1