1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import javax.swing.Icon ; 11 import javax.swing.event.*; 12 13 25 public class IconView extends View { 26 27 32 public IconView(Element elem) { 33 super(elem); 34 AttributeSet attr = elem.getAttributes(); 35 c = StyleConstants.getIcon(attr); 36 } 37 38 40 53 public void paint(Graphics g, Shape a) { 54 Rectangle alloc = a.getBounds(); 55 c.paintIcon(getContainer(), g, alloc.x, alloc.y); 56 } 57 58 69 public float getPreferredSpan(int axis) { 70 switch (axis) { 71 case View.X_AXIS: 72 return c.getIconWidth(); 73 case View.Y_AXIS: 74 return c.getIconHeight(); 75 default: 76 throw new IllegalArgumentException ("Invalid axis: " + axis); 77 } 78 } 79 80 93 public float getAlignment(int axis) { 94 switch (axis) { 95 case View.Y_AXIS: 96 return 1; 97 default: 98 return super.getAlignment(axis); 99 } 100 } 101 102 113 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 114 int p0 = getStartOffset(); 115 int p1 = getEndOffset(); 116 if ((pos >= p0) && (pos <= p1)) { 117 Rectangle r = a.getBounds(); 118 if (pos == p1) { 119 r.x += r.width; 120 } 121 r.width = 0; 122 return r; 123 } 124 throw new BadLocationException (pos + " not in range " + p0 + "," + p1, pos); 125 } 126 127 138 public int viewToModel(float x, float y, Shape a, Position.Bias [] bias) { 139 Rectangle alloc = (Rectangle) a; 140 if (x < alloc.x + (alloc.width / 2)) { 141 bias[0] = Position.Bias.Forward; 142 return getStartOffset(); 143 } 144 bias[0] = Position.Bias.Backward; 145 return getEndOffset(); 146 } 147 148 150 private Icon c; 151 } 152 153 | Popular Tags |