View (Java SE 15 & JDK 15) (original) (raw)

All Implemented Interfaces:

[SwingConstants](../SwingConstants.html "interface in javax.swing")

Direct Known Subclasses:

[AsyncBoxView](AsyncBoxView.html "class in javax.swing.text"), [ComponentView](ComponentView.html "class in javax.swing.text"), [CompositeView](CompositeView.html "class in javax.swing.text"), [GlyphView](GlyphView.html "class in javax.swing.text"), [IconView](IconView.html "class in javax.swing.text"), [ImageView](html/ImageView.html "class in javax.swing.text.html"), [PlainView](PlainView.html "class in javax.swing.text")


public abstract class View extends Object implements SwingConstants

A very important part of the text package is the View class. As the name suggests it represents a view of the text model, or a piece of the text model. It is this class that is responsible for the look of the text component. The view is not intended to be some completely new thing that one must learn, but rather is much like a lightweight component.

By default, a view is very light. It contains a reference to the parent view from which it can fetch many things without holding state, and it contains a reference to a portion of the model (Element). A view does not have to exactly represent an element in the model, that is simply a typical and therefore convenient mapping. A view can alternatively maintain a couple of Position objects to maintain its location in the model (i.e. represent a fragment of an element). This is typically the result of formatting where views have been broken down into pieces. The convenience of a substantial relationship to the element makes it easier to build factories to produce the views, and makes it easier to keep track of the view pieces as the model is changed and the view must be changed to reflect the model. Simple views therefore represent an Element directly and complex views do not.

A view has the following responsibilities:

Participate in layout.

The view has a setSize method which is likedoLayout and setSize in Component combined. The view has a preferenceChanged method which is like invalidate in Component except that one can invalidate just one axis and the child requesting the change is identified.

A View expresses the size that it would like to be in terms of three values, a minimum, a preferred, and a maximum span. Layout in a view is can be done independently upon each axis. For a properly functioning View implementation, the minimum span will be <= the preferred span which in turn will be <= the maximum span.

The above text describes this graphic.

The minimum set of methods for layout are:

The setSize method should be prepared to be called a number of times (i.e. It may be called even if the size didn't change). The setSize method is generally called to make sure the View layout is complete prior to trying to perform an operation on it that requires an up-to-date layout. A view's size should always be set to a value within the minimum and maximum span specified by that view. Additionally, the view must always call thepreferenceChanged method on the parent if it has changed the values for the layout it would like, and expects the parent to honor. The parent View is not required to recognize a change until the preferenceChanged has been sent. This allows parent View implementations to cache the child requirements if desired. The calling sequence looks something like the following:

Sample calling sequence between parent view and child view:
       setSize, getMinimum, getPreferred, getMaximum, getAlignment, setSize

The exact calling sequence is up to the layout functionality of the parent view (if the view has any children). The view may collect the preferences of the children prior to determining what it will give each child, or it might iteratively update the children one at a time.

Render a portion of the model.

This is done in the paint method, which is pretty much like a component paint method. Views are expected to potentially populate a fairly large tree. A View has the following semantics for rendering:

The methods for rendering are:

Translate between the model and view coordinate systems.

Because the view objects are produced from a factory and therefore cannot necessarily be counted upon to be in a particular pattern, one must be able to perform translation to properly locate spatial representation of the model. The methods for doing this are:

The layout must be valid prior to attempting to make the translation. The translation is not valid, and must not be attempted while changes are being broadcasted from the model via a DocumentEvent.

Respond to changes from the model.

If the overall view is represented by many pieces (which is the best situation if one want to be able to change the view and write the least amount of new code), it would be impractical to have a huge number of DocumentListeners. If each view listened to the model, only a few would actually be interested in the changes broadcasted at any given time. Since the model has no knowledge of views, it has no way to filter the broadcast of change information. The view hierarchy itself is instead responsible for propagating the change information. At any level in the view hierarchy, that view knows enough about its children to best distribute the change information further. Changes are therefore broadcasted starting from the root of the view hierarchy. The methods for doing this are:

Fields

Modifier and Type Field Description
static int BadBreakWeight The weight to indicate a view is a bad break opportunity for the purpose of formatting.
static int ExcellentBreakWeight The weight to indicate a view supports breaking, and this represents a very attractive place to break.
static int ForcedBreakWeight The weight to indicate a view supports breaking, and must be broken to be represented properly when placed in a view that formats its children by breaking them.
static int GoodBreakWeight The weight to indicate a view supports breaking, but better opportunities probably exist.
static int X_AXIS Axis for format/break operations.
static int Y_AXIS Axis for format/break operations.

Fields declared in interface javax.swing.SwingConstants

[BOTTOM](../SwingConstants.html#BOTTOM), [CENTER](../SwingConstants.html#CENTER), [EAST](../SwingConstants.html#EAST), [HORIZONTAL](../SwingConstants.html#HORIZONTAL), [LEADING](../SwingConstants.html#LEADING), [LEFT](../SwingConstants.html#LEFT), [NEXT](../SwingConstants.html#NEXT), [NORTH](../SwingConstants.html#NORTH), [NORTH_EAST](../SwingConstants.html#NORTH%5FEAST), [NORTH_WEST](../SwingConstants.html#NORTH%5FWEST), [PREVIOUS](../SwingConstants.html#PREVIOUS), [RIGHT](../SwingConstants.html#RIGHT), [SOUTH](../SwingConstants.html#SOUTH), [SOUTH_EAST](../SwingConstants.html#SOUTH%5FEAST), [SOUTH_WEST](../SwingConstants.html#SOUTH%5FWEST), [TOP](../SwingConstants.html#TOP), [TRAILING](../SwingConstants.html#TRAILING), [VERTICAL](../SwingConstants.html#VERTICAL), [WEST](../SwingConstants.html#WEST)

Constructors

Constructor Description
View​(Element elem) Creates a new View object.
Modifier and Type Method Description
void append​(View v) Appends a single child view.
View breakView​(int axis, int offset, float pos, float len) Tries to break this view on the given axis.
void changedUpdate​(DocumentEvent e,Shape a,ViewFactory f) Gives notification from the document that attributes were changed in a location that this view is responsible for.
View createFragment​(int p0, int p1) Creates a view that represents a portion of the element.
protected void forwardUpdate​(DocumentEvent.ElementChange ec,DocumentEvent e,Shape a,ViewFactory f) Forwards the given DocumentEvent to the child views that need to be notified of the change to the model.
protected void forwardUpdateToView​(View v,DocumentEvent e,Shape a,ViewFactory f) Forwards the DocumentEvent to the give child view.
float getAlignment​(int axis) Determines the desired alignment for this view along an axis.
AttributeSet getAttributes() Fetches the attributes to use when rendering.
int getBreakWeight​(int axis, float pos, float len) Determines how attractive a break opportunity in this view is.
Shape getChildAllocation​(int index,Shape a) Fetches the allocation for the given child view.
Container getContainer() Fetches the container hosting the view.
Document getDocument() Fetches the model associated with the view.
Element getElement() Fetches the structural portion of the subject that this view is mapped to.
int getEndOffset() Fetches the portion of the model for which this view is responsible.
Graphics getGraphics() Fetch a Graphics for rendering.
float getMaximumSpan​(int axis) Determines the maximum span for this view along an axis.
float getMinimumSpan​(int axis) Determines the minimum span for this view along an axis.
int getNextVisualPositionFrom​(int pos,Position.Bias b,Shape a, int direction,Position.Bias[] biasRet) Provides a way to determine the next visually represented model location at which one might place a caret.
View getParent() Returns the parent of the view.
abstract float getPreferredSpan​(int axis) Determines the preferred span for this view along an axis.
int getResizeWeight​(int axis) Determines the resizability of the view along the given axis.
int getStartOffset() Fetches the portion of the model for which this view is responsible.
String getToolTipText​(float x, float y,Shape allocation) Returns the tooltip text at the specified location.
View getView​(int n) Gets the _n_th child view.
int getViewCount() Returns the number of views in this view.
ViewFactory getViewFactory() Fetches the ViewFactory implementation that is feeding the view hierarchy.
int getViewIndex​(float x, float y,Shape allocation) Returns the child view index representing the given position in the view.
int getViewIndex​(int pos,Position.Bias b) Returns the child view index representing the given position in the model.
void insert​(int offs,View v) Inserts a single child view.
void insertUpdate​(DocumentEvent e,Shape a,ViewFactory f) Gives notification that something was inserted into the document in a location that this view is responsible for.
boolean isVisible() Returns a boolean that indicates whether the view is visible or not.
Shape modelToView​(int pos,Shape a) Deprecated.
abstract Shape modelToView​(int pos,Shape a,Position.Bias b) Provides a mapping, for a given character, from the document model coordinate space to the view coordinate space.
Shape modelToView​(int p0,Position.Bias b0, int p1,Position.Bias b1,Shape a) Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space.
abstract void paint​(Graphics g,Shape allocation) Renders using the given rendering surface and area on that surface.
void preferenceChanged​(View child, boolean width, boolean height) Child views can call this on the parent to indicate that the preference has changed and should be reconsidered for layout.
void remove​(int i) Removes one of the children at the given position.
void removeAll() Removes all of the children.
void removeUpdate​(DocumentEvent e,Shape a,ViewFactory f) Gives notification that something was removed from the document in a location that this view is responsible for.
void replace​(int offset, int length,View[] views) Replaces child views.
void setParent​(View parent) Establishes the parent view for this view.
void setSize​(float width, float height) Sets the size of the view.
protected boolean updateChildren​(DocumentEvent.ElementChange ec,DocumentEvent e,ViewFactory f) Updates the child views in response to receiving notification that the model changed, and there is change record for the element this view is responsible for.
protected void updateLayout​(DocumentEvent.ElementChange ec,DocumentEvent e,Shape a) Updates the layout in response to receiving notification of change from the model.
int viewToModel​(float x, float y,Shape a) Deprecated.
abstract int viewToModel​(float x, float y,Shape a,Position.Bias[] biasReturn) Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Methods declared in class java.lang.Object

[clone](../../../../java.base/java/lang/Object.html#clone%28%29), [equals](../../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../../java.base/java/lang/Object.html#finalize%28%29), [getClass](../../../../java.base/java/lang/Object.html#getClass%28%29), [hashCode](../../../../java.base/java/lang/Object.html#hashCode%28%29), [notify](../../../../java.base/java/lang/Object.html#notify%28%29), [notifyAll](../../../../java.base/java/lang/Object.html#notifyAll%28%29), [toString](../../../../java.base/java/lang/Object.html#toString%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long,int%29)