logahawk.formatters
Class SwingArgFormatter

java.lang.Object
  extended by logahawk.formatters.AbstractArgumentFormatter
      extended by logahawk.formatters.SwingArgFormatter
All Implemented Interfaces:
ArgumentFormatter

@ThreadSafe
public class SwingArgFormatter
extends AbstractArgumentFormatter

Formats Swing and AWT components. Technically this will format any component that inherits from Component. The format of each component depends on what it is. If the component is a non-input component like a JButton or JPanel, the type and name will be output. For example "JButton: name". If there is no name, the label or text (where appropriate) will be output instead. If there is no acceptable alternative, getAnonymousValue() will be used instead. If it is a component input component like a JTextField or JRadioButton, the type, name, and value will be output. For example "JTextField: name = value" It is highly recommended that this class not be used directly but be used within SwingParentArgFormatter or similar, which will show the complete hierarchy of the component in question. This is recommended mostly because it is not required to provide useful names to components in AWT/Swing; with the hierarchy in hand the component is much easier to identify and the name becomes less critical.


Field Summary
 
Fields inherited from class logahawk.formatters.AbstractArgumentFormatter
indentor
 
Constructor Summary
SwingArgFormatter()
           
 
Method Summary
 boolean canFormat(Object obj)
          This should return true only if this can provide a useful formatting for this type of object.
 String format(Object obj, Collection<ArgumentFormatter> formatters, int indentLevel)
          This converts the object into a String that represent the object.
 String getAnonymousValue()
          Used when a Component.getName() (or similar) is null or empty and there is no alternative label or text value to substitute.
protected  String getName(Object obj)
          Returns the name of the Object.
protected  String getType(Object obj)
          Returns the type of the Component.
protected  String getValue(Object obj, Collection<ArgumentFormatter> formatters, int indentLevel)
          Returns the value of the Object; returns null for non-input controls.
 void setAnonymousValue(String anonymousValue)
           
 
Methods inherited from class logahawk.formatters.AbstractArgumentFormatter
findFormatter, getChildObjectFormat, getIndentor, setIndentor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwingArgFormatter

public SwingArgFormatter()
Method Detail

getAnonymousValue

public String getAnonymousValue()
Used when a Component.getName() (or similar) is null or empty and there is no alternative label or text value to substitute. This is often the case for components such as JTextField. Used by getName(Object). By default this is set to "(anonymous)".


setAnonymousValue

public void setAnonymousValue(String anonymousValue)

canFormat

public boolean canFormat(Object obj)
Description copied from interface: ArgumentFormatter
This should return true only if this can provide a useful formatting for this type of object.


format

public String format(Object obj,
                     Collection<ArgumentFormatter> formatters,
                     int indentLevel)
Description copied from interface: ArgumentFormatter
This converts the object into a String that represent the object. The String may contain line breaks, but should not include a trailing line break. The first line of the result should NOT include any indentation, that should be handled by the calling class. If the object is complex and contains other objects, this ArgumentFormatter may use the provided list of ArgumentFormatter objects to find a more appropriate formatter. To make things look nice, the "indentLevel" is provided. The indent level should always be incremented by one when calling downward into other ArgumentFormatters.


getType

protected String getType(Object obj)
Returns the type of the Component. By default this returns Class.getSimpleName(). This method exists to allow easily altering the type without altering any of the formatting. It is recommended that this return a simple string with no new-lines. If new-lines are needed, it is highly recommended that you override format(Object, Collection, int).


getName

protected String getName(Object obj)
Returns the name of the Object. By default this returns Component.getName() (or similar). If the name is null this will return the label (if appropriate). If there is no label and the name is null or empty this will use getAnonymousValue() instead. This method exists to allow easily altering the name without altering any of the formatting. It is recommended that this return a simple string with no new-lines. If new-lines are needed, it is highly recommended that you override format(Object, Collection, int).


getValue

protected String getValue(Object obj,
                          Collection<ArgumentFormatter> formatters,
                          int indentLevel)
Returns the value of the Object; returns null for non-input controls. The value will be formatted using the appropriate ArgumentFormatters. This method exists to allow easily altering the value without altering any of the formatting. It is recommended that this return a simple string with no new-lines. If new-lines are needed, it is highly recommended that you override format(Object, Collection, int).