public abstract class BasicTextUI extends TextUIimplements ViewFactory
Basis of a text components look-and-feel. This provides the basic editor view and controller services that may be useful when creating a look-and-feel for an extension of JTextComponent.
Most state is held in the associated JTextComponent as bound properties, and the UI installs default values for the various properties. This default will install something for all of the properties. Typically, a LAF implementation will do more however. At a minimum, a LAF would generally install key bindings.
This class also provides some concurrency support if the Document associated with the JTextComponent is a subclass of AbstractDocument. Access to the View (or View hierarchy) is serialized between any thread mutating the model and the Swing event thread (which is expected to render, do model/view coordinate translation, etc). Any access to the root view should first acquire a read-lock on the AbstractDocument and release that lock in a finally block.
An important method to define is the getPropertyPrefix() method which is used as the basis of the keys used to fetch defaults from the UIManager. The string should reflect the type of TextUI (eg. TextField, TextArea, etc) without the particular LAF part of the name (eg Metal, Motif, etc).
To build a view of the model, one of the following strategies can be employed.
create(javax.swing.text.Element) method. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.
| Modifier and Type | Class and Description |
|---|---|
static class |
BasicTextUI
|
static class |
BasicTextUI
|
| Constructor and Description |
|---|
BasicTextUI()
Creates a new UI.
|
| Modifier and Type | Method and Description |
|---|---|
View |
create(Element
Creates a view for an element.
|
View |
create(Element
Creates a view for an element.
|
protected Caret |
createCaret()
Creates the object to use for a caret.
|
protected Highlighter |
createHighlighter()
Creates the object to use for adding highlights.
|
protected Keymap |
createKeymap()
Creates the keymap to use for the text component, and installs any necessary bindings into it.
|
void |
damageRange(JTextComponent
Causes the portion of the view responsible for the given part of the model to be repainted.
|
void |
damageRange(JTextComponent
Causes the portion of the view responsible for the given part of the model to be repainted.
|
protected JTextComponent |
getComponent()
Fetches the text component associated with this UI implementation.
|
EditorKit |
getEditorKit(JTextComponent
Fetches the EditorKit for the UI.
|
protected String |
getKeymapName()
Fetches the name of the keymap that will be installed/used by default for this UI.
|
Dimension |
getMaximumSize(JComponent
Gets the maximum size for the editor component.
|
Dimension |
getMinimumSize(JComponent
Gets the minimum size for the editor component.
|
int |
getNextVisualPositionFrom(JTextComponent
Provides a way to determine the next visually represented model location that one might place a caret.
|
Dimension |
getPreferredSize(JComponent
Gets the preferred size for the editor component.
|
protected abstract String |
getPropertyPrefix()
Gets the name used as a key to look up properties through the UIManager.
|
View |
getRootView(JTextComponent
Fetches a View with the allocation of the associated text component (i.e.
|
String |
getToolTipText(JTextComponent
Returns the string to be used as the tooltip at the passed in location.
|
protected Rectangle |
getVisibleEditorRect()
Gets the allocation to give the root View.
|
protected void |
installDefaults()
Initializes component properties, such as font, foreground, background, caret color, selection color, selected text color, disabled text color, and border color.
|
protected void |
installKeyboardActions()
|
protected void |
installListeners()
Installs listeners for the UI.
|
void |
installUI(JComponent
Installs the UI for a component.
|
protected void |
modelChanged()
Flags model changes.
|
Rectangle |
modelToView(JTextComponent
Converts the given location in the model to a place in the view coordinate system.
|
Rectangle |
modelToView(JTextComponent
Converts the given location in the model to a place in the view coordinate system.
|
void |
paint(Graphics
Paints the interface.
|
protected void |
paintBackground(Graphics
Paints a background for the view.
|
protected void |
paintSafely(Graphics
Paints the interface safely with a guarantee that the model won't change from the view of this thread.
|
protected void |
propertyChange(PropertyChangeEvent
This method gets called when a bound property is changed on the associated JTextComponent.
|
protected void |
setView(View
Sets the current root of the view hierarchy and calls invalidate().
|
protected void |
uninstallDefaults()
Sets the component properties that have not been explicitly overridden to
null.
|
protected void |
uninstallKeyboardActions()
|
protected void |
uninstallListeners()
Uninstalls listeners for the UI.
|
void |
uninstallUI(JComponent
Deinstalls the UI for a component.
|
void |
update(Graphics
Superclass paints background in an uncontrollable way (i.e.
|
int |
viewToModel(JTextComponent
Converts the given place in the view coordinate system to the nearest representative location in the model.
|
int |
viewToModel(JTextComponent
Converts the given place in the view coordinate system to the nearest representative location in the model.
|
contains, createUI, getAccessibleChild, getAccessibleChildrenCount, getBaseline, getBaselineResizeBehaviorprotected CaretcreateCaret()
protected HighlightercreateHighlighter()
protected StringgetKeymapName()
protected KeymapcreateKeymap()
The set of bindings used to create the keymap is fetched from the UIManager using a key formed by combining the getPropertyPrefix() method and the string .keyBindings. The type is expected to be JTextComponent.KeyBinding[].
getKeymapName(),
JTextComponent
protected void propertyChange(PropertyChangeEventevt)
evt - the property change event
protected abstract StringgetPropertyPrefix()
protected void installDefaults()
protected void uninstallDefaults()
null. A property is considered overridden if its current value is not a
UIResource.
protected void installListeners()
protected void uninstallListeners()
protected void installKeyboardActions()
protected void uninstallKeyboardActions()
protected void paintBackground(Graphicsg)
g - the graphics context
protected final JTextComponentgetComponent()
protected void modelChanged()
protected final void setView(Viewv)
v - the root view
protected void paintSafely(Graphicsg)
g - the graphics context
public void installUI(JComponentc)
null or an instance of UIResource. installUI in class
ComponentUI
c - the editor component
ComponentUI.installUI(javax.swing.JComponent)
public void uninstallUI(JComponentc)
uninstallUI in class
ComponentUI
c - the editor component
ComponentUI.uninstallUI(javax.swing.JComponent)
public void update(Graphicsg, JComponent c)
NOTE: NOTE: Superclass is also not thread-safe in its rendering of the background, although that is not an issue with the default rendering.
update in class
ComponentUI
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
ComponentUI.paint(java.awt.Graphics, javax.swing.JComponent) ,
JComponent.paintComponent(java.awt.Graphics)
public final void paint(Graphicsg, JComponent c)
paint in class
ComponentUI
g - the graphics context
c - the editor component
ComponentUI.update(java.awt.Graphics, javax.swing.JComponent)
public DimensiongetPreferredSize(JComponent c)
getPreferredSize in class
ComponentUI
c - the editor component
JComponent.getPreferredSize() ,
LayoutManager.preferredLayoutSize(java.awt.Container)
public DimensiongetMinimumSize(JComponent c)
getMinimumSize in class
ComponentUI
c - the editor component
JComponent.getMinimumSize() ,
LayoutManager.minimumLayoutSize(java.awt.Container) ,
ComponentUI.getPreferredSize(javax.swing.JComponent)
public DimensiongetMaximumSize(JComponent c)
getMaximumSize in class
ComponentUI
c - the editor component
JComponent.getMaximumSize() ,
LayoutManager2.maximumLayoutSize(java.awt.Container)
protected RectanglegetVisibleEditorRect()
public RectanglemodelToView(JTextComponent tc, int pos) throws BadLocationException
modelToView in class
TextUI
tc - the text component for which this UI is installed
pos - the local location in the model to translate >= 0
BadLocationException - if the given position does not represent a valid location in the associated document
TextUI.modelToView(javax.swing.text.JTextComponent, int)
public RectanglemodelToView(JTextComponent tc, int pos, Position .Bias bias) throws BadLocationException
modelToView in class
TextUI
tc - the text component for which this UI is installed
pos - the local location in the model to translate >= 0
BadLocationException - if the given position does not represent a valid location in the associated document
TextUI.modelToView(javax.swing.text.JTextComponent, int)
public int viewToModel(JTextComponenttc, Point pt)
viewToModel in class
TextUI
tc - the text component for which this UI is installed
pt - the location in the view to translate. This should be in the same coordinate system as the mouse events.
TextUI.viewToModel(javax.swing.text.JTextComponent, java.awt.Point)
public int viewToModel(JTextComponenttc, Point pt, Position .Bias [] biasReturn)
viewToModel in class
TextUI
tc - the text component for which this UI is installed
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
TextUI.viewToModel(javax.swing.text.JTextComponent, java.awt.Point)
public int getNextVisualPositionFrom(JTextComponentt, int pos, Position .Bias b, int direction, Position .Bias [] biasRet) throws BadLocationException
getNextVisualPositionFrom in class
TextUI
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
BadLocationException -
public void damageRange(JTextComponenttc, int p0, int p1)
damageRange in class
TextUI
tc - the text component for which this UI is installed
p0 - the beginning of the range >= 0
p1 - the end of the range >= p0
TextUI.damageRange(javax.swing.text.JTextComponent, int, int)
public void damageRange(JTextComponentt, int p0, int p1, Position .Bias p0Bias, Position .Bias p1Bias)
damageRange in class
TextUI
p0 - the beginning of the range >= 0
p1 - the end of the range >= p0
public EditorKitgetEditorKit(JTextComponent tc)
getEditorKit in class
TextUI
tc - the text component for which this UI is installed
TextUI.getEditorKit(javax.swing.text.JTextComponent)
public ViewgetRootView(JTextComponent tc)
NOTE:The View hierarchy can be traversed from the root view, and other things can be done as well. Things done in this way cannot be protected like simple method calls through the TextUI. Therefore, proper operation in the presence of concurrency must be arranged by any logic that calls this method!
getRootView in class
TextUI
tc - the text component for which this UI is installed
TextUI.getRootView(javax.swing.text.JTextComponent)
public StringgetToolTipText(JTextComponent t, Point pt)
getToolTipText in class
TextUI
JTextComponent.getToolTipText(java.awt.event.MouseEvent) ,
View.getToolTipText(float, float, java.awt.Shape)
public Viewcreate(Element elem)
create in interface
ViewFactory
elem - the element
View
public Viewcreate(Element elem, int p0, int p1)
elem - the element
p0 - the starting offset >= 0
p1 - the ending offset >= p0