1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import javax.swing.*; 11 import javax.swing.event.*; 12 13 26 public class FieldView extends PlainView { 27 28 33 public FieldView(Element elem) { 34 super(elem); 35 } 36 37 43 protected FontMetrics getFontMetrics() { 44 Component c = getContainer(); 45 return c.getFontMetrics(c.getFont()); 46 } 47 48 62 protected Shape adjustAllocation(Shape a) { 63 if (a != null) { 64 Rectangle bounds = a.getBounds(); 65 int vspan = (int) getPreferredSpan(Y_AXIS); 66 int hspan = (int) getPreferredSpan(X_AXIS); 67 if (bounds.height != vspan) { 68 int slop = bounds.height - vspan; 69 bounds.y += slop / 2; 70 bounds.height -= slop; 71 } 72 73 Component c = getContainer(); 75 if (c instanceof JTextField) { 76 JTextField field = (JTextField) c; 77 BoundedRangeModel vis = field.getHorizontalVisibility(); 78 int max = Math.max(hspan, bounds.width); 79 int value = vis.getValue(); 80 int extent = Math.min(max, bounds.width - 1); 81 if ((value + extent) > max) { 82 value = max - extent; 83 } 84 vis.setRangeProperties(value, extent, vis.getMinimum(), 85 max, false); 86 if (hspan < bounds.width) { 87 int slop = bounds.width - 1 - hspan; 89 90 int align = ((JTextField)c).getHorizontalAlignment(); 91 if(Utilities.isLeftToRight(c)) { 92 if(align==LEADING) { 93 align = LEFT; 94 } 95 else if(align==TRAILING) { 96 align = RIGHT; 97 } 98 } 99 else { 100 if(align==LEADING) { 101 align = RIGHT; 102 } 103 else if(align==TRAILING) { 104 align = LEFT; 105 } 106 } 107 108 switch (align) { 109 case SwingConstants.CENTER: 110 bounds.x += slop / 2; 111 bounds.width -= slop; 112 break; 113 case SwingConstants.RIGHT: 114 bounds.x += slop; 115 bounds.width -= slop; 116 break; 117 } 118 } else { 119 bounds.width = hspan; 121 bounds.x -= vis.getValue(); 122 } 123 } 124 return bounds; 125 } 126 return null; 127 } 128 129 137 void updateVisibilityModel() { 138 Component c = getContainer(); 139 if (c instanceof JTextField) { 140 JTextField field = (JTextField) c; 141 BoundedRangeModel vis = field.getHorizontalVisibility(); 142 int hspan = (int) getPreferredSpan(X_AXIS); 143 int extent = vis.getExtent(); 144 int maximum = Math.max(hspan, extent); 145 extent = (extent == 0) ? maximum : extent; 146 int value = maximum - extent; 147 int oldValue = vis.getValue(); 148 if ((oldValue + extent) > maximum) { 149 oldValue = maximum - extent; 150 } 151 value = Math.max(0, Math.min(value, oldValue)); 152 vis.setRangeProperties(value, extent, 0, maximum, false); 153 } 154 } 155 156 158 168 public void paint(Graphics g, Shape a) { 169 Rectangle r = (Rectangle) a; 170 g.clipRect(r.x, r.y, r.width, r.height); 171 super.paint(g, a); 172 } 173 174 177 Shape adjustPaintRegion(Shape a) { 178 return adjustAllocation(a); 179 } 180 181 191 public float getPreferredSpan(int axis) { 192 switch (axis) { 193 case View.X_AXIS: 194 Segment buff = SegmentCache.getSharedSegment(); 195 Document doc = getDocument(); 196 int width; 197 try { 198 FontMetrics fm = getFontMetrics(); 199 doc.getText(0, doc.getLength(), buff); 200 width = Utilities.getTabbedTextWidth(buff, fm, 0, this, 0); 201 if (buff.count > 0) { 202 Component c = getContainer(); 203 firstLineOffset = com.sun.java.swing.SwingUtilities2. 204 getLeftSideBearing((c instanceof JComponent) ? 205 (JComponent)c : null, fm, 206 buff.array[buff.offset]); 207 firstLineOffset = Math.max(0, -firstLineOffset); 208 } 209 else { 210 firstLineOffset = 0; 211 } 212 } catch (BadLocationException bl) { 213 width = 0; 214 } 215 SegmentCache.releaseSharedSegment(buff); 216 return width + firstLineOffset; 217 default: 218 return super.getPreferredSpan(axis); 219 } 220 } 221 222 229 public int getResizeWeight(int axis) { 230 if (axis == View.X_AXIS) { 231 return 1; 232 } 233 return 0; 234 } 235 236 247 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 248 return super.modelToView(pos, adjustAllocation(a), b); 249 } 250 251 262 public int viewToModel(float fx, float fy, Shape a, Position.Bias [] bias) { 263 return super.viewToModel(fx, fy, adjustAllocation(a), bias); 264 } 265 266 275 public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 276 super.insertUpdate(changes, adjustAllocation(a), f); 277 updateVisibilityModel(); 278 } 279 280 289 public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 290 super.removeUpdate(changes, adjustAllocation(a), f); 291 updateVisibilityModel(); 292 } 293 294 } 295 | Popular Tags |