ViewFactory
BasicEditorPaneUI
, BasicTextAreaUI
, BasicTextFieldUI
, DefaultTextUI
public abstract class BasicTextUI extends TextUI implements 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 | Description |
---|---|---|
static class |
BasicTextUI.BasicCaret |
Default implementation of the interface Caret . |
static class |
BasicTextUI.BasicHighlighter |
Default implementation of the interface Highlighter . |
Constructor | Description |
---|---|
BasicTextUI() |
Creates a new UI. |
Modifier and Type | Method | Description |
---|---|---|
View |
create |
Creates a view for an element. |
View |
create |
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 |
Causes the portion of the view responsible for the given part of the model to be repainted. |
void |
damageRange |
Causes the portion of the view responsible for the given part of the model to be repainted. |
protected final JTextComponent |
getComponent() |
Fetches the text component associated with this UI implementation. |
EditorKit |
getEditorKit |
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 |
Gets the maximum size for the editor component. |
Dimension |
getMinimumSize |
Gets the minimum size for the editor component. |
int |
getNextVisualPositionFrom |
Provides a way to determine the next visually represented model location that one might place a caret. |
Dimension |
getPreferredSize |
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 |
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. |
String |
getToolTipText |
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, border, and margin. |
protected void |
installKeyboardActions() |
Registers keyboard actions. |
protected void |
installListeners() |
Installs listeners for the UI. |
void |
installUI |
Installs the UI for a component. |
protected void |
modelChanged() |
Flags model changes. |
Rectangle |
modelToView |
Deprecated. |
Rectangle |
modelToView |
Deprecated. |
final void |
paint |
Paints the interface. |
protected void |
paintBackground |
Paints a background for the view. |
protected void |
paintSafely |
Paints the interface safely with a guarantee that the model won't change from the view of this thread. |
protected void |
propertyChange |
This method gets called when a bound property is changed on the associated JTextComponent. |
protected final void |
setView |
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() |
Unregisters keyboard actions. |
protected void |
uninstallListeners() |
Uninstalls listeners for the UI. |
void |
uninstallUI |
Deinstalls the UI for a component. |
void |
update |
Superclass paints background in an uncontrollable way (i.e. one might want an image tiled into the background). |
int |
viewToModel |
Deprecated. |
int |
viewToModel |
Deprecated. |
getToolTipText2D, modelToView2D, viewToModel2D
contains, createUI, getAccessibleChild, getAccessibleChildrenCount, getBaseline, getBaselineResizeBehavior
public BasicTextUI()
protected Caret createCaret()
protected Highlighter createHighlighter()
protected String getKeymapName()
protected Keymap createKeymap()
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[]
.
protected void propertyChange(PropertyChangeEvent evt)
evt
- the property change eventprotected abstract String getPropertyPrefix()
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(Graphics g)
g
- the graphics contextprotected final JTextComponent getComponent()
protected void modelChanged()
protected final void setView(View v)
v
- the root viewprotected void paintSafely(Graphics g)
g
- the graphics contextpublic void installUI(JComponent c)
null
or an instance of UIResource
. installUI
in class ComponentUI
c
- the editor componentpublic void uninstallUI(JComponent c)
uninstallUI
in class ComponentUI
c
- the editor componentpublic void update(Graphics g, JComponent c)
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 paintc
- the component being painted; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple componentspublic final void paint(Graphics g, JComponent c)
paint
in class ComponentUI
g
- the graphics contextc
- the editor componentpublic Dimension getPreferredSize(JComponent c)
getPreferredSize
in class ComponentUI
c
- the editor componentpublic Dimension getMinimumSize(JComponent c)
getMinimumSize
in class ComponentUI
c
- the editor componentpublic Dimension getMaximumSize(JComponent c)
getMaximumSize
in class ComponentUI
c
- the editor componentprotected Rectangle getVisibleEditorRect()
@Deprecated(since="9") public Rectangle modelToView(JTextComponent tc, int pos) throws BadLocationException
modelToView
in class TextUI
tc
- the text component for which this UI is installedpos
- the local location in the model to translate >= 0BadLocationException
- if the given position does not represent a valid location in the associated document@Deprecated(since="9") public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException
modelToView
in class TextUI
tc
- the text component for which this UI is installedpos
- the local location in the model to translate >= 0bias
- the bias for the positionBadLocationException
- if the given position does not represent a valid location in the associated document@Deprecated(since="9") public int viewToModel(JTextComponent tc, Point pt)
viewToModel
in class TextUI
tc
- the text component for which this UI is installedpt
- the location in the view to translate. This should be in the same coordinate system as the mouse events.@Deprecated(since="9") public int viewToModel(JTextComponent tc, Point pt, Position.Bias[] biasReturn)
viewToModel
in class TextUI
tc
- the text component for which this UI is installedpt
- 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 modelpublic int getNextVisualPositionFrom(JTextComponent t, 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 installedpos
- the position to convert >= 0b
- the bias for the positiondirection
- 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.SOUTHbiasRet
- an array to contain the bias for the returned positionBadLocationException
- for a bad location within a document modelpublic void damageRange(JTextComponent tc, int p0, int p1)
damageRange
in class TextUI
tc
- the text component for which this UI is installedp0
- the beginning of the range >= 0p1
- the end of the range >= p0public void damageRange(JTextComponent t, int p0, int p1, Position.Bias p0Bias, Position.Bias p1Bias)
damageRange
in class TextUI
t
- the text component for which this UI is installedp0
- the beginning of the range >= 0p1
- the end of the range >= p0p0Bias
- the bias of the first character position, toward the previous character or the next characterp1Bias
- the bias of the second character position, toward the previous character or the next characterpublic EditorKit getEditorKit(JTextComponent tc)
getEditorKit
in class TextUI
tc
- the text component for which this UI is installedpublic View getRootView(JTextComponent tc)
Warning: 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 installedpublic String getToolTipText(JTextComponent t, Point pt)
getToolTipText
in class TextUI
t
- the text component for which this UI is installedpt
- a Point
specifying location for which to get a tooltipString
containing the tooltippublic View create(Element elem)
create
in interface ViewFactory
elem
- the elementpublic View create(Element elem, int p0, int p1)
elem
- the elementp0
- the starting offset >= 0p1
- the ending offset >= p0
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/plaf/basic/BasicTextUI.html
TextUI.modelToView2D(JTextComponent, int, Position.Bias)