com.abstractics.xmlpanel.swing
Class XmlPanel

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended bycom.abstractics.xmlpanel.swing.XmlPanel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.event.ActionListener, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.swing.SwingConstants

public class XmlPanel
extends javax.swing.JPanel
implements java.awt.event.ActionListener, javax.swing.SwingConstants

This subclass of JPanel provides the functionality for the layout to be defined via XML. This class must be created through the create() factory method.

There are various setXml() methods that can then be called to set the XML for they layout.

This class also implements ActionListener The root XmlPanel is added as an ActionListener to every AbstractButton object that is contained in the panel. This allows the developer to add ActionListeners to the XmlPanel that will get the action events for every button contained in the panel.

Version:
$Id: XmlPanel.java,v 1.3 2006/03/15 05:37:43 andyman99 Exp $
Author:
Andrew Lawrence, Copyright 2006 Abstractics, L.L.C., Licensed under the Apache License, Version 2.0
See Also:
XmlLayout, Serialized Form

Nested Class Summary
static class XmlPanel.XmlPanelComponentCreator
          ComponentCreator that creates XmlPanels.
 
Nested classes inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
XmlPanel()
          Public empty constructor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Passes on the action performed to all of our ActionListeners
Note: If this method is overridden in a subclass, it is important to call the super method, so that anything else that registers to listen will be notified.
 void addActionListener(java.awt.event.ActionListener l)
          Adds an ActionListener to the panel.
static XmlPanel create()
          Deprecated. Factory method is no longer needed. Use the XmlPanel() constructor.
 java.awt.event.ActionListener[] getActionListeners()
          Returns an array of all the ActionListeners added to this panel with addActionListener().
 javax.swing.ImageIcon getBackgroundImage()
          Gets the current background image, or null if it has not been specified
 int getBackgroundImageAlign()
          Returns the current background image alignment Alignment values come from SwingConstants such as CENTER, NORTHWEST, etc.
Default value is SwingConstants.NORTHWEST
 java.awt.Component getComponentNamed(java.lang.String name)
          Returns the first component found in the panel with the given name.
 org.w3c.dom.Element getElement()
           
 javax.swing.JButton getJButton(java.lang.String name)
          Convenience method for finding a specific JButton in the panel with the given name.
 javax.swing.JCheckBox getJCheckBox(java.lang.String name)
          Convenience method for finding a specific JCheckBox in the panel with the given name.
 javax.swing.JComboBox getJComboBox(java.lang.String name)
          Convenience method for finding a specific JComboBox in the panel with the given name.
 javax.swing.JLabel getJLabel(java.lang.String name)
          Convenience method for finding a specific JLabel in the panel with the given name.
 javax.swing.JRadioButton getJRadioButton(java.lang.String name)
          Convenience method for finding a specific JRadioButton in the panel with the given name.
 javax.swing.JTextArea getJTextArea(java.lang.String name)
          Convenience method for finding a specific JTextArea in the panel with the given name.
 javax.swing.JTextField getJTextField(java.lang.String name)
          Convenience method for finding a specific JTextField in the panel with the given name.
 XmlPanel getParentXmlPanel()
           
 XmlPanel getRootXmlPanel()
           
 boolean isBackgroundImageTileX()
          Returns true if the background image is set to be tiled horizontally.
 boolean isBackgroundImageTileY()
          Returns true if the background image is set to be tiled vertically.
 boolean isRootXmlPanel()
           
static void main(java.lang.String[] args)
          Running this object results in a simple display of a panel in a window that can then be used to design an initial layout
 void paint(java.awt.Graphics g)
          Overridden to draw the cell border lines when in edit mode.
protected  void paintComponent(java.awt.Graphics g)
          Overriden to handle the painting of the background image, if there is one
 void removeActionListener(java.awt.event.ActionListener l)
          Removes an ActionListener from the panel.
 void setAllowEditorDialog(boolean allowEditorDialog)
          Controls if the panel allows double-right-click editing on the panel.

A System property for xmlpanel.allowEditorDialog can also be set to true which will cause all panels to allow the editing, regardless of this setting.
 void setBackgroundImage(javax.swing.ImageIcon image)
          Sets an image that should be painted one the background of the panel.
 void setBackgroundImageAlign(int align)
          If a background image has been specified using setBackgroundImage(ImageIcon) and isBackgroundImageTileX() is false, then this setting can be used to control the placement of the image on the panel.
Use SwingConstants alignment variables (CENTER, NORTHWEST, etc.) to specify the alignment.
Default setting is SwingConstants.NORTHWEST
 void setBackgroundImageAlign(java.lang.String align)
          Provided so the alignment can be set from XML.
 void setBackgroundImageTileX(boolean tiled)
          If a background image has been specified using setBackgroundImage(ImageIcon), then setting this property to true will cause the image to be repeatedly painted from left to right, and the vertical placement will be determined by getBackgroundImageAlign() - NORTH, CENTER, or SOUTH.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.
Default setting is false.
 void setBackgroundImageTileY(boolean tiled)
          If a background image has been specified using setBackgroundImage(ImageIcon), then setting this property to true will cause the image to be repeatedly painted from top to bottom, and the horizontal placement will be determined by getBackgroundImageAlign() - WEST, CENTER, or EAST.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.
Default setting is false.
 void setXml(java.io.File file)
           
 void setXml(java.io.InputStream stream)
           
 void setXml(java.io.Reader reader)
           
 void setXml(java.lang.String xml)
           
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XmlPanel

public XmlPanel()
Public empty constructor. Will create a blank XML panel. Use one of the setXml methods after construction to change the layout.

Method Detail

create

public static XmlPanel create()
Deprecated. Factory method is no longer needed. Use the XmlPanel() constructor.

Factory method to create an XmlPanel object. Creates an initial XmlPanel with empty XML.

Returns:

main

public static void main(java.lang.String[] args)
Running this object results in a simple display of a panel in a window that can then be used to design an initial layout

Parameters:
args -

setBackgroundImage

public void setBackgroundImage(javax.swing.ImageIcon image)
Sets an image that should be painted one the background of the panel. Painting will occur by painting the panel background first (if opaque) then painting the image, then painting all of the components.

Parameters:
image - image to paint in the background

getBackgroundImage

public javax.swing.ImageIcon getBackgroundImage()
Gets the current background image, or null if it has not been specified

Returns:
the current background image

setBackgroundImageTileX

public void setBackgroundImageTileX(boolean tiled)
If a background image has been specified using setBackgroundImage(ImageIcon), then setting this property to true will cause the image to be repeatedly painted from left to right, and the vertical placement will be determined by getBackgroundImageAlign() - NORTH, CENTER, or SOUTH.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.
Default setting is false.

Parameters:
tiled - set to true to tile the background image horizontally

setBackgroundImageTileY

public void setBackgroundImageTileY(boolean tiled)
If a background image has been specified using setBackgroundImage(ImageIcon), then setting this property to true will cause the image to be repeatedly painted from top to bottom, and the horizontal placement will be determined by getBackgroundImageAlign() - WEST, CENTER, or EAST.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.
Default setting is false.

Parameters:
tiled - set to true to tile the background image vertically

setBackgroundImageAlign

public void setBackgroundImageAlign(int align)
If a background image has been specified using setBackgroundImage(ImageIcon) and isBackgroundImageTileX() is false, then this setting can be used to control the placement of the image on the panel.
Use SwingConstants alignment variables (CENTER, NORTHWEST, etc.) to specify the alignment.
Default setting is SwingConstants.NORTHWEST

Parameters:
align - the alignment to use for the background image

setBackgroundImageAlign

public void setBackgroundImageAlign(java.lang.String align)
Provided so the alignment can be set from XML. The String value is converted to a SwingConstants value using SwingUtils.convertStringAlign(String, int) and then is passed to the setBackgroundImageAlign(int) method.

Parameters:
align - the alignment to use for the background image

setXml

public void setXml(java.lang.String xml)
            throws XmlException
Parameters:
xml -
Throws:
XmlException - Thrown if an error occurs in reading or parsing the XML
java.lang.UnsupportedOperationException - Thrown if this panel is not a root XmlPanel - XML can only be set on the root panel.

setXml

public void setXml(java.io.Reader reader)
            throws XmlException
Parameters:
reader -
Throws:
XmlException - Thrown if an error occurs in reading or parsing the XML
java.lang.UnsupportedOperationException - Thrown if this panel is not a root XmlPanel - XML can only be set on the root panel.

setXml

public void setXml(java.io.File file)
            throws XmlException,
                   java.io.FileNotFoundException
Parameters:
file -
Throws:
XmlException - Thrown if an error occurs in reading or parsing the XML
java.io.FileNotFoundException - Thrown if the given file cannot be read
java.lang.UnsupportedOperationException - Thrown if this panel is not a root XmlPanel - XML can only be set on the root panel.

setXml

public void setXml(java.io.InputStream stream)
            throws XmlException
Parameters:
stream -
Throws:
XmlException - Thrown if an error occurs in reading or parsing the XML
java.lang.UnsupportedOperationException - Thrown if this panel is not a root XmlPanel - XML can only be set on the root panel.

getComponentNamed

public java.awt.Component getComponentNamed(java.lang.String name)
Returns the first component found in the panel with the given name. Recursively searches through all children, grandchildren, etc.

Parameters:
name -
Returns:
Component found
Throws:
java.lang.IllegalArgumentException - Thrown if a component cannot be found with the given name.
See Also:
SwingUtils.getChildNamed(String, Container)

getJTextField

public javax.swing.JTextField getJTextField(java.lang.String name)
Convenience method for finding a specific JTextField in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJTextArea

public javax.swing.JTextArea getJTextArea(java.lang.String name)
Convenience method for finding a specific JTextArea in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJLabel

public javax.swing.JLabel getJLabel(java.lang.String name)
Convenience method for finding a specific JLabel in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJCheckBox

public javax.swing.JCheckBox getJCheckBox(java.lang.String name)
Convenience method for finding a specific JCheckBox in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJButton

public javax.swing.JButton getJButton(java.lang.String name)
Convenience method for finding a specific JButton in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJRadioButton

public javax.swing.JRadioButton getJRadioButton(java.lang.String name)
Convenience method for finding a specific JRadioButton in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getJComboBox

public javax.swing.JComboBox getJComboBox(java.lang.String name)
Convenience method for finding a specific JComboBox in the panel with the given name. Recursively searches through all children, grandchildren, etc. If not found, an exception is thrown. The exception is thrown so that errors are more informative, rather than return a null which would result in confusing NullPointerExceptions

Parameters:
name - Name of the component
Returns:
Throws:
java.lang.IllegalArgumentException - Thrown if the component could not be found, or if it was found but is not the correct type

getElement

public org.w3c.dom.Element getElement()
Returns:
Returns the XML element object, or null if not available

getParentXmlPanel

public XmlPanel getParentXmlPanel()
Returns:
Returns the parentXmlPanel.

getRootXmlPanel

public XmlPanel getRootXmlPanel()
Returns:
Returns the root XmlPanel - the one that doesn't have a parent

isRootXmlPanel

public boolean isRootXmlPanel()

setAllowEditorDialog

public void setAllowEditorDialog(boolean allowEditorDialog)
Controls if the panel allows double-right-click editing on the panel.

A System property for xmlpanel.allowEditorDialog can also be set to true which will cause all panels to allow the editing, regardless of this setting.

Parameters:
allowEditorDialog - Set to true to allow the double-right-click editing of the panel.

paint

public void paint(java.awt.Graphics g)
Overridden to draw the cell border lines when in edit mode.

See Also:
Component.paint(java.awt.Graphics)

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Passes on the action performed to all of our ActionListeners
Note: If this method is overridden in a subclass, it is important to call the super method, so that anything else that registers to listen will be notified.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
See Also:
ActionListener.actionPerformed(java.awt.event.ActionEvent)

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds an ActionListener to the panel. This ActionListener will receive notification from every AbstractButton in the panel.

Parameters:
l - the ActionListener to be added

getActionListeners

public java.awt.event.ActionListener[] getActionListeners()
Returns an array of all the ActionListeners added to this panel with addActionListener().

Returns:
all of the ActionListeners added or an empty array if no listeners have been added

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes an ActionListener from the panel.

Parameters:
l - the listener to be removed

paintComponent

protected void paintComponent(java.awt.Graphics g)
Overriden to handle the painting of the background image, if there is one

See Also:
JComponent.paintComponent(java.awt.Graphics)

getBackgroundImageAlign

public int getBackgroundImageAlign()
Returns the current background image alignment Alignment values come from SwingConstants such as CENTER, NORTHWEST, etc.
Default value is SwingConstants.NORTHWEST

Returns:
the current background image alignment

isBackgroundImageTileX

public boolean isBackgroundImageTileX()
Returns true if the background image is set to be tiled horizontally. Tiling will stretch all across the panel, and the vertical placement will be determined by getBackgroundImageAlign() - NORTH, CENTER, or SOUTH.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.

Returns:
true if the background image is set to be tiled horizontally

isBackgroundImageTileY

public boolean isBackgroundImageTileY()
Returns true if the background image is set to be tiled vertically. Tiling will stretch all down the panel, and the horizontal placement will be determined by getBackgroundImageAlign() - WEST, CENTER, or EAST.
If both the X and Y tiling are on, then the image will be tiled everywhere, starting at the northwest corner.

Returns:
true if the background image is set to be tiled vertically