org.jdesktop.swingx.plaf
Class PromptTextUI

java.lang.Object
  extended by javax.swing.plaf.ComponentUI
      extended by javax.swing.plaf.TextUI
          extended by org.jdesktop.swingx.plaf.PromptTextUI
Direct Known Subclasses:
PromptTextAreaUI, PromptTextFieldUI

public abstract class PromptTextUI
extends TextUI

Abstract TextUI class that delegates most work to another TextUI and additionally renders a prompt text as specified in the JTextComponents client properties by PromptSupport.

Subclasses of this class must provide a prompt component used for rendering the prompt text.


Nested Class Summary
protected  class PromptTextUI.PainterHighlighter
           
 
Field Summary
protected  TextUI delegate
          Delegate the hard work to this object.
protected  JTextComponent promptComponent
          This component ist painted when rendering the prompt text.
 
Constructor Summary
PromptTextUI(TextUI delegate)
          Creates a new PromptTextUI which delegates most work to another TextUI.
 
Method Summary
 boolean contains(JComponent c, int x, int y)
          Returns true if the specified x,y location is contained within the look and feel's defined shape of the specified component.
protected abstract  JTextComponent createPromptComponent()
          Creates a component which should be used to render the prompt text.
 void damageRange(JTextComponent t, int p0, int p1)
          Causes the portion of the view responsible for the given part of the model to be repainted.
 void damageRange(JTextComponent t, int p0, int p1, Position.Bias firstBias, Position.Bias secondBias)
          Causes the portion of the view responsible for the given part of the model to be repainted.
 boolean equals(Object obj)
          Indicates whether some other object is "equal to" this one.
 Accessible getAccessibleChild(JComponent c, int i)
          Returns the ith Accessible child of the object.
 int getAccessibleChildrenCount(JComponent c)
          Returns the number of accessible children in the object.
 int getBaseline(JComponent c, int width, int height)
          Tries to call ComponentUI#getBaseline(int, int) on the delegate via Reflection.
 EditorKit getEditorKit(JTextComponent t)
          Fetches the binding of services that set a policy for the type of document being edited.
 Dimension getMaximumSize(JComponent c)
          Returns the specified component's maximum size appropriate for the look and feel.
 Dimension getMinimumSize(JComponent c)
          Returns the specified component's minimum size appropriate for the look and feel.
 int getNextVisualPositionFrom(JTextComponent t, int pos, Position.Bias b, int direction, Position.Bias[] biasRet)
          Provides a way to determine the next visually represented model location that one might place a caret.
 Dimension getPreferredSize(JComponent c)
          When shouldPaintPrompt(JTextComponent) returns true, the prompt component is retrieved by calling getPromptComponent(JTextComponent) and it's preferred size is returned.
 JTextComponent getPromptComponent(JTextComponent txt)
          Creates a label component, if none has already been created.
 View getRootView(JTextComponent t)
          Fetches a View with the allocation of the associated text component (i.e.
 String getToolTipText(JTextComponent t, Point pt)
          Returns the string to be used as the tooltip at the passed in location.
 int hashCode()
          Returns a hash code value for the object.
 void installUI(JComponent c)
          Calls TextUI#installUI(JComponent) on the delegate and installs a focus listener on c which repaints the component when it gains or loses the focus.
 Rectangle modelToView(JTextComponent t, int pos)
          Calls #modelToView(JTextComponent, int, Bias) with Position.Bias.Forward.
 Rectangle modelToView(JTextComponent t, int pos, Position.Bias bias)
          Delegate when shouldPaintPrompt(JTextComponent) returns false.
 void paint(Graphics g, JComponent c)
          Delegates painting when shouldPaintPrompt(JTextComponent) returns false.
protected  void paintPromptComponent(Graphics g, JTextComponent txt)
           
 boolean shouldPaintPrompt(JTextComponent txt)
          Returns if the prompt or the text field should be painted, depending on the state of txt.
 String toString()
          Returns a string representation of the object.
 void uninstallUI(JComponent c)
          Delegates, then uninstalls the focus listener.
 void update(Graphics g, JComponent c)
          Calls super.update(Graphics, JComponent), which in turn calls the paint method of this object.
 int viewToModel(JTextComponent t, Point pt)
          Converts the given place in the view coordinate system to the nearest representative location in the model.
 int viewToModel(JTextComponent t, Point pt, Position.Bias[] biasReturn)
          Provides a mapping from the view coordinate space to the logical coordinate space of the model.
 
Methods inherited from class javax.swing.plaf.ComponentUI
createUI, getBaselineResizeBehavior
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

delegate

protected final TextUI delegate
Delegate the hard work to this object.


promptComponent

protected JTextComponent promptComponent
This component ist painted when rendering the prompt text.

Constructor Detail

PromptTextUI

public PromptTextUI(TextUI delegate)
Creates a new PromptTextUI which delegates most work to another TextUI.

Parameters:
delegate -
Method Detail

createPromptComponent

protected abstract JTextComponent createPromptComponent()
Creates a component which should be used to render the prompt text.

Returns:

installUI

public void installUI(JComponent c)
Calls TextUI#installUI(JComponent) on the delegate and installs a focus listener on c which repaints the component when it gains or loses the focus.

Overrides:
installUI in class ComponentUI
Parameters:
c - the component where this UI delegate is being installed
See Also:
ComponentUI.uninstallUI(javax.swing.JComponent), JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

uninstallUI

public void uninstallUI(JComponent c)
Delegates, then uninstalls the focus listener.

Overrides:
uninstallUI in class ComponentUI
Parameters:
c - the component from which this UI delegate is being removed; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
ComponentUI.installUI(javax.swing.JComponent), JComponent.updateUI()

getPromptComponent

public JTextComponent getPromptComponent(JTextComponent txt)
Creates a label component, if none has already been created. Sets the prompt components properties to reflect the given JTextComponents properties and returns it.

Parameters:
txt -
Returns:
the adjusted prompt component

getPreferredSize

public Dimension getPreferredSize(JComponent c)
When shouldPaintPrompt(JTextComponent) returns true, the prompt component is retrieved by calling getPromptComponent(JTextComponent) and it's preferred size is returned. Otherwise supergetPreferredSize(JComponent) is called.

Overrides:
getPreferredSize in class ComponentUI
Parameters:
c - the component whose preferred size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
JComponent.getPreferredSize(), LayoutManager.preferredLayoutSize(java.awt.Container)

paint

public void paint(Graphics g,
                  JComponent c)
Delegates painting when shouldPaintPrompt(JTextComponent) returns false. Otherwise the prompt component is retrieved by calling getPromptComponent(JTextComponent) and painted. Then the caret of the given text component is painted.

Overrides:
paint in class ComponentUI
Parameters:
g - the Graphics context in which to paint
c - the component being painted; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
ComponentUI.update(java.awt.Graphics, javax.swing.JComponent)

paintPromptComponent

protected void paintPromptComponent(Graphics g,
                                    JTextComponent txt)

shouldPaintPrompt

public boolean shouldPaintPrompt(JTextComponent txt)
Returns if the prompt or the text field should be painted, depending on the state of txt.

Parameters:
txt -
Returns:
true when txt contains not text, otherwise false

update

public void update(Graphics g,
                   JComponent c)
Calls super.update(Graphics, JComponent), which in turn calls the paint method of this object.

Overrides:
update in class ComponentUI
Parameters:
g - the Graphics context in which to paint
c - the component being painted; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
ComponentUI.paint(java.awt.Graphics, javax.swing.JComponent), JComponent.paintComponent(java.awt.Graphics)

modelToView

public Rectangle modelToView(JTextComponent t,
                             int pos,
                             Position.Bias bias)
                      throws BadLocationException
Delegate when shouldPaintPrompt(JTextComponent) returns false. Otherwise get the prompt component's UI and delegate to it. This ensures that the Caret is painted on the correct position (this is important when the text is centered, so that the caret will not be painted inside the label text)

Specified by:
modelToView in class TextUI
pos - the local location in the model to translate >= 0
Returns:
the coordinates as a rectangle
Throws:
BadLocationException - if the given position does not represent a valid location in the associated document

modelToView

public Rectangle modelToView(JTextComponent t,
                             int pos)
                      throws BadLocationException
Calls #modelToView(JTextComponent, int, Bias) with Position.Bias.Forward.

Specified by:
modelToView in class TextUI
pos - the local location in the model to translate >= 0
Returns:
the coordinates as a rectangle
Throws:
BadLocationException - if the given position does not represent a valid location in the associated document

contains

public boolean contains(JComponent c,
                        int x,
                        int y)
Description copied from class: javax.swing.plaf.ComponentUI
Returns true if the specified x,y location is contained within the look and feel's defined shape of the specified component. x and y are defined to be relative to the coordinate system of the specified component. Although a component's bounds is constrained to a rectangle, this method provides the means for defining a non-rectangular shape within those bounds for the purpose of hit detection.

Overrides:
contains in class ComponentUI
Parameters:
c - the component where the x,y location is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
x - the x coordinate of the point
y - the y coordinate of the point
See Also:
JComponent.contains(int, int), Component.contains(int, int)

damageRange

public void damageRange(JTextComponent t,
                        int p0,
                        int p1,
                        Position.Bias firstBias,
                        Position.Bias secondBias)
Description copied from class: javax.swing.plaf.TextUI
Causes the portion of the view responsible for the given part of the model to be repainted.

Specified by:
damageRange in class TextUI
p0 - the beginning of the range >= 0
p1 - the end of the range >= p0

damageRange

public void damageRange(JTextComponent t,
                        int p0,
                        int p1)
Description copied from class: javax.swing.plaf.TextUI
Causes the portion of the view responsible for the given part of the model to be repainted.

Specified by:
damageRange in class TextUI
p0 - the beginning of the range >= 0
p1 - the end of the range >= p0

equals

public boolean equals(Object obj)
Description copied from class: java.lang.Object
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
See Also:
Object.hashCode(), Hashtable

getAccessibleChild

public Accessible getAccessibleChild(JComponent c,
                                     int i)
Description copied from class: javax.swing.plaf.ComponentUI
Returns the ith Accessible child of the object. UIs might need to override this if they present areas on the screen that can be viewed as components, but actual components are not used for presenting those areas.

Note: As of v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleChild() instead of this method.

Overrides:
getAccessibleChild in class ComponentUI
i - zero-based index of child
Returns:
the ith Accessible child of the object
See Also:
ComponentUI.getAccessibleChildrenCount(javax.swing.JComponent)

getAccessibleChildrenCount

public int getAccessibleChildrenCount(JComponent c)
Description copied from class: javax.swing.plaf.ComponentUI
Returns the number of accessible children in the object. If all of the children of this object implement Accessible, this method should return the number of children of this object. UIs might wish to override this if they present areas on the screen that can be viewed as components, but actual components are not used for presenting those areas. Note: As of v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleChildrenCount() instead of this method.

Overrides:
getAccessibleChildrenCount in class ComponentUI
Returns:
the number of accessible children in the object
See Also:
ComponentUI.getAccessibleChild(javax.swing.JComponent, int)

getEditorKit

public EditorKit getEditorKit(JTextComponent t)
Description copied from class: javax.swing.plaf.TextUI
Fetches the binding of services that set a policy for the type of document being edited. This contains things like the commands available, stream readers and writers, etc.

Specified by:
getEditorKit in class TextUI
Returns:
the editor kit binding

getMaximumSize

public Dimension getMaximumSize(JComponent c)
Description copied from class: javax.swing.plaf.ComponentUI
Returns the specified component's maximum size appropriate for the look and feel. If null is returned, the maximum size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method invokes getPreferredSize and returns that value.

Overrides:
getMaximumSize in class ComponentUI
Parameters:
c - the component whose maximum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns:
a Dimension object or null
See Also:
JComponent.getMaximumSize(), LayoutManager2.maximumLayoutSize(java.awt.Container)

getMinimumSize

public Dimension getMinimumSize(JComponent c)
Description copied from class: javax.swing.plaf.ComponentUI
Returns the specified component's minimum size appropriate for the look and feel. If null is returned, the minimum size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method invokes getPreferredSize and returns that value.

Overrides:
getMinimumSize in class ComponentUI
Parameters:
c - the component whose minimum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns:
a Dimension object or null
See Also:
JComponent.getMinimumSize(), LayoutManager.minimumLayoutSize(java.awt.Container), ComponentUI.getPreferredSize(javax.swing.JComponent)

getNextVisualPositionFrom

public int getNextVisualPositionFrom(JTextComponent t,
                                     int pos,
                                     Position.Bias b,
                                     int direction,
                                     Position.Bias[] biasRet)
                              throws BadLocationException
Description copied from class: javax.swing.plaf.TextUI
Provides a way to determine the next visually represented model location that one might place a caret. Some views may not be visible, they might not be in the same order found in the model, or they just might not allow access to some of the locations in the model.

Specified by:
getNextVisualPositionFrom in class TextUI
Parameters:
t - the text component for which this UI is installed
pos - the position to convert >= 0
b - the bias for the position
direction - the direction from the current position that can be thought of as the arrow keys typically found on a keyboard. This may be SwingConstants.WEST, SwingConstants.EAST, SwingConstants.NORTH, or SwingConstants.SOUTH
biasRet - an array to contain the bias for the returned position
Returns:
the location within the model that best represents the next location visual position
Throws:
BadLocationException

getRootView

public View getRootView(JTextComponent t)
Description copied from class: javax.swing.plaf.TextUI
Fetches a View with the allocation of the associated text component (i.e. the root of the hierarchy) that can be traversed to determine how the model is being represented spatially.

Specified by:
getRootView in class TextUI
Returns:
the view

getToolTipText

public String getToolTipText(JTextComponent t,
                             Point pt)
Description copied from class: javax.swing.plaf.TextUI
Returns the string to be used as the tooltip at the passed in location.

Overrides:
getToolTipText in class TextUI
See Also:
JTextComponent.getToolTipText(java.awt.event.MouseEvent)

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable

toString

public String toString()
Description copied from class: java.lang.Object
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Overrides:
toString in class Object
Returns:
a string representation of the object.

viewToModel

public int viewToModel(JTextComponent t,
                       Point pt,
                       Position.Bias[] biasReturn)
Description copied from class: javax.swing.plaf.TextUI
Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Specified by:
viewToModel in class TextUI
pt - the location in the view to translate. This should be in the same coordinate system as the mouse events.
biasReturn - filled in by this method to indicate whether the point given is closer to the previous or the next character in the model
Returns:
the location within the model that best represents the given point in the view >= 0

viewToModel

public int viewToModel(JTextComponent t,
                       Point pt)
Description copied from class: javax.swing.plaf.TextUI
Converts the given place in the view coordinate system to the nearest representative location in the model.

Specified by:
viewToModel in class TextUI
pt - the location in the view to translate. This should be in the same coordinate system as the mouse events.
Returns:
the offset from the start of the document >= 0

getBaseline

public int getBaseline(JComponent c,
                       int width,
                       int height)
Tries to call ComponentUI#getBaseline(int, int) on the delegate via Reflection. Workaround to maintain compatibility with Java 5. Ideally we should also override ComponentUI.getBaselineResizeBehavior(JComponent), but that's impossible since the Component.BaselineResizeBehavior class, which does not exist in Java 5, is involved.

Overrides:
getBaseline in class ComponentUI
Parameters:
c - JComponent baseline is being requested for
width - the width to get the baseline for
height - the height to get the baseline for
Returns:
the baseline, or -2 if getBaseline could not be invoked on the delegate.
See Also:
JComponent.getBaseline(int,int)