FieldViewpublic class FieldView extends PlainView Extends the multi-line plain text view to be suitable
for a single-line editor view. If the view is
allocated extra space, the field must adjust for it.
If the hosting component is a JTextField, this view
will manage the ranges of the associated BoundedRangeModel
and will adjust the horizontal allocation to match the
current visibility settings of the JTextField. |
Constructors Summary |
---|
public FieldView(Element elem)Constructs a new FieldView wrapped on an element.
super(elem);
|
Methods Summary |
---|
protected java.awt.Shape | adjustAllocation(java.awt.Shape a)Adjusts the allocation given to the view
to be a suitable allocation for a text field.
If the view has been allocated more than the
preferred span vertically, the allocation is
changed to be centered vertically. Horizontally
the view is adjusted according to the horizontal
alignment property set on the associated JTextField
(if that is the type of the hosting component).
if (a != null) {
Rectangle bounds = a.getBounds();
int vspan = (int) getPreferredSpan(Y_AXIS);
int hspan = (int) getPreferredSpan(X_AXIS);
if (bounds.height != vspan) {
int slop = bounds.height - vspan;
bounds.y += slop / 2;
bounds.height -= slop;
}
// horizontal adjustments
Component c = getContainer();
if (c instanceof JTextField) {
JTextField field = (JTextField) c;
BoundedRangeModel vis = field.getHorizontalVisibility();
int max = Math.max(hspan, bounds.width);
int value = vis.getValue();
int extent = Math.min(max, bounds.width - 1);
if ((value + extent) > max) {
value = max - extent;
}
vis.setRangeProperties(value, extent, vis.getMinimum(),
max, false);
if (hspan < bounds.width) {
// horizontally align the interior
int slop = bounds.width - 1 - hspan;
int align = ((JTextField)c).getHorizontalAlignment();
if(Utilities.isLeftToRight(c)) {
if(align==LEADING) {
align = LEFT;
}
else if(align==TRAILING) {
align = RIGHT;
}
}
else {
if(align==LEADING) {
align = RIGHT;
}
else if(align==TRAILING) {
align = LEFT;
}
}
switch (align) {
case SwingConstants.CENTER:
bounds.x += slop / 2;
bounds.width -= slop;
break;
case SwingConstants.RIGHT:
bounds.x += slop;
bounds.width -= slop;
break;
}
} else {
// adjust the allocation to match the bounded range.
bounds.width = hspan;
bounds.x -= vis.getValue();
}
}
return bounds;
}
return null;
| java.awt.Shape | adjustPaintRegion(java.awt.Shape a)Adjusts a based on the visible region and returns it.
return adjustAllocation(a);
| protected java.awt.FontMetrics | getFontMetrics()Fetches the font metrics associated with the component hosting
this view.
Component c = getContainer();
return c.getFontMetrics(c.getFont());
| public float | getPreferredSpan(int axis)Determines the preferred span for this view along an
axis.
switch (axis) {
case View.X_AXIS:
Segment buff = SegmentCache.getSharedSegment();
Document doc = getDocument();
int width;
try {
FontMetrics fm = getFontMetrics();
doc.getText(0, doc.getLength(), buff);
width = Utilities.getTabbedTextWidth(buff, fm, 0, this, 0);
if (buff.count > 0) {
Component c = getContainer();
firstLineOffset = sun.swing.SwingUtilities2.
getLeftSideBearing((c instanceof JComponent) ?
(JComponent)c : null, fm,
buff.array[buff.offset]);
firstLineOffset = Math.max(0, -firstLineOffset);
}
else {
firstLineOffset = 0;
}
} catch (BadLocationException bl) {
width = 0;
}
SegmentCache.releaseSharedSegment(buff);
return width + firstLineOffset;
default:
return super.getPreferredSpan(axis);
}
| public int | getResizeWeight(int axis)Determines the resizability of the view along the
given axis. A value of 0 or less is not resizable.
if (axis == View.X_AXIS) {
return 1;
}
return 0;
| public void | insertUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)Gives notification that something was inserted into the document
in a location that this view is responsible for.
super.insertUpdate(changes, adjustAllocation(a), f);
updateVisibilityModel();
| public java.awt.Shape | modelToView(int pos, java.awt.Shape a, javax.swing.text.Position$Bias b)Provides a mapping from the document model coordinate space
to the coordinate space of the view mapped to it.
return super.modelToView(pos, adjustAllocation(a), b);
| public void | paint(java.awt.Graphics g, java.awt.Shape a)Renders using the given rendering surface and area on that surface.
The view may need to do layout and create child views to enable
itself to render into the given allocation.
Rectangle r = (Rectangle) a;
g.clipRect(r.x, r.y, r.width, r.height);
super.paint(g, a);
| public void | removeUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)Gives notification that something was removed from the document
in a location that this view is responsible for.
super.removeUpdate(changes, adjustAllocation(a), f);
updateVisibilityModel();
| void | updateVisibilityModel()Update the visibility model with the associated JTextField
(if there is one) to reflect the current visibility as a
result of changes to the document model. The bounded
range properties are updated. If the view hasn't yet been
shown the extent will be zero and we just set it to be full
until determined otherwise.
Component c = getContainer();
if (c instanceof JTextField) {
JTextField field = (JTextField) c;
BoundedRangeModel vis = field.getHorizontalVisibility();
int hspan = (int) getPreferredSpan(X_AXIS);
int extent = vis.getExtent();
int maximum = Math.max(hspan, extent);
extent = (extent == 0) ? maximum : extent;
int value = maximum - extent;
int oldValue = vis.getValue();
if ((oldValue + extent) > maximum) {
oldValue = maximum - extent;
}
value = Math.max(0, Math.min(value, oldValue));
vis.setRangeProperties(value, extent, 0, maximum, false);
}
| public int | viewToModel(float fx, float fy, java.awt.Shape a, javax.swing.text.Position$Bias[] bias)Provides a mapping from the view coordinate space to the logical
coordinate space of the model.
return super.viewToModel(fx, fy, adjustAllocation(a), bias);
|
|