1 7 package javax.swing.plaf.basic; 8 9 import java.awt.*; 10 import java.awt.event.KeyEvent ; 11 import java.awt.event.FocusEvent ; 12 import java.awt.event.InputEvent ; 13 import java.beans.PropertyChangeEvent ; 14 import java.io.Reader ; 15 import javax.swing.*; 16 import javax.swing.border.*; 17 import javax.swing.event.*; 18 import javax.swing.text.*; 19 import javax.swing.plaf.*; 20 import sun.swing.DefaultLookup; 21 22 37 public class BasicTextFieldUI extends BasicTextUI { 38 39 45 public static ComponentUI createUI(JComponent c) { 46 return new BasicTextFieldUI (); 47 } 48 49 52 public BasicTextFieldUI() { 53 super(); 54 } 55 56 public void installUI(JComponent c) { 57 super.installUI(c); 58 updateBackground((JTextComponent)c); 59 } 60 61 69 protected void propertyChange(PropertyChangeEvent evt) { 70 if (evt.getPropertyName().equals("editable") || 71 evt.getPropertyName().equals("enabled")) { 72 73 updateBackground((JTextComponent)evt.getSource()); 74 } 75 } 76 77 private void updateBackground(JTextComponent c) { 78 Color background = c.getBackground(); 79 if (background instanceof UIResource) { 80 Color newColor = null; 81 String prefix = getPropertyPrefix(); 82 if (!c.isEnabled()) { 83 newColor = DefaultLookup.getColor(c, this, 84 prefix + ".disabledBackground", 85 null); 86 } 87 if (newColor == null && !c.isEditable()) { 88 newColor = DefaultLookup.getColor(c, this, 89 prefix + ".inactiveBackground", 90 null); 91 } 92 if (newColor == null) { 93 newColor = DefaultLookup.getColor(c, this, 94 prefix + ".background", 95 null); 96 } 97 if (newColor != null && newColor != background) { 98 c.setBackground(newColor); 99 } 100 } 101 } 102 103 110 protected String getPropertyPrefix() { 111 return "TextField"; 112 } 113 114 120 public View create(Element elem) { 121 Document doc = elem.getDocument(); 122 Object i18nFlag = doc.getProperty("i18n"); 123 if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { 124 String kind = elem.getName(); 127 if (kind != null) { 128 if (kind.equals(AbstractDocument.ContentElementName)) { 129 return new GlyphView(elem); 130 } else if (kind.equals(AbstractDocument.ParagraphElementName)) { 131 return new I18nFieldView(elem); 132 } 133 } 134 } 136 return new FieldView(elem); 137 } 138 139 143 static class I18nFieldView extends ParagraphView { 144 145 I18nFieldView(Element elem) { 146 super(elem); 147 } 148 149 155 public int getFlowSpan(int index) { 156 return Integer.MAX_VALUE; 157 } 158 159 protected void setJustification(int j) { 160 } 163 164 static boolean isLeftToRight( java.awt.Component c ) { 165 return c.getComponentOrientation().isLeftToRight(); 166 } 167 168 182 Shape adjustAllocation(Shape a) { 183 if (a != null) { 184 Rectangle bounds = a.getBounds(); 185 int vspan = (int) getPreferredSpan(Y_AXIS); 186 int hspan = (int) getPreferredSpan(X_AXIS); 187 if (bounds.height != vspan) { 188 int slop = bounds.height - vspan; 189 bounds.y += slop / 2; 190 bounds.height -= slop; 191 } 192 193 Component c = getContainer(); 195 if (c instanceof JTextField) { 196 JTextField field = (JTextField) c; 197 BoundedRangeModel vis = field.getHorizontalVisibility(); 198 int max = Math.max(hspan, bounds.width); 199 int value = vis.getValue(); 200 int extent = Math.min(max, bounds.width - 1); 201 if ((value + extent) > max) { 202 value = max - extent; 203 } 204 vis.setRangeProperties(value, extent, vis.getMinimum(), 205 max, false); 206 if (hspan < bounds.width) { 207 int slop = bounds.width - 1 - hspan; 209 210 int align = ((JTextField)c).getHorizontalAlignment(); 211 if(isLeftToRight(c)) { 212 if(align==LEADING) { 213 align = LEFT; 214 } 215 else if(align==TRAILING) { 216 align = RIGHT; 217 } 218 } 219 else { 220 if(align==LEADING) { 221 align = RIGHT; 222 } 223 else if(align==TRAILING) { 224 align = LEFT; 225 } 226 } 227 228 switch (align) { 229 case SwingConstants.CENTER: 230 bounds.x += slop / 2; 231 bounds.width -= slop; 232 break; 233 case SwingConstants.RIGHT: 234 bounds.x += slop; 235 bounds.width -= slop; 236 break; 237 } 238 } else { 239 bounds.width = hspan; 241 bounds.x -= vis.getValue(); 242 } 243 } 244 return bounds; 245 } 246 return null; 247 } 248 249 257 void updateVisibilityModel() { 258 Component c = getContainer(); 259 if (c instanceof JTextField) { 260 JTextField field = (JTextField) c; 261 BoundedRangeModel vis = field.getHorizontalVisibility(); 262 int hspan = (int) getPreferredSpan(X_AXIS); 263 int extent = vis.getExtent(); 264 int maximum = Math.max(hspan, extent); 265 extent = (extent == 0) ? maximum : extent; 266 int value = maximum - extent; 267 int oldValue = vis.getValue(); 268 if ((oldValue + extent) > maximum) { 269 oldValue = maximum - extent; 270 } 271 value = Math.max(0, Math.min(value, oldValue)); 272 vis.setRangeProperties(value, extent, 0, maximum, false); 273 } 274 } 275 276 278 288 public void paint(Graphics g, Shape a) { 289 Rectangle r = (Rectangle) a; 290 g.clipRect(r.x, r.y, r.width, r.height); 291 super.paint(g, adjustAllocation(a)); 292 } 293 294 301 public int getResizeWeight(int axis) { 302 if (axis == View.X_AXIS) { 303 return 1; 304 } 305 return 0; 306 } 307 308 319 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 320 return super.modelToView(pos, adjustAllocation(a), b); 321 } 322 323 342 public Shape modelToView(int p0, Position.Bias b0, 343 int p1, Position.Bias b1, Shape a) 344 throws BadLocationException 345 { 346 return super.modelToView(p0, b0, p1, b1, adjustAllocation(a)); 347 } 348 349 360 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) { 361 return super.viewToModel(fx, fy, adjustAllocation(a), bias); 362 } 363 364 373 public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 374 super.insertUpdate(changes, adjustAllocation(a), f); 375 updateVisibilityModel(); 376 } 377 378 387 public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 388 super.removeUpdate(changes, adjustAllocation(a), f); 389 updateVisibilityModel(); 390 } 391 392 } 393 394 } 395 | Popular Tags |