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.
In fact, the original View
implementation was a
lightweight component. There were several reasons why
the Component
implementation was abandoned in favor of
an alternative.
There was barely time to get the lightweight component support in the
1.1 version of the JDK. There simply wasn't time to lighten up the
component further to where it would need to be to be used for text purposes.
The additions made to JComponent
increased the memory
consumption, and as it currently stands its much too heavy for
representing text.
The layout semantics aren't quite right for text, and changing the current layout semantics of component might break existing applications.
The component api uses integers, but in 1.2 one can use floating point
device independent coordinates. An api that works in both 1.1 and 1.2
would be convenient for minimizing transition difficulties. The
View
class uses the Shape
interface and
float arguments to enable View implementations for the Java 2 platform
v1.2 and later while still functioning in the older 1.1 JDK.
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:
The view has a setSize
method which is like
doLayout
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 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 the
preferenceChanged
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:
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.
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:
Component
(i.e. the Component
returned by the
getContainer method).
This means a child view lives in the same coordinate system as the parent
view unless the parent has explicitly changed the coordinate system.
To schedule itself to be repainted a view can call repaint on the hosting
Component
.
Graphics
object given is not initialized in any way.
A view should set any settings needed.
View
is inherently transparent. While a view may render into its
entire allocation, typically a view does not. Rendering is performed by
tranversing down the tree of View
implementations.
Each View
is responsible
for rendering its children. This behavior is depended upon for thread
safety. While view implementations do not necessarily have to be implemented
with thread safety in mind, other view implementations that do make use of
concurrency can depend upon a tree traversal to guarantee thread safety.
The methods for rendering are:
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
.
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 DocumentListener
s.
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:
View
object.replace
.This is implemented to return the view itself, which represents the default behavior on not being breakable. If the view does support breaking, the starting offset of the view returned should be the given offset, and the end offset should be less than or equal to the end offset of the view being broken.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
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.
AttributeSet
returned
by this method.breakView
on in the process of formatting. A view that represents
text that has whitespace in it might be more attractive
than a view that has no whitespace, for example. The
higher the weight, the more attractive the break. A
value equal to or lower than BadBreakWeight
should not be considered for a break. A value greater
than or equal to ForcedBreakWeight
should
be broken.
This is implemented to provide the default behavior
of returning BadBreakWeight
unless the length
is greater than the length of the view in which case the
entire view represents the fragment. Unless a view has
been written to support breaking behavior, it is not
attractive to try and break the view. An example of
a view that does support breaking is LabelView
.
An example of a view that uses break weight is
ParagraphView
.
null
since the
default is to not have any child views.Graphics
for rendering.
This can be used to determine
font characteristics, and will be different for a print view
than a component view.null
.ViewFactory
implementation that is feeding
the view hierarchy. Normally the views are given this
as an argument to updates from the model when they
are most likely to need the factory, but this
method serves to provide it at other times.x
, y
.java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
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.)
replace
.Position.Bias.Forward
which was previously implied.wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
revalidate
on the associated text component.replace
.replace
.null
, and the internal reference
to them removed so that they can be garbage collected.
This is implemented to do nothing, because by default
a view has no children.super.setParent()
should
be called.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())
biasReturn
argument will be filled in to indicate that the point given is
closer to the next character in the model or the previous
character in the model.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.